Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/wiki_page_spec.rb')
-rw-r--r--spec/models/wiki_page_spec.rb118
1 files changed, 115 insertions, 3 deletions
diff --git a/spec/models/wiki_page_spec.rb b/spec/models/wiki_page_spec.rb
index a2ca6441f28..aa8b9ce58b9 100644
--- a/spec/models/wiki_page_spec.rb
+++ b/spec/models/wiki_page_spec.rb
@@ -7,7 +7,11 @@ RSpec.describe WikiPage do
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') }
+
+ let(:existing_page) do
+ create(:wiki_page, wiki: wiki, title: 'test page', content: 'test content', message: 'test commit')
+ wiki.find_page('test page')
+ end
subject { new_page }
@@ -45,9 +49,11 @@ RSpec.describe WikiPage do
let(:dir_1) do
WikiDirectory.new('dir_1', [wiki.find_page('dir_1/page_2')])
end
+
let(:dir_1_1) do
WikiDirectory.new('dir_1/dir_1_1', [wiki.find_page('dir_1/dir_1_1/page_3')])
end
+
let(:dir_2) do
pages = [wiki.find_page('dir_2/page_5'),
wiki.find_page('dir_2/page_4')]
@@ -257,14 +263,68 @@ RSpec.describe WikiPage do
subject.attributes.delete(:title)
expect(subject).not_to be_valid
- expect(subject.errors.keys).to contain_exactly(:title)
+ expect(subject.errors.messages).to eq(title: ["can't be blank"])
end
it "validates presence of content" do
subject.attributes.delete(:content)
expect(subject).not_to be_valid
- expect(subject.errors.keys).to contain_exactly(:content)
+ expect(subject.errors.messages).to eq(content: ["can't be blank"])
+ end
+
+ describe '#validate_content_size_limit' do
+ context 'with a new page' do
+ before do
+ stub_application_setting(wiki_page_max_content_bytes: 10)
+ end
+
+ it 'accepts content below the limit' do
+ subject.attributes[:content] = 'a' * 10
+
+ expect(subject).to be_valid
+ end
+
+ it 'rejects content exceeding the limit' do
+ subject.attributes[:content] = 'a' * 11
+
+ expect(subject).not_to be_valid
+ expect(subject.errors.messages).to eq(
+ content: ['is too long (11 Bytes). The maximum size is 10 Bytes.']
+ )
+ end
+
+ it 'counts content size in bytes rather than characters' do
+ subject.attributes[:content] = '💩💩💩'
+
+ expect(subject).not_to be_valid
+ expect(subject.errors.messages).to eq(
+ content: ['is too long (12 Bytes). The maximum size is 10 Bytes.']
+ )
+ end
+ end
+
+ context 'with an existing page exceeding the limit' do
+ subject { existing_page }
+
+ before do
+ subject
+ stub_application_setting(wiki_page_max_content_bytes: 11)
+ end
+
+ it 'accepts content when it has not changed' do
+ expect(subject).to be_valid
+ end
+
+ it 'rejects content when it has changed' do
+ subject.attributes[:content] = 'a' * 12
+
+ expect(subject).not_to be_valid
+ expect(subject.errors.messages).to eq(
+ content: ['is too long (12 Bytes). The maximum size is 11 Bytes.']
+ )
+ end
+ end
end
describe '#validate_path_limits' do
@@ -702,6 +762,58 @@ RSpec.describe WikiPage do
end
end
+ describe '#content_changed?' do
+ context 'with a new page' do
+ subject { new_page }
+
+ it 'returns true if content is set' do
+ subject.attributes[:content] = 'new'
+
+ expect(subject.content_changed?).to be(true)
+ end
+
+ it 'returns false if content is blank' do
+ subject.attributes[:content] = ' '
+
+ expect(subject.content_changed?).to be(false)
+ end
+ end
+
+ context 'with an existing page' do
+ subject { existing_page }
+
+ it 'returns false' do
+ expect(subject.content_changed?).to be(false)
+ end
+
+ it 'returns false if content is set to the same value' do
+ subject.attributes[:content] = 'test content'
+
+ expect(subject.content_changed?).to be(false)
+ end
+
+ it 'returns true if content is changed' do
+ subject.attributes[:content] = 'new'
+
+ expect(subject.content_changed?).to be(true)
+ end
+
+ it 'returns true if content is changed to a blank string' do
+ subject.attributes[:content] = ' '
+
+ expect(subject.content_changed?).to be(true)
+ end
+
+ it 'returns false if only the newline format has changed' do
+ expect(subject.page).to receive(:text_data).and_return("foo\nbar")
+
+ subject.attributes[:content] = "foo\r\nbar"
+
+ expect(subject.content_changed?).to be(false)
+ end
+ end
+ end
+
describe '#path' do
it 'returns the path when persisted' do
expect(existing_page.path).to eq('test-page.md')