diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index d14bf1e1b..c86ed77da 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -1,5 +1,6 @@ require 'deserializers/xml/legal_document' require 'deserializers/xml/ident' +require 'deserializers/xml/contact' class Epp::Contact < Contact include EppErrors @@ -23,25 +24,8 @@ class Epp::Contact < Contact end def attrs_from(frame, new_record: false) - f = frame - at = {}.with_indifferent_access - at[:name] = f.css('postalInfo name').text if f.css('postalInfo name').present? - at[:org_name] = f.css('postalInfo org').text if f.css('postalInfo org').present? - at[:email] = f.css('email').text if f.css('email').present? - at[:fax] = f.css('fax').text if f.css('fax').present? - at[:phone] = f.css('voice').text if f.css('voice').present? - - if address_processing? - at[:city] = f.css('postalInfo addr city').text if f.css('postalInfo addr city').present? - at[:zip] = f.css('postalInfo addr pc').text if f.css('postalInfo addr pc').present? - at[:street] = f.css('postalInfo addr street').text if f.css('postalInfo addr street').present? - at[:state] = f.css('postalInfo addr sp').text if f.css('postalInfo addr sp').present? - at[:country_code] = f.css('postalInfo addr cc').text if f.css('postalInfo addr cc').present? - end - - at[:auth_info] = f.css('authInfo pw').text if f.css('authInfo pw').present? - - ident_attrs = ::Deserializers::Xml::Ident.new(f).call + at = ::Deserializers::Xml::Contact.new(frame).call + ident_attrs = ::Deserializers::Xml::Ident.new(frame).call at.merge!(ident_attrs) if new_record at end @@ -72,8 +56,8 @@ class Epp::Contact < Contact res end - end + delegate :ident_attr_valid?, to: :class def epp_code_map @@ -106,11 +90,11 @@ class Epp::Contact < Contact def update_attributes(frame, current_user) return super if frame.blank? - at = {}.with_indifferent_access - at.deep_merge!(self.class.attrs_from(frame.css('chg'), new_record: false)) + new_attributes = self.class.attrs_from(frame, new_record: false) if Setting.client_status_editing_enabled - at[:statuses] = statuses - statuses_attrs(frame.css('rem'), 'rem') + statuses_attrs(frame.css('add'), 'add') + new_attributes[:statuses] = + statuses - new_attributes[:statuses_to_remove] + new_attributes[:statuses_to_add] end if doc = attach_legal_document(::Deserializers::Xml::LegalDocument.new(frame).call) @@ -118,30 +102,30 @@ class Epp::Contact < Contact self.legal_document_id = doc.id end - ident_frame = frame.css('ident').first + ident_attributes = ::Deserializers::Xml::Ident.new(frame).call # https://github.com/internetee/registry/issues/576 - if ident_frame + if ident_attributes[:ident] if identifier.valid? - submitted_ident = Ident.new(code: ident_frame.text, - type: ident_frame.attr('type'), - country_code: ident_frame.attr('cc')) + submitted_ident = Ident.new(code: ident_attributes[:ident], + type: ident_attributes[:ident_type], + country_code: ident_attributes[:ident_country_code]) if submitted_ident != identifier add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident')) return end else - ident_update_attempt = ident_frame.text.present? && (ident_frame.text != ident) + ident_update_attempt = ident_attributes[:ident] != ident if ident_update_attempt add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.ident_update')) return end - identifier = Ident.new(code: ident, - type: ident_frame.attr('type'), - country_code: ident_frame.attr('cc')) + identifier = Ident.new(code: ident_attributes[:ident], + type: ident_attributes[:ident_type], + country_code: ident_attributes[:ident_country_code]) identifier.validate @@ -153,7 +137,7 @@ class Epp::Contact < Contact self.upid = current_user.registrar.id if current_user.registrar self.up_date = Time.zone.now - self.attributes = at + self.attributes = new_attributes email_changed = will_save_change_to_email? old_email = email_was diff --git a/lib/deserializers/xml/contact.rb b/lib/deserializers/xml/contact.rb index d0a2ce8f4..81eb90b51 100644 --- a/lib/deserializers/xml/contact.rb +++ b/lib/deserializers/xml/contact.rb @@ -24,6 +24,10 @@ module Deserializers # Auth info auth_info: if_present('authInfo pw'), + + # statuses + statuses_to_add: statuses_to_add, + statuses_to_remove: statuses_to_remove, } attributes.compact @@ -34,6 +38,24 @@ module Deserializers frame.css(css_path).text end + + def statuses_to_add + statuses_frame = frame.css('add') + return unless statuses_frame.present? + + statuses_frame.css('status').map do |status| + status['s'] + end + end + + def statuses_to_remove + statuses_frame = frame.css('rem') + return unless statuses_frame.present? + + statuses_frame.css('status').map do |status| + status['s'] + end + end end end end diff --git a/test/lib/deserializers/xml/contact_test.rb b/test/lib/deserializers/xml/contact_test.rb index c755e8f24..c49d919b5 100644 --- a/test/lib/deserializers/xml/contact_test.rb +++ b/test/lib/deserializers/xml/contact_test.rb @@ -70,4 +70,43 @@ class DeserializersXmlContactTest < ActiveSupport::TestCase instance = ::Deserializers::Xml::Contact.new(nokogiri_frame) assert_equal instance.call, { name: 'new', email: 'new@registrar.test', phone: '+1.2' } end + + def test_handles_statuses + xml_string = <<-XML + + + + + + john-001 + + + new name + + +123.4 + new-email@inbox.test + + + Payment overdue. + + + + + + + + + + XML + + nokogiri_frame = Nokogiri::XML(xml_string).remove_namespaces! + instance = ::Deserializers::Xml::Contact.new(nokogiri_frame) + assert_equal instance.call, { name: 'new name', + email: 'new-email@inbox.test', + phone: '+123.4', + statuses_to_add: ['clientDeleteProhibited', + 'clientUpdateProhibited'], + statuses_to_remove: ['pendingDelete'] + } + end end