diff --git a/app/models/contact/ident.rb b/app/models/contact/ident.rb index d531b833c..c2e1790f7 100644 --- a/app/models/contact/ident.rb +++ b/app/models/contact/ident.rb @@ -13,20 +13,22 @@ 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 { '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], - [: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 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/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 b2b65a7ef..72d8597c4 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 = '2050-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 :parameter_value_syntax_error + + 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 :parameter_value_syntax_error + + 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 :parameter_value_syntax_error + end + def test_responces_error_with_email_error name = 'new' email = 'new@registrar@test'