diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 23c180647..a5a680439 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -1,7 +1,7 @@ require 'rails_helper' describe Contact do - before :all do + before :example do Fabricate(:zonefile_setting, origin: 'ee') @api_user = Fabricate(:api_user) end @@ -17,28 +17,10 @@ describe Contact do end context 'with invalid attribute' do - before :all do + before :example do @contact = Contact.new end - it 'should not be valid' do - @contact.valid? - @contact.errors.full_messages.should match_array([ - "Name Required parameter missing - name", - "Phone Required parameter missing - phone", - "Phone Phone nr is invalid", - "Email Required parameter missing - email", - "Email Email is invalid", - "Ident Required parameter missing - ident", - "Registrar is missing", - "Ident type is missing", - "City is missing", - "Country code is missing", - "Street is missing", - "Zip is missing" - ]) - end - it 'should not have creator' do @contact.creator.should == nil end @@ -66,6 +48,7 @@ describe Contact do end it 'should validate correct country code' do + @contact.ident = 1 @contact.ident_type = 'org' @contact.ident_country_code = 'EE' @contact.valid? @@ -79,14 +62,14 @@ describe Contact do @contact.ident_country_code = 'INVALID' @contact.valid? - @contact.errors[:ident].should == - ['Ident country code is not valid, should be in ISO_3166-1 alpha 2 format'] + 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.valid? + @contact.validate @contact.ident_country_code.should == 'EE' end @@ -102,9 +85,11 @@ describe Contact do end it 'should not allow double status' do - @contact.statuses = ['ok', 'ok'] - @contact.valid? - @contact.errors[:statuses].should == ['not uniq'] + contact = described_class.new(statuses: %w(ok ok)) + + contact.validate + + expect(contact.statuses).to eq(%w(ok)) end it 'should have no related domain descriptions' do @@ -123,21 +108,10 @@ describe Contact do end context 'with valid attributes' do - before :all do + before :example do @contact = Fabricate(:contact) end - it 'should be valid' do - @contact.valid? - @contact.errors.full_messages.should match_array([]) - end - - it 'should be valid twice' do - @contact = Fabricate(:contact) - @contact.valid? - @contact.errors.full_messages.should match_array([]) - end - it 'should have one version' do with_versioning do @contact.versions.reload.should == [] @@ -153,10 +127,11 @@ describe Contact do end it 'org should be valid' do - @contact.ident_type = 'org' - @contact.ident = '1234' - @contact.valid? - @contact.errors.full_messages.should match_array([]) + 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 @@ -188,45 +163,11 @@ describe Contact do contact.statuses.should == [Contact::SERVER_UPDATE_PROHIBITED] end - it 'should have linked status when domain' do - contact = Fabricate(:contact) - tech_domain_contact = Fabricate(:tech_domain_contact, contact_id: contact.id) - contact.statuses.should == %w(ok) - domain = Fabricate(:domain, tech_domain_contacts: [tech_domain_contact]) - - contact = domain.contacts.first - contact.save - contact.statuses.sort.should == %w(linked ok) - - contact = domain.contacts.second - contact.save - contact.statuses.sort.should == %w(linked ok) - end - - it 'should not have linked status when no domain' do - @admin_domain_contact = Fabricate(:admin_domain_contact, contact_id: @contact.id) - @domain = Fabricate(:domain, admin_domain_contacts: [@admin_domain_contact]) - contact = @domain.contacts.first - contact.save - - contact.statuses.sort.should == %w(linked ok) - - contact.domains.first.destroy - contact.reload - contact.statuses.should == %w(ok) - end - it 'should have code' do - @contact.code.should =~ /FIXED:..../ - end + registrar = Fabricate.create(:registrar, code: 'registrarcode') + contact = Fabricate.create(:contact, registrar: registrar, code: 'contactcode') - it 'should have linked status when domain is created' do - # @admin_domain_contact = Fabricate(:admin_domain_contact) - # @domain = Fabricate(:domain, admin_domain_contacts: [@admin_domain_contact]) - # puts @domain.contacts.size - # contact = @domain.contacts.first - - # contact.statuses.map(&:value).should == %w(ok linked) + expect(contact.code).to eq('REGISTRARCODE:CONTACTCODE') end it 'should save status notes' do @@ -258,17 +199,17 @@ describe Contact do it 'should have related domain descriptions hash' do contact = @domain.registrant contact.reload # somehow it registrant_domains are empty? - contact.related_domain_descriptions.should == { "#{@domain.name}" => [:registrant] } + contact.related_domain_descriptions.should == {"#{@domain.name}" => [:registrant]} end it 'should have related domain descriptions hash when find directly' do contact = @domain.registrant - Contact.find(contact.id).related_domain_descriptions.should == { "#{@domain.name}" => [:registrant] } + Contact.find(contact.id).related_domain_descriptions.should == {"#{@domain.name}" => [:registrant]} end it 'should have related domain descriptions hash' do contact = @domain.contacts.first - contact.related_domain_descriptions.should == { "#{@domain.name}" => [:admin] } + contact.related_domain_descriptions.should == {"#{@domain.name}" => [:admin]} end it 'should fully validate email syntax for old records' do @@ -281,7 +222,7 @@ describe Contact do end context 'as birthday' do - before :all do + before :example do @contact.ident_type = 'birthday' end @@ -300,13 +241,13 @@ describe Contact do @contact.ident = date @contact.valid? @contact.errors.full_messages.should == - ["Ident Ident not in valid birthady format, should be YYYY-MM-DD"] + ["Ident Ident not in valid birthady format, should be YYYY-MM-DD"] end end end context 'with callbacks' do - before :all do + before :example do # Ensure callbacks are not taken out from other specs Contact.set_callback(:create, :before, :generate_auth_info) end @@ -314,6 +255,7 @@ describe Contact do context 'after create' do it 'should not generate a new code when code is present' do @contact = Fabricate.build(:contact, + registrar: Fabricate(:registrar, code: 'FIXED'), code: 'FIXED:new-code', auth_info: 'qwe321') @contact.code.should == 'FIXED:new-code' # still new record @@ -321,20 +263,21 @@ describe Contact do @contact.code.should == 'FIXED:NEW-CODE' end - it 'should not allaw to use same code' do - @contact = Fabricate.build(:contact, - code: 'FIXED:new-code', - auth_info: 'qwe321') - @contact.code.should == 'FIXED:new-code' # still new record - @contact.save.should == true - @contact.code.should == 'FIXED:NEW-CODE' + it 'should not allow to use same code' do + registrar = Fabricate.create(:registrar, code: 'FIXED') + Fabricate.create(:contact, + registrar: registrar, + code: 'FIXED:new-code', + auth_info: 'qwe321') @contact = Fabricate.build(:contact, + registrar: registrar, code: 'FIXED:new-code', auth_info: 'qwe321') - @contact.code.should == 'FIXED:new-code' # still new record - @contact.valid? - @contact.errors.full_messages.should == ["Code Contact id already exists"] + + @contact.validate + + expect(@contact.errors).to have_key(:code) end it 'should generate a new password' do @@ -344,20 +287,14 @@ describe Contact do @contact.auth_info.should_not be_nil end - it 'should not allow same code' do - @double_contact = Fabricate.build(:contact, code: @contact.code) - @double_contact.valid? - @double_contact.errors.full_messages.should == ["Code Contact id already exists"] - end - it 'should allow supported code format' do - @contact = Fabricate.build(:contact, code: 'CID:REG1:12345') + @contact = Fabricate.build(:contact, code: 'CID:REG1:12345', registrar: Fabricate(:registrar, code: 'FIXED')) @contact.valid? @contact.errors.full_messages.should == [] end it 'should not allow unsupported characters in code' do - @contact = Fabricate.build(:contact, code: 'unsupported!ÄÖÜ~?') + @contact = Fabricate.build(:contact, code: 'unsupported!ÄÖÜ~?', registrar: Fabricate(:registrar, code: 'FIXED')) @contact.valid? @contact.errors.full_messages.should == ['Code is invalid'] end @@ -368,15 +305,16 @@ describe Contact do end it 'should not ignore empty spaces as code and generate new one' do - @contact = Fabricate.build(:contact, code: ' ') + @contact = Fabricate.build(:contact, code: ' ', registrar: Fabricate(:registrar, code: 'FIXED')) @contact.valid?.should == true @contact.code.should =~ /FIXED:..../ end end context 'after update' do - before :all do + before :example do @contact = Fabricate.build(:contact, + registrar: Fabricate(:registrar, code: 'FIXED'), code: '123asd', auth_info: 'qwe321') @contact.save