From 77d2e402b418a4b370cded6aa7c713dd8bf85099 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Thu, 8 Jul 2021 15:49:31 +0300 Subject: [PATCH 1/6] 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' From 5bed817c354814e5426dc0c12e565166a6135db5 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Thu, 8 Jul 2021 17:07:43 +0300 Subject: [PATCH 2/6] fixes by review and codeclimat --- app/models/epp/contact.rb | 11 +++++++++-- config/locales/epp/contacts.en.yml | 2 +- test/integration/epp/contact/create/base_test.rb | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index e98e88368..57c4d033a 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -8,6 +8,9 @@ class Epp::Contact < Contact # disable STI, there is type column present self.inheritance_column = :sti_disabled + VALID_BIRTH_DATE_FROM = Time.zone.today - 150.years + VALID_BIRTH_DATE_TO = Time.zone.tomorrow + before_validation :manage_permissions def manage_permissions @@ -21,14 +24,18 @@ class Epp::Contact < Contact def validate_birthday_ident return unless Depp::Contact::SELECTION_TYPES[2].include?(ident_type) - if (Date.parse(ident) rescue ArgumentError) == ArgumentError + if begin + Date.parse(ident) + rescue StandardError + ArgumentError + end == 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) + valid_time_range = VALID_BIRTH_DATE_FROM...VALID_BIRTH_DATE_TO return if valid_time_range.cover?(contact_ident_date) add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident_date_range')) diff --git a/config/locales/epp/contacts.en.yml b/config/locales/epp/contacts.en.yml index 1da66f31d..341630299 100644 --- a/config/locales/epp/contacts.en.yml +++ b/config/locales/epp/contacts.en.yml @@ -13,7 +13,7 @@ en: Date format is invalid valid_ident_date_range: >- Ident update is not allowed. - Age must be over 18 and under 125 + Age must be over 0 and under 150 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 956735257..5d965596a 100644 --- a/test/integration/epp/contact/create/base_test.rb +++ b/test/integration/epp/contact/create/base_test.rb @@ -87,7 +87,7 @@ class EppContactCreateBaseTest < EppTestCase phone = '+1.2' birthday_wrong_format = '1111-22-33' birthday_above_valid_range = '1800-01-01' - birthday_below_valid_range = '2008-07-09' + birthday_below_valid_range = '2050-07-09' request_xml = <<-XML From 33a855fbe7b15eff11b4634c016c33baf13b8271 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Thu, 8 Jul 2021 17:56:42 +0300 Subject: [PATCH 3/6] codeclimate fixes --- app/models/epp/contact.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index 57c4d033a..6f53eaed5 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -24,11 +24,9 @@ class Epp::Contact < Contact def validate_birthday_ident return unless Depp::Contact::SELECTION_TYPES[2].include?(ident_type) - if begin + begin Date.parse(ident) - rescue StandardError - ArgumentError - end == ArgumentError + rescue ArgumentError add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident_date_format')) @error = true return From addeebfeb3f16d4a365c4d7d20be073c812f3b9d Mon Sep 17 00:00:00 2001 From: dinsmol Date: Wed, 14 Jul 2021 12:40:49 +0300 Subject: [PATCH 4/6] created birth date validator --- app/models/contact/ident.rb | 2 ++ app/models/epp/contact.rb | 25 ------------------ .../contact/ident/birth_date_validator.rb | 26 +++++++++++++++++++ config/locales/epp/contacts.en.yml | 6 ----- config/locales/idents.yml | 1 + .../epp/contact/create/base_test.rb | 6 ++--- 6 files changed, 32 insertions(+), 34 deletions(-) create mode 100644 app/validators/contact/ident/birth_date_validator.rb diff --git a/app/models/contact/ident.rb b/app/models/contact/ident.rb index d531b833c..8d1179afe 100644 --- a/app/models/contact/ident.rb +++ b/app/models/contact/ident.rb @@ -13,6 +13,7 @@ class Contact::Ident validates :type, presence: true, inclusion: { in: proc { types } } validates :country_code, presence: true, iso31661_alpha2: true validates_with MismatchValidator + validates_with BirthDateValidator, if: :birthday? def self.epp_code_map { @@ -26,6 +27,7 @@ class Contact::Ident [:code, :invalid_national_id], [:code, :invalid_reg_no], [:code, :invalid_iso8601_date], + [:code, :invalid_birth_date], [:country_code, :invalid_iso31661_alpha2] ] } diff --git a/app/models/epp/contact.rb b/app/models/epp/contact.rb index 6f53eaed5..6bd5507b9 100644 --- a/app/models/epp/contact.rb +++ b/app/models/epp/contact.rb @@ -8,9 +8,6 @@ class Epp::Contact < Contact # disable STI, there is type column present self.inheritance_column = :sti_disabled - VALID_BIRTH_DATE_FROM = Time.zone.today - 150.years - VALID_BIRTH_DATE_TO = Time.zone.tomorrow - before_validation :manage_permissions def manage_permissions @@ -19,28 +16,6 @@ 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) - - begin - Date.parse(ident) - rescue 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 = VALID_BIRTH_DATE_FROM...VALID_BIRTH_DATE_TO - 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/app/validators/contact/ident/birth_date_validator.rb b/app/validators/contact/ident/birth_date_validator.rb new file mode 100644 index 000000000..38cfc98f9 --- /dev/null +++ b/app/validators/contact/ident/birth_date_validator.rb @@ -0,0 +1,26 @@ +class Contact::Ident::BirthDateValidator < ActiveModel::Validator + VALID_BIRTH_DATE_FROM = Time.zone.today - 150.years + VALID_BIRTH_DATE_TO = Time.zone.tomorrow + + def validate(record) + record.errors.add(:code, :invalid_birth_date) if birth_date_wrong?(record) + end + + private + + def birth_date_wrong?(record) + return unless record.birthday? + + begin + Date.parse(record.code) + rescue ArgumentError + return true + end + + contact_ident_date = Date.parse(record.code) + valid_time_range = VALID_BIRTH_DATE_FROM...VALID_BIRTH_DATE_TO + return if valid_time_range.cover?(contact_ident_date) + + true + end +end diff --git a/config/locales/epp/contacts.en.yml b/config/locales/epp/contacts.en.yml index 341630299..fe4ed7ccf 100644 --- a/config/locales/epp/contacts.en.yml +++ b/config/locales/epp/contacts.en.yml @@ -8,12 +8,6 @@ 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 0 and under 150 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/config/locales/idents.yml b/config/locales/idents.yml index 33b935833..8e6a677de 100644 --- a/config/locales/idents.yml +++ b/config/locales/idents.yml @@ -9,3 +9,4 @@ en: code: invalid_national_id: does not conform to national identification number format of %{country} invalid_reg_no: does not conform to registration number format of %{country} + invalid_birth_date: Birth date is invalid, age must be over 0 and under 150 diff --git a/test/integration/epp/contact/create/base_test.rb b/test/integration/epp/contact/create/base_test.rb index 5d965596a..72d8597c4 100644 --- a/test/integration/epp/contact/create/base_test.rb +++ b/test/integration/epp/contact/create/base_test.rb @@ -119,7 +119,7 @@ class EppContactCreateBaseTest < EppTestCase response_xml = Nokogiri::XML(response.body) assert_correct_against_schema response_xml - assert_epp_response :data_management_policy_violation + assert_epp_response :parameter_value_syntax_error request_xml.sub! ">#{birthday_wrong_format}<", ">#{birthday_above_valid_range}<" assert_no_difference 'Contact.count' do @@ -129,7 +129,7 @@ class EppContactCreateBaseTest < EppTestCase response_xml = Nokogiri::XML(response.body) assert_correct_against_schema response_xml - assert_epp_response :data_management_policy_violation + assert_epp_response :parameter_value_syntax_error request_xml.sub! ">#{birthday_above_valid_range}<", ">#{birthday_below_valid_range}<" assert_no_difference 'Contact.count' do @@ -139,7 +139,7 @@ class EppContactCreateBaseTest < EppTestCase response_xml = Nokogiri::XML(response.body) assert_correct_against_schema response_xml - assert_epp_response :data_management_policy_violation + assert_epp_response :parameter_value_syntax_error end def test_responces_error_with_email_error From 939efdba0f8d058904e96fe9b9f17d8f7a53c33c Mon Sep 17 00:00:00 2001 From: dinsmol Date: Wed, 14 Jul 2021 13:26:01 +0300 Subject: [PATCH 5/6] fixed codeclimate errors --- app/models/contact/ident.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/app/models/contact/ident.rb b/app/models/contact/ident.rb index 8d1179afe..854fecb20 100644 --- a/app/models/contact/ident.rb +++ b/app/models/contact/ident.rb @@ -18,17 +18,17 @@ class Contact::Ident def self.epp_code_map { '2003' => [ - [:code, :blank], - [:type, :blank], - [:country_code, :blank] + %i[code blank], + %i[type blank], + %i[country_code blank] ], '2005' => [ - [:base, :mismatch], - [:code, :invalid_national_id], - [:code, :invalid_reg_no], - [:code, :invalid_iso8601_date], - [:code, :invalid_birth_date], - [:country_code, :invalid_iso31661_alpha2] + %i[base mismatch], + %i[code invalid_national_id], + %i[code invalid_reg_no], + %i[code invalid_iso8601_date], + %i[code invalid_birth_date], + %i[country_code invalid_iso31661_alpha2] ] } end From 2a33b02f8f8a67ff25a25d764354911bb7bfff52 Mon Sep 17 00:00:00 2001 From: dinsmol Date: Wed, 14 Jul 2021 13:32:12 +0300 Subject: [PATCH 6/6] fixed codeclimate errors --- app/models/contact/ident.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/contact/ident.rb b/app/models/contact/ident.rb index 854fecb20..c2e1790f7 100644 --- a/app/models/contact/ident.rb +++ b/app/models/contact/ident.rb @@ -20,7 +20,7 @@ class Contact::Ident '2003' => [ %i[code blank], %i[type blank], - %i[country_code blank] + %i[country_code blank], ], '2005' => [ %i[base mismatch], @@ -28,7 +28,7 @@ class Contact::Ident %i[code invalid_reg_no], %i[code invalid_iso8601_date], %i[code invalid_birth_date], - %i[country_code invalid_iso31661_alpha2] + %i[country_code invalid_iso31661_alpha2], ] } end