From 45710ed66164d69793fe0dfef34586973de2e179 Mon Sep 17 00:00:00 2001 From: Jack <113976590+bibblobcode@users.noreply.github.com> Date: Thu, 19 Jan 2023 09:24:45 +0000 Subject: [PATCH] CLDC 911 add hard validation to cash deposit (#1186) * Tidy up test file * Add hard validation to cash deposit * Load validations --- .../form/sales/questions/deposit_amount.rb | 1 + app/models/sales_log.rb | 4 +- .../sales/sale_information_validations.rb | 9 ++++ .../sales/questions/deposit_amount_spec.rb | 4 ++ .../sales/household_validations_spec.rb | 16 +++--- .../sale_information_validations_spec.rb | 49 +++++++++++++++++++ spec/requests/form_controller_spec.rb | 2 +- 7 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 app/models/validations/sales/sale_information_validations.rb create mode 100644 spec/models/validations/sales/sale_information_validations_spec.rb diff --git a/app/models/form/sales/questions/deposit_amount.rb b/app/models/form/sales/questions/deposit_amount.rb index 094f47697..05403dbab 100644 --- a/app/models/form/sales/questions/deposit_amount.rb +++ b/app/models/form/sales/questions/deposit_amount.rb @@ -7,6 +7,7 @@ class Form::Sales::Questions::DepositAmount < ::Form::Question @type = "numeric" @min = 0 @width = 5 + @max = 9_999_999 @prefix = "£" @hint_text = "Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage" @derived = true diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index df76ffcbf..0f660ceb0 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -1,7 +1,9 @@ class SalesLogValidator < ActiveModel::Validator include Validations::Sales::HouseholdValidations - include Validations::SharedValidations include Validations::Sales::FinancialValidations + include Validations::Sales::SaleInformationValidations + + include Validations::SharedValidations include Validations::LocalAuthorityValidations def validate(record) diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb new file mode 100644 index 000000000..4c8747f18 --- /dev/null +++ b/app/models/validations/sales/sale_information_validations.rb @@ -0,0 +1,9 @@ +module Validations::Sales::SaleInformationValidations + def validate_deposit_range(record) + return if record.deposit.blank? + + unless record.deposit >= 0 && record.deposit <= 999_999 + record.errors.add :deposit, "Cash deposit must be £0 - £999,999" + end + end +end diff --git a/spec/models/form/sales/questions/deposit_amount_spec.rb b/spec/models/form/sales/questions/deposit_amount_spec.rb index db47387d3..961576d62 100644 --- a/spec/models/form/sales/questions/deposit_amount_spec.rb +++ b/spec/models/form/sales/questions/deposit_amount_spec.rb @@ -46,4 +46,8 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do it "has correct min" do expect(question.min).to eq(0) end + + it "has correct max" do + expect(question.max).to eq(9_999_999) + end end diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index ea581c096..b7ea4f663 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -7,42 +7,42 @@ RSpec.describe Validations::Sales::HouseholdValidations do describe "#validate_number_of_other_people_living_in_the_property" do context "when within permitted bounds" do - let(:record) { FactoryBot.build(:sales_log, hholdcount: 2) } + let(:record) { build(:sales_log, hholdcount: 2) } it "does not add an error" do household_validator.validate_number_of_other_people_living_in_the_property(record) - expect(record.errors).not_to be_present + expect(record.errors[:hholdcount]).not_to be_present end end context "when blank" do - let(:record) { FactoryBot.build(:sales_log, hholdcount: nil) } + let(:record) { build(:sales_log, hholdcount: nil) } it "does not add an error" do household_validator.validate_number_of_other_people_living_in_the_property(record) - expect(record.errors).not_to be_present + expect(record.errors[:hholdcount]).not_to be_present end end context "when below lower bound" do - let(:record) { FactoryBot.build(:sales_log, hholdcount: -1) } + let(:record) { build(:sales_log, hholdcount: -1) } it "adds an error" do household_validator.validate_number_of_other_people_living_in_the_property(record) - expect(record.errors).to be_present + expect(record.errors[:hholdcount]).to be_present end end context "when higher than upper bound" do - let(:record) { FactoryBot.build(:sales_log, hholdcount: 5) } + let(:record) { build(:sales_log, hholdcount: 5) } it "adds an error" do household_validator.validate_number_of_other_people_living_in_the_property(record) - expect(record.errors).to be_present + expect(record.errors[:hholdcount]).to be_present end end end diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb new file mode 100644 index 000000000..575eedfff --- /dev/null +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -0,0 +1,49 @@ +require "rails_helper" + +RSpec.describe Validations::Sales::SaleInformationValidations do + subject(:sale_information_validator) { validator_class.new } + + let(:validator_class) { Class.new { include Validations::Sales::SaleInformationValidations } } + + describe "#validate_deposit_range" do + context "when within permitted bounds" do + let(:record) { build(:sales_log, deposit: 0) } + + it "does not add an error" do + sale_information_validator.validate_deposit_range(record) + + expect(record.errors[:deposit]).not_to be_present + end + end + + context "when blank" do + let(:record) { build(:sales_log, deposit: nil) } + + it "does not add an error" do + sale_information_validator.validate_deposit_range(record) + + expect(record.errors[:deposit]).not_to be_present + end + end + + context "when below lower bound" do + let(:record) { build(:sales_log, deposit: -1) } + + it "adds an error" do + sale_information_validator.validate_deposit_range(record) + + expect(record.errors[:deposit]).to be_present + end + end + + context "when higher than upper bound" do + let(:record) { build(:sales_log, deposit: 1_000_000) } + + it "adds an error" do + sale_information_validator.validate_deposit_range(record) + + expect(record.errors[:deposit]).to be_present + end + end + end +end diff --git a/spec/requests/form_controller_spec.rb b/spec/requests/form_controller_spec.rb index 04a740dc1..dcf67cb71 100644 --- a/spec/requests/form_controller_spec.rb +++ b/spec/requests/form_controller_spec.rb @@ -658,7 +658,7 @@ RSpec.describe FormController, type: :request do let(:referrer) { "/lettings-logs/#{completed_lettings_log.id}/net-income-value-check?referrer=check_answers" } before do - completed_lettings_log.update!(ecstat1: 1, earnings: 130, hhmemb: 1) # we're not routing to that page, so it gets cleared?§ + completed_lettings_log.update!(ecstat1: 1, earnings: 130, hhmemb: 1) # we're not routing to that page, so it gets cleared? allow(completed_lettings_log).to receive(:net_income_soft_validation_triggered?).and_return(true) allow(completed_lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) post "/lettings-logs/#{completed_lettings_log.id}/form", params: interrupt_params, headers: headers.merge({ "HTTP_REFERER" => referrer })