From 678fd29c09d24dd81786951d4088c6df7989cf70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Thu, 28 Aug 2014 12:53:37 +0300 Subject: [PATCH] PostalInfo type checking for contact create --- app/helpers/epp/contacts_helper.rb | 10 ++++------ app/models/address.rb | 17 +++++++++++++++++ config/locales/en.yml | 2 ++ spec/support/epp_contact_xml_builder.rb | 2 +- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 481bd564e..955cb364c 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -48,14 +48,12 @@ module Epp ## CREATE def validate_contact_create_request @ph = params_hash['epp']['command']['create']['create'] - xml_attrs_present?(@ph, [%w(id), - %w(authInfo pw), - %w(postalInfo)]) + xml_attrs_present?(@ph, [%w(id), %w(authInfo pw), %w(postalInfo)]) + return epp_errors.empty? unless @ph['postalInfo'].is_a?(Hash) || @ph['postalInfo'].is_a?(Array) - xml_attrs_array_present?(@ph['postalInfo'], [%w(name), - %w(addr city), - %w(addr cc)]) + (epp_errors << Address.validate_postal_info_types(parsed_frame)).flatten! + xml_attrs_array_present?(@ph['postalInfo'], [%w(name), %w(addr city), %w(addr cc)]) end ## UPDATE diff --git a/app/models/address.rb b/app/models/address.rb index 6fe586900..bd80eea9a 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -14,6 +14,23 @@ class Address < ActiveRecord::Base # validates_inclusion_of :type, in: TYPES class << self + + def validate_postal_info_types(parsed_frame) + errors, used = [], [] + + parsed_frame.css('postalInfo').each do |pi| + attr = pi.attributes['type'].try(:value) + errors << { code: 2003, msg: I18n.t('errors.messages.attr_missing', key: 'type')} and next unless attr + if !TYPES.include?(attr) + errors << { code: 2005, msg: I18n.t('errors.messages.invalid_type'), value: { obj: 'type', val: attr }} + next + end + errors << { code: 2005, msg: I18n.t('errors.messages.repeating_postal_info') } and next if used.include?(attr) + used << attr + end + errors + end + def extract_attributes(ah) address_hash = {} [ah].flatten.each do |pi| diff --git a/config/locales/en.yml b/config/locales/en.yml index 69c8c1a21..9ffeab2c6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -98,6 +98,8 @@ 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}' + attr_missing: 'Required parameter missing: %{key}' + repeating_postal_info: 'Only one of each postal info types may be provided' setting_groups: codes: diff --git a/spec/support/epp_contact_xml_builder.rb b/spec/support/epp_contact_xml_builder.rb index ba5f656ef..6ee409612 100644 --- a/spec/support/epp_contact_xml_builder.rb +++ b/spec/support/epp_contact_xml_builder.rb @@ -36,7 +36,7 @@ module EppContactXmlBuilder xml.tag!('contact:create', 'xmlns:contact' => 'urn:ietf:params:xml:ns:contact-1.0') do xml.tag!('contact:id', xml_params[:id], 'sh8013') unless xml_params[:id] == false unless xml_params[:postalInfo] == [false] - xml.tag!('contact:postalInfo') do + xml.tag!('contact:postalInfo', type: 'int') do xml.tag!('contact:name', ( xml_params[:name] || 'Sillius Soddus')) unless xml_params[:name] == false xml.tag!('contact:org', ( xml_params[:org_name] || 'Example Inc.')) unless xml_params[:org_name] == false unless xml_params[:addr] == [false]