diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index 388bb0aa4..24ba0e671 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -51,12 +51,15 @@ class Epp::Contact < Contact def new(frame, registrar) return super if frame.blank? - custom_code = - if frame.css('id').text.present? - "#{registrar.code}:#{frame.css('id').text.parameterize}" - else - nil + custom_code = frame.css('id').text + + # add prefix when needed + if custom_code.present? + prefix, custom = custom_code.split(':') + if prefix != registrar.code && custom != registrar.code + custom_code = "#{registrar.code}:#{custom_code}" end + end super( attrs_from(frame).merge( diff --git a/app/models/registrar.rb b/app/models/registrar.rb index b1826abf2..8436f7873 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -13,8 +13,7 @@ class Registrar < ActiveRecord::Base belongs_to :country_deprecated, foreign_key: :country_id validates :name, :reg_no, :country_code, :email, :code, presence: true - validates :name, :reg_no, :reference_no, uniqueness: true - validate :set_code, if: :new_record? + validates :name, :reg_no, :reference_no, :code, uniqueness: true before_validation :generate_iso_11649_reference_no def generate_iso_11649_reference_no @@ -141,24 +140,4 @@ class Registrar < ActiveRecord::Base def code=(code) self[:code] = code.upcase if new_record? && code.present? end - - def contact_prefix - "CID:#{code}:" - end - - private - - def set_code - return false if name.blank? - new_code = name.parameterize - - # ensure code is always uniq automatically for a new record - seq = 1 - while self.class.find_by_code(new_code) - new_code += seq.to_s - seq += 1 - end - - self.code = new_code - end end diff --git a/app/views/admin/registrars/_form.haml b/app/views/admin/registrars/_form.haml index f456ddde1..10698f338 100644 --- a/app/views/admin/registrars/_form.haml +++ b/app/views/admin/registrars/_form.haml @@ -81,7 +81,7 @@ .col-md-4.control-label = f.label :code .col-md-7 - = f.text_field(:code, class: 'form-control') + = f.text_field(:code, class: 'form-control', disabled: !f.object.new_record?) %hr .row diff --git a/spec/epp/contact_spec.rb b/spec/epp/contact_spec.rb index 0c745dc54..7b464eff1 100644 --- a/spec/epp/contact_spec.rb +++ b/spec/epp/contact_spec.rb @@ -142,12 +142,58 @@ describe 'EPP Contact', epp: true do cr_date.text.to_time.should be_within(5).of(Time.zone.now) end - it 'successfully saves custom code' do - response = create_request({ id: { value: '12345' } }) + it 'should add registrar prefix for code when missing' do + response = create_request({ id: { value: 'abc:ABC:12345' } }) response[:msg].should == 'Command completed successfully' response[:result_code].should == '1000' - Contact.last.code.should == 'registrar1:12345' + Contact.last.code.should == 'FIRST0:abc:ABC:12345' + end + + it 'should add registrar prefix for code when missing' do + response = create_request({ id: { value: 'abc12345' } }) + response[:msg].should == 'Command completed successfully' + response[:result_code].should == '1000' + + Contact.last.code.should == 'FIRST0:abc12345' + end + + it 'should not allow spaces in custom code' do + response = create_request({ id: { value: 'abc 123' } }) + response[:msg].should == 'is invalid [code]' + response[:result_code].should == '2005' + end + + it 'should not add registrar prefix for code when prefix present' do + response = create_request({ id: { value: 'FIRST0:abc:ABC:NEW:12345' } }) + response[:msg].should == 'Command completed successfully' + response[:result_code].should == '1000' + + Contact.last.code.should == 'FIRST0:abc:ABC:NEW:12345' + end + + it 'should not add registrar prefix for code when prefix present' do + response = create_request({ id: { value: 'FIRST0:abc22' } }) + response[:msg].should == 'Command completed successfully' + response[:result_code].should == '1000' + + Contact.last.code.should == 'FIRST0:abc22' + end + + it 'should add registrar prefix for code does not match exactly to prefix' do + response = create_request({ id: { value: 'first0:abc:ABC:11111' } }) + response[:msg].should == 'Command completed successfully' + response[:result_code].should == '1000' + + Contact.last.code.should == 'FIRST0:first0:abc:ABC:11111' + end + + it 'should ignore custom code when value is prefix' do + response = create_request({ id: { value: 'FIRST0' } }) + response[:msg].should == 'Command completed successfully' + response[:result_code].should == '1000' + + Contact.last.code.match(':').should == nil end it 'should generate server id when id is empty' do diff --git a/spec/fabricators/registrar_fabricator.rb b/spec/fabricators/registrar_fabricator.rb index 33e4053f3..59fed19c5 100644 --- a/spec/fabricators/registrar_fabricator.rb +++ b/spec/fabricators/registrar_fabricator.rb @@ -7,7 +7,7 @@ Fabricator(:registrar) do zip 'Postal' email 'info@registrar1.ee' country_code 'EE' - code 'REG' + code { sequence(:code) { |i| "REGISTRAR#{i}" } } reference_no { sequence(:reference_no) { |i| "RF#{i}" } } accounts(count: 1) end @@ -24,6 +24,7 @@ Fabricator(:registrar1, from: :registrar) do state 'County' zip 'Postal' email 'info@registrar1.ee' + code { sequence(:code) { |i| "FIRST#{i}" } } end Fabricator(:registrar2, from: :registrar) do @@ -34,6 +35,7 @@ Fabricator(:registrar2, from: :registrar) do state 'County' zip 'Postal' email 'info@registrar2.ee' + code { sequence(:code) { |i| "SECOND#{i}" } } end Fabricator(:eis, from: :registrar) do @@ -48,5 +50,6 @@ Fabricator(:eis, from: :registrar) do street 'Paldiski mnt 80' zip '10617' url 'www.internet.ee' + code { sequence(:code) { |i| "EIS#{i}" } } accounts(count: 1) { Fabricate(:account, account_activities: []) } end diff --git a/spec/models/registrar_spec.rb b/spec/models/registrar_spec.rb index a5b763db9..63ec34bfc 100644 --- a/spec/models/registrar_spec.rb +++ b/spec/models/registrar_spec.rb @@ -17,7 +17,7 @@ describe Registrar do 'Country code is missing', 'Name is missing', 'Reg no is missing', - 'Code is missing', + 'Code is missing' ]) end @@ -57,6 +57,14 @@ describe Registrar do @registrar.errors.full_messages.should match_array([]) end + it 'should validates uniqueness of code' do + registrar = Fabricate.build(:registrar, code: @registrar.code) + registrar.valid? + registrar.errors.full_messages.should match_array([ + 'Code has already been taken' + ]) + end + it 'should have one version' do with_versioning do @registrar.versions.should == [] @@ -72,24 +80,12 @@ describe Registrar do end it 'should have code' do - @registrar.code.should =~ /registrar/ + @registrar.code.should =~ /REGISTRAR/ end it 'should not be able to change code' do @registrar.code = 'not-updated' - @registrar.code.should =~ /registrar/ - end - - it 'should automatically add next code if original is taken' do - @registrar = Fabricate(:registrar, name: 'uniq') - @registrar.name = 'New name' - @registrar.code.should == 'uniq' - @registrar.save - - @new_registrar = Fabricate.build(:registrar, name: 'uniq') - @new_registrar.valid? - @new_registrar.errors.full_messages.should == [] - @new_registrar.code.should == 'uniq1' + @registrar.code.should =~ /REGISTRAR/ end it 'should be able to issue a prepayment invoice' do