Commit d1a6d7e8 authored by Uday Aggarwal's avatar Uday Aggarwal

added minimum limit to wiki page

parent 877c9eaf
...@@ -328,7 +328,8 @@ module ApplicationSettingsHelper ...@@ -328,7 +328,8 @@ module ApplicationSettingsHelper
:group_import_limit, :group_import_limit,
:group_export_limit, :group_export_limit,
:group_download_export_limit, :group_download_export_limit,
:wiki_page_max_content_bytes :wiki_page_max_content_bytes,
:wiki_page_min_content_bytes
] ]
end end
......
...@@ -273,6 +273,7 @@ class ApplicationSetting < ApplicationRecord ...@@ -273,6 +273,7 @@ class ApplicationSetting < ApplicationRecord
validates :snippet_size_limit, numericality: { only_integer: true, greater_than: 0 } validates :snippet_size_limit, numericality: { only_integer: true, greater_than: 0 }
validates :wiki_page_max_content_bytes, numericality: { only_integer: true, greater_than: 0 } validates :wiki_page_max_content_bytes, numericality: { only_integer: true, greater_than: 0 }
validates :wiki_page_min_content_bytes, numericality: { only_integer: true, greater_than: 0 }
validates :email_restrictions, untrusted_regexp: true validates :email_restrictions, untrusted_regexp: true
......
...@@ -165,7 +165,8 @@ module ApplicationSettingImplementation ...@@ -165,7 +165,8 @@ module ApplicationSettingImplementation
user_default_external: false, user_default_external: false,
user_default_internal_regex: nil, user_default_internal_regex: nil,
user_show_add_ssh_key_message: true, user_show_add_ssh_key_message: true,
wiki_page_max_content_bytes: 50.megabytes wiki_page_max_content_bytes: 50.megabytes,
wiki_page_min_content_bytes: 1.kilobytes
} }
end end
......
...@@ -408,11 +408,19 @@ class WikiPage ...@@ -408,11 +408,19 @@ class WikiPage
def validate_content_size_limit def validate_content_size_limit
current_value = raw_content.to_s.bytesize current_value = raw_content.to_s.bytesize
max_size = Gitlab::CurrentSettings.wiki_page_max_content_bytes max_size = Gitlab::CurrentSettings.wiki_page_max_content_bytes
return if current_value <= max_size min_size = Gitlab::CurrentSettings.wiki_page_min_content_bytes
return if current_value <= max_size && current_value >= min_size
errors.add(:content, _('is too long (%{current_value}). The maximum size is %{max_size}.') % { if current_value > max_size
current_value: ActiveSupport::NumberHelper.number_to_human_size(current_value), errors.add(:content, _('is too long (%{current_value}). The maximum size is %{max_size}.') % {
max_size: ActiveSupport::NumberHelper.number_to_human_size(max_size) current_value: ActiveSupport::NumberHelper.number_to_human_size(current_value),
}) max_size: ActiveSupport::NumberHelper.number_to_human_size(max_size)
})
else
errors.add(:content, _('is too short (%{current_value}). The minimum size is %{min_size}.') % {
current_value: ActiveSupport::NumberHelper.number_to_human_size(current_value),
min_size: ActiveSupport::NumberHelper.number_to_human_size(min_size)
})
end
end end
end end
...@@ -73,6 +73,7 @@ RSpec.describe ApplicationSetting do ...@@ -73,6 +73,7 @@ RSpec.describe ApplicationSetting do
it { is_expected.to validate_numericality_of(:snippet_size_limit).only_integer.is_greater_than(0) } it { is_expected.to validate_numericality_of(:snippet_size_limit).only_integer.is_greater_than(0) }
it { is_expected.to validate_numericality_of(:wiki_page_max_content_bytes).only_integer.is_greater_than(0) } it { is_expected.to validate_numericality_of(:wiki_page_max_content_bytes).only_integer.is_greater_than(0) }
it { is_expected.to validate_numericality_of(:wiki_page_min_content_bytes).only_integer.is_greater_than(0) }
it { is_expected.to validate_presence_of(:max_artifacts_size) } it { is_expected.to validate_presence_of(:max_artifacts_size) }
it { is_expected.to validate_numericality_of(:max_artifacts_size).only_integer.is_greater_than(0) } it { is_expected.to validate_numericality_of(:max_artifacts_size).only_integer.is_greater_than(0) }
it { is_expected.to validate_presence_of(:max_pages_size) } it { is_expected.to validate_presence_of(:max_pages_size) }
......
...@@ -275,6 +275,7 @@ RSpec.describe WikiPage do ...@@ -275,6 +275,7 @@ RSpec.describe WikiPage do
context 'with a new page' do context 'with a new page' do
before do before do
stub_application_setting(wiki_page_max_content_bytes: 10) stub_application_setting(wiki_page_max_content_bytes: 10)
stub_application_setting(wiki_page_min_content_bytes: 2)
end end
it 'accepts content below the limit' do it 'accepts content below the limit' do
...@@ -283,6 +284,12 @@ RSpec.describe WikiPage do ...@@ -283,6 +284,12 @@ RSpec.describe WikiPage do
expect(subject).to be_valid expect(subject).to be_valid
end end
it 'accepts content above the limit' do
subject.attributes[:content] = 'a' * 2
expect(subject).to be_valid
end
it 'rejects content exceeding the limit' do it 'rejects content exceeding the limit' do
subject.attributes[:content] = 'a' * 11 subject.attributes[:content] = 'a' * 11
...@@ -292,6 +299,15 @@ RSpec.describe WikiPage do ...@@ -292,6 +299,15 @@ RSpec.describe WikiPage do
) )
end end
it 'rejects content below the limit' do
subject.attributes[:content] = 'a'
expect(subject).not_to be_valid
expect(subject.errors.messages).to eq(
content: ['is too short (1 Bytes). The minimum size is 2 Bytes.']
)
end
it 'counts content size in bytes rather than characters' do it 'counts content size in bytes rather than characters' do
subject.attributes[:content] = '💩💩💩' subject.attributes[:content] = '💩💩💩'
...@@ -308,13 +324,14 @@ RSpec.describe WikiPage do ...@@ -308,13 +324,14 @@ RSpec.describe WikiPage do
before do before do
subject subject
stub_application_setting(wiki_page_max_content_bytes: 11) stub_application_setting(wiki_page_max_content_bytes: 11)
stub_application_setting(wiki_page_min_content_bytes: 2)
end end
it 'accepts content when it has not changed' do it 'accepts content when it has not changed' do
expect(subject).to be_valid expect(subject).to be_valid
end end
it 'rejects content when it has changed' do it 'rejects content when it has changed and exceeds the limit' do
subject.attributes[:content] = 'a' * 12 subject.attributes[:content] = 'a' * 12
expect(subject).not_to be_valid expect(subject).not_to be_valid
...@@ -322,6 +339,15 @@ RSpec.describe WikiPage do ...@@ -322,6 +339,15 @@ RSpec.describe WikiPage do
content: ['is too long (12 Bytes). The maximum size is 11 Bytes.'] content: ['is too long (12 Bytes). The maximum size is 11 Bytes.']
) )
end end
it 'rejects content when it has changed and is below the limit' do
subject.attributes[:content] = 'a' * 1
expect(subject).not_to be_valid
expect(subject.errors.messages).to eq(
content: ['is too short (1 Bytes). The minimum size is 2 Bytes.']
)
end
end 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