Merge pull request #2081 from internetee/1796-contact-birthday-field-validation

Fix for contact birth date validation
This commit is contained in:
Alex Sherman 2021-07-15 12:34:17 +05:00 committed by GitHub
commit fc338dea16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 8 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="https://epp.tld.ee/schema/epp-ee-1.0.xsd">
<command>
<create>
<contact:create xmlns:contact="https://epp.tld.ee/schema/contact-ee-1.1.xsd">
<contact:postalInfo>
<contact:name>#{name}</contact:name>
</contact:postalInfo>
<contact:voice>#{phone}</contact:voice>
<contact:email>#{email}</contact:email>
</contact:create>
</create>
<extension>
<eis:extdata xmlns:eis="https://epp.tld.ee/schema/eis-1.0.xsd">
<eis:ident type="birthday" cc="LV">#{birthday_wrong_format}</eis:ident>
</eis:extdata>
</extension>
</command>
</epp>
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'