Move to new serializer

This commit is contained in:
Maciej Szlosarczyk 2020-06-26 11:23:30 +03:00
parent cba445ee5b
commit d005a61888
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
3 changed files with 78 additions and 33 deletions

View file

@ -1,5 +1,6 @@
require 'deserializers/xml/legal_document' require 'deserializers/xml/legal_document'
require 'deserializers/xml/ident' require 'deserializers/xml/ident'
require 'deserializers/xml/contact'
class Epp::Contact < Contact class Epp::Contact < Contact
include EppErrors include EppErrors
@ -23,25 +24,8 @@ class Epp::Contact < Contact
end end
def attrs_from(frame, new_record: false) def attrs_from(frame, new_record: false)
f = frame at = ::Deserializers::Xml::Contact.new(frame).call
at = {}.with_indifferent_access ident_attrs = ::Deserializers::Xml::Ident.new(frame).call
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.merge!(ident_attrs) if new_record at.merge!(ident_attrs) if new_record
at at
end end
@ -72,8 +56,8 @@ class Epp::Contact < Contact
res res
end end
end end
delegate :ident_attr_valid?, to: :class delegate :ident_attr_valid?, to: :class
def epp_code_map def epp_code_map
@ -106,11 +90,11 @@ class Epp::Contact < Contact
def update_attributes(frame, current_user) def update_attributes(frame, current_user)
return super if frame.blank? return super if frame.blank?
at = {}.with_indifferent_access new_attributes = self.class.attrs_from(frame, new_record: false)
at.deep_merge!(self.class.attrs_from(frame.css('chg'), new_record: false))
if Setting.client_status_editing_enabled 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 end
if doc = attach_legal_document(::Deserializers::Xml::LegalDocument.new(frame).call) 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 self.legal_document_id = doc.id
end end
ident_frame = frame.css('ident').first ident_attributes = ::Deserializers::Xml::Ident.new(frame).call
# https://github.com/internetee/registry/issues/576 # https://github.com/internetee/registry/issues/576
if ident_frame if ident_attributes[:ident]
if identifier.valid? if identifier.valid?
submitted_ident = Ident.new(code: ident_frame.text, submitted_ident = Ident.new(code: ident_attributes[:ident],
type: ident_frame.attr('type'), type: ident_attributes[:ident_type],
country_code: ident_frame.attr('cc')) country_code: ident_attributes[:ident_country_code])
if submitted_ident != identifier if submitted_ident != identifier
add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident')) add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident'))
return return
end end
else else
ident_update_attempt = ident_frame.text.present? && (ident_frame.text != ident) ident_update_attempt = ident_attributes[:ident] != ident
if ident_update_attempt if ident_update_attempt
add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.ident_update')) add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.ident_update'))
return return
end end
identifier = Ident.new(code: ident, identifier = Ident.new(code: ident_attributes[:ident],
type: ident_frame.attr('type'), type: ident_attributes[:ident_type],
country_code: ident_frame.attr('cc')) country_code: ident_attributes[:ident_country_code])
identifier.validate identifier.validate
@ -153,7 +137,7 @@ class Epp::Contact < Contact
self.upid = current_user.registrar.id if current_user.registrar self.upid = current_user.registrar.id if current_user.registrar
self.up_date = Time.zone.now self.up_date = Time.zone.now
self.attributes = at self.attributes = new_attributes
email_changed = will_save_change_to_email? email_changed = will_save_change_to_email?
old_email = email_was old_email = email_was

View file

@ -24,6 +24,10 @@ module Deserializers
# Auth info # Auth info
auth_info: if_present('authInfo pw'), auth_info: if_present('authInfo pw'),
# statuses
statuses_to_add: statuses_to_add,
statuses_to_remove: statuses_to_remove,
} }
attributes.compact attributes.compact
@ -34,6 +38,24 @@ module Deserializers
frame.css(css_path).text frame.css(css_path).text
end 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 end
end end

View file

@ -70,4 +70,43 @@ class DeserializersXmlContactTest < ActiveSupport::TestCase
instance = ::Deserializers::Xml::Contact.new(nokogiri_frame) instance = ::Deserializers::Xml::Contact.new(nokogiri_frame)
assert_equal instance.call, { name: 'new', email: 'new@registrar.test', phone: '+1.2' } assert_equal instance.call, { name: 'new', email: 'new@registrar.test', phone: '+1.2' }
end end
def test_handles_statuses
xml_string = <<-XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="https://epp.tld.ee/schema/epp-ee-1.0.xsd">
<command>
<update>
<contact:update xmlns:contact="https://epp.tld.ee/schema/contact-ee-1.1.xsd">
<contact:id>john-001</contact:id>
<contact:chg>
<contact:postalInfo>
<contact:name>new name</contact:name>
</contact:postalInfo>
<contact:voice>+123.4</contact:voice>
<contact:email>new-email@inbox.test</contact:email>
</contact:chg>
<contact:add>
<contact:status s="clientDeleteProhibited" lang="en">Payment overdue.</contact:status>
<contact:status s="clientUpdateProhibited"/>
</contact:add>
<contact:rem>
<contact:status s="pendingDelete"/>
</contact:rem>
</contact:update>
</update>
</command>
</epp>
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 end