From 36553f47779b809f85724669f29532e32b65dfd3 Mon Sep 17 00:00:00 2001 From: Rachael Booth Date: Mon, 4 Mar 2024 09:43:29 +0000 Subject: [PATCH] CLDC-1970: Use more relevant error message for shared ownership income below 0 (#2270) --- .../sales/financial_validations.rb | 16 ++-- config/locales/en.yml | 6 +- .../sales/financial_validations_spec.rb | 76 ++++++++++++++----- 3 files changed, 67 insertions(+), 31 deletions(-) diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 9e30d49f3..77bd60102 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -6,10 +6,10 @@ module Validations::Sales::FinancialValidations return unless record.income1 && record.la && record.shared_ownership_scheme? relevant_fields = %i[income1 ownershipsch uprn la postcode_full] - if record.london_property? && record.income1 > 90_000 - relevant_fields.each { |field| record.errors.add field, :over_hard_max_for_london, message: I18n.t("validations.financial.income.over_hard_max_for_london") } - elsif record.property_not_in_london? && record.income1 > 80_000 - relevant_fields.each { |field| record.errors.add field, :over_hard_max_for_outside_london, message: I18n.t("validations.financial.income.over_hard_max_for_outside_london") } + if record.london_property? && !record.income1.between?(0, 90_000) + relevant_fields.each { |field| record.errors.add field, :outside_london_income_range, message: I18n.t("validations.financial.income.outside_london_income_range") } + elsif record.property_not_in_london? && !record.income1.between?(0, 80_000) + relevant_fields.each { |field| record.errors.add field, :outside_non_london_income_range, message: I18n.t("validations.financial.income.outside_non_london_income_range") } end end @@ -17,10 +17,10 @@ module Validations::Sales::FinancialValidations return unless record.income2 && record.la && record.shared_ownership_scheme? relevant_fields = %i[income2 ownershipsch uprn la postcode_full] - if record.london_property? && record.income2 > 90_000 - relevant_fields.each { |field| record.errors.add field, :over_hard_max_for_london, message: I18n.t("validations.financial.income.over_hard_max_for_london") } - elsif record.property_not_in_london? && record.income2 > 80_000 - relevant_fields.each { |field| record.errors.add field, :over_hard_max_for_outside_london, message: I18n.t("validations.financial.income.over_hard_max_for_outside_london") } + if record.london_property? && !record.income2.between?(0, 90_000) + relevant_fields.each { |field| record.errors.add field, :outside_london_income_range, message: I18n.t("validations.financial.income.outside_london_income_range") } + elsif record.property_not_in_london? && !record.income2.between?(0, 80_000) + relevant_fields.each { |field| record.errors.add field, :outside_non_london_income_range, message: I18n.t("validations.financial.income.outside_non_london_income_range") } end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 0ed12c806..70c0ac398 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -368,8 +368,8 @@ en: freq_missing: "Select how often the household receives income" earnings_missing: "Enter how much income the household has in total" income: - over_hard_max_for_london: "Income must be £90,000 or lower for properties within a London local authority" - over_hard_max_for_outside_london: "Income must be £80,000 or lower for properties outside London local authority" + outside_london_income_range: "Income must be between £0 and £90,000 for properties within a London local authority" + outside_non_london_income_range: "Income must be between £0 and £80,000 for properties in a non-London local authority" combined_over_hard_max_for_london: "Combined income must be £90,000 or lower for properties within a London local authority" combined_over_hard_max_for_outside_london: "Combined income must be £80,000 or lower for properties outside London local authorities" child_has_income: "Child's income must be £0" @@ -627,7 +627,7 @@ en: value: over_discounted_london_max: "The percentage discount multiplied by the purchase price is %{discount_value}. This figure should not be more than £136,400 for properties in London." over_discounted_max: "The percentage discount multiplied by the purchase price is %{discount_value}. This figure should not be more than £102,400 for properties outside of London." - non_staircasing_mortgage: + non_staircasing_mortgage: mortgage_used: "The mortgage and deposit added together is %{mortgage_and_deposit_total} and the purchase price times by the equity is %{expected_shared_ownership_deposit_value}. These figures should be the same." mortgage_not_used: "The deposit is %{deposit} and the purchase price times by the equity is %{expected_shared_ownership_deposit_value}. As no mortgage was used, these figures should be the same." staircasing_mortgage: diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 8a57df6b6..470148478 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -17,33 +17,51 @@ RSpec.describe Validations::Sales::FinancialValidations do it "adds errors if buyer 1 has income over 80,000" do record.income1 = 85_000 financial_validator.validate_income1(record) - expect(record.errors["income1"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_outside_london")) - expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_outside_london")) - expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_outside_london")) - expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_outside_london")) + expect(record.errors["income1"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) end it "adds errors if buyer 2 has income over 80,000" do record.income2 = 85_000 financial_validator.validate_income2(record) - expect(record.errors["income2"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_outside_london")) - expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_outside_london")) - expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_outside_london")) - expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_outside_london")) + expect(record.errors["income2"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) end - it "does not add errors if buyer 1 has income below 80_000" do + it "does not add errors if buyer 1 has income above 0 and below 80_000" do record.income1 = 75_000 financial_validator.validate_income1(record) expect(record.errors).to be_empty end - it "does not add errors if buyer 2 has income below 80_000" do + it "does not add errors if buyer 2 has income above 0 and below 80_000" do record.income2 = 75_000 financial_validator.validate_income2(record) expect(record.errors).to be_empty end + it "adds errors if buyer 1 has income below 0" do + record.income1 = -500 + financial_validator.validate_income1(record) + expect(record.errors["income1"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + end + + it "adds errors if buyer 2 has income below 0" do + record.income2 = -5 + financial_validator.validate_income2(record) + expect(record.errors["income2"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.outside_non_london_income_range")) + end + it "adds errors when combined income is over 80_000" do record.income1 = 45_000 record.income2 = 40_000 @@ -69,33 +87,51 @@ RSpec.describe Validations::Sales::FinancialValidations do it "adds errors if buyer 1 has income over 90,000" do record.income1 = 95_000 financial_validator.validate_income1(record) - expect(record.errors["income1"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_london")) - expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_london")) - expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_london")) - expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_london")) + expect(record.errors["income1"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) end it "adds errors if buyer 2 has income over 90,000" do record.income2 = 95_000 financial_validator.validate_income2(record) - expect(record.errors["income2"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_london")) - expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_london")) - expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_london")) - expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.over_hard_max_for_london")) + expect(record.errors["income2"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) end - it "does not add errors if buyer 1 has income below 90_000" do + it "does not add errors if buyer 1 has income above 0 and below 90_000" do record.income1 = 75_000 financial_validator.validate_income1(record) expect(record.errors).to be_empty end - it "does not add errors if buyer 2 has income below 90_000" do + it "does not add errors if buyer 2 has income above 0 and below 90_000" do record.income2 = 75_000 financial_validator.validate_income2(record) expect(record.errors).to be_empty end + it "adds errors if buyer 1 has income below 0" do + record.income1 = -500 + financial_validator.validate_income1(record) + expect(record.errors["income1"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + end + + it "adds errors if buyer 2 has income below 0" do + record.income2 = -2 + financial_validator.validate_income2(record) + expect(record.errors["income2"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["la"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + expect(record.errors["postcode_full"]).to include(match I18n.t("validations.financial.income.outside_london_income_range")) + end + it "adds errors when combined income is over 90_000" do record.income1 = 55_000 record.income2 = 40_000