From 6e684ff8de60662327ab299723cfbd9fc1520ce5 Mon Sep 17 00:00:00 2001 From: fumimowdan Date: Wed, 18 Oct 2023 11:53:33 +0100 Subject: [PATCH] Fix upper check in application progress date validation --- .../application_progress_validator.rb | 2 +- .../application_progress_validator_spec.rb | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/validators/application_progress_validator.rb b/app/validators/application_progress_validator.rb index f1afb0ac..7c4b8ba5 100644 --- a/app/validators/application_progress_validator.rb +++ b/app/validators/application_progress_validator.rb @@ -66,7 +66,7 @@ def invalid_date_range(date_param, field_date) end def date_out_range?(field_date) - field_date < 12.months.ago || 12.months.from_now > field_date + field_date < 12.months.ago || 12.months.from_now < field_date end def parse_date(year, month, day) diff --git a/spec/validators/application_progress_validator_spec.rb b/spec/validators/application_progress_validator_spec.rb index 3053b167..3c6317fa 100644 --- a/spec/validators/application_progress_validator_spec.rb +++ b/spec/validators/application_progress_validator_spec.rb @@ -43,7 +43,7 @@ it_behaves_like "requires a rejection reason" - context "date really out of range" do + context "date really out of range on the lower side" do let(:date_param) { ApplicationProgressValidator::DATE_PARAMS.first } let(:params) { { "#{date_param}(3i)" => "31", "#{date_param}(2i)" => "1", "#{date_param}(1i)" => "23" } } @@ -54,4 +54,30 @@ expect(progress.errors.details[date_param]).to include(error: "out of range") end end + + context "date really out of range on the higher side" do + let(:date_param) { ApplicationProgressValidator::DATE_PARAMS.first } + let(:params) { { "#{date_param}(3i)" => "31", "#{date_param}(2i)" => "1", "#{date_param}(1i)" => "2003" } } + + it "adds an error for the invalid date" do + validator = described_class.new(progress, params) + + expect(validator.valid?).to be false + expect(progress.errors.details[date_param]).to include(error: "out of range") + end + end + + context "dates valid" do + let(:date_param) { ApplicationProgressValidator::DATE_PARAMS.first } + let(:year) { Date.current.year.to_s } + let(:params) do + { "#{date_param}(3i)" => "31", "#{date_param}(2i)" => "1", "#{date_param}(1i)" => year } + end + + it "adds an error for the invalid date" do + validator = described_class.new(progress, params) + + expect(validator.valid?).to be true + end + end end