diff options
Diffstat (limited to 'spec/validators')
| -rw-r--r-- | spec/validators/color_validator_spec.rb | 43 | ||||
| -rw-r--r-- | spec/validators/public_url_validator_spec.rb | 24 | ||||
| -rw-r--r-- | spec/validators/qualified_domain_array_validator_spec.rb | 103 | ||||
| -rw-r--r-- | spec/validators/system_hook_url_validator_spec.rb | 8 |
4 files changed, 155 insertions, 23 deletions
diff --git a/spec/validators/color_validator_spec.rb b/spec/validators/color_validator_spec.rb new file mode 100644 index 00000000000..e5a38ac9372 --- /dev/null +++ b/spec/validators/color_validator_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe ColorValidator do + using RSpec::Parameterized::TableSyntax + + subject do + Class.new do + include ActiveModel::Model + include ActiveModel::Validations + attr_accessor :color + validates :color, color: true + end.new + end + + where(:color, :is_valid) do + '#000abc' | true + '#aaa' | true + '#BBB' | true + '#cCc' | true + '#ffff' | false + '#000111222' | false + 'invalid' | false + '000' | false + end + + with_them do + it 'only accepts valid colors' do + subject.color = color + + expect(subject.valid?).to eq(is_valid) + end + end + + it 'fails fast for long invalid string' do + subject.color = '#' + ('0' * 50_000) + 'xxx' + + expect do + Timeout.timeout(5.seconds) { subject.valid? } + end.not_to raise_error + end +end diff --git a/spec/validators/public_url_validator_spec.rb b/spec/validators/public_url_validator_spec.rb index f6364fb1dd5..3cbf1002730 100644 --- a/spec/validators/public_url_validator_spec.rb +++ b/spec/validators/public_url_validator_spec.rb @@ -2,27 +2,5 @@ require 'spec_helper' describe PublicUrlValidator do include_examples 'url validator examples', AddressableUrlValidator::DEFAULT_OPTIONS[:schemes] - - context 'by default' do - let(:validator) { described_class.new(attributes: [:link_url]) } - let!(:badge) { build(:badge, link_url: 'http://www.example.com') } - - subject { validator.validate(badge) } - - it 'blocks urls pointing to localhost' do - badge.link_url = 'https://127.0.0.1' - - subject - - expect(badge.errors).to be_present - end - - it 'blocks urls pointing to the local network' do - badge.link_url = 'https://192.168.1.1' - - subject - - expect(badge.errors).to be_present - end - end + include_examples 'public url validator examples', allow_local_requests_from_web_hooks_and_services: true end diff --git a/spec/validators/qualified_domain_array_validator_spec.rb b/spec/validators/qualified_domain_array_validator_spec.rb new file mode 100644 index 00000000000..6beb4c67f6f --- /dev/null +++ b/spec/validators/qualified_domain_array_validator_spec.rb @@ -0,0 +1,103 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe QualifiedDomainArrayValidator do + class TestClass + include ActiveModel::Validations + + attr_accessor :domain_array + + def initialize(domain_array) + self.domain_array = domain_array + end + end + + let!(:record) do + TestClass.new(['gitlab.com']) + end + + subject { validator.validate(record) } + + shared_examples 'can be nil' do + it 'allows when attribute is nil' do + record.domain_array = nil + + subject + + expect(record.errors).to be_empty + end + end + + shared_examples 'can be blank' do + it 'allows when attribute is blank' do + record.domain_array = [] + + subject + + expect(record.errors).to be_empty + end + end + + describe 'validations' do + let(:validator) { described_class.new(attributes: [:domain_array]) } + + it_behaves_like 'can be blank' + + it 'returns error when attribute is nil' do + record.domain_array = nil + + subject + + expect(record.errors).to be_present + expect(record.errors.first[1]).to eq('entries cannot be nil') + end + + it 'allows when domain is valid' do + subject + + expect(record.errors).to be_empty + end + + it 'returns error when domain contains unicode' do + record.domain_array = ['ğitlab.com'] + + subject + + expect(record.errors).to be_present + expect(record.errors.first[1]).to eq 'unicode domains should use IDNA encoding' + end + + it 'returns error when entry is larger than 255 chars' do + record.domain_array = ['a' * 256] + + subject + + expect(record.errors).to be_present + expect(record.errors.first[1]).to eq 'entries cannot be larger than 255 characters' + end + + it 'returns error when entry contains HTML tags' do + record.domain_array = ['gitlab.com<h1>something</h1>'] + + subject + + expect(record.errors).to be_present + expect(record.errors.first[1]).to eq 'entries cannot contain HTML tags' + end + end + + context 'when allow_nil is set to true' do + let(:validator) { described_class.new(attributes: [:domain_array], allow_nil: true) } + + it_behaves_like 'can be nil' + it_behaves_like 'can be blank' + end + + context 'when allow_blank is set to true' do + let(:validator) { described_class.new(attributes: [:domain_array], allow_blank: true) } + + it_behaves_like 'can be nil' + it_behaves_like 'can be blank' + end +end diff --git a/spec/validators/system_hook_url_validator_spec.rb b/spec/validators/system_hook_url_validator_spec.rb new file mode 100644 index 00000000000..02384bbd1ce --- /dev/null +++ b/spec/validators/system_hook_url_validator_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe SystemHookUrlValidator do + include_examples 'url validator examples', AddressableUrlValidator::DEFAULT_OPTIONS[:schemes] + include_examples 'public url validator examples', allow_local_requests_from_system_hooks: true +end |
