diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 4b9d196b9..3657628ae 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -6,6 +6,7 @@ class CaseLogValidator < ActiveModel::Validator include Validations::FinancialValidations include Validations::TenancyValidations include Validations::DateValidations + include Validations::LocalAuthorityValidations def validate(record) validation_methods = public_methods.select { |method| method.starts_with?("validate_") } diff --git a/app/models/validations/local_authority_validations.rb b/app/models/validations/local_authority_validations.rb new file mode 100644 index 000000000..cae230900 --- /dev/null +++ b/app/models/validations/local_authority_validations.rb @@ -0,0 +1,11 @@ +module Validations::LocalAuthorityValidations + POSTCODE_REGEXP = Validations::PropertyValidations::POSTCODE_REGEXP + + def validate_previous_accommodation_postcode(record) + postcode = record.previous_postcode + if postcode.present? && !postcode.match(POSTCODE_REGEXP) + error_message = "Enter a postcode in the correct format, for example AA1 1AA" + record.errors.add :previous_postcode, error_message + end + end +end diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index 158b237cb..4d6792cd5 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -3,7 +3,7 @@ module Validations::PropertyValidations # or 'validate_' to run on submit as well include Constants::CaseLog - POSTCODE_REGEXP = /^(([A-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$/ + POSTCODE_REGEXP = /^(([A-Z]{1,2}[0-9][A-Z0-9]?|ASCN|STHL|TDCU|BBND|[BFS]IQQ|PCRN|TKCA) ?[0-9][A-Z]{2}|BFPO ?[0-9]{1,4}|(KY[0-9]|MSR|VG|AI)[ -]?[0-9]{4}|[A-Z]{2} ?[0-9]{2}|GE ?CX|GIR ?0A{2}|SAN ?TA1)$/i def validate_property_number_of_times_relet(record) if record.offered && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.offered.to_s) diff --git a/spec/models/validations/local_authority_validations_spec.rb b/spec/models/validations/local_authority_validations_spec.rb new file mode 100644 index 000000000..c62607e2a --- /dev/null +++ b/spec/models/validations/local_authority_validations_spec.rb @@ -0,0 +1,56 @@ +require "rails_helper" +require_relative "../../request_helper" + +RSpec.describe CaseLog do + let(:owning_organisation) { FactoryBot.create(:organisation) } + let(:managing_organisation) { owning_organisation } + + before do + RequestHelper.stub_http_requests + end + + describe "#new" do + it "raises an error when previous_postcode is present and invalid" do + expect { + CaseLog.create!( + previous_postcode: "invalid_postcode", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation, + ) + }.to raise_error(ActiveRecord::RecordInvalid, /Enter a postcode in the correct format/) + end + end +end + +RSpec.describe Validations::LocalAuthorityValidations do + let(:subject) { subject_class.new } + let(:subject_class) { Class.new { include Validations::LocalAuthorityValidations } } + let(:record) { FactoryBot.create(:case_log) } + + describe "#validate_previous_accommodation_postcode" do + it "does not add an error if the record previous_postcode is missing" do + record.previous_postcode = nil + subject.validate_previous_accommodation_postcode(record) + expect(record.errors).to be_empty + end + + it "does not add an error if the record previous_postcode is valid (uppercase space)" do + record.previous_postcode = "M1 1AE" + subject.validate_previous_accommodation_postcode(record) + expect(record.errors).to be_empty + end + + it "does not add an error if the record previous_postcode is valid (lowercase no space)" do + record.previous_postcode = "m11ae" + subject.validate_previous_accommodation_postcode(record) + expect(record.errors).to be_empty + end + + it "does add an error when the postcode is invalid" do + record.previous_postcode = "invalid" + subject.validate_previous_accommodation_postcode(record) + expect(record.errors).to_not be_empty + expect(record.errors["previous_postcode"]).to include(match /Enter a postcode in the correct format/) + end + end +end