Browse Source

CLDC-509: Previous postcode validation (#212)

Add a validation test separated from the main case_log_spec
Create a new validator for the "Local Authority" section
Changes the postcode regexp to be case insensitive
pull/217/head
Stéphane Meny 3 years ago committed by GitHub
parent
commit
5b792bc0b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/models/case_log.rb
  2. 11
      app/models/validations/local_authority_validations.rb
  3. 2
      app/models/validations/property_validations.rb
  4. 56
      spec/models/validations/local_authority_validations_spec.rb

1
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_") }

11
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

2
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)

56
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
Loading…
Cancel
Save