From 77d2e402b418a4b370cded6aa7c713dd8bf85099 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Thu, 8 Jul 2021 15:49:31 +0300 Subject: [PATCH] added validation and tests --- app/models/epp/contact.rb | 20 ++++++ config/locales/epp/contacts.en.yml | 6 ++ .../epp/contact/create/base_test.rb | 61 +++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index 6bd5507b9..e98e88368 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -16,6 +16,26 @@ class Epp::Contact < Contact throw(:abort) end + validate :validate_birthday_ident + + def validate_birthday_ident + return unless Depp::Contact::SELECTION_TYPES[2].include?(ident_type) + + if (Date.parse(ident) rescue ArgumentError) == ArgumentError + add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident_date_format')) + @error = true + return + end + + contact_ident_date = Date.parse(ident) + valid_time_range = (Date.today - 125.years)...(Date.tomorrow - 18.years) + return if valid_time_range.cover?(contact_ident_date) + + add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident_date_range')) + @error = true + nil + end + class << self # support legacy search def find_by_epp_code(code) diff --git a/config/locales/epp/contacts.en.yml b/config/locales/epp/contacts.en.yml index fe4ed7ccf..1da66f31d 100644 --- a/config/locales/epp/contacts.en.yml +++ b/config/locales/epp/contacts.en.yml @@ -8,6 +8,12 @@ en: valid_ident: >- Ident update is not allowed. Consider creating new contact object + valid_ident_date_format: >- + Ident update is not allowed. + Date format is invalid + valid_ident_date_range: >- + Ident update is not allowed. + Age must be over 18 and under 125 ident_update: >- Only ident type and country can be updated in case of invalid ident. Please create new contact object to update ident code diff --git a/test/integration/epp/contact/create/base_test.rb b/test/integration/epp/contact/create/base_test.rb index b2b65a7ef..956735257 100644 --- a/test/integration/epp/contact/create/base_test.rb +++ b/test/integration/epp/contact/create/base_test.rb @@ -81,6 +81,67 @@ class EppContactCreateBaseTest < EppTestCase assert_epp_response :parameter_value_syntax_error end + def test_responses_with_error_on_invalid_birthday_date + name = 'new' + email = 'new@registrar.test' + phone = '+1.2' + birthday_wrong_format = '1111-22-33' + birthday_above_valid_range = '1800-01-01' + birthday_below_valid_range = '2008-07-09' + + request_xml = <<-XML + + + + + + + #{name} + + #{phone} + #{email} + + + + + #{birthday_wrong_format} + + + + + XML + + + assert_no_difference 'Contact.count' do + post epp_create_path, params: { frame: request_xml }, + headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } + end + + response_xml = Nokogiri::XML(response.body) + assert_correct_against_schema response_xml + assert_epp_response :data_management_policy_violation + + request_xml.sub! ">#{birthday_wrong_format}<", ">#{birthday_above_valid_range}<" + assert_no_difference 'Contact.count' do + post epp_create_path, params: { frame: request_xml }, + headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } + end + + response_xml = Nokogiri::XML(response.body) + assert_correct_against_schema response_xml + assert_epp_response :data_management_policy_violation + + request_xml.sub! ">#{birthday_above_valid_range}<", ">#{birthday_below_valid_range}<" + assert_no_difference 'Contact.count' do + post epp_create_path, params: { frame: request_xml }, + headers: { 'HTTP_COOKIE' => 'session=api_bestnames' } + end + + response_xml = Nokogiri::XML(response.body) + assert_correct_against_schema response_xml + assert_epp_response :data_management_policy_violation + end + def test_responces_error_with_email_error name = 'new' email = 'new@registrar@test'