From 9d0f9a59927f513f722152ca3a09c8f090db81fe Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 3 Mar 2023 14:50:49 +0000 Subject: [PATCH] feat: add validation with feature flag, typo fix and update tests --- app/models/validations/date_validations.rb | 28 +---- app/models/validations/setup_validations.rb | 42 ++++++- config/initializers/feature_toggle.rb | 6 +- config/locales/en.yml | 4 + db/schema.rb | 8 +- .../form/accessible_autocomplete_spec.rb | 2 +- .../form/progressive_total_field_spec.rb | 2 +- .../validations/date_validations_spec.rb | 24 +--- .../validations/setup_validations_spec.rb | 106 ++++++++++++++++-- 9 files changed, 157 insertions(+), 65 deletions(-) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 66e472ba0..db93a9cdb 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -33,16 +33,6 @@ module Validations::DateValidations def validate_startdate(record) return unless record.startdate && date_valid?("startdate", record) - created_at = record.created_at || Time.zone.now - - if created_at > first_collection_end_date && record.startdate < second_collection_start_date - record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") - end - - if (record.startdate < first_collection_start_date || record.startdate > second_collection_end_date) && FeatureToggle.startdate_collection_window_validation_enabled? - record.errors.add :startdate, I18n.t("validations.date.outside_collection_window") - end - if FeatureToggle.startdate_two_week_validation_enabled? && record.startdate > Time.zone.today + 14 record.errors.add :startdate, I18n.t("validations.setup.startdate.later_than_14_days_after") end @@ -66,23 +56,7 @@ module Validations::DateValidations scheme_during_startdate_validation(record, :startdate) end -private - - def first_collection_start_date - @first_collection_start_date ||= FormHandler.instance.lettings_forms["previous_lettings"].start_date - end - - def first_collection_end_date - @first_collection_end_date ||= FormHandler.instance.lettings_forms["previous_lettings"].end_date - end - - def second_collection_start_date - @second_collection_start_date ||= FormHandler.instance.lettings_forms["current_lettings"].start_date - end - - def second_collection_end_date - @second_collection_end_date ||= FormHandler.instance.lettings_forms["current_lettings"].end_date - end + private def is_rsnvac_first_let?(record) [15, 16, 17].include?(record["rsnvac"]) diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index 93105f9ea..fe0ee06ec 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -1,5 +1,14 @@ module Validations::SetupValidations include Validations::SharedValidations + include CollectionTimeHelper + + def validate_startdate_setup(record) + return unless record.startdate && date_valid?("startdate", record) + + if !FeatureToggle.startdate_collection_window_validation_enabled? && !record.startdate.between?(active_collection_start_date, current_collection_end_date) + record.errors.add :startdate, validation_error_message + end + end def validate_irproduct_other(record) if intermediate_product_rent_type?(record) && record.irproduct_other.blank? @@ -25,7 +34,38 @@ module Validations::SetupValidations end end -private + private + + def active_collection_start_date + if FormHandler.instance.lettings_in_crossover_period? + previous_collection_start_date + else + current_collection_start_date + end + end + + def validation_error_message + current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y") + + if FormHandler.instance.lettings_in_crossover_period? + I18n.t( + "validations.setup.startdate.previous_and_current_financial_year", + previous_start_year_short: previous_collection_start_date.strftime("%y"), + previous_end_year_short: previous_collection_end_date.strftime("%y"), + previous_start_year_long: previous_collection_start_date.strftime("#{previous_collection_start_date.day.ordinalize} %B %Y"), + current_end_year_short: current_collection_end_date.strftime("%y"), + current_end_year_long:, + ) + else + I18n.t( + "validations.setup.startdate.current_financial_year", + current_start_year_short: current_collection_start_date.strftime("%y"), + current_end_year_short: current_collection_end_date.strftime("%y"), + current_start_year_long: current_collection_start_date.strftime("#{current_collection_start_date.day.ordinalize} %B %Y"), + current_end_year_long:, + ) + end + end def intermediate_product_rent_type?(record) record.rent_type == 5 diff --git a/config/initializers/feature_toggle.rb b/config/initializers/feature_toggle.rb index 4fc402212..617eaa85f 100644 --- a/config/initializers/feature_toggle.rb +++ b/config/initializers/feature_toggle.rb @@ -4,11 +4,11 @@ class FeatureToggle Rails.env.production? || Rails.env.test? || Rails.env.staging? end - def self.startdate_two_week_validation_enabled? - Rails.env.production? || Rails.env.test? || Rails.env.staging? + def self.startdate_collection_window_validation_enabled? + !Rails.env.test? end - def self.startdate_collection_window_validation_enabled? + def self.startdate_two_week_validation_enabled? Rails.env.production? || Rails.env.test? || Rails.env.staging? end diff --git a/config/locales/en.yml b/config/locales/en.yml index 8665c022a..f285b9a1f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -156,6 +156,10 @@ en: "Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} financial years, which is between %{previous_start_year_long} and %{current_end_year_long}" startdate: + current_financial_year: + Enter a date within the %{current_start_year_short}/%{current_end_year_short} financial year, which is between %{current_start_year_long} and %{current_end_year_long} + previous_and_current_financial_year: + "Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} financial years, which is between %{previous_start_year_long} and %{current_end_year_long}" later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date" before_scheme_end_date: "The tenancy start date must be before the end date for this supported housing scheme" after_void_date: "Enter a tenancy start date that is after the void date" diff --git a/db/schema.rb b/db/schema.rb index afa0b6b74..2880ecd2d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -490,6 +490,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_01_144555) do t.integer "prevten" t.integer "mortgageused" t.integer "wchair" + t.integer "income2_value_check" t.integer "armedforcesspouse" t.datetime "hodate", precision: nil t.integer "hoday" @@ -514,14 +515,13 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_01_144555) do t.integer "retirement_value_check" t.integer "hodate_check" t.integer "extrabor_value_check" - t.integer "grant_value_check" - t.integer "staircase_bought_value_check" t.integer "deposit_and_mortgage_value_check" t.integer "shared_ownership_deposit_value_check" + t.integer "grant_value_check" + t.integer "value_value_check" t.integer "old_persons_shared_ownership_value_check" - t.integer "income2_value_check" + t.integer "staircase_bought_value_check" t.integer "monthly_charges_value_check" - t.integer "value_value_check" t.integer "details_known_5" t.integer "details_known_6" t.integer "saledate_check" diff --git a/spec/features/form/accessible_autocomplete_spec.rb b/spec/features/form/accessible_autocomplete_spec.rb index fd8102dcc..160d32d38 100644 --- a/spec/features/form/accessible_autocomplete_spec.rb +++ b/spec/features/form/accessible_autocomplete_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" require_relative "helpers" -RSpec.describe "Accessible Automcomplete" do +RSpec.describe "Accessible Autocomplete" do include Helpers let(:user) { FactoryBot.create(:user) } let(:lettings_log) do diff --git a/spec/features/form/progressive_total_field_spec.rb b/spec/features/form/progressive_total_field_spec.rb index e5959d16d..787174c20 100644 --- a/spec/features/form/progressive_total_field_spec.rb +++ b/spec/features/form/progressive_total_field_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" require_relative "helpers" -RSpec.describe "Accessible Automcomplete" do +RSpec.describe "Accessible Autocomplete" do include Helpers let(:user) { FactoryBot.create(:user) } let(:lettings_log) do diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 20d1e094c..cd6e38f50 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -9,18 +9,6 @@ RSpec.describe Validations::DateValidations do let(:scheme_no_end_date) { FactoryBot.create(:scheme, end_date: nil) } describe "tenancy start date" do - it "cannot be before the first collection window start date" do - record.startdate = Time.zone.local(2020, 1, 1) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match I18n.t("validations.date.outside_collection_window")) - end - - it "cannot be after the second collection window end date" do - record.startdate = Time.zone.local(2023, 7, 1, 6) - date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match I18n.t("validations.date.outside_collection_window")) - end - it "must be a valid date" do record.startdate = Time.zone.local(0, 7, 1) date_validator.validate_startdate(record) @@ -98,7 +86,7 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022")) + .to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -123,7 +111,7 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022")) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -150,7 +138,7 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 September 2022")) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 September 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -177,7 +165,7 @@ RSpec.describe Validations::DateValidations do record.location = location date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022")) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022")) end end @@ -194,7 +182,7 @@ RSpec.describe Validations::DateValidations do record.scheme = scheme date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 August 2022")) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 August 2022")) end it "produces no error when tenancy start date is during an active scheme period" do @@ -220,7 +208,7 @@ RSpec.describe Validations::DateValidations do record.scheme = scheme date_validator.validate_startdate(record) expect(record.errors["startdate"]) - .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 September 2022")) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 September 2022")) end it "produces no error when tenancy start date is during an active scheme period" do diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index 326a6936d..ce8ac1dc3 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -6,13 +6,99 @@ RSpec.describe Validations::SetupValidations do let(:setup_validator_class) { Class.new { include Validations::SetupValidations } } let(:record) { FactoryBot.create(:lettings_log) } + before do + allow(FeatureToggle).to receive(:startdate_validation_enabled?).and_return(true) + end + + describe "tenancy start date" do + context "when in 22/23 collection" do + context "when in the crossover period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2022, 4, 1)) + record.created_at = Time.zone.local(2022, 4, 1) + end + + it "cannot be before the first collection window start date" do + record.startdate = Time.zone.local(2021, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 31st March 2023") + end + + it "cannot be after the second collection window end date" do + record.startdate = Time.zone.local(2023, 7, 1, 6) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 31st March 2023") + end + end + + context "when after the crossover period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 1, 1)) + record.created_at = Time.zone.local(2023, 1, 1) + end + + it "cannot be before the first collection window start date" do + record.startdate = Time.zone.local(2022, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023") + end + + it "cannot be after the second collection window end date" do + record.startdate = Time.zone.local(2023, 7, 1, 6) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023") + end + end + end + + context "when in 23/24 collection" do + context "when in the crossover period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2023, 4, 1)) + record.created_at = Time.zone.local(2023, 4, 1) + end + + it "cannot be before the first collection window start date" do + record.startdate = Time.zone.local(2022, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 31st March 2024") + end + + it "cannot be after the second collection window end date" do + record.startdate = Time.zone.local(2024, 7, 1, 6) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 31st March 2024") + end + end + + context "when after the crossover period" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2024, 1, 1)) + record.created_at = Time.zone.local(2024, 1, 1) + end + + it "cannot be before the first collection window start date" do + record.startdate = Time.zone.local(2023, 1, 1) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 31st March 2024") + end + + it "cannot be after the second collection window end date" do + record.startdate = Time.zone.local(2024, 7, 1, 6) + setup_validator.validate_startdate_setup(record) + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 31st March 2024") + end + end + end + end + describe "#validate_irproduct" do it "adds an error when the intermediate rent product name is not provided but the rent type was given as other intermediate rent product" do record.rent_type = 5 record.irproduct_other = nil setup_validator.validate_irproduct_other(record) expect(record.errors["irproduct_other"]) - .to include(match I18n.t("validations.setup.intermediate_rent_product_name.blank")) + .to include(match I18n.t("validations.setup.intermediate_rent_product_name.blank")) end it "adds an error when the intermediate rent product name is blank but the rent type was given as other intermediate rent product" do @@ -20,7 +106,7 @@ RSpec.describe Validations::SetupValidations do record.irproduct_other = "" setup_validator.validate_irproduct_other(record) expect(record.errors["irproduct_other"]) - .to include(match I18n.t("validations.setup.intermediate_rent_product_name.blank")) + .to include(match I18n.t("validations.setup.intermediate_rent_product_name.blank")) end it "Does not add an error when the intermediate rent product name is provided and the rent type was given as other intermediate rent product" do @@ -46,7 +132,7 @@ RSpec.describe Validations::SetupValidations do record.location = location setup_validator.validate_scheme(record) expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022")) + .to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -71,7 +157,7 @@ RSpec.describe Validations::SetupValidations do record.location = location setup_validator.validate_scheme(record) expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022")) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -98,7 +184,7 @@ RSpec.describe Validations::SetupValidations do record.location = location setup_validator.validate_scheme(record) expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022")) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022")) end end @@ -115,7 +201,7 @@ RSpec.describe Validations::SetupValidations do record.scheme = scheme setup_validator.validate_scheme(record) expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 August 2022")) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 August 2022")) end it "produces no error when tenancy start date is during an active scheme period" do @@ -141,7 +227,7 @@ RSpec.describe Validations::SetupValidations do record.scheme = scheme setup_validator.validate_scheme(record) expect(record.errors["scheme_id"]) - .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 September 2022")) + .to include(match I18n.t("validations.setup.startdate.scheme.reactivating_soon", name: scheme.service_name, date: "4 September 2022")) end it "produces no error when tenancy start date is during an active scheme period" do @@ -168,7 +254,7 @@ RSpec.describe Validations::SetupValidations do record.location = location setup_validator.validate_location(record) expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022")) + .to include(match I18n.t("validations.setup.startdate.location.deactivated", postcode: location.postcode, date: "4 June 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -193,7 +279,7 @@ RSpec.describe Validations::SetupValidations do record.location = location setup_validator.validate_location(record) expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022")) + .to include(match I18n.t("validations.setup.startdate.location.reactivating_soon", postcode: location.postcode, date: "4 August 2022")) end it "produces no error when tenancy start date is during an active location period" do @@ -220,7 +306,7 @@ RSpec.describe Validations::SetupValidations do record.location = location setup_validator.validate_location(record) expect(record.errors["location_id"]) - .to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022")) + .to include(match I18n.t("validations.setup.startdate.location.activating_soon", postcode: location.postcode, date: "15 September 2022")) end end end