2019-07-23 19:47:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe QualifiedDomainArrayValidator do
|
2019-12-24 09:07:44 +00:00
|
|
|
let(:qualified_domain_array_validator_test_class) do
|
|
|
|
Class.new do
|
|
|
|
include ActiveModel::Validations
|
2019-07-23 19:47:17 +00:00
|
|
|
|
2019-12-24 09:07:44 +00:00
|
|
|
attr_accessor :domain_array
|
2019-07-23 19:47:17 +00:00
|
|
|
|
2019-12-24 09:07:44 +00:00
|
|
|
def initialize(domain_array)
|
|
|
|
self.domain_array = domain_array
|
|
|
|
end
|
2019-07-23 19:47:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
let!(:record) do
|
2019-12-24 09:07:44 +00:00
|
|
|
qualified_domain_array_validator_test_class.new(['gitlab.com'])
|
2019-07-23 19:47:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
subject { validator.validate(record) }
|
|
|
|
|
2019-07-31 06:54:03 +00:00
|
|
|
shared_examples 'can be nil' do
|
|
|
|
it 'allows when attribute is nil' do
|
|
|
|
record.domain_array = nil
|
2019-07-23 19:47:17 +00:00
|
|
|
|
|
|
|
subject
|
|
|
|
|
2019-07-31 06:54:03 +00:00
|
|
|
expect(record.errors).to be_empty
|
2019-07-23 19:47:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-31 06:54:03 +00:00
|
|
|
shared_examples 'can be blank' do
|
|
|
|
it 'allows when attribute is blank' do
|
|
|
|
record.domain_array = []
|
2019-07-23 19:47:17 +00:00
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(record.errors).to be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'validations' do
|
|
|
|
let(:validator) { described_class.new(attributes: [:domain_array]) }
|
|
|
|
|
2019-07-31 06:54:03 +00:00
|
|
|
it_behaves_like 'can be blank'
|
2019-07-23 19:47:17 +00:00
|
|
|
|
|
|
|
it 'returns error when attribute is nil' do
|
|
|
|
record.domain_array = nil
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(record.errors).to be_present
|
2019-07-31 06:54:03 +00:00
|
|
|
expect(record.errors.first[1]).to eq('entries cannot be nil')
|
2019-07-23 19:47:17 +00:00
|
|
|
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'
|
2019-07-31 06:54:03 +00:00
|
|
|
it_behaves_like 'can be blank'
|
2019-07-23 19:47:17 +00:00
|
|
|
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'
|
2019-07-31 06:54:03 +00:00
|
|
|
it_behaves_like 'can be blank'
|
2019-07-23 19:47:17 +00:00
|
|
|
end
|
|
|
|
end
|