Skip to content

Commit

Permalink
Future dates not allowed
Browse files Browse the repository at this point in the history
* date of entry
* start date
* date of birth
  • Loading branch information
fumimowdan committed Sep 12, 2023
1 parent cc4fb62 commit 901e21a
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 15 deletions.
4 changes: 4 additions & 0 deletions app/steps/entry_date_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ class EntryDateStep < BaseStep

REQUIRED_FIELDS = %i[date_of_entry].freeze

validate do |record|
FutureDateValidator.new(record, :date_of_entry).validate
end

def configure_step
@question = t("steps.entry_date.question.#{form.application_route}")
@question_type = :date
Expand Down
12 changes: 3 additions & 9 deletions app/steps/personal_details_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ class PersonalDetailsStep < BaseStep
validates :postcode, postcode: true
validates :passport_number, length: { maximum: 20 }
validate :valid_passport_number
validate :date_of_birth_not_in_future
validate :age_less_than_maximum
validate :minimum_age

validate do |record|
EmailFormatValidator.new(record).validate
FutureDateValidator.new(record, :date_of_birth).validate
end

def configure_step
Expand All @@ -54,16 +54,10 @@ def template

private

def date_of_birth_not_in_future
return unless date_of_birth.present? && date_of_birth > Date.current

errors.add(:date_of_birth, "cannot be in the future")
end

def age_less_than_maximum
return unless date_of_birth.present? && (Date.current.year - date_of_birth.year) >= MAX_AGE

errors.add(:date_of_birth)
errors.add(:date_of_birth, :over_max_age)
end

def valid_passport_number
Expand All @@ -86,7 +80,7 @@ def minimum_age
return unless date_of_birth.present?
# rubocop:enable Rails/Blank

errors.add(:date_of_birth, "must be at least #{MIN_AGE} years") if date_of_birth > MIN_AGE.years.ago.to_date
errors.add(:date_of_birth, :below_min_age) if date_of_birth > MIN_AGE.years.ago.to_date
end

MAX_AGE = 80
Expand Down
4 changes: 4 additions & 0 deletions app/steps/start_date_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ class StartDateStep < BaseStep

REQUIRED_FIELDS = %i[start_date].freeze

validate do |record|
FutureDateValidator.new(record, :start_date).validate
end

def configure_step
@question = t("steps.start_date.question")
@question_type = :date
Expand Down
17 changes: 17 additions & 0 deletions app/validators/future_date_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class FutureDateValidator
def initialize(record, field)
@record = record
@field = field
@test_date = record.public_send(field)
end

def validate
return unless test_date.present? && test_date > Date.current

record.errors.add(field, :not_in_future)
end

private

attr_reader :record, :field, :test_date
end
7 changes: 6 additions & 1 deletion config/locales/steps.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,14 @@ en:
blank: Choose the option that applies to you
start_date:
blank: Enter your contract start date
not_in_future: Start date cannot be in the future
subject:
blank: Choose a subject
visa_type:
blank: Choose your visa type
date_of_entry:
blank: Enter your entry date
not_in_future: Date of entry cannot be in the future
given_name:
blank: Enter your given name(s)
middle_name:
Expand All @@ -191,13 +193,16 @@ en:
invalid: Enter a valid telephone number, for example, 07700 900 982 or +34 626 587 210
date_of_birth:
blank: Enter your date of birth
invalid: Age must be below 80
not_in_future: Date of birth cannot be in the future
over_max_age: Age must be below 80
below_min_age: Age must be above 22
sex:
blank: Enter your sex
inclusion: Enter your sex
passport_number:
blank: Enter your passport number
invalid: Invalid passport number
too_long: Invalid passport number
nationality:
blank: Choose your nationality
inclusion: Choose your nationality
Expand Down
8 changes: 4 additions & 4 deletions spec/factories/applications.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@
#
FactoryBot.define do
factory :application do
application_route { ApplicationRouteStep::VALID_ANSWERS_OPTIONS.reject { _1 == "other"}.sample }
application_route { ApplicationRouteStep::VALID_ANSWERS_OPTIONS.reject { _1 == "other" }.sample }
application_date { Faker::Date.in_date_period }
applicant
application_progress strategy: :build, factory: :application_progress
subject { SubjectStep::VALID_ANSWERS_OPTIONS.reject { _1 == "other"}.sample }
subject { SubjectStep::VALID_ANSWERS_OPTIONS.reject { _1 == "other" }.sample }
visa_type { VisaStep::VALID_ANSWERS_OPTIONS.reject { _1 == "Other" }.sample }
date_of_entry { Time.zone.today }
start_date { 1.month.from_now.to_date }
submitted

factory :teacher_application do
application_route { "teacher" }
subject { SubjectStep::VALID_ANSWERS_OPTIONS.reject { _1 == "other"}.sample }
subject { SubjectStep::VALID_ANSWERS_OPTIONS.reject { _1 == "other" }.sample }
applicant strategy: :create, factory: :applicant
end

factory :salaried_trainee_application do
application_route { "salaried_trainee" }
subject { SubjectStep::VALID_ANSWERS_OPTIONS.reject { _1 == "other"}.sample }
subject { SubjectStep::VALID_ANSWERS_OPTIONS.reject { _1 == "other" }.sample }
applicant strategy: :create, factory: :applicant
end

Expand Down
21 changes: 21 additions & 0 deletions spec/steps/entry_date_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,25 @@
question: "Enter the date you moved to England to start your teacher training course",
question_type: :date
end

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

before { step.valid? }

context "when not in the future" do
let(:date_of_entry) { 1.day.ago }

it { expect(error).to be_blank }
end

context "when in the future" do
let(:date_of_entry) { 1.day.from_now }

it { expect(error).to be_present }
end
end
end
end
21 changes: 21 additions & 0 deletions spec/steps/start_date_step_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,25 @@
required_fields: %i[start_date],
question: "Enter the start date of your contract",
question_type: :date

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

before { step.valid? }

context "when not in the future" do
let(:start_date) { 1.day.ago }

it { expect(error).to be_blank }
end

context "when in the future" do
let(:start_date) { 1.day.from_now }

it { expect(error).to be_present }
end
end
end
end
1 change: 1 addition & 0 deletions spec/validators/email_format_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

describe EmailFormatValidator do
subject(:validator) { described_class.new(form) }

let(:form) { build(:form) }

before do
Expand Down
24 changes: 24 additions & 0 deletions spec/validators/future_date_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "rails_helper"

describe FutureDateValidator do
subject(:validator) { described_class.new(form, :date_of_birth) }

let(:form) { build(:form) }

before do
form.date_of_birth = dob
validator.validate
end

context "returns no error when date not in the future" do
let(:dob) { 1.day.ago }

it { expect(form.errors[:date_of_birth]).to be_blank }
end

context "returns an error when date is in the future" do
let(:dob) { 1.day.from_now }

it { expect(form.errors[:date_of_birth]).to be_present }
end
end
3 changes: 2 additions & 1 deletion spec/validators/postcode_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

describe PostcodeValidator do
subject(:employment) { EmploymentDetailsStep.new(form) }

let(:form) { build(:form, school_postcode: postcode) }

context "with a valid UK postcode" do
Expand All @@ -22,7 +23,7 @@
it "adds an error" do
employment.valid?

expect(employment.errors[:school_postcode]).not_to be_blank
expect(employment.errors[:school_postcode]).not_to be_blank
end
end
end

0 comments on commit 901e21a

Please sign in to comment.