Always generate new code when transferring contacts

This commit is contained in:
Martin Lensment 2015-03-16 12:33:27 +02:00
parent 9cbe38effe
commit 9f9bffae57
3 changed files with 42 additions and 11 deletions

View file

@ -13,6 +13,8 @@ class Contact < ActiveRecord::Base
accepts_nested_attributes_for :address, :disclosure, :legal_documents accepts_nested_attributes_for :address, :disclosure, :legal_documents
attr_accessor :code_overwrite_allowed
validates :name, :phone, :email, :ident, :address, :registrar, :ident_type, presence: true validates :name, :phone, :email, :ident, :address, :registrar, :ident_type, presence: true
# # Phone nr validation is very minimam in order to support legacy requirements # # Phone nr validation is very minimam in order to support legacy requirements
@ -112,7 +114,7 @@ class Contact < ActiveRecord::Base
end end
def generate_code def generate_code
self.code = SecureRandom.hex(4) if code.blank? self.code = SecureRandom.hex(4) if code.blank? || code_overwrite_allowed
end end
def generate_auth_info def generate_auth_info
@ -129,7 +131,7 @@ class Contact < ActiveRecord::Base
end end
def code=(code) def code=(code)
self[:code] = code if new_record? self[:code] = code if new_record? || code_overwrite_allowed
end end
# Find a way to use self.domains with contact # Find a way to use self.domains with contact

View file

@ -416,7 +416,7 @@ class Epp::Domain < Domain
is_other_domains_contact = DomainContact.where('contact_id = ? AND domain_id != ?', owner_contact_id, id).count > 0 is_other_domains_contact = DomainContact.where('contact_id = ? AND domain_id != ?', owner_contact_id, id).count > 0
if owner_contact.domains_owned.count > 1 || is_other_domains_contact if owner_contact.domains_owned.count > 1 || is_other_domains_contact
# copy contact # copy contact
c = Contact.find(owner_contact_id) # n+1 workaround c = Contact.find(owner_contact_id)
oc = c.deep_clone include: [:statuses, :address] oc = c.deep_clone include: [:statuses, :address]
oc.code = nil oc.code = nil
oc.registrar_id = registrar_id oc.registrar_id = registrar_id
@ -424,7 +424,13 @@ class Epp::Domain < Domain
self.owner_contact_id = oc.id self.owner_contact_id = oc.id
else else
# transfer contact # transfer contact
owner_contact.update_column(:registrar_id, registrar_id) # n+1 workaround oc = Contact.find(owner_contact_id) # n+1 workaround
oc.code_overwrite_allowed = true
oc.generate_code
oc.registrar_id = registrar_id
oc.save!
self.owner_contact = oc
end end
end end
@ -452,7 +458,11 @@ class Epp::Domain < Domain
copied_ids << c.id copied_ids << c.id
else else
# transfer contact # transfer contact
c.update_column(:registrar_id, registrar_id) # n+1 workaround cnt = Contact.find(c.id) # n+1 workaround
cnt.code_overwrite_allowed = true
cnt.generate_code
cnt.registrar_id = registrar_id
cnt.save!
end end
end end
end end

View file

@ -872,6 +872,9 @@ describe 'EPP Domain', epp: true do
it 'transfers domain with contacts' do it 'transfers domain with contacts' do
original_oc_id = domain.owner_contact.id original_oc_id = domain.owner_contact.id
original_oc_code = domain.owner_contact.code
original_contact_codes = domain.contacts.pluck(:code)
pw = domain.auth_info pw = domain.auth_info
xml = domain_transfer_xml({ xml = domain_transfer_xml({
@ -890,14 +893,21 @@ describe 'EPP Domain', epp: true do
domain.owner_contact.registrar_id.should == @registrar2.id domain.owner_contact.registrar_id.should == @registrar2.id
domain.owner_contact.id.should == original_oc_id domain.owner_contact.id.should == original_oc_id
domain.contacts.pluck(:registrar_id).each do |reg_id| # must generate new code
reg_id.should == @registrar2.id domain.owner_contact.code.should_not == original_oc_code
domain.contacts.each do |c|
c.registrar_id.should == @registrar2.id
original_contact_codes.include?(c.code).should_not == true
end end
end end
it 'transfers domain when registrant has more domains' do it 'transfers domain when registrant has more domains' do
Fabricate(:domain, owner_contact: domain.owner_contact) Fabricate(:domain, owner_contact: domain.owner_contact)
original_oc_id = domain.owner_contact.id original_oc_id = domain.owner_contact.id
original_oc_code = domain.owner_contact.code
original_contact_codes = domain.contacts.pluck(:code)
pw = domain.auth_info pw = domain.auth_info
xml = domain_transfer_xml({ xml = domain_transfer_xml({
@ -916,9 +926,12 @@ describe 'EPP Domain', epp: true do
domain.owner_contact.registrar_id.should == @registrar2.id domain.owner_contact.registrar_id.should == @registrar2.id
# owner_contact should be a new record # owner_contact should be a new record
domain.owner_contact.id.should_not == original_oc_id domain.owner_contact.id.should_not == original_oc_id
# must generate new code
domain.owner_contact.code.should_not == original_oc_code
domain.contacts.pluck(:registrar_id).each do |reg_id| domain.contacts.each do |c|
reg_id.should == @registrar2.id c.registrar_id.should == @registrar2.id
original_contact_codes.include?(c.code).should_not == true
end end
end end
@ -927,6 +940,9 @@ describe 'EPP Domain', epp: true do
d.tech_contacts << domain.owner_contact d.tech_contacts << domain.owner_contact
original_oc_id = domain.owner_contact.id original_oc_id = domain.owner_contact.id
original_oc_code = domain.owner_contact.code
original_contact_codes = domain.contacts.pluck(:code)
pw = domain.auth_info pw = domain.auth_info
xml = domain_transfer_xml({ xml = domain_transfer_xml({
@ -945,9 +961,12 @@ describe 'EPP Domain', epp: true do
domain.owner_contact.registrar_id.should == @registrar2.id domain.owner_contact.registrar_id.should == @registrar2.id
# owner_contact should be a new record # owner_contact should be a new record
domain.owner_contact.id.should_not == original_oc_id domain.owner_contact.id.should_not == original_oc_id
# must generate new code
domain.owner_contact.code.should_not == original_oc_code
domain.contacts.pluck(:registrar_id).each do |reg_id| domain.contacts.each do |c|
reg_id.should == @registrar2.id c.registrar_id.should == @registrar2.id
original_contact_codes.include?(c.code).should_not == true
end end
end end