diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index 10a74426a..398c5715f 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -1,4 +1,5 @@ require 'deserializers/xml/legal_document' +require 'deserializers/xml/ident' class Epp::Contact < Contact include EppErrors @@ -40,8 +41,8 @@ class Epp::Contact < Contact at[:auth_info] = f.css('authInfo pw').text if f.css('authInfo pw').present? - - at.merge!(ident_attrs(f.css('ident').first)) if new_record + ident_attrs = ::Deserializers::Xml::Ident.new(f).call + at.merge!(ident_attrs) if new_record at end @@ -56,25 +57,6 @@ class Epp::Contact < Contact ) end - def ident_attrs(ident_frame) - return {} unless ident_attr_valid?(ident_frame) - - { - ident: ident_frame.text, - ident_type: ident_frame.attr('type'), - ident_country_code: ident_frame.attr('cc') - } - end - - def ident_attr_valid?(ident_frame) - return false if ident_frame.blank? - return false if ident_frame.try('text').blank? - return false if ident_frame.attr('type').blank? - return false if ident_frame.attr('cc').blank? - - true - end - def legal_document_attrs(legal_frame) return [] if legal_frame.blank? return [] if legal_frame.try('text').blank? diff --git a/lib/deserializers/xml/ident.rb b/lib/deserializers/xml/ident.rb new file mode 100644 index 000000000..966b3727f --- /dev/null +++ b/lib/deserializers/xml/ident.rb @@ -0,0 +1,34 @@ +module Deserializers + module Xml + class Ident + attr_reader :frame + + def initialize(frame) + @frame = frame.css('ident').first + end + + def call + if valid? + { + ident: frame.text, + ident_type: frame.attr('type'), + ident_country_code: frame.attr('cc') + } + else + {} + end + end + + private + + def valid? + return false if frame.blank? + return false if frame.try('text').blank? + return false if frame.attr('type').blank? + return false if frame.attr('cc').blank? + + true + end + end + end +end diff --git a/test/lib/deserializers/xml/ident_test.rb b/test/lib/deserializers/xml/ident_test.rb new file mode 100644 index 000000000..beff7aef5 --- /dev/null +++ b/test/lib/deserializers/xml/ident_test.rb @@ -0,0 +1,95 @@ +require 'test_helper' +require 'deserializers/xml/ident' + +class DeserializersXmlIdentTest < ActiveSupport::TestCase + def test_returns_empty_hash_when_not_present + xml_string = <<-XML + + + + + + john-001 + + + new name + + +123.4 + new-email@inbox.test + + + + + + XML + + nokogiri_frame = Nokogiri::XML(xml_string).remove_namespaces! + instance = ::Deserializers::Xml::Ident.new(nokogiri_frame) + assert_equal instance.call, {} + end + + def test_returns_empty_hash_when_not_valid + xml_string = <<-XML + + + + + + FIRST0:SH2027223711 + + wrong password + + + + + + 37605030299 + dGVzdCBmYWlsCg== + + + ABC-12345 + + + XML + + nokogiri_frame = Nokogiri::XML(xml_string).remove_namespaces! + instance = ::Deserializers::Xml::Ident.new(nokogiri_frame) + assert_equal instance.call, {} + end + + def test_returns_complete_hash_when_valid + xml_string = <<-XML + + + + + + FIRST0:SH2027223711 + + wrong password + + + + + + 37605030299 + dGVzdCBmYWlsCg== + + + ABC-12345 + + + XML + + nokogiri_frame = Nokogiri::XML(xml_string).remove_namespaces! + instance = ::Deserializers::Xml::Ident.new(nokogiri_frame) + + expected_result = { + ident: '37605030299', + ident_type: 'priv', + ident_country_code: 'EE' + } + + assert_equal instance.call, expected_result + end +end