Commit 6d0e752c authored by Uday Aggarwal's avatar Uday Aggarwal

added min value for max_content validation

parent d1a6d7e8
...@@ -328,8 +328,7 @@ module ApplicationSettingsHelper ...@@ -328,8 +328,7 @@ 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
......
...@@ -272,8 +272,7 @@ class ApplicationSetting < ApplicationRecord ...@@ -272,8 +272,7 @@ class ApplicationSetting < ApplicationRecord
numericality: { greater_than_or_equal_to: 0 } numericality: { greater_than_or_equal_to: 0 }
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_or_equal_to: 1.kilobytes }
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,8 +165,7 @@ module ApplicationSettingImplementation ...@@ -165,8 +165,7 @@ 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,19 +408,11 @@ class WikiPage ...@@ -408,19 +408,11 @@ 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
min_size = Gitlab::CurrentSettings.wiki_page_min_content_bytes return if current_value <= max_size
return if current_value <= max_size && current_value >= min_size
if current_value > max_size
errors.add(:content, _('is too long (%{current_value}). The maximum size is %{max_size}.') % { errors.add(:content, _('is too long (%{current_value}). The maximum size is %{max_size}.') % {
current_value: ActiveSupport::NumberHelper.number_to_human_size(current_value), current_value: ActiveSupport::NumberHelper.number_to_human_size(current_value),
max_size: ActiveSupport::NumberHelper.number_to_human_size(max_size) 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
...@@ -72,8 +72,7 @@ RSpec.describe ApplicationSetting do ...@@ -72,8 +72,7 @@ RSpec.describe ApplicationSetting do
it { is_expected.not_to allow_value(nil).for(:push_event_activities_limit) } it { is_expected.not_to allow_value(nil).for(:push_event_activities_limit) }
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_or_equal_to(1024) }
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,7 +275,6 @@ RSpec.describe WikiPage do ...@@ -275,7 +275,6 @@ 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
...@@ -284,12 +283,6 @@ RSpec.describe WikiPage do ...@@ -284,12 +283,6 @@ 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
...@@ -299,15 +292,6 @@ RSpec.describe WikiPage do ...@@ -299,15 +292,6 @@ 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] = '💩💩💩'
...@@ -324,14 +308,13 @@ RSpec.describe WikiPage do ...@@ -324,14 +308,13 @@ 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 and exceeds the limit' do it 'rejects content when it has changed' do
subject.attributes[:content] = 'a' * 12 subject.attributes[:content] = 'a' * 12
expect(subject).not_to be_valid expect(subject).not_to be_valid
...@@ -339,15 +322,6 @@ RSpec.describe WikiPage do ...@@ -339,15 +322,6 @@ 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