From fdf9301d41b5bbef35a6bb05d6d56e237866d234 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Fri, 22 Aug 2014 11:25:25 +0300 Subject: [PATCH 1/2] Relation check for contact#delete --- app/models/contact.rb | 17 ++++++++++++----- app/views/epp/contacts/info.xml.builder | 11 +++++------ config/locales/en.yml | 1 + spec/epp/contact_spec.rb | 20 +++++++++++++++++--- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index ecce51574..81938499b 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -7,6 +7,7 @@ class Contact < ActiveRecord::Base EPP_CODE_MAP = { '2302' => ['Contact id already exists'], '2303' => [:not_found, :epp_obj_does_not_exist], + '2305' => ['Object association prohibits operation'], '2005' => ['Phone nr is invalid', 'Email is invalid'] } @@ -71,6 +72,12 @@ class Contact < ActiveRecord::Base return false end + #Find a way to use self.domains with contact + def domains_owned + Domain.find_by(owner_contact_id: id) + end + + #TODO Refactor the relation methods to something more sensible def get_relation( model = :domain_contacts ) send(model) rescue NoMethodError => e @@ -83,15 +90,15 @@ class Contact < ActiveRecord::Base false end - - #should use only in transaction def destroy_and_clean clean_up_address + + if has_relation(:domain_contacts) || domains_owned.present? + errors.add(:contact, msg: I18n.t('errors.messages.epp_obj_association_error'), value: { obj: 'contact', val: code }) + return false + end destroy - rescue - errors.add(:contact, msg: I18n.t('errors.messages.epp_command_failed'), value: { obj: 'contact', val: code }) - false end class << self diff --git a/app/views/epp/contacts/info.xml.builder b/app/views/epp/contacts/info.xml.builder index 0bd13fc83..e5a4a0fe8 100644 --- a/app/views/epp/contacts/info.xml.builder +++ b/app/views/epp/contacts/info.xml.builder @@ -9,12 +9,11 @@ xml.epp_head do xml.tag!('contact:name', @contact.name) xml.tag!('contact:org', @contact.org_name) xml.tag!('contact:addr') do - @contact.address do |address| - xml.tag!('contact:street', address.street) if address.street - xml.tag!('contact:street', address.street2) if address.street2 - xml.tag!('contact:street', address.street3) if address.street3 - xml.tag!('contact:cc', address.try(:country).try(:iso)) unless address.try(:country).nil? - end + address = @contact.address + xml.tag!('contact:street', address.street) if address.street + xml.tag!('contact:street', address.street2) if address.street2 + xml.tag!('contact:street', address.street3) if address.street3 + xml.tag!('contact:cc', address.try(:country).try(:iso)) unless address.try(:country).nil? end xml.tag!('contact:voice', @contact.phone) xml.tag!('contact:fax', @contact.fax) diff --git a/config/locales/en.yml b/config/locales/en.yml index 1fcbfb07b..f12ea140c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -87,6 +87,7 @@ en: epp_exp_dates_do_not_match: 'Given and current expire dates do not match' epp_registrant_not_found: 'Registrant not found' required_parameter_missing: 'Required parameter missing: %{key}' + epp_obj_association_error: 'Object association prohibits operation' setting_groups: codes: diff --git a/spec/epp/contact_spec.rb b/spec/epp/contact_spec.rb index 1a7c81227..bf3bb0dbb 100644 --- a/spec/epp/contact_spec.rb +++ b/spec/epp/contact_spec.rb @@ -4,8 +4,10 @@ describe 'EPP Contact', epp: true do let(:server) { Epp::Server.new({server: 'localhost', tag: 'gitlab', password: 'ghyt9e4fu', port: 701}) } context 'with valid user' do - before(:each) { Fabricate(:epp_user) } - + before(:each) { + Fabricate(:epp_user) + Fabricate(:domain_validation_setting_group) + } context 'create command' do it "fails if request is invalid" do @@ -133,7 +135,7 @@ describe 'EPP Contact', epp: true do end it 'deletes contact' do - Fabricate(:contact, code: "dwa1234", auth_info: '2fooBAR') + Fabricate(:contact, code: "dwa1234") response = epp_request('contacts/delete.xml') expect(response[:result_code]).to eq('1000') expect(response[:msg]).to eq('Command completed successfully') @@ -147,6 +149,18 @@ describe 'EPP Contact', epp: true do expect(response[:result_code]).to eq('2303') expect(response[:msg]).to eq('Object does not exist') end + + it 'fails if contact has associated domain' do + Fabricate(:domain, owner_contact: Fabricate(:contact, code: 'dwa1234')) + expect(Domain.first.owner_contact.address.present?).to be true + response = epp_request('contacts/delete.xml') + + expect(response[:result_code]).to eq('2305') + expect(response[:msg]).to eq('Object association prohibits operation') + + expect(Domain.first.owner_contact.present?).to be true + + end end From 60451a3f6fdf79d028ca55ae65aecafb2ed9a0fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Fri, 22 Aug 2014 11:57:13 +0300 Subject: [PATCH 2/2] Quick fix for Contact error code map --- app/models/contact.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/contact.rb b/app/models/contact.rb index d114f90a8..39d616512 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -98,6 +98,7 @@ class Contact < ActiveRecord::Base { '2302' => [[:code, :epp_id_taken]], '2303' => [:not_found, :epp_obj_does_not_exist], + '2305' => ['Object association prohibits operation' ], '2005' => ['Phone nr is invalid', 'Email is invalid'] } end