Ignore trailing spaces in postal codes verification

This way both administrators and regular citizens have a certain margin
of error when entering the postal codes.
This commit is contained in:
Javi Martín
2021-11-14 15:56:55 +01:00
parent 5cc10cbadf
commit 35e0477e03
2 changed files with 30 additions and 2 deletions

View File

@@ -106,9 +106,9 @@ class Verification::Residence
def valid_postal_code? def valid_postal_code?
Setting["postal_codes"].split(",").any? do |code_or_range| Setting["postal_codes"].split(",").any? do |code_or_range|
if code_or_range.include?(":") if code_or_range.include?(":")
Range.new(*code_or_range.split(":")).include?(postal_code) Range.new(*code_or_range.split(":").map(&:strip)).include?(postal_code&.strip)
else else
postal_code == code_or_range postal_code&.strip == code_or_range.strip
end end
end end
end end

View File

@@ -57,6 +57,12 @@ describe Verification::Residence do
allow(residence).to receive(:census_data).and_return(census_data) allow(residence).to receive(:census_data).and_return(census_data)
end end
it "is not valid when it's nil" do
residence.postal_code = nil
expect(residence).not_to be_valid
end
it "is valid with postal codes included in settings" do it "is valid with postal codes included in settings" do
residence.postal_code = "28012" residence.postal_code = "28012"
expect(residence).to be_valid expect(residence).to be_valid
@@ -98,6 +104,28 @@ describe Verification::Residence do
expect(residence).not_to be_valid expect(residence).not_to be_valid
end end
it "does not ignore spaces inside the postal code" do
Setting["postal_codes"] = "00001,000 05,00011"
residence.postal_code = "000 05"
expect(residence).to be_valid
residence.postal_code = "00005"
expect(residence).not_to be_valid
end
it "ignores trailing spaces in both the setting and the postal codes" do
Setting["postal_codes"] = " 00001,00002: 00005, 00011 "
residence.postal_code = " 00003 "
expect(residence).to be_valid
residence.postal_code = "00011 "
expect(residence).to be_valid
end
it "is not valid with postal codes not included in settings" do it "is not valid with postal codes not included in settings" do
residence.postal_code = "12345" residence.postal_code = "12345"
expect(residence).not_to be_valid expect(residence).not_to be_valid