From 18d893544dab7d9a16ba468f7a5393f42f71fefb Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 17 Feb 2022 09:27:41 +0000 Subject: [PATCH] Fix referral validations and move referral validation from local_authority to household --- app/models/validations/household_validations.rb | 5 ++++- .../validations/local_authority_validations.rb | 6 ------ app/models/validations/tenancy_validations.rb | 2 +- config/locales/en.yml | 5 +---- .../validations/household_validations_spec.rb | 13 ++++++++++--- spec/models/validations/tenancy_validations_spec.rb | 13 +++++++++++-- 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb index bfd025b35..7e3847a03 100644 --- a/app/models/validations/household_validations.rb +++ b/app/models/validations/household_validations.rb @@ -74,7 +74,10 @@ module Validations::HouseholdValidations end def validate_referral(record) - if record.tenancy == "Secure (including flexible)" && record.referral == "Internal transfer" + if record.rsnvac == "Re-let to tenant who occupied same property as temporary accommodation" && REFERRAL_INVALID_TMP.include?(record.referral) + record.errors.add :referral, I18n.t("validations.household.referral.rsnvac_non_temp") + end + if record.tenancy == "Secure (including flexible)" && record.referral.present? && record.referral != "Internal transfer" record.errors.add :referral, I18n.t("validations.household.referral.secure_tenancy") end end diff --git a/app/models/validations/local_authority_validations.rb b/app/models/validations/local_authority_validations.rb index c8e8c5557..b1554607d 100644 --- a/app/models/validations/local_authority_validations.rb +++ b/app/models/validations/local_authority_validations.rb @@ -9,10 +9,4 @@ module Validations::LocalAuthorityValidations record.errors.add :previous_postcode, error_message end end - - def validate_referral(record) - if record.rsnvac == "Re-let to tenant who occupied same property as temporary accommodation" && REFERRAL_INVALID_TMP.include?(record.referral) - record.errors.add :referral, I18n.t("validations.local_authority.referral.rsnvac_non_temp") - end - end end diff --git a/app/models/validations/tenancy_validations.rb b/app/models/validations/tenancy_validations.rb index 43b96c8a9..2a3dff1cc 100644 --- a/app/models/validations/tenancy_validations.rb +++ b/app/models/validations/tenancy_validations.rb @@ -27,7 +27,7 @@ module Validations::TenancyValidations end def validate_tenancy(record) - if record.tenancy == "Secure (including flexible)" && record.referral == "Internal transfer" + if record.tenancy.present? && record.tenancy != "Secure (including flexible)" && record.referral == "Internal transfer" record.errors.add :tenancy, I18n.t("validations.tenancy.internal_referral") end end diff --git a/config/locales/en.yml b/config/locales/en.yml index c69ee8724..f8e1ac01d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -112,6 +112,7 @@ en: non_temp_accommodation: "Answer cannot be non-temporary accommodation as you already told us this is a re-let to tenant who occupied the same property as temporary accommodation" referral: secure_tenancy: "Answer must be internal transfer as you already told us this is a secure tenancy" + rsnvac_non_temp: "Answer cannot be this source of referral as you already told us this is a re-let to tenant who occupied the same property as temporary accommodation" tenancy: length: @@ -120,10 +121,6 @@ en: secure: "Secure (including flexible) should be between 2 and 99 years or not specified" internal_referral: "Answer must be secure tenancy as you already told us this tenancy is an internal transfer" - local_authority: - referral: - rsnvac_non_temp: "Answer cannot be this source of referral as you already told us this is a re-let to tenant who occupied the same property as temporary accommodation" - declaration: missing: "You must show the DLUHC privacy notice to the tenant before you can submit this log." diff --git a/spec/models/validations/household_validations_spec.rb b/spec/models/validations/household_validations_spec.rb index 0c01b3fe7..1d987bcd7 100644 --- a/spec/models/validations/household_validations_spec.rb +++ b/spec/models/validations/household_validations_spec.rb @@ -479,14 +479,21 @@ RSpec.describe Validations::HouseholdValidations do end describe "referral validations" do - context "when type of tenancy is secure" do - it "must be internal transfer" do + context "when type of tenancy is not secure" do + it "cannot be not internal transfer" do record.tenancy = "Secure (including flexible)" - record.referral = "Internal transfer" + record.referral = "Other social landlord" household_validator.validate_referral(record) expect(record.errors["referral"]) .to include(match I18n.t("validations.household.referral.secure_tenancy")) end + + it "can be internal transfer" do + record.tenancy = "Secure (including flexible)" + record.referral = "Internal transfer" + household_validator.validate_referral(record) + expect(record.errors["referral"]).to be_empty + end end end end diff --git a/spec/models/validations/tenancy_validations_spec.rb b/spec/models/validations/tenancy_validations_spec.rb index 762ac7278..f78b1c4a5 100644 --- a/spec/models/validations/tenancy_validations_spec.rb +++ b/spec/models/validations/tenancy_validations_spec.rb @@ -107,15 +107,24 @@ RSpec.describe Validations::TenancyValidations do end end - context "when referral is internal transfer" do + context "when referral is not internal transfer" do it "adds an error" do - record.tenancy = "Secure (including flexible)" + record.tenancy = "Assured" record.referral = "Internal transfer" tenancy_validator.validate_tenancy(record) expect(record.errors["tenancy"]) .to include(match I18n.t("validations.tenancy.internal_referral")) end end + + context "when referral is internal transfer" do + it "does not add an error" do + record.tenancy = "Secure (including flexible)" + record.referral = "Internal transfer" + tenancy_validator.validate_tenancy(record) + expect(record.errors["tenancy"]).to be_empty + end + end end end end