summaryrefslogtreecommitdiff
path: root/spec/models/wiki_page_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/wiki_page_spec.rb')
-rw-r--r--spec/models/wiki_page_spec.rb161
1 files changed, 73 insertions, 88 deletions
diff --git a/spec/models/wiki_page_spec.rb b/spec/models/wiki_page_spec.rb
index 718b386b3fd..201dc85daf8 100644
--- a/spec/models/wiki_page_spec.rb
+++ b/spec/models/wiki_page_spec.rb
@@ -3,20 +3,11 @@
require "spec_helper"
describe WikiPage do
- let(:project) { create(:project, :wiki_repo) }
- let(:user) { project.owner }
- let(:wiki) { ProjectWiki.new(project, user) }
-
- let(:new_page) do
- 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
+ let_it_be(:user) { create(:user) }
+ let(:container) { create(:project, :wiki_repo) }
+ let(:wiki) { Wiki.for_container(container, user) }
+ let(:new_page) { build(:wiki_page, wiki: wiki, title: 'test page', content: 'test content') }
+ let(:existing_page) { create(:wiki_page, wiki: wiki, title: 'test page', content: 'test content', message: 'test commit') }
subject { new_page }
@@ -24,11 +15,8 @@ describe WikiPage do
stub_feature_flags(Gitlab::WikiPages::FrontMatterParser::FEATURE_FLAG => false)
end
- def enable_front_matter_for_project
- stub_feature_flags(Gitlab::WikiPages::FrontMatterParser::FEATURE_FLAG => {
- thing: project,
- enabled: true
- })
+ def enable_front_matter_for(thing)
+ stub_feature_flags(Gitlab::WikiPages::FrontMatterParser::FEATURE_FLAG => thing)
end
describe '.group_by_directory' do
@@ -41,13 +29,13 @@ describe WikiPage do
context 'when there are pages' do
before do
- create_page('dir_1/dir_1_1/page_3', 'content')
- create_page('page_1', 'content')
- create_page('dir_1/page_2', 'content')
- create_page('dir_2', 'page with dir name')
- create_page('dir_2/page_5', 'content')
- create_page('page_6', 'content')
- create_page('dir_2/page_4', 'content')
+ wiki.create_page('dir_1/dir_1_1/page_3', 'content')
+ wiki.create_page('page_1', 'content')
+ wiki.create_page('dir_1/page_2', 'content')
+ wiki.create_page('dir_2', 'page with dir name')
+ wiki.create_page('dir_2/page_5', 'content')
+ wiki.create_page('page_6', 'content')
+ wiki.create_page('dir_2/page_4', 'content')
end
let(:page_1) { wiki.find_page('page_1') }
@@ -114,7 +102,8 @@ describe WikiPage do
describe '#front_matter' do
let_it_be(:project) { create(:project) }
- let(:wiki_page) { create(:wiki_page, project: project, content: content) }
+ let(:container) { project }
+ let(:wiki_page) { create(:wiki_page, container: container, content: content) }
shared_examples 'a page without front-matter' do
it { expect(wiki_page).to have_attributes(front_matter: {}, content: content) }
@@ -153,9 +142,9 @@ describe WikiPage do
it_behaves_like 'a page without front-matter'
- context 'but enabled for the project' do
+ context 'but enabled for the container' do
before do
- enable_front_matter_for_project
+ enable_front_matter_for(container)
end
it_behaves_like 'a page with front-matter'
@@ -344,7 +333,7 @@ describe WikiPage do
context 'with an existing page title exceeding the limit' do
subject do
title = 'a' * (max_title + 1)
- create_page(title, 'content')
+ wiki.create_page(title, 'content')
wiki.find_page(title)
end
@@ -388,6 +377,20 @@ describe WikiPage do
expect(wiki.find_page("Index").message).to eq 'Custom Commit Message'
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
@@ -410,14 +413,11 @@ describe WikiPage do
end
end
- describe "#update" do
- subject do
- create_page(title, "content")
- wiki.find_page(title)
- end
+ describe '#update' do
+ subject { create(:wiki_page, wiki: wiki, title: title) }
- it "updates the content of the page" do
- subject.update(content: "new content")
+ it 'updates the content of the page' do
+ subject.update(content: 'new content')
page = wiki.find_page(title)
expect([subject.content, page.content]).to all(eq('new content'))
@@ -429,24 +429,6 @@ describe WikiPage do
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
subject { existing_page }
@@ -514,9 +496,9 @@ describe WikiPage do
expect([subject, page]).to all(have_attributes(front_matter: be_empty, content: content))
end
- context 'but it is enabled for the project' do
+ context 'but it is enabled for the container' do
before do
- enable_front_matter_for_project
+ enable_front_matter_for(container)
end
it_behaves_like 'able to update front-matter'
@@ -556,7 +538,7 @@ describe WikiPage do
context 'when renaming a page' 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.title).to eq 'test page'
@@ -578,7 +560,7 @@ describe WikiPage do
context 'when moving a page' 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.title).to eq 'test page'
@@ -598,10 +580,7 @@ describe WikiPage do
end
context 'in subdir' do
- subject do
- create_page('foo/Existing Page', 'content')
- wiki.find_page('foo/Existing Page')
- end
+ subject { create(:wiki_page, wiki: wiki, title: 'foo/Existing Page') }
it 'moves the page to the root folder if the title is preceded by /' do
expect(subject.slug).to eq 'foo/Existing-Page'
@@ -639,7 +618,7 @@ describe WikiPage do
end
end
- describe "#destroy" do
+ describe "#delete" do
subject { existing_page }
it "deletes the page" do
@@ -671,10 +650,7 @@ describe WikiPage do
using RSpec::Parameterized::TableSyntax
let(:untitled_page) { described_class.new(wiki) }
- let(:directory_page) do
- create_page('parent directory/child page', 'test content')
- wiki.find_page('parent directory/child page')
- end
+ let(:directory_page) { create(:wiki_page, title: 'parent directory/child page') }
where(:page, :title, :changed) do
:untitled_page | nil | false
@@ -737,10 +713,7 @@ describe WikiPage do
end
context 'when the page is inside an actual directory' do
- subject do
- create_page('dir_1/dir_1_1/file', 'content')
- wiki.find_page('dir_1/dir_1_1/file')
- end
+ subject { create(:wiki_page, title: 'dir_1/dir_1_1/file') }
it 'returns the full directory hierarchy' do
expect(subject.directory).to eq('dir_1/dir_1_1')
@@ -787,6 +760,16 @@ describe WikiPage do
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
it 'returns the relative path to the partial to be used' do
expect(subject.to_partial_path).to eq('projects/wikis/wiki_page')
@@ -812,23 +795,23 @@ describe WikiPage do
other_page = create(:wiki_page)
expect(subject.slug).not_to eq(other_page.slug)
- expect(subject.project).not_to eq(other_page.project)
+ expect(subject.container).not_to eq(other_page.container)
expect(subject).not_to eq(other_page)
end
- it 'returns false for page with different slug on same project' do
- other_page = create(:wiki_page, project: subject.project)
+ it 'returns false for page with different slug on same container' do
+ other_page = create(:wiki_page, container: subject.container)
expect(subject.slug).not_to eq(other_page.slug)
- expect(subject.project).to eq(other_page.project)
+ expect(subject.container).to eq(other_page.container)
expect(subject).not_to eq(other_page)
end
- it 'returns false for page with the same slug on a different project' do
+ it 'returns false for page with the same slug on a different container' do
other_page = create(:wiki_page, title: existing_page.slug)
expect(subject.slug).to eq(other_page.slug)
- expect(subject.project).not_to eq(other_page.project)
+ expect(subject.container).not_to eq(other_page.container)
expect(subject).not_to eq(other_page)
end
end
@@ -858,19 +841,21 @@ describe WikiPage do
end
end
- private
-
- def remove_temp_repo(path)
- FileUtils.rm_rf path
- end
+ describe '#version_commit_timestamp' do
+ context 'for a new page' do
+ it 'returns nil' do
+ expect(new_page.version_commit_timestamp).to be_nil
+ end
+ end
- def commit_details
- Gitlab::Git::Wiki::CommitDetails.new(user.id, user.username, user.name, user.email, "test commit")
+ context 'for page that exists' do
+ it 'returns the timestamp of the commit' do
+ expect(existing_page.version_commit_timestamp).to eq(existing_page.version.commit.committed_date)
+ end
+ end
end
- def create_page(name, content)
- wiki.wiki.write_page(name, :markdown, content, commit_details)
- end
+ private
def get_slugs(page_or_dir)
if page_or_dir.is_a? WikiPage