diff --git a/app/models/contact.rb b/app/models/contact.rb index 17924149e..d49804fb0 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -27,7 +27,7 @@ class Contact < ActiveRecord::Base validate :ident_valid_format? before_validation :set_ident_country_code - before_create :update_code + before_validation :prefix_code before_create :generate_auth_info after_save :manage_statuses def manage_statuses @@ -126,7 +126,8 @@ class Contact < ActiveRecord::Base self[:code] = code if new_record? # cannot change code later end - def update_code + def prefix_code + return nil if registrar.blank? code = self[:code] # custom code from client @@ -135,7 +136,6 @@ class Contact < ActiveRecord::Base code.sub!(/^CID:/, '') prefix, *custom_code = code.split(':') code = custom_code.join(':') if prefix == registrar.code - code = nil if code == registrar.code end code = SecureRandom.hex(4) if code.blank? || code == registrar.code @@ -143,7 +143,7 @@ class Contact < ActiveRecord::Base self[:code] = "#{registrar.code}:#{code}".upcase end - # used only for contact trasfere + # used only for contact trasphere def generate_new_code! return nil if registrar.blank? registrar.reload # for contact transfere diff --git a/app/models/registrar.rb b/app/models/registrar.rb index d297545a3..bd529d3d7 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -14,6 +14,12 @@ class Registrar < ActiveRecord::Base validates :name, :reg_no, :country_code, :email, :code, presence: true validates :name, :reg_no, :reference_no, :code, uniqueness: true + validate :forbidden_codes + def forbidden_codes + return true unless ['CID'].include? code + errors.add(:code, I18n.t(:forbidden_code)) + false + end before_validation :generate_iso_11649_reference_no def generate_iso_11649_reference_no diff --git a/config/locales/en.yml b/config/locales/en.yml index 4e1d5fdd6..06205f495 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -756,3 +756,4 @@ en: hostnames_will_be_replaced_only_if_domain_validates_with_the_new_nameserver: 'Hostnames will be replaced only if domain validates with the new nameserver' back_to_domains: 'Back to domains' no_hostnames_replaced: 'No hostnames replaced' + forbidden_code: 'is forbidden to use' diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index d0553bc8e..05be04d48 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -228,6 +228,22 @@ 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' + + @contact = Fabricate.build(:contact, + 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"] + end + it 'should generate a new password' do @contact = Fabricate.build(:contact, code: '123asd', auth_info: 'qwe321') @contact.auth_info.should == 'qwe321' @@ -258,10 +274,10 @@ describe Contact do @contact.code.should_not == '' end - it 'should not allow empty spaces as code' do + it 'should not ignore empty spaces as code and generate new one' do @contact = Fabricate.build(:contact, code: ' ') - @contact.valid? - @contact.errors.full_messages.should == ['Code is invalid'] + @contact.valid?.should == true + @contact.code.should =~ /FIXED:..../ end end diff --git a/spec/models/registrar_spec.rb b/spec/models/registrar_spec.rb index 208ae9906..764eaf036 100644 --- a/spec/models/registrar_spec.rb +++ b/spec/models/registrar_spec.rb @@ -120,5 +120,11 @@ describe Registrar do i.sum.should == BigDecimal.new('240.0') i.description.should == 'add some money' end + + fit 'should not allaw to use CID as code for leagcy reasons' do + registrar = Fabricate.build(:registrar, code: 'CID') + registrar.valid? + registrar.errors.full_messages.should == ['Code is forbidden to use'] + end end end