From c75603755d3089e91715a93169c32abd307acc47 Mon Sep 17 00:00:00 2001 From: Dushan Despotovic Date: Thu, 23 Jun 2022 14:50:20 +0100 Subject: [PATCH] validations --- app/models/validations/date_validations.rb | 4 +++ config/initializers/feature_toggle.rb | 6 +++++ config/locales/en.yml | 2 +- .../validations/date_validations_spec.rb | 25 ++++++++++++++++--- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index d22946dce..aa22cc979 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -35,6 +35,10 @@ module Validations::DateValidations 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 + if record.scheme_id.present? scheme_end_date = Scheme.find(record.scheme_id).end_date if scheme_end_date.present? && (record.startdate > scheme_end_date) diff --git a/config/initializers/feature_toggle.rb b/config/initializers/feature_toggle.rb index 7cf1dbb76..cf53a5010 100644 --- a/config/initializers/feature_toggle.rb +++ b/config/initializers/feature_toggle.rb @@ -4,4 +4,10 @@ class FeatureToggle false end + + def self.startdate_two_week_validation_enabled? + return true if Rails.env.production? + + false + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 07608b462..5424a5461 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -62,7 +62,7 @@ en: intermediate_rent_product_name: blank: "Enter name of other intermediate rent product" startdate: - later_than_or_14_days_before: "The start date of a supported housing tenancy must be 14 days before today or later" + later_than_14_days_after: "The tenancy start date must be not be later than 14 days from today's date" before_scheme_end_date: "The start date of a supported housing tenancy must be before the supported housing scheme end date" property: diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 8d53cdbd4..642f605d6 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -36,7 +36,7 @@ RSpec.describe Validations::DateValidations do it "validates that the tenancy start date is before the end date of the chosen scheme if it has an end date" do record.startdate = Time.zone.today - 3.days record.scheme = scheme - setup_validator.validate_startdate(record) + date_validator.validate_startdate(record) expect(record.errors["startdate"]) .to include(match I18n.t("validations.setup.startdate.before_scheme_end_date")) end @@ -44,16 +44,35 @@ RSpec.describe Validations::DateValidations do it "produces no error when the tenancy start date is before the end date of the chosen scheme if it has an end date" do record.startdate = Time.zone.today - 30.days record.scheme = scheme - setup_validator.validate_startdate(record) + date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty end it "produces no startdate error for scheme end dates when the chosen scheme does not have an end date" do record.startdate = Time.zone.today record.scheme = scheme_no_end_date - setup_validator.validate_startdate(record) + date_validator.validate_startdate(record) expect(record.errors["startdate"]).to be_empty end + + context "when in the production environment" do + before do + allow(Rails.env).to receive(:production?).and_return(true) + end + + it "validates that the tenancy start date is not later than 14 days from the current date" do + record.startdate = Time.zone.today + 15.days + date_validator.validate_startdate(record) + expect(record.errors["startdate"]) + .to include(match I18n.t("validations.setup.startdate.later_than_14_days_after")) + end + + it "produces no error when tenancy start date is not later than 14 days from the current date" do + record.startdate = Time.zone.today + 7.days + date_validator.validate_startdate(record) + expect(record.errors["startdate"]).to be_empty + end + end end describe "major repairs date" do