Browse Source

CLDC-3690 Extract lettings property validations copy (#2742)

* Extract lettings property validations copy

* Update uprn question

* Remove redundant validations (can't trigger them because on inferrance)
pull/2771/head
kosiakkatrina 2 months ago committed by GitHub
parent
commit
0083485b42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      app/models/form/lettings/questions/uprn.rb
  2. 39
      app/models/validations/property_validations.rb
  3. 14
      config/locales/en.yml
  4. 17
      config/locales/validations/lettings/property_information.en.yml
  5. 2
      spec/models/form/lettings/questions/uprn_spec.rb
  6. 12
      spec/models/lettings_log_spec.rb
  7. 112
      spec/models/validations/property_validations_spec.rb

2
app/models/form/lettings/questions/uprn.rb

@ -15,7 +15,7 @@ class Form::Lettings::Questions::Uprn < ::Form::Question
end end
def unanswered_error_message def unanswered_error_message
I18n.t("validations.property.uprn.invalid") I18n.t("validations.lettings.property.uprn.invalid")
end end
def get_extra_check_answer_value(log) def get_extra_check_answer_value(log)

39
app/models/validations/property_validations.rb

@ -4,49 +4,30 @@ module Validations::PropertyValidations
REFERRAL_INVALID_TMP = [8, 10, 12, 13, 14, 15].freeze REFERRAL_INVALID_TMP = [8, 10, 12, 13, 14, 15].freeze
def validate_rsnvac(record) def validate_rsnvac(record)
if !record.first_time_property_let_as_social_housing? && record.has_first_let_vacancy_reason?
record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.first_let_not_social")
end
if record.first_time_property_let_as_social_housing? && record.rsnvac.present? && !record.has_first_let_vacancy_reason?
record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.first_let_social")
end
if record.is_relet_to_temp_tenant? && !record.previous_tenancy_was_temporary? if record.is_relet_to_temp_tenant? && !record.previous_tenancy_was_temporary?
record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.non_temp_accommodation") record.errors.add :rsnvac, I18n.t("validations.lettings.property.rsnvac.non_temp_accommodation")
end end
if record.is_relet_to_temp_tenant? && REFERRAL_INVALID_TMP.include?(record.referral) if record.is_relet_to_temp_tenant? && REFERRAL_INVALID_TMP.include?(record.referral)
record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.referral_invalid") record.errors.add :rsnvac, I18n.t("validations.lettings.property.rsnvac.referral_invalid")
record.errors.add :referral, :referral_invalid, message: I18n.t("validations.household.referral.rsnvac_non_temp") record.errors.add :referral, :referral_invalid, message: I18n.t("validations.lettings.property.referral.rsnvac_non_temp")
end end
if record.renewal.present? && record.renewal.zero? && record.rsnvac == 14 if record.renewal.present? && record.renewal.zero? && record.rsnvac == 14
record.errors.add :rsnvac, I18n.t("validations.property.rsnvac.not_a_renewal") record.errors.add :rsnvac, I18n.t("validations.lettings.property.rsnvac.not_a_renewal")
end
end
def validate_unitletas(record)
if record.first_time_property_let_as_social_housing? && record.unitletas.present?
record.errors.add :unitletas, I18n.t("validations.property.rsnvac.previous_let_social")
end end
end end
def validate_shared_housing_rooms(record) def validate_shared_housing_rooms(record)
return unless record.unittype_gn return unless record.unittype_gn
if record.is_bedsit? && record.beds != 1 && record.beds.present? && !record.form.start_year_2024_or_later?
record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")
record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_bedroom_bedsit")
end
if record.hhmemb == 1 && record.is_shared_housing? && if record.hhmemb == 1 && record.is_shared_housing? &&
!record.beds.to_i.between?(1, 3) && record.beds.present? !record.beds.to_i.between?(1, 3) && record.beds.present?
record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared") record.errors.add :unittype_gn, I18n.t("validations.lettings.property.unittype_gn.one_three_bedroom_single_tenant_shared")
record.errors.add :beds, :one_three_bedroom_single_tenant_shared, message: I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared") record.errors.add :beds, :one_three_bedroom_single_tenant_shared, message: I18n.t("validations.lettings.property.beds.one_three_bedroom_single_tenant_shared")
elsif record.is_shared_housing? && record.beds.present? && !record.beds.to_i.between?(1, 7) elsif record.is_shared_housing? && record.beds.present? && !record.beds.to_i.between?(1, 7)
record.errors.add :unittype_gn, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") record.errors.add :unittype_gn, I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared")
record.errors.add :beds, I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") record.errors.add :beds, I18n.t("validations.lettings.property.beds.one_seven_bedroom_shared")
end end
end end
@ -55,13 +36,13 @@ module Validations::PropertyValidations
return if record.uprn.match?(/^[0-9]{1,12}$/) return if record.uprn.match?(/^[0-9]{1,12}$/)
record.errors.add :uprn, I18n.t("validations.property.uprn.invalid") record.errors.add :uprn, I18n.t("validations.lettings.property.uprn.invalid")
end end
def validate_property_postcode(record) def validate_property_postcode(record)
postcode = record.postcode_full postcode = record.postcode_full
if record.postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP)) if record.postcode_known? && (postcode.blank? || !postcode.match(POSTCODE_REGEXP))
error_message = I18n.t("validations.lettings.property_information.postcode_full.invalid") error_message = I18n.t("validations.lettings.property.postcode_full.invalid")
record.errors.add :postcode_full, :wrong_format, message: error_message record.errors.add :postcode_full, :wrong_format, message: error_message
end end
end end

14
config/locales/en.yml

@ -263,24 +263,11 @@ en:
owning_organisation: owning_organisation:
data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the owning organisation." data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the owning organisation."
property: property:
uprn:
invalid: "UPRN must be 12 digits or less."
uprn_known: uprn_known:
invalid: "You must answer UPRN known?" invalid: "You must answer UPRN known?"
la: la:
la_invalid_for_org: "%{org_name} does not operate in %{la_name}." la_invalid_for_org: "%{org_name} does not operate in %{la_name}."
postcode_invalid_for_org: "Enter a postcode in an area covered by %{org_name}." postcode_invalid_for_org: "Enter a postcode in an area covered by %{org_name}."
rsnvac:
first_let_not_social: "Enter a reason for vacancy that is not 'first let' if unit has been previously let as social housing."
first_let_social: "Reason for vacancy must be first let if unit has been previously let as social housing."
previous_let_social: "Property cannot have a previous let type if being let as social housing for the first time."
non_temp_accommodation: "Answer cannot be re-let to tenant who occupied the same property as temporary accommodation as this accommodation is not temporary."
referral_invalid: "Answer cannot be re-let to tenant who occupied the same property as temporary accommodation as a different source of referral for this letting."
not_a_renewal: "Reason for vacancy cannot be 'Renewal of fixed-term tenancy' if letting is not a renewal."
unittype_gn:
one_bedroom_bedsit: "A bedsit can only have one bedroom."
one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms."
one_three_bedroom_single_tenant_shared: "A shared house with fewer than two tenants must have 1 to 3 bedrooms."
financial: financial:
charges: charges:
@ -319,7 +306,6 @@ en:
one_or_two_choices: "You can only select one option or ‘other disabled access needs’ plus ‘wheelchair-accessible housing’, ‘wheelchair access to essential rooms’ or ‘level access housing’." one_or_two_choices: "You can only select one option or ‘other disabled access needs’ plus ‘wheelchair-accessible housing’, ‘wheelchair access to essential rooms’ or ‘level access housing’."
referral: referral:
secure_tenancy: "Answer must be internal transfer as this is a secure tenancy." secure_tenancy: "Answer must be internal transfer as this is a secure tenancy."
rsnvac_non_temp: "Answer cannot be this source of referral as this is a re-let to tenant who occupied the same property as temporary accommodation."
cannot_be_secure_tenancy: "Answer cannot be secure tenancy as this is not an internal transfer." cannot_be_secure_tenancy: "Answer cannot be secure tenancy as this is not an internal transfer."
assessed_homeless: "Answer cannot be internal transfer as the tenant was assessed as homeless." assessed_homeless: "Answer cannot be internal transfer as the tenant was assessed as homeless."
other_homeless: "Answer cannot be internal transfer as the tenant was considered homeless by their landlord." other_homeless: "Answer cannot be internal transfer as the tenant was considered homeless by their landlord."

17
config/locales/validations/lettings/property_information.en.yml

@ -1,6 +1,21 @@
en: en:
validations: validations:
lettings: lettings:
property_information: property:
postcode_full: postcode_full:
invalid: "Enter a postcode in the correct format, for example AA1 1AA." invalid: "Enter a postcode in the correct format, for example AA1 1AA."
rsnvac:
non_temp_accommodation: "Answer cannot be re-let to tenant who occupied the same property as temporary accommodation as this accommodation is not temporary."
referral_invalid: "Answer cannot be re-let to tenant who occupied the same property as temporary accommodation as a different source of referral for this letting."
not_a_renewal: "Reason for vacancy cannot be 'Renewal of fixed-term tenancy' if letting is not a renewal."
referral:
rsnvac_non_temp: "Answer cannot be this source of referral as this is a re-let to tenant who occupied the same property as temporary accommodation."
unittype_gn:
one_three_bedroom_single_tenant_shared: "A shared house with fewer than two tenants must have 1 to 3 bedrooms."
one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms."
beds:
one_three_bedroom_single_tenant_shared: "A shared house with fewer than two tenants must have 1 to 3 bedrooms."
one_seven_bedroom_shared: "A shared house must have 1 to 7 bedrooms."
uprn:
invalid: "UPRN must be 12 digits or less."

2
spec/models/form/lettings/questions/uprn_spec.rb

@ -28,7 +28,7 @@ RSpec.describe Form::Lettings::Questions::Uprn, type: :model do
end end
it "has the correct unanswered_error_message" do it "has the correct unanswered_error_message" do
expect(question.unanswered_error_message).to eq("UPRN must be 12 digits or less.") expect(question.unanswered_error_message).to eq(I18n.t("validations.lettings.property.uprn.invalid"))
end end
describe "get_extra_check_answer_value" do describe "get_extra_check_answer_value" do

12
spec/models/lettings_log_spec.rb

@ -194,10 +194,6 @@ RSpec.describe LettingsLog do
expect(validator).to receive(:validate_tshortfall) expect(validator).to receive(:validate_tshortfall)
end end
it "validates let type" do
expect(validator).to receive(:validate_unitletas)
end
it "validates reason for vacancy" do it "validates reason for vacancy" do
expect(validator).to receive(:validate_rsnvac) expect(validator).to receive(:validate_rsnvac)
end end
@ -1010,8 +1006,8 @@ RSpec.describe LettingsLog do
end end
it "does not impact other validations" do it "does not impact other validations" do
expect { lettings_log.update!(startdate: Time.zone.yesterday, first_time_property_let_as_social_housing: 0, rsnvac: 16) } expect { lettings_log.update!(startdate: Time.zone.yesterday, referral: 8, rsnvac: 9) }
.to raise_error(ActiveRecord::RecordInvalid, /Enter a reason for vacancy that is not 'first let' if unit has been previously let as social housing/) .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.lettings.property.rsnvac.referral_invalid")}/)
end end
end end
@ -1046,8 +1042,8 @@ RSpec.describe LettingsLog do
end end
it "does not impact other validations" do it "does not impact other validations" do
expect { lettings_log.update!(location:, scheme:, first_time_property_let_as_social_housing: 0, rsnvac: 16) } expect { lettings_log.update!(startdate: Time.zone.yesterday, referral: 8, rsnvac: 9) }
.to raise_error(ActiveRecord::RecordInvalid, /Enter a reason for vacancy that is not 'first let' if unit has been previously let as social housing/) .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.lettings.property.rsnvac.referral_invalid")}/)
end end
end end
end end

112
spec/models/validations/property_validations_spec.rb

@ -47,47 +47,10 @@ RSpec.describe Validations::PropertyValidations do
expect(log.errors).to be_empty expect(log.errors).to be_empty
end end
end end
context "and the log is from before 24/25" do
it "adds an error" do
allow(log.form).to receive(:start_year_2024_or_later?).and_return false
property_validator.validate_shared_housing_rooms(log)
expect(log.errors["unittype_gn"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit"))
expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit"))
end
end
end
context "when a bedsit has less than 1 bedroom" do
before do
log.beds = 0
log.unittype_gn = 2
end
context "and the log is for 24/25 or later" do
it "does not add an error" do
property_validator.validate_shared_housing_rooms(log)
expect(log.errors).to be_empty
end
end
context "and the log is from before 24/25" do
it "adds an error" do
allow(log.form).to receive(:start_year_2024_or_later?).and_return false
property_validator.validate_shared_housing_rooms(log)
expect(log.errors["unittype_gn"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit"))
expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_bedroom_bedsit"))
end
end
end end
context "when shared housing has more than 7 bedrooms" do context "when shared housing has more than 7 bedrooms" do
let(:expected_error) { I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") } let(:expected_error) { I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared") }
it "adds an error if the number of bedrooms is not between 1 and 7" do it "adds an error if the number of bedrooms is not between 1 and 7" do
log.beds = 8 log.beds = 8
@ -95,12 +58,12 @@ RSpec.describe Validations::PropertyValidations do
log.hhmemb = 3 log.hhmemb = 3
property_validator.validate_shared_housing_rooms(log) property_validator.validate_shared_housing_rooms(log)
expect(log.errors["unittype_gn"]).to include(match(expected_error)) expect(log.errors["unittype_gn"]).to include(match(expected_error))
expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared")) expect(log.errors["beds"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared"))
end end
end end
context "when shared housing has less than 1 bedrooms" do context "when shared housing has less than 1 bedrooms" do
let(:expected_error) { I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared") } let(:expected_error) { I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared") }
it "adds an error if the number of bedrooms is not between 1 and 7" do it "adds an error if the number of bedrooms is not between 1 and 7" do
log.beds = 0 log.beds = 0
@ -108,12 +71,12 @@ RSpec.describe Validations::PropertyValidations do
log.hhmemb = 3 log.hhmemb = 3
property_validator.validate_shared_housing_rooms(log) property_validator.validate_shared_housing_rooms(log)
expect(log.errors["unittype_gn"]).to include(match(expected_error)) expect(log.errors["unittype_gn"]).to include(match(expected_error))
expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_seven_bedroom_shared")) expect(log.errors["beds"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_seven_bedroom_shared"))
end end
end end
context "when there are too many bedrooms for the number of household members and unit type" do context "when there are too many bedrooms for the number of household members and unit type" do
let(:expected_error) { I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared") } let(:expected_error) { I18n.t("validations.lettings.property.unittype_gn.one_three_bedroom_single_tenant_shared") }
it "adds an error" do it "adds an error" do
log.beds = 4 log.beds = 4
@ -121,54 +84,13 @@ RSpec.describe Validations::PropertyValidations do
log.hhmemb = 1 log.hhmemb = 1
property_validator.validate_shared_housing_rooms(log) property_validator.validate_shared_housing_rooms(log)
expect(log.errors["unittype_gn"]).to include(match(expected_error)) expect(log.errors["unittype_gn"]).to include(match(expected_error))
expect(log.errors["beds"]).to include(I18n.t("validations.property.unittype_gn.one_three_bedroom_single_tenant_shared")) expect(log.errors["beds"]).to include(I18n.t("validations.lettings.property.unittype_gn.one_three_bedroom_single_tenant_shared"))
end
end
end
describe "#validate_unitletas" do
context "when the property has not been let before" do
it "validates that no previous let type is provided" do
log.first_time_property_let_as_social_housing = 1
log.unitletas = 0
property_validator.validate_unitletas(log)
expect(log.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social"))
log.unitletas = 1
property_validator.validate_unitletas(log)
expect(log.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social"))
log.unitletas = 2
property_validator.validate_unitletas(log)
expect(log.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social"))
log.unitletas = 3
property_validator.validate_unitletas(log)
expect(log.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social"))
end
end
context "when the property has been let previously" do
it "expects to have a previous let type" do
log.first_time_property_let_as_social_housing = 0
log.unitletas = 0
property_validator.validate_unitletas(log)
expect(log.errors["unitletas"]).to be_empty
end end
end end
end end
describe "validate_rsnvac" do describe "validate_rsnvac" do
context "when the property has not been let before" do context "when the property has not been let before" do
it "validates that it has a first let reason for vacancy" do
log.first_time_property_let_as_social_housing = 1
log.rsnvac = 6
property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_social"))
end
it "expects to have a first let reason for vacancy" do it "expects to have a first let reason for vacancy" do
log.first_time_property_let_as_social_housing = 1 log.first_time_property_let_as_social_housing = 1
log.rsnvac = 15 log.rsnvac = 15
@ -184,22 +106,6 @@ RSpec.describe Validations::PropertyValidations do
end end
context "when the property has been let as social housing before" do context "when the property has been let as social housing before" do
it "validates that the reason for vacancy is not a first let as social housing reason" do
log.first_time_property_let_as_social_housing = 0
log.rsnvac = 15
property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_not_social"))
log.rsnvac = 16
property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_not_social"))
log.rsnvac = 17
property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_not_social"))
end
it "expects the reason for vacancy to be a first let as social housing reason" do it "expects the reason for vacancy to be a first let as social housing reason" do
log.first_time_property_let_as_social_housing = 1 log.first_time_property_let_as_social_housing = 1
log.rsnvac = 15 log.rsnvac = 15
@ -220,7 +126,7 @@ RSpec.describe Validations::PropertyValidations do
log.rsnvac = 14 log.rsnvac = 14
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.not_a_renewal")) .to include(match I18n.t("validations.lettings.property.rsnvac.not_a_renewal"))
end end
end end
end end
@ -237,7 +143,7 @@ RSpec.describe Validations::PropertyValidations do
log.prevten = prevten log.prevten = prevten
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.non_temp_accommodation")) .to include(match I18n.t("validations.lettings.property.rsnvac.non_temp_accommodation"))
end end
end end
@ -247,7 +153,7 @@ RSpec.describe Validations::PropertyValidations do
log.referral = src log.referral = src
property_validator.validate_rsnvac(log) property_validator.validate_rsnvac(log)
expect(log.errors["rsnvac"]) expect(log.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.referral_invalid")) .to include(match I18n.t("validations.lettings.property.rsnvac.referral_invalid"))
end end
end end
end end

Loading…
Cancel
Save