Commit 5407ed81 authored by James Lopez's avatar James Lopez

Merge branch 'wiki-model-specs-instance-variables' into 'master'

Clean up wiki model specs

Closes #213982

See merge request gitlab-org/gitlab!30158
parents 44b4bab3 9e235dc4
...@@ -38,7 +38,7 @@ describe API::Search do ...@@ -38,7 +38,7 @@ describe API::Search do
context 'for wiki_blobs scope', :sidekiq_might_not_need_inline do context 'for wiki_blobs scope', :sidekiq_might_not_need_inline do
before do before do
wiki = create(:project_wiki, project: project) wiki = create(:project_wiki, project: project)
create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: "Awesome page" }) create(:wiki_page, wiki: wiki, title: 'home', content: "Awesome page")
project.wiki.index_wiki_blobs project.wiki.index_wiki_blobs
ensure_elasticsearch_index! ensure_elasticsearch_index!
...@@ -183,7 +183,7 @@ describe API::Search do ...@@ -183,7 +183,7 @@ describe API::Search do
context 'for wiki_blobs scope' do context 'for wiki_blobs scope' do
before do before do
wiki = create(:project_wiki, project: project) wiki = create(:project_wiki, project: project)
create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: "Awesome page" }) create(:wiki_page, wiki: wiki, title: 'home', content: "Awesome page")
get api(endpoint, user), params: { scope: 'wiki_blobs', search: 'awesome' } get api(endpoint, user), params: { scope: 'wiki_blobs', search: 'awesome' }
end end
......
...@@ -84,7 +84,7 @@ describe SearchController do ...@@ -84,7 +84,7 @@ describe SearchController do
with_them do with_them do
it do it do
project_wiki = create(:project_wiki, project: project, user: user) project_wiki = create(:project_wiki, project: project, user: user)
create(:wiki_page, wiki: project_wiki, attrs: { title: 'merge', content: 'merge' }) create(:wiki_page, wiki: project_wiki, title: 'merge', content: 'merge')
expect(subject).to render_template("search/results/#{partial}") expect(subject).to render_template("search/results/#{partial}")
end end
......
...@@ -7,11 +7,17 @@ FactoryBot.define do ...@@ -7,11 +7,17 @@ FactoryBot.define do
transient do transient do
title { generate(:wiki_page_title) } title { generate(:wiki_page_title) }
content { 'Content for wiki page' } content { 'Content for wiki page' }
format { 'markdown' } format { :markdown }
message { nil }
project { association(:project, :wiki_repo) } project { association(:project, :wiki_repo) }
container { project } container { project }
attrs do wiki { association(:wiki, container: container) }
{ page { OpenStruct.new(url_path: title) }
end
initialize_with do
new(wiki, page).tap do |page|
page.attributes = {
title: title, title: title,
content: content, content: content,
format: format format: format
...@@ -19,25 +25,13 @@ FactoryBot.define do ...@@ -19,25 +25,13 @@ FactoryBot.define do
end end
end end
page { OpenStruct.new(url_path: 'some-name') } # Clear our default @page, except when using build_stubbed
wiki { association(:wiki, container: container) } after(:build) do |page|
page.instance_variable_set('@page', nil)
initialize_with { new(wiki, page) }
before(:create) do |page, evaluator|
page.attributes = evaluator.attrs
end
to_create do |page|
page.create
end end
trait :with_real_page do to_create do |page, evaluator|
page do page.create(message: evaluator.message)
wiki.create_page(title, content)
page_title, page_dir = wiki.page_title_and_dir(title)
wiki.wiki.page(title: page_title, dir: page_dir, version: nil)
end
end end
end end
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
describe 'Projects > Wiki > User previews markdown changes', :js do describe 'Projects > Wiki > User previews markdown changes', :js do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:project) { create(:project, :wiki_repo, namespace: user.namespace) } let(:project) { create(:project, :wiki_repo, namespace: user.namespace) }
let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: { title: 'home', content: '[some link](other-page)' }) } let(:wiki_page) { create(:wiki_page, wiki: project.wiki, title: 'home', content: '[some link](other-page)') }
let(:wiki_content) do let(:wiki_content) do
<<-HEREDOC <<-HEREDOC
[regular link](regular) [regular link](regular)
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
describe 'Wiki shortcuts', :js do describe 'Wiki shortcuts', :js do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :wiki_repo, namespace: user.namespace) } let(:project) { create(:project, :wiki_repo, namespace: user.namespace) }
let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: { title: 'home', content: 'Home page' }) } let(:wiki_page) { create(:wiki_page, wiki: project.wiki, title: 'home', content: 'Home page') }
before do before do
sign_in(user) sign_in(user)
......
...@@ -195,7 +195,7 @@ describe "User creates wiki page" do ...@@ -195,7 +195,7 @@ describe "User creates wiki page" do
context "when wiki is not empty", :js do context "when wiki is not empty", :js do
before do before do
create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: 'Home page' }) create(:wiki_page, wiki: wiki, title: 'home', content: 'Home page')
visit(project_wikis_path(project)) visit(project_wikis_path(project))
end end
...@@ -304,8 +304,8 @@ describe "User creates wiki page" do ...@@ -304,8 +304,8 @@ describe "User creates wiki page" do
describe 'sidebar feature' do describe 'sidebar feature' do
context 'when there are some existing pages' do context 'when there are some existing pages' do
before do before do
create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: 'home' }) create(:wiki_page, wiki: wiki, title: 'home', content: 'home')
create(:wiki_page, wiki: wiki, attrs: { title: 'another', content: 'another' }) create(:wiki_page, wiki: wiki, title: 'another', content: 'another')
end end
it 'renders a default sidebar when there is no customized sidebar' do it 'renders a default sidebar when there is no customized sidebar' do
...@@ -316,7 +316,7 @@ describe "User creates wiki page" do ...@@ -316,7 +316,7 @@ describe "User creates wiki page" do
context 'when there is a customized sidebar' do context 'when there is a customized sidebar' do
before do before do
create(:wiki_page, wiki: wiki, attrs: { title: '_sidebar', content: 'My customized sidebar' }) create(:wiki_page, wiki: wiki, title: '_sidebar', content: 'My customized sidebar')
end end
it 'renders my customized sidebar instead of the default one' do it 'renders my customized sidebar instead of the default one' do
...@@ -330,8 +330,8 @@ describe "User creates wiki page" do ...@@ -330,8 +330,8 @@ describe "User creates wiki page" do
context 'when there are more than 15 existing pages' do context 'when there are more than 15 existing pages' do
before do before do
create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: 'home' }) create(:wiki_page, wiki: wiki, title: 'home', content: 'home')
(1..14).each { |i| create(:wiki_page, wiki: wiki, attrs: { title: "page-#{i}", content: "page #{i}" }) } (1..14).each { |i| create(:wiki_page, wiki: wiki, title: "page-#{i}", content: "page #{i}") }
end end
it 'renders a default sidebar when there is no customized sidebar' do it 'renders a default sidebar when there is no customized sidebar' do
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
describe 'Projects > Wiki > User views Git access wiki page' do describe 'Projects > Wiki > User views Git access wiki page' do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :wiki_repo, :public) } let(:project) { create(:project, :wiki_repo, :public) }
let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: { title: 'home', content: '[some link](other-page)' }) } let(:wiki_page) { create(:wiki_page, wiki: project.wiki, title: 'home', content: '[some link](other-page)') }
before do before do
sign_in(user) sign_in(user)
......
...@@ -64,7 +64,7 @@ describe 'User updates wiki page' do ...@@ -64,7 +64,7 @@ describe 'User updates wiki page' do
context 'when wiki is not empty' do context 'when wiki is not empty' do
let(:project_wiki) { create(:project_wiki, project: project, user: project.creator) } let(:project_wiki) { create(:project_wiki, project: project, user: project.creator) }
let!(:wiki_page) { create(:wiki_page, wiki: project_wiki, attrs: { title: 'home', content: 'Home page' }) } let!(:wiki_page) { create(:wiki_page, wiki: project_wiki, title: 'home', content: 'Home page') }
before do before do
visit(project_wikis_path(project)) visit(project_wikis_path(project))
...@@ -168,7 +168,7 @@ describe 'User updates wiki page' do ...@@ -168,7 +168,7 @@ describe 'User updates wiki page' do
let(:project_wiki) { create(:project_wiki, project: project, user: project.creator) } let(:project_wiki) { create(:project_wiki, project: project, user: project.creator) }
let(:page_name) { 'page_name' } let(:page_name) { 'page_name' }
let(:page_dir) { "foo/bar/#{page_name}" } let(:page_dir) { "foo/bar/#{page_name}" }
let!(:wiki_page) { create(:wiki_page, wiki: project_wiki, attrs: { title: page_dir, content: 'Home page' }) } let!(:wiki_page) { create(:wiki_page, wiki: project_wiki, title: page_dir, content: 'Home page') }
before do before do
visit(project_wiki_edit_path(project, wiki_page)) visit(project_wiki_edit_path(project, wiki_page))
......
...@@ -21,7 +21,7 @@ describe 'Projects > Wiki > User views wiki in project page' do ...@@ -21,7 +21,7 @@ describe 'Projects > Wiki > User views wiki in project page' do
context 'when wiki homepage contains a link' do context 'when wiki homepage contains a link' do
before do before do
create(:wiki_page, wiki: project.wiki, attrs: { title: 'home', content: '[some link](other-page)' }) create(:wiki_page, wiki: project.wiki, title: 'home', content: '[some link](other-page)')
end end
it 'displays the correct URL for the link' do it 'displays the correct URL for the link' do
......
...@@ -11,7 +11,7 @@ describe 'User views a wiki page' do ...@@ -11,7 +11,7 @@ describe 'User views a wiki page' do
let(:wiki_page) do let(:wiki_page) do
create(:wiki_page, create(:wiki_page,
wiki: project.wiki, wiki: project.wiki,
attrs: { title: 'home', content: "Look at this [image](#{path})\n\n ![alt text](#{path})" }) title: 'home', content: "Look at this [image](#{path})\n\n ![alt text](#{path})")
end end
before do before do
......
...@@ -9,13 +9,13 @@ describe 'User views wiki pages' do ...@@ -9,13 +9,13 @@ describe 'User views wiki pages' do
let(:project) { create(:project, :wiki_repo, namespace: user.namespace) } let(:project) { create(:project, :wiki_repo, namespace: user.namespace) }
let!(:wiki_page1) do let!(:wiki_page1) do
create(:wiki_page, wiki: project.wiki, attrs: { title: '3 home', content: '3' }) create(:wiki_page, wiki: project.wiki, title: '3 home', content: '3')
end end
let!(:wiki_page2) do let!(:wiki_page2) do
create(:wiki_page, wiki: project.wiki, attrs: { title: '1 home', content: '1' }) create(:wiki_page, wiki: project.wiki, title: '1 home', content: '1')
end end
let!(:wiki_page3) do let!(:wiki_page3) do
create(:wiki_page, wiki: project.wiki, attrs: { title: '2 home', content: '2' }) create(:wiki_page, wiki: project.wiki, title: '2 home', content: '2')
end end
let(:pages) do let(:pages) do
......
...@@ -16,7 +16,7 @@ describe 'User views AsciiDoc page with includes', :js do ...@@ -16,7 +16,7 @@ describe 'User views AsciiDoc page with includes', :js do
format: :asciidoc format: :asciidoc
} }
create(:wiki_page, wiki: project.wiki, attrs: attrs) create(:wiki_page, wiki: project.wiki, **attrs)
end end
before do before do
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
describe 'User searches for wiki pages', :js do describe 'User searches for wiki pages', :js do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :repository, :wiki_repo, namespace: user.namespace) } let(:project) { create(:project, :repository, :wiki_repo, namespace: user.namespace) }
let!(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: { title: 'directory/title', content: 'Some Wiki content' }) } let!(:wiki_page) { create(:wiki_page, wiki: project.wiki, title: 'directory/title', content: 'Some Wiki content') }
before do before do
project.add_maintainer(user) project.add_maintainer(user)
......
...@@ -57,7 +57,7 @@ describe Banzai::Pipeline::WikiPipeline do ...@@ -57,7 +57,7 @@ describe Banzai::Pipeline::WikiPipeline do
let(:namespace) { create(:namespace, name: "wiki_link_ns") } let(:namespace) { create(:namespace, name: "wiki_link_ns") }
let(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) } let(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) }
let(:project_wiki) { ProjectWiki.new(project, double(:user)) } let(:project_wiki) { ProjectWiki.new(project, double(:user)) }
let(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) } let(:page) { build(:wiki_page, wiki: project_wiki, title: 'nested/twice/start-page') }
{ 'when GitLab is hosted at a root URL' => '', { 'when GitLab is hosted at a root URL' => '',
'when GitLab is hosted at a relative URL' => '/nested/relative/gitlab' }.each do |test_name, relative_url_root| 'when GitLab is hosted at a relative URL' => '/nested/relative/gitlab' }.each do |test_name, relative_url_root|
...@@ -264,7 +264,7 @@ describe Banzai::Pipeline::WikiPipeline do ...@@ -264,7 +264,7 @@ describe Banzai::Pipeline::WikiPipeline do
let_it_be(:namespace) { create(:namespace, name: "wiki_link_ns") } let_it_be(:namespace) { create(:namespace, name: "wiki_link_ns") }
let_it_be(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) } let_it_be(:project) { create(:project, :public, name: "wiki_link_project", namespace: namespace) }
let_it_be(:project_wiki) { ProjectWiki.new(project, double(:user)) } let_it_be(:project_wiki) { ProjectWiki.new(project, double(:user)) }
let_it_be(:page) { build(:wiki_page, wiki: project_wiki, page: OpenStruct.new(url_path: 'nested/twice/start-page')) } let_it_be(:page) { build(:wiki_page, wiki: project_wiki, title: 'nested/twice/start-page') }
it 'generates video html structure' do it 'generates video html structure' do
markdown = "![video_file](video_file_name.mp4)" markdown = "![video_file](video_file_name.mp4)"
......
...@@ -40,7 +40,7 @@ describe BlobViewer::Readme do ...@@ -40,7 +40,7 @@ describe BlobViewer::Readme do
context 'when the wiki is not empty' do context 'when the wiki is not empty' do
before do before do
create(:wiki_page, wiki: project.wiki, attrs: { title: 'home', content: 'Home page' }) create(:wiki_page, wiki: project.wiki, title: 'home', content: 'Home page')
end end
it 'returns nil' do it 'returns nil' do
......
...@@ -490,7 +490,7 @@ describe Event do ...@@ -490,7 +490,7 @@ describe Event do
context 'for a wiki page event' do context 'for a wiki page event' do
let(:wiki_page) do let(:wiki_page) do
create(:wiki_page, :with_real_page, project: project) create(:wiki_page, project: project)
end end
subject(:event) { create(:wiki_page_event, project: project, wiki_page: wiki_page) } subject(:event) { create(:wiki_page_event, project: project, wiki_page: wiki_page) }
......
...@@ -121,7 +121,7 @@ describe MicrosoftTeamsService do ...@@ -121,7 +121,7 @@ describe MicrosoftTeamsService do
message: "user created page: Awesome wiki_page" message: "user created page: Awesome wiki_page"
} }
end end
let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: opts) } let(:wiki_page) { create(:wiki_page, wiki: project.wiki, **opts) }
let(:wiki_page_sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, 'create') } let(:wiki_page_sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, 'create') }
it "calls Microsoft Teams API" do it "calls Microsoft Teams API" do
......
...@@ -6,17 +6,8 @@ describe WikiPage do ...@@ -6,17 +6,8 @@ describe WikiPage do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:container) { create(:project, :wiki_repo) } let(:container) { create(:project, :wiki_repo) }
let(:wiki) { Wiki.for_container(container, user) } let(:wiki) { Wiki.for_container(container, user) }
let(:new_page) { build(:wiki_page, wiki: wiki, title: 'test page', content: 'test content') }
let(:new_page) do let(:existing_page) { create(:wiki_page, wiki: wiki, title: 'test page', content: 'test content', message: 'test commit') }
described_class.new(wiki).tap do |page|
page.attributes = { title: 'test page', content: 'test content' }
end
end
let(:existing_page) do
create_page('test page', 'test content')
wiki.find_page('test page')
end
subject { new_page } subject { new_page }
...@@ -41,13 +32,13 @@ describe WikiPage do ...@@ -41,13 +32,13 @@ describe WikiPage do
context 'when there are pages' do context 'when there are pages' do
before do before do
create_page('dir_1/dir_1_1/page_3', 'content') wiki.create_page('dir_1/dir_1_1/page_3', 'content')
create_page('page_1', 'content') wiki.create_page('page_1', 'content')
create_page('dir_1/page_2', 'content') wiki.create_page('dir_1/page_2', 'content')
create_page('dir_2', 'page with dir name') wiki.create_page('dir_2', 'page with dir name')
create_page('dir_2/page_5', 'content') wiki.create_page('dir_2/page_5', 'content')
create_page('page_6', 'content') wiki.create_page('page_6', 'content')
create_page('dir_2/page_4', 'content') wiki.create_page('dir_2/page_4', 'content')
end end
let(:page_1) { wiki.find_page('page_1') } let(:page_1) { wiki.find_page('page_1') }
...@@ -353,7 +344,7 @@ describe WikiPage do ...@@ -353,7 +344,7 @@ describe WikiPage do
context 'with an existing page title exceeding the limit' do context 'with an existing page title exceeding the limit' do
subject do subject do
title = 'a' * (max_title + 1) title = 'a' * (max_title + 1)
create_page(title, 'content') wiki.create_page(title, 'content')
wiki.find_page(title) wiki.find_page(title)
end end
...@@ -397,6 +388,20 @@ describe WikiPage do ...@@ -397,6 +388,20 @@ describe WikiPage do
expect(wiki.find_page("Index").message).to eq 'Custom Commit Message' expect(wiki.find_page("Index").message).to eq 'Custom Commit Message'
end end
it 'if the title is preceded by a / it is removed' do
subject.create(attributes.merge(title: '/New Page'))
expect(wiki.find_page('New Page')).not_to be_nil
end
end
context "with invalid attributes" do
it 'does not create the page' do
subject.create(title: '')
expect(wiki.find_page('New Page')).to be_nil
end
end end
end end
...@@ -419,14 +424,11 @@ describe WikiPage do ...@@ -419,14 +424,11 @@ describe WikiPage do
end end
end end
describe "#update" do describe '#update' do
subject do subject { create(:wiki_page, wiki: wiki, title: title) }
create_page(title, "content")
wiki.find_page(title)
end
it "updates the content of the page" do it 'updates the content of the page' do
subject.update(content: "new content") subject.update(content: 'new content')
page = wiki.find_page(title) page = wiki.find_page(title)
expect([subject.content, page.content]).to all(eq('new content')) expect([subject.content, page.content]).to all(eq('new content'))
...@@ -438,24 +440,6 @@ describe WikiPage do ...@@ -438,24 +440,6 @@ describe WikiPage do
end end
end end
describe '#create' do
context 'with valid attributes' do
it 'raises an error if a page with the same path already exists' do
create_page('New Page', 'content')
create_page('foo/bar', 'content')
expect { create_page('New Page', 'other content') }.to raise_error Gitlab::Git::Wiki::DuplicatePageError
expect { create_page('foo/bar', 'other content') }.to raise_error Gitlab::Git::Wiki::DuplicatePageError
end
it 'if the title is preceded by a / it is removed' do
create_page('/New Page', 'content')
expect(wiki.find_page('New Page')).not_to be_nil
end
end
end
describe "#update" do describe "#update" do
subject { existing_page } subject { existing_page }
...@@ -573,7 +557,7 @@ describe WikiPage do ...@@ -573,7 +557,7 @@ describe WikiPage do
context 'when renaming a page' do context 'when renaming a page' do
it 'raises an error if the page already exists' do it 'raises an error if the page already exists' do
create_page('Existing Page', 'content') wiki.create_page('Existing Page', 'content')
expect { subject.update(title: 'Existing Page', content: 'new_content') }.to raise_error(WikiPage::PageRenameError) expect { subject.update(title: 'Existing Page', content: 'new_content') }.to raise_error(WikiPage::PageRenameError)
expect(subject.title).to eq 'test page' expect(subject.title).to eq 'test page'
...@@ -595,7 +579,7 @@ describe WikiPage do ...@@ -595,7 +579,7 @@ describe WikiPage do
context 'when moving a page' do context 'when moving a page' do
it 'raises an error if the page already exists' do it 'raises an error if the page already exists' do
create_page('foo/Existing Page', 'content') wiki.create_page('foo/Existing Page', 'content')
expect { subject.update(title: 'foo/Existing Page', content: 'new_content') }.to raise_error(WikiPage::PageRenameError) expect { subject.update(title: 'foo/Existing Page', content: 'new_content') }.to raise_error(WikiPage::PageRenameError)
expect(subject.title).to eq 'test page' expect(subject.title).to eq 'test page'
...@@ -615,10 +599,7 @@ describe WikiPage do ...@@ -615,10 +599,7 @@ describe WikiPage do
end end
context 'in subdir' do context 'in subdir' do
subject do subject { create(:wiki_page, wiki: wiki, title: 'foo/Existing Page') }
create_page('foo/Existing Page', 'content')
wiki.find_page('foo/Existing Page')
end
it 'moves the page to the root folder if the title is preceded by /' do it 'moves the page to the root folder if the title is preceded by /' do
expect(subject.slug).to eq 'foo/Existing-Page' expect(subject.slug).to eq 'foo/Existing-Page'
...@@ -656,7 +637,7 @@ describe WikiPage do ...@@ -656,7 +637,7 @@ describe WikiPage do
end end
end end
describe "#destroy" do describe "#delete" do
subject { existing_page } subject { existing_page }
it "deletes the page" do it "deletes the page" do
...@@ -688,10 +669,7 @@ describe WikiPage do ...@@ -688,10 +669,7 @@ describe WikiPage do
using RSpec::Parameterized::TableSyntax using RSpec::Parameterized::TableSyntax
let(:untitled_page) { described_class.new(wiki) } let(:untitled_page) { described_class.new(wiki) }
let(:directory_page) do let(:directory_page) { create(:wiki_page, title: 'parent directory/child page') }
create_page('parent directory/child page', 'test content')
wiki.find_page('parent directory/child page')
end
where(:page, :title, :changed) do where(:page, :title, :changed) do
:untitled_page | nil | false :untitled_page | nil | false
...@@ -754,10 +732,7 @@ describe WikiPage do ...@@ -754,10 +732,7 @@ describe WikiPage do
end end
context 'when the page is inside an actual directory' do context 'when the page is inside an actual directory' do
subject do subject { create(:wiki_page, title: 'dir_1/dir_1_1/file') }
create_page('dir_1/dir_1_1/file', 'content')
wiki.find_page('dir_1/dir_1_1/file')
end
it 'returns the full directory hierarchy' do it 'returns the full directory hierarchy' do
expect(subject.directory).to eq('dir_1/dir_1_1') expect(subject.directory).to eq('dir_1/dir_1_1')
...@@ -804,6 +779,16 @@ describe WikiPage do ...@@ -804,6 +779,16 @@ describe WikiPage do
end end
end end
describe '#persisted?' do
it 'returns true for a persisted page' do
expect(existing_page).to be_persisted
end
it 'returns false for an unpersisted page' do
expect(new_page).not_to be_persisted
end
end
describe '#to_partial_path' do describe '#to_partial_path' do
it 'returns the relative path to the partial to be used' do it 'returns the relative path to the partial to be used' do
expect(subject.to_partial_path).to eq('projects/wikis/wiki_page') expect(subject.to_partial_path).to eq('projects/wikis/wiki_page')
...@@ -886,18 +871,6 @@ describe WikiPage do ...@@ -886,18 +871,6 @@ describe WikiPage do
private private
def remove_temp_repo(path)
FileUtils.rm_rf path
end
def commit_details
Gitlab::Git::Wiki::CommitDetails.new(user.id, user.username, user.name, user.email, "test commit")
end
def create_page(name, content)
wiki.wiki.write_page(name, :markdown, content, commit_details)
end
def get_slugs(page_or_dir) def get_slugs(page_or_dir)
if page_or_dir.is_a? WikiPage if page_or_dir.is_a? WikiPage
[page_or_dir.slug] [page_or_dir.slug]
......
...@@ -401,7 +401,7 @@ describe API::Search do ...@@ -401,7 +401,7 @@ describe API::Search do
context 'for wiki_blobs scope' do context 'for wiki_blobs scope' do
before do before do
wiki = create(:project_wiki, project: project) wiki = create(:project_wiki, project: project)
create(:wiki_page, wiki: wiki, attrs: { title: 'home', content: "Awesome page" }) create(:wiki_page, wiki: wiki, title: 'home', content: "Awesome page")
get api("/projects/#{project.id}/search", user), params: { scope: 'wiki_blobs', search: 'awesome' } get api("/projects/#{project.id}/search", user), params: { scope: 'wiki_blobs', search: 'awesome' }
end end
......
...@@ -25,8 +25,8 @@ describe API::Wikis do ...@@ -25,8 +25,8 @@ describe API::Wikis do
shared_examples_for 'returns list of wiki pages' do shared_examples_for 'returns list of wiki pages' do
context 'when wiki has pages' do context 'when wiki has pages' do
let!(:pages) do let!(:pages) do
[create(:wiki_page, wiki: project_wiki, attrs: { title: 'page1', content: 'content of page1' }), [create(:wiki_page, wiki: project_wiki, title: 'page1', content: 'content of page1'),
create(:wiki_page, wiki: project_wiki, attrs: { title: 'page2.with.dot', content: 'content of page2' })] create(:wiki_page, wiki: project_wiki, title: 'page2.with.dot', content: 'content of page2')]
end end
it 'returns the list of wiki pages without content' do it 'returns the list of wiki pages without content' do
......
...@@ -198,7 +198,7 @@ RSpec.shared_examples "chat service" do |service_name| ...@@ -198,7 +198,7 @@ RSpec.shared_examples "chat service" do |service_name|
message: "user created page: Awesome wiki_page" message: "user created page: Awesome wiki_page"
} }
end end
let(:wiki_page) { create(:wiki_page, wiki: project.wiki, attrs: opts) } let(:wiki_page) { create(:wiki_page, wiki: project.wiki, **opts) }
let(:sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, "create") } let(:sample_data) { Gitlab::DataBuilder::WikiPage.build(wiki_page, user, "create") }
it_behaves_like "triggered #{service_name} service" it_behaves_like "triggered #{service_name} service"
......
...@@ -112,7 +112,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do |service_name| ...@@ -112,7 +112,7 @@ RSpec.shared_examples 'slack or mattermost notifications' do |service_name|
message: "user created page: Awesome wiki_page" message: "user created page: Awesome wiki_page"
} }
@wiki_page = create(:wiki_page, wiki: project.wiki, attrs: opts) @wiki_page = create(:wiki_page, wiki: project.wiki, **opts)
@wiki_page_sample_data = Gitlab::DataBuilder::WikiPage.build(@wiki_page, user, 'create') @wiki_page_sample_data = Gitlab::DataBuilder::WikiPage.build(@wiki_page, user, 'create')
end end
......
...@@ -95,13 +95,14 @@ RSpec.shared_examples 'wiki model' do ...@@ -95,13 +95,14 @@ RSpec.shared_examples 'wiki model' do
let(:wiki_pages) { subject.list_pages } let(:wiki_pages) { subject.list_pages }
before do before do
create_page('index', 'This is an index') subject.create_page('index', 'This is an index')
create_page('index2', 'This is an index2') subject.create_page('index2', 'This is an index2')
create_page('an index3', 'This is an index3') subject.create_page('an index3', 'This is an index3')
end end
it 'returns an array of WikiPage instances' do it 'returns an array of WikiPage instances' do
expect(wiki_pages.first).to be_a WikiPage expect(wiki_pages).to be_present
expect(wiki_pages).to all(be_a(WikiPage))
end end
it 'does not load WikiPage content by default' do it 'does not load WikiPage content by default' do
...@@ -149,7 +150,7 @@ RSpec.shared_examples 'wiki model' do ...@@ -149,7 +150,7 @@ RSpec.shared_examples 'wiki model' do
describe '#find_page' do describe '#find_page' do
before do before do
create_page('index page', 'This is an awesome Gollum Wiki') subject.create_page('index page', 'This is an awesome Gollum Wiki')
end end
it 'returns the latest version of the page if it exists' do it 'returns the latest version of the page if it exists' do
...@@ -176,7 +177,7 @@ RSpec.shared_examples 'wiki model' do ...@@ -176,7 +177,7 @@ RSpec.shared_examples 'wiki model' do
context 'pages with multibyte-character title' do context 'pages with multibyte-character title' do
before do before do
create_page('autre pagé', "C'est un génial Gollum Wiki") subject.create_page('autre pagé', "C'est un génial Gollum Wiki")
end end
it 'can find a page by slug' do it 'can find a page by slug' do
...@@ -188,7 +189,7 @@ RSpec.shared_examples 'wiki model' do ...@@ -188,7 +189,7 @@ RSpec.shared_examples 'wiki model' do
context 'pages with invalidly-encoded content' do context 'pages with invalidly-encoded content' do
before do before do
create_page('encoding is fun', "f\xFCr".b) subject.create_page('encoding is fun', "f\xFCr".b)
end end
it 'can find the page' do it 'can find the page' do
...@@ -201,11 +202,11 @@ RSpec.shared_examples 'wiki model' do ...@@ -201,11 +202,11 @@ RSpec.shared_examples 'wiki model' do
describe '#find_sidebar' do describe '#find_sidebar' do
before do before do
create_page(described_class::SIDEBAR, 'This is an awesome Sidebar') subject.create_page(described_class::SIDEBAR, 'This is an awesome Sidebar')
end end
it 'finds the page defined as _sidebar' do it 'finds the page defined as _sidebar' do
page = subject.find_page('_sidebar') page = subject.find_sidebar
expect(page.content).to eq('This is an awesome Sidebar') expect(page.content).to eq('This is an awesome Sidebar')
end end
...@@ -284,58 +285,59 @@ RSpec.shared_examples 'wiki model' do ...@@ -284,58 +285,59 @@ RSpec.shared_examples 'wiki model' do
end end
describe '#update_page' do describe '#update_page' do
before do let(:page) { create(:wiki_page, wiki: subject, title: 'update-page') }
create_page('update-page', 'some content')
@gitlab_git_wiki_page = subject.wiki.page(title: 'update-page') def update_page
subject.update_page( subject.update_page(
@gitlab_git_wiki_page, page.page,
content: 'some other content', content: 'some other content',
format: :markdown, format: :markdown,
message: 'updated page' message: 'updated page'
) )
@page = subject.list_pages(load_content: true).first.page
end end
it 'updates the content of the page' do it 'updates the content of the page' do
expect(@page.raw_data).to eq('some other content') update_page
page = subject.find_page('update-page')
expect(page.raw_content).to eq('some other content')
end end
it 'sets the correct commit message' do it 'sets the correct commit message' do
expect(@page.version.message).to eq('updated page') update_page
page = subject.find_page('update-page')
expect(page.version.message).to eq('updated page')
end end
it 'sets the correct commit email' do it 'sets the correct commit email' do
update_page
expect(user.commit_email).not_to eq(user.email) expect(user.commit_email).not_to eq(user.email)
expect(commit.author_email).to eq(user.commit_email) expect(commit.author_email).to eq(user.commit_email)
expect(commit.committer_email).to eq(user.commit_email) expect(commit.committer_email).to eq(user.commit_email)
end end
it 'updates container activity' do it 'updates container activity' do
page
expect(subject).to receive(:update_container_activity) expect(subject).to receive(:update_container_activity)
subject.update_page( update_page
@gitlab_git_wiki_page,
content: 'Yet more content',
format: :markdown,
message: 'Updated page again'
)
end end
end end
describe '#delete_page' do describe '#delete_page' do
before do let(:page) { create(:wiki_page, wiki: wiki) }
create_page('index', 'some content')
@page = subject.wiki.page(title: 'index')
end
it 'deletes the page' do it 'deletes the page' do
subject.delete_page(@page) subject.delete_page(page)
expect(subject.list_pages.count).to eq(0) expect(subject.list_pages.count).to eq(0)
end end
it 'sets the correct commit email' do it 'sets the correct commit email' do
subject.delete_page(@page) subject.delete_page(page)
expect(user.commit_email).not_to eq(user.email) expect(user.commit_email).not_to eq(user.email)
expect(commit.author_email).to eq(user.commit_email) expect(commit.author_email).to eq(user.commit_email)
...@@ -343,9 +345,11 @@ RSpec.shared_examples 'wiki model' do ...@@ -343,9 +345,11 @@ RSpec.shared_examples 'wiki model' do
end end
it 'updates container activity' do it 'updates container activity' do
page
expect(subject).to receive(:update_container_activity) expect(subject).to receive(:update_container_activity)
subject.delete_page(@page) subject.delete_page(page)
end end
end end
...@@ -378,23 +382,4 @@ RSpec.shared_examples 'wiki model' do ...@@ -378,23 +382,4 @@ RSpec.shared_examples 'wiki model' do
expect(subject.hook_attrs.keys).to contain_exactly(:web_url, :git_ssh_url, :git_http_url, :path_with_namespace, :default_branch) expect(subject.hook_attrs.keys).to contain_exactly(:web_url, :git_ssh_url, :git_http_url, :path_with_namespace, :default_branch)
end end
end end
private
def create_temp_repo(path)
FileUtils.mkdir_p path
system(*%W(#{Gitlab.config.git.bin_path} init --quiet --bare -- #{path}))
end
def remove_temp_repo(path)
FileUtils.rm_rf path
end
def commit_details
Gitlab::Git::Wiki::CommitDetails.new(user.id, user.username, user.name, user.commit_email, 'test commit')
end
def create_page(name, content)
subject.wiki.write_page(name, :markdown, content, commit_details)
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment