Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding passport validation #237

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion app/models/applicants/personal_detail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class PersonalDetail
validate :age_less_than_maximum
validate :minimum_age
validates :sex, presence: true, inclusion: { in: SEX_OPTIONS }
validates :passport_number, presence: true
validates :passport_number, presence: true, length: { maximum: 20, message: "passport number is invalid" } # rubocop:disable Rails/I18nLocaleTexts
validate :valid_passport_number
validates :nationality, presence: true, inclusion: { in: NATIONALITIES }
validates :address_line_1, presence: true
validates :city, presence: true
Expand Down Expand Up @@ -91,6 +92,21 @@ def age_less_than_maximum
errors.add(:date_of_birth)
end

def valid_passport_number
return if passport_number.blank?

# Reject if it contains any characters other than alphanumeric
unless /\A[a-zA-Z0-9]+\z/.match?(passport_number)
errors.add(:passport_number, "passport number is invalid")
return
end

# Reject if it doesn't contain at least one number
unless /\d/.match?(passport_number)
errors.add(:passport_number, "passport number is invalid")
end
end

def minimum_age
# rubocop:disable Rails/Blank
return unless date_of_birth.present?
Expand Down
17 changes: 17 additions & 0 deletions app/steps/personal_details_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class PersonalDetailsStep < BaseStep
validates :nationality, inclusion: { in: NATIONALITIES }
validates :sex, inclusion: { in: SEX_OPTIONS }
validates :postcode, postcode: true
validates :passport_number, length: { maximum: 20, message: "passport number is invalid" } # rubocop:disable Rails/I18nLocaleTexts
validate :valid_passport_number
validate :date_of_birth_not_in_future
validate :age_less_than_maximum
validate :minimum_age
Expand Down Expand Up @@ -60,6 +62,21 @@ def age_less_than_maximum
errors.add(:date_of_birth)
end

def valid_passport_number
return if passport_number.blank?

# Reject if it contains any characters other than alphanumeric
unless /\A[a-zA-Z0-9]+\z/.match?(passport_number)
errors.add(:passport_number, "passport number is invalid")
return
end

# Reject if it doesn't contain at least one number
unless /\d/.match?(passport_number)
errors.add(:passport_number, "passport number is invalid")
end
end

def minimum_age
# rubocop:disable Rails/Blank
return unless date_of_birth.present?
Expand Down
30 changes: 30 additions & 0 deletions spec/models/applicants/personal_detail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,36 @@ module Applicants
end
end
end

describe "passport_number validations" do
subject(:model) { described_class.new(passport_number:) }

before { model.valid? }

context "when passport_number is valid" do
valid_passports = %w[PAT34566 A345667 JS3445 434455AFT 2453454]

valid_passports.each do |valid_passport|
let(:passport_number) { valid_passport }

it "#{valid_passport} should be valid" do
expect(model.errors.messages_for(:passport_number)).to be_blank
end
end
end

context "when passport_number is invalid" do
invalid_passports = ["asdfasdf", "nil", "%$^%%^%"]

invalid_passports.each do |invalid_passport|
let(:passport_number) { invalid_passport }

it "#{invalid_passport} should be invalid" do
expect(model.errors.messages_for(:passport_number)).to include("passport number is invalid")
end
end
end
end
end
end
end
59 changes: 59 additions & 0 deletions spec/steps/personal_details_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,64 @@
it { expect(error).to be_blank }
end
end

describe "passport_number" do
let(:form) { build(:form, passport_number:) }
let(:error) { step.errors.messages_for(:passport_number) }

before { step.valid? }

context "numbers and letters" do
let(:passport_number) { "AB1234567" }

it "is valid" do
expect(error).to be_blank
end
end

context "only numbers" do
let(:passport_number) { "123456789" }

it "is valid" do
expect(error).to be_blank
end
end

context "only letters" do
let(:passport_number) { "ABCDEFGHI" }

it "is invalid" do
expect(error).to be_present
end
end

context "other characters" do
let(:passport_number) { "&^%$£*()" }

it "is invalid" do
expect(error).to be_present
end
end

context "length > 20" do
let(:passport_number) { "ABCDEEFGHIJKLMNOPQRSTUVWXYZ1234567890" }

it "is invalid" do
expect(error).to be_present
end
end
end

describe "private constants" do
it "has MAX_AGE set to 80" do
max_age = described_class.send(:const_get, :MAX_AGE)
expect(max_age).to eq(80)
end

it "has MIN_AGE set to 22" do
min_age = described_class.send(:const_get, :MIN_AGE)
expect(min_age).to eq(22)
end
end
end
end