Improve Ident validation, extract Ident from Contact

#569
This commit is contained in:
Artur Beljajev 2017-08-18 13:24:56 +03:00
parent c5e1516d89
commit feb8d83a26
17 changed files with 809 additions and 195 deletions

View file

@ -0,0 +1,217 @@
require 'active_model'
require 'lib/validators/iso31661_alpha2'
require 'lib/validators/iso8601'
RSpec.describe Contact::Ident, db: false do
let(:ident) { described_class.new }
describe 'country code' do
it_behaves_like 'iso31661_alpha2' do
let(:model) { ident }
let(:attribute) { :country_code }
end
end
describe 'code validation' do
it 'rejects absent' do
ident.code = nil
ident.validate
expect(ident.errors).to be_added(:code, :blank)
end
context 'when type is :birthday' do
let(:ident) { described_class.new(type: 'birthday') }
it_behaves_like 'iso8601' do
let(:model) { ident }
let(:attribute) { :code }
end
end
context 'when type is not :birthday' do
let(:ident) { described_class.new(type: 'priv') }
it 'accepts any' do
ident.code = '%123456789%'
ident.validate
expect(ident.errors).to_not include(:code)
end
end
context 'when country code is EE' do
context 'when type is :priv' do
let(:ident) { described_class.new(country_code: 'EE', type: 'priv') }
it 'rejects invalid' do
ident.code = 'invalid'
ident.validate
expect(ident.errors).to be_added(:code, :invalid_national_id, country: 'Estonia')
end
it 'accepts valid' do
ident.code = '47101010033'
ident.validate
expect(ident.errors).to_not be_added(:code, :invalid_national_id, country: 'Estonia')
end
end
context 'when ident type is :org' do
let(:ident) { described_class.new(country_code: 'EE', type: 'org') }
it 'rejects invalid' do
ident.code = '1' * 7
ident.validate
expect(ident.errors).to be_added(:code, :invalid_reg_no, country: 'Estonia')
end
it 'accepts valid length' do
ident.code = '1' * 8
ident.validate
expect(ident.errors).to_not be_added(:code, :invalid_reg_no, country: 'Estonia')
end
end
end
context 'when ident country code is not EE' do
let(:ident) { described_class.new(country_code: 'US') }
it 'accepts any' do
ident.code = 'test-123456789'
ident.validate
expect(ident.errors).to_not include(:code)
end
end
it 'translates :invalid_national_id error message' do
expect(ident.errors.generate_message(:code, :invalid_national_id, country: 'Germany'))
.to eq('does not conform to national identification number format of Germany')
end
it 'translates :invalid_reg_no error message' do
expect(ident.errors.generate_message(:code, :invalid_reg_no, country: 'Germany'))
.to eq('does not conform to registration number format of Germany')
end
end
describe 'type validation' do
before do
allow(described_class).to receive(:types).and_return(%w(valid))
end
it 'rejects absent' do
ident.type = nil
ident.validate
expect(ident.errors).to be_added(:type, :blank)
end
it 'rejects invalid' do
ident.type = 'invalid'
ident.validate
expect(ident.errors).to be_added(:type, :inclusion)
end
it 'accepts valid' do
ident.type = 'valid'
ident.validate
expect(ident.errors).to_not be_added(:type, :inclusion)
end
end
describe 'country code validation' do
it 'rejects absent' do
ident.country_code = nil
ident.validate
expect(ident.errors).to be_added(:country_code, :blank)
end
end
describe 'mismatch validation' do
let(:ident) { described_class.new(type: 'test', country_code: 'DE') }
before do
mismatches = [Contact::Ident::Mismatch.new('test', Country.new('DE'))]
allow(described_class).to receive(:mismatches).and_return(mismatches)
end
it 'rejects mismatched' do
ident.validate
expect(ident.errors).to be_added(:base, :mismatch, type: 'test', country: 'Germany')
end
it 'accepts matched' do
ident.validate
expect(ident.errors).to_not be_added(:base, :mismatch, type: 'another-test', country: 'Germany')
end
it 'translates :mismatch error message' do
expect(ident.errors.generate_message(:base, :mismatch, type: 'test', country: 'Germany'))
.to eq('Ident type "test" is invalid for Germany')
end
end
describe '::types' do
it 'returns types' do
types = %w[
org
priv
birthday
]
expect(described_class.types).to eq(types)
end
end
describe '::mismatches' do
it 'returns mismatches' do
mismatches = [
Contact::Ident::Mismatch.new('birthday', Country.new('EE')),
]
expect(described_class.mismatches).to eq(mismatches)
end
end
describe '#birthday?' do
context 'when type is birthday' do
subject(:ident) { described_class.new(type: 'birthday') }
it { is_expected.to be_birthday }
end
context 'when type is not birthday' do
subject(:ident) { described_class.new(type: 'priv') }
it { is_expected.to_not be_birthday }
end
end
describe '#national_id?' do
context 'when type is priv' do
subject(:ident) { described_class.new(type: 'priv') }
it { is_expected.to be_national_id }
end
context 'when type is not' do
subject(:ident) { described_class.new(type: 'org') }
it { is_expected.to_not be_national_id }
end
end
describe '#reg_no?' do
context 'when type is birthday' do
subject(:ident) { described_class.new(type: 'org') }
it { is_expected.to be_reg_no }
end
context 'when type is not birthday' do
subject(:ident) { described_class.new(type: 'priv') }
it { is_expected.to_not be_reg_no }
end
end
describe '#country' do
let(:ident) { described_class.new(country_code: 'US') }
it 'returns country' do
expect(ident.country).to eq(Country.new('US'))
end
end
end

View file

@ -28,45 +28,6 @@ RSpec.describe Contact do
@contact.updator.should == nil
end
it 'should require country code when org' do
@contact.ident_type = 'org'
@contact.valid?
@contact.errors[:ident_country_code].should == ['is missing']
end
it 'should require country code when priv' do
@contact.ident_type = 'priv'
@contact.valid?
@contact.errors[:ident_country_code].should == ['is missing']
end
it 'should validate correct country code' do
@contact.ident = 1
@contact.ident_type = 'org'
@contact.ident_country_code = 'EE'
@contact.valid?
@contact.errors[:ident_country_code].should == []
end
it 'should require valid country code' do
@contact.ident = '123'
@contact.ident_type = 'org'
@contact.ident_country_code = 'INVALID'
@contact.valid?
expect(@contact.errors).to have_key(:ident)
end
it 'should convert to alpha2 country code' do
@contact.ident = 1
@contact.ident_type = 'org'
@contact.ident_country_code = 'ee'
@contact.validate
@contact.ident_country_code.should == 'EE'
end
it 'should not have any versions' do
@contact.versions.should == []
end
@ -119,14 +80,6 @@ RSpec.describe Contact do
@contact.domains_present?.should == false
end
it 'org should be valid' do
contact = Fabricate.build(:contact, ident_type: 'org', ident: '1' * 8)
contact.validate
contact.errors.full_messages.should match_array([])
end
it 'should not overwrite code' do
old_code = @contact.code
@contact.code = 'CID:REG1:should-not-overwrite-old-code-12345'
@ -217,31 +170,6 @@ RSpec.describe Contact do
end
end
context 'as birthday' do
before :example do
@contact.ident_type = 'birthday'
end
it 'birthday should be valid' do
valid = ['2012-12-11', '1990-02-16']
valid.each do |date|
@contact.ident = date
@contact.valid?
@contact.errors.full_messages.should match_array([])
end
end
it 'birthday should be invalid' do
invalid = ['123' '12/12/2012', 'aaaa', '12/12/12', '02-11-1999']
invalid.each do |date|
@contact.ident = date
@contact.valid?
@contact.errors.full_messages.should ==
["Ident Ident not in valid birthday format, should be YYYY-MM-DD"]
end
end
end
context 'with callbacks' do
before :example do
# Ensure callbacks are not taken out from other specs
@ -445,7 +373,7 @@ RSpec.describe Contact do
end
end
describe 'country code validation' do
describe 'country code validation', db: false do
let(:contact) { described_class.new(country_code: 'test') }
it 'rejects invalid' do
@ -455,6 +383,28 @@ RSpec.describe Contact do
end
end
describe 'identifier validation', db: false do
let(:contact) { described_class.new }
it 'rejects invalid' do
ident = Contact::Ident.new
ident.validate
contact.identifier = ident
contact.validate
expect(contact.errors).to be_added(:identifier, :invalid)
end
it 'accepts valid' do
ident = Contact::Ident.new(code: 'test', type: 'priv', country_code: 'US')
ident.validate
contact.identifier = ident
contact.validate
expect(contact.errors).to_not be_added(:identifier, :invalid)
end
end
describe '#remove_address' do
let(:contact) { described_class.new(city: 'test',
street: 'test',
@ -561,4 +511,13 @@ RSpec.describe Contact do
expect(domain_names).to eq({ 'test.com' => %i[admin_domain_contact].to_set })
end
end
it 'normalizes ident country code', db: false do
contact = described_class.new
contact.ident_country_code = 'ee'
contact.validate
expect(contact.ident_country_code).to eq('EE')
end
end