Browse Source

Refactor validation specs (#304)

* Refactor other household member validation specs

* Remove dupes

* Tenancy type validation

* LA validations

* Benefits proportion validations

* Outstanding rent and shortfall validations

* Typo

* Net income hard validations

* First let vacancy reason validations
pull/306/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
9d77a93859
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      app/models/validations/financial_validations.rb
  2. 2
      app/models/validations/household_validations.rb
  3. 4
      config/locales/en.yml
  4. 323
      spec/models/case_log_spec.rb
  5. 135
      spec/models/validations/financial_validations_spec.rb
  6. 132
      spec/models/validations/household_validations_spec.rb
  7. 109
      spec/models/validations/property_validations_spec.rb
  8. 43
      spec/models/validations/tenancy_validations_spec.rb

4
app/models/validations/financial_validations.rb

@ -23,11 +23,11 @@ module Validations::FinancialValidations
def validate_net_income(record) def validate_net_income(record)
if record.ecstat1 && record.weekly_net_income if record.ecstat1 && record.weekly_net_income
if record.weekly_net_income > record.applicable_income_range.hard_max if record.weekly_net_income > record.applicable_income_range.hard_max
record.errors.add :earnings, I18n.t("validations.financial.earnings.under_hard_max", hard_max: record.applicable_income_range.hard_max) record.errors.add :earnings, I18n.t("validations.financial.earnings.over_hard_max", hard_max: record.applicable_income_range.hard_max)
end end
if record.weekly_net_income < record.applicable_income_range.hard_min if record.weekly_net_income < record.applicable_income_range.hard_min
record.errors.add :earnings, I18n.t("validations.financial.earnings.over_hard_min", hard_min: record.applicable_income_range.hard_min) record.errors.add :earnings, I18n.t("validations.financial.earnings.under_hard_min", hard_min: record.applicable_income_range.hard_min)
end end
end end

2
app/models/validations/household_validations.rb

@ -127,7 +127,7 @@ private
relationship = record.public_send("relat#{person_num}") relationship = record.public_send("relat#{person_num}")
return unless age && economic_status && relationship return unless age && economic_status && relationship
if age >= 16 && age <= 19 && relationship == "Child - includes young adult and grown-up" && (economic_status != "Full-time student" || economic_status != "Prefer not to say") if age >= 16 && age <= 19 && relationship == "Child - includes young adult and grown-up" && (economic_status != "Full-time student" && economic_status != "Prefer not to say")
record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19", person_num:) record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19", person_num:)
end end
end end

4
config/locales/en.yml

@ -74,8 +74,8 @@ en:
benefits: benefits:
part_or_full_time: "income is from Universal Credit, state pensions or benefits cannot be All if the tenant or the partner works part or full time" part_or_full_time: "income is from Universal Credit, state pensions or benefits cannot be All if the tenant or the partner works part or full time"
earnings: earnings:
under_hard_max: "Net income cannot be greater than %{hard_max} given the tenant’s working situation" over_hard_max: "Net income cannot be greater than %{hard_max} given the tenant’s working situation"
over_hard_min: "Net income cannot be less than %{hard_min} given the tenant’s working situation" under_hard_min: "Net income cannot be less than %{hard_min} given the tenant’s working situation"
freq_missing: "Select how often the household receives income" freq_missing: "Select how often the household receives income"
earnings_missing: "Enter how much income the household has in total" earnings_missing: "Enter how much income the household has in total"

323
spec/models/case_log_spec.rb

@ -44,285 +44,7 @@ RSpec.describe CaseLog do
end end
# TODO: replace these with validator specs and checks for method call here # TODO: replace these with validator specs and checks for method call here
context "when validating property vacancy and let as" do
it "cannot have a previously let as type, if it hasn't been let before" do
expect {
described_class.create!(
first_time_property_let_as_social_housing: "No",
unitletas: "Social rent basis",
owning_organisation:,
managing_organisation:,
)
}.not_to raise_error
expect {
described_class.create!(
first_time_property_let_as_social_housing: "Yes",
unitletas: "Social rent basis",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(
first_time_property_let_as_social_housing: "Yes",
unitletas: "Affordable rent basis",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(
first_time_property_let_as_social_housing: "Yes",
unitletas: "Intermediate rent basis",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(
first_time_property_let_as_social_housing: "Yes",
unitletas: "Don’t know",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "must have a first let reason for vacancy if it's being let as social housing for the first time" do
expect {
described_class.create!(
first_time_property_let_as_social_housing: "Yes",
rsnvac: "First let of new-build property",
owning_organisation:,
managing_organisation:,
)
}.not_to raise_error
expect {
described_class.create!(
first_time_property_let_as_social_housing: "Yes",
rsnvac: "First let of conversion, rehabilitation or acquired property",
owning_organisation:,
managing_organisation:,
)
}.not_to raise_error
expect {
described_class.create!(
first_time_property_let_as_social_housing: "Yes",
rsnvac: "First let of leased property",
owning_organisation:,
managing_organisation:,
)
}.not_to raise_error
expect {
described_class.create!(
first_time_property_let_as_social_housing: "Yes",
rsnvac: "Tenant moved to care home",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "when validating outstanding rent or charges" do
it "must be not be anwered if answered no to outstanding rent or charges" do
expect {
described_class.create!(hbrentshortfall: "No",
tshortfall: 99,
owning_organisation:,
managing_organisation:)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "with tenant’s income from Universal Credit, state pensions or benefits" do
it "Cannot be All if person 1 works full time" do
expect {
described_class.create!(
benefits: "All",
ecstat1: "Full-time - 30 hours or more",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot be All if person 1 works part time" do
expect {
described_class.create!(
benefits: "All",
ecstat1: "Part-time - Less than 30 hours",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot be 1 All if any of persons 2-4 are person 1's partner and work part or full time" do
expect {
described_class.create!(
benefits: "All",
relat2: "Partner",
ecstat2: "Part-time - Less than 30 hours",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "when validating household members" do
it "validate that persons aged under 16 must have relationship Child" do
expect {
described_class.create!(
age2: 14,
relat2: "Partner",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "validate that persons aged over 70 must be retired" do
expect {
described_class.create!(
age2: 71,
ecstat2: "Full-time - 30 hours or more",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "validate that a male, retired persons must be over 65" do
expect {
described_class.create!(
age2: 64,
sex2: "Male",
ecstat2: "Retired",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "validate that a female, retired persons must be over 60" do
expect {
described_class.create!(
age2: 59,
sex2: "Female",
ecstat2: "Retired",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "validate that persons aged under 16 must be a child (economically speaking)" do
expect {
described_class.create!(
age2: 15,
ecstat2: "Full-time - 30 hours or more",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "validate that persons aged between 16 and 19 that are a child must be a full time student or economic status refused" do
expect {
described_class.create!(
age2: 17,
relat2: "Child - includes young adult and grown-up",
ecstat2: "Full-time - 30 hours or more",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "validate that persons aged under 16 must be a child relationship" do
expect {
described_class.create!(
age2: 15,
relat2: "Partner",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "validate that no more than 1 partner relationship exists" do
expect {
described_class.create!(
relat2: "Partner",
relat3: "Partner",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "when validating other tenancy type" do
it "must be provided if tenancy type was given as other" do
expect {
described_class.create!(tenancy: "Other",
tenancyother: nil,
owning_organisation:,
managing_organisation:)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(tenancy: "Other",
tenancyother: "type",
owning_organisation:,
managing_organisation:)
}.not_to raise_error
end
it "must not be provided if tenancy type is not other" do
expect {
described_class.create!(tenancy: "Secure (including flexible)",
tenancyother: "the other reason provided",
owning_organisation:,
managing_organisation:)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(tenancy: "Secure (including flexible)",
tenancyother: nil,
owning_organisation:,
managing_organisation:)
}.not_to raise_error
end
end
context "when saving income ranges" do context "when saving income ranges" do
it "validates net income maximum" do
expect {
described_class.create!(
ecstat1: "Full-time - 30 hours or more",
earnings: 5000,
incfreq: "Weekly",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "validates net income minimum" do
expect {
described_class.create!(
ecstat1: "Full-time - 30 hours or more",
earnings: 1,
incfreq: "Weekly",
owning_organisation:,
managing_organisation:,
)
}.to raise_error(ActiveRecord::RecordInvalid)
end
context "with an income in upper soft range" do context "with an income in upper soft range" do
let(:case_log) do let(:case_log) do
FactoryBot.create(:case_log, FactoryBot.create(:case_log,
@ -354,24 +76,6 @@ RSpec.describe CaseLog do
end end
end end
context "when validating local authority" do
it "Has to be london if rent type london affordable rent" do
expect {
described_class.create!(la: "Ashford",
rent_type: "London Affordable rent",
owning_organisation:,
managing_organisation:)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
described_class.create!(la: "Westminster",
rent_type: "London Affordable rent",
owning_organisation:,
managing_organisation:)
}.not_to raise_error
end
end
context "with accessibility requirements" do context "with accessibility requirements" do
it "validates that only one option can be selected" do it "validates that only one option can be selected" do
expect { expect {
@ -491,8 +195,9 @@ RSpec.describe CaseLog do
expect(validator).to receive(:validate_property_number_of_times_relet) expect(validator).to receive(:validate_property_number_of_times_relet)
end end
it "validates tenancy length for tenancy type" do it "validates tenancy type" do
expect(validator).to receive(:validate_fixed_term_tenancy) expect(validator).to receive(:validate_fixed_term_tenancy)
expect(validator).to receive(:validate_other_tenancy_type)
end end
it "validates the previous postcode" do it "validates the previous postcode" do
@ -522,6 +227,30 @@ RSpec.describe CaseLog do
it "validates property void date" do it "validates property void date" do
expect(validator).to receive(:validate_property_void_date) expect(validator).to receive(:validate_property_void_date)
end end
it "validates local authority" do
expect(validator).to receive(:validate_la)
end
it "validates benefits as proportion of income" do
expect(validator).to receive(:validate_net_income_uc_proportion)
end
it "validates outstanding rent amount" do
expect(validator).to receive(:validate_outstanding_rent_amount)
end
it "validates housing benefit rent shortfall" do
expect(validator).to receive(:validate_tshortfall)
end
it "validates let type" do
expect(validator).to receive(:validate_unitletas)
end
it "validates reason for vacancy" do
expect(validator).to receive(:validate_rsnvac)
end
end end
describe "status" do describe "status" do

135
spec/models/validations/financial_validations_spec.rb

@ -11,14 +11,145 @@ RSpec.describe Validations::FinancialValidations do
record.earnings = 500 record.earnings = 500
record.incfreq = nil record.incfreq = nil
financial_validator.validate_net_income(record) financial_validator.validate_net_income(record)
expect(record.errors["incfreq"]).to include(match I18n.t("validations.financial.earnings.freq_missing")) expect(record.errors["incfreq"])
.to include(match I18n.t("validations.financial.earnings.freq_missing"))
end end
it "when income frequency is provided it validates that earnings must be provided" do it "when income frequency is provided it validates that earnings must be provided" do
record.earnings = nil record.earnings = nil
record.incfreq = "Weekly" record.incfreq = "Weekly"
financial_validator.validate_net_income(record) financial_validator.validate_net_income(record)
expect(record.errors["earnings"]).to include(match I18n.t("validations.financial.earnings.earnings_missing")) expect(record.errors["earnings"])
.to include(match I18n.t("validations.financial.earnings.earnings_missing"))
end
end
describe "benefits proportion validations" do
context "when the proportion is all" do
it "validates that the lead tenant is not in full time employment" do
record.benefits = "All"
record.ecstat1 = "Full-time - 30 hours or more"
financial_validator.validate_net_income_uc_proportion(record)
expect(record.errors["benefits"]).to include(match I18n.t("validations.financial.benefits.part_or_full_time"))
end
it "validates that the lead tenant is not in part time employment" do
record.benefits = "All"
record.ecstat1 = "Part-time - Less than 30 hours"
financial_validator.validate_net_income_uc_proportion(record)
expect(record.errors["benefits"]).to include(match I18n.t("validations.financial.benefits.part_or_full_time"))
end
it "expects that the lead tenant is not in full-time or part-time employment" do
record.benefits = "All"
record.ecstat1 = "Retired"
financial_validator.validate_net_income_uc_proportion(record)
expect(record.errors["benefits"]).to be_empty
end
it "validates that the tenant's partner is not in full time employment" do
record.benefits = "All"
record.ecstat2 = "Part-time - Less than 30 hours"
record.relat2 = "Partner"
financial_validator.validate_net_income_uc_proportion(record)
expect(record.errors["benefits"]).to include(match I18n.t("validations.financial.benefits.part_or_full_time"))
end
it "expects that the tenant's partner is not in full-time or part-time employment" do
record.benefits = "All"
record.ecstat2 = "Retired"
record.relat2 = "Partner"
financial_validator.validate_net_income_uc_proportion(record)
expect(record.errors["benefits"]).to be_empty
end
end
end
describe "outstanding rent amount validations" do
context "when outstanding rent or charges is no" do
it "validates that no shortfall is provided" do
record.hbrentshortfall = "No"
record.tshortfall = 99
financial_validator.validate_outstanding_rent_amount(record)
expect(record.errors["tshortfall"])
.to include(match I18n.t("validations.financial.tshortfall.outstanding_amount_not_required"))
end
end
context "when outstanding rent or charges is yes" do
it "expects that a shortfall is provided" do
record.hbrentshortfall = "Yes"
record.tshortfall = 99
financial_validator.validate_outstanding_rent_amount(record)
expect(record.errors["tshortfall"]).to be_empty
end
end
end
describe "housing benefit rent shortfall validations" do
context "when shortfall is yes" do
it "validates that housing benefit is not none" do
record.hbrentshortfall = "Yes"
record.hb = "None"
financial_validator.validate_tshortfall(record)
expect(record.errors["tshortfall"])
.to include(match I18n.t("validations.financial.hbrentshortfall.outstanding_no_benefits"))
end
it "validates that housing benefit is not don't know" do
record.hbrentshortfall = "Yes"
record.hb = "Don’t know"
financial_validator.validate_tshortfall(record)
expect(record.errors["tshortfall"])
.to include(match I18n.t("validations.financial.hbrentshortfall.outstanding_no_benefits"))
end
it "validates that housing benefit is not Universal Credit without housing benefit" do
record.hbrentshortfall = "Yes"
record.hb = "Universal Credit (without housing element)"
financial_validator.validate_tshortfall(record)
expect(record.errors["tshortfall"])
.to include(match I18n.t("validations.financial.hbrentshortfall.outstanding_no_benefits"))
end
it "validates that housing benefit is provided" do
record.hbrentshortfall = "Yes"
record.hb = "Housing benefit"
financial_validator.validate_tshortfall(record)
expect(record.errors["tshortfall"]).to be_empty
end
end
end
describe "Net income validations" do
it "validates that the net income is within the expected range for the tenant's employment status" do
record.earnings = 200
record.incfreq = "Weekly"
record.ecstat1 = "Full-time - 30 hours or more"
financial_validator.validate_net_income(record)
expect(record.errors["earnings"]).to be_empty
end
context "when the net income is higher than the hard max for their employment status" do
it "adds an error" do
record.earnings = 5000
record.incfreq = "Weekly"
record.ecstat1 = "Full-time - 30 hours or more"
financial_validator.validate_net_income(record)
expect(record.errors["earnings"])
.to include(match I18n.t("validations.financial.earnings.over_hard_max", hard_max: 1230))
end
end
context "when the net income is lower than the hard min for their employment status" do
it "adds an error" do
record.earnings = 50
record.incfreq = "Weekly"
record.ecstat1 = "Full-time - 30 hours or more"
financial_validator.validate_net_income(record)
expect(record.errors["earnings"])
.to include(match I18n.t("validations.financial.earnings.under_hard_min", hard_min: 90))
end
end end
end end
end end

132
spec/models/validations/household_validations_spec.rb

@ -305,4 +305,136 @@ RSpec.describe Validations::HouseholdValidations do
end end
end end
end end
describe "household member validations" do
it "validates that only 1 partner exists" do
record.relat2 = "Partner"
record.relat3 = "Partner"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["base"])
.to include(match I18n.t("validations.household.relat.one_partner"))
end
it "expects that a tenant can have a partner" do
record.relat3 = "Partner"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["base"]).to be_empty
end
context "when the household contains a person under 16" do
it "validates that person must be a child of the tenant" do
record.age2 = "14"
record.relat2 = "Partner"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["relat2"])
.to include(match I18n.t("validations.household.relat.child_under_16", person_num: 2))
end
it "expects that person is a child of the tenant" do
record.age2 = "14"
record.relat2 = "Child - includes young adult and grown-up"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["relat2"]).to be_empty
end
it "validates that person's economic status must be Child" do
record.age2 = "14"
record.ecstat2 = "Full-time - 30 hours or more"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.household.ecstat.child_under_16", person_num: 2))
end
it "expects that person's economic status is Child" do
record.age2 = "14"
record.ecstat2 = "Child under 16"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["ecstat2"]).to be_empty
end
end
context "when the household contains a tenant's child between the ages of 16 and 19" do
it "validates that person's economic status must be full time student or refused" do
record.age2 = "17"
record.relat2 = "Child - includes young adult and grown-up"
record.ecstat2 = "Full-time - 30 hours or more"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.household.ecstat.student_16_19", person_num: 2))
end
it "expects that person can be a full time student" do
record.age2 = "17"
record.relat2 = "Child - includes young adult and grown-up"
record.ecstat2 = "Full-time student"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["ecstat2"]).to be_empty
end
it "expects that person can refuse to share their work status" do
record.age2 = "17"
record.relat2 = "Child - includes young adult and grown-up"
record.ecstat2 = "Prefer not to say"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["ecstat2"]).to be_empty
end
end
context "when the household contains a person over 70" do
it "validates that person must be retired" do
record.age2 = "71"
record.ecstat2 = "Full-time - 30 hours or more"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["ecstat2"])
.to include(match I18n.t("validations.household.ecstat.retired_over_70", person_num: 2))
end
it "expects that person is retired" do
record.age2 = "50"
record.ecstat2 = "Full-time - 30 hours or more"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["ecstat2"]).to be_empty
end
end
context "when the household contains a retired male" do
it "validates that person must be over 65" do
record.age2 = "64"
record.sex2 = "Male"
record.ecstat2 = "Retired"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["age2"])
.to include(match I18n.t("validations.household.age.retired_male"))
end
it "expects that person is over 65" do
record.age2 = "66"
record.sex2 = "Male"
record.ecstat2 = "Retired"
household_validator.validate_household_number_of_other_members(record)
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["ecstat2"]).to be_empty
end
end
context "when the household contains a retired female" do
it "validates that person must be over 60" do
record.age2 = "59"
record.sex2 = "Female"
record.ecstat2 = "Retired"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["age2"])
.to include(match I18n.t("validations.household.age.retired_female"))
end
it "expects that person is over 60" do
record.age2 = "61"
record.sex2 = "Female"
record.ecstat2 = "Retired"
household_validator.validate_household_number_of_other_members(record)
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["ecstat2"]).to be_empty
end
end
end
end end

109
spec/models/validations/property_validations_spec.rb

@ -125,4 +125,113 @@ RSpec.describe Validations::PropertyValidations do
end end
end end
end end
describe "#validate_la" do
context "when the rent type is London affordable" do
let(:expected_error) { I18n.t("validations.property.la.london_rent") }
it "validates that the local authority is in London" do
record.la = "Ashford"
record.rent_type = "London Affordable rent"
property_validator.validate_la(record)
expect(record.errors["la"]).to include(match(expected_error))
end
it "expects that the local authority is in London" do
record.la = "Westminster"
record.rent_type = "London Affordable rent"
property_validator.validate_la(record)
expect(record.errors["la"]).to be_empty
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
record.first_time_property_let_as_social_housing = "Yes"
record.unitletas = "Social rent basis"
property_validator.validate_unitletas(record)
expect(record.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social"))
record.unitletas = "Affordable rent basis"
property_validator.validate_unitletas(record)
expect(record.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social"))
record.unitletas = "Intermediate rent basis"
property_validator.validate_unitletas(record)
expect(record.errors["unitletas"])
.to include(match I18n.t("validations.property.rsnvac.previous_let_social"))
record.unitletas = "Don’t know"
property_validator.validate_unitletas(record)
expect(record.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
record.first_time_property_let_as_social_housing = "No"
record.unitletas = "Social rent basis"
property_validator.validate_unitletas(record)
expect(record.errors["unitletas"]).to be_empty
end
end
end
describe "validate_rsnvac" do
context "when the property has not been let before" do
it "validates that it has a first let reason for vacancy" do
record.first_time_property_let_as_social_housing = "Yes"
record.rsnvac = "Tenant moved to care home"
property_validator.validate_rsnvac(record)
expect(record.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
record.first_time_property_let_as_social_housing = "Yes"
record.rsnvac = "First let of new-build property"
property_validator.validate_rsnvac(record)
expect(record.errors["rsnvac"]).to be_empty
record.rsnvac = "First let of conversion, rehabilitation or acquired property"
property_validator.validate_rsnvac(record)
expect(record.errors["rsnvac"]).to be_empty
record.rsnvac = "First let of leased property"
property_validator.validate_rsnvac(record)
expect(record.errors["rsnvac"]).to be_empty
end
end
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
record.first_time_property_let_as_social_housing = "No"
record.rsnvac = "First let of new-build property"
property_validator.validate_rsnvac(record)
expect(record.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_not_social"))
record.rsnvac = "First let of conversion, rehabilitation or acquired property"
property_validator.validate_rsnvac(record)
expect(record.errors["rsnvac"])
.to include(match I18n.t("validations.property.rsnvac.first_let_not_social"))
record.rsnvac = "First let of leased property"
property_validator.validate_rsnvac(record)
expect(record.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
record.first_time_property_let_as_social_housing = "Yes"
record.rsnvac = "First let of new-build property"
property_validator.validate_rsnvac(record)
expect(record.errors["rsnvac"]).to be_empty
record.rsnvac = "First let of conversion, rehabilitation or acquired property"
property_validator.validate_rsnvac(record)
expect(record.errors["rsnvac"]).to be_empty
record.rsnvac = "First let of leased property"
property_validator.validate_rsnvac(record)
expect(record.errors["rsnvac"]).to be_empty
end
end
end
end end

43
spec/models/validations/tenancy_validations_spec.rb

@ -6,7 +6,7 @@ RSpec.describe Validations::TenancyValidations do
let(:validator_class) { Class.new { include Validations::TenancyValidations } } let(:validator_class) { Class.new { include Validations::TenancyValidations } }
let(:record) { FactoryBot.create(:case_log) } let(:record) { FactoryBot.create(:case_log) }
describe "#validate_fixed_term_tenancy" do describe "fixed term tenancy validations" do
context "when fixed term tenancy" do context "when fixed term tenancy" do
context "when type of tenancy is not assured or assured shorthold" do context "when type of tenancy is not assured or assured shorthold" do
let(:expected_error) { I18n.t("validations.tenancy.length.fixed_term_not_required") } let(:expected_error) { I18n.t("validations.tenancy.length.fixed_term_not_required") }
@ -109,4 +109,45 @@ RSpec.describe Validations::TenancyValidations do
end end
end end
end end
describe "tenancy type validations" do
let(:field) { "validations.other_field_missing" }
let(:main_field_label) { "tenancy" }
let(:other_field_label) { "tenancyother" }
let(:expected_error) { I18n.t(field, main_field_label:, other_field_label:) }
context "when tenancy type is other" do
it "validates that other tenancy type is provided" do
record.tenancy = "Other"
record.tenancyother = nil
tenancy_validator.validate_other_tenancy_type(record)
expect(record.errors[other_field_label]).to include(match(expected_error))
end
it "expects that other tenancy type is provided" do
record.tenancy = "Other"
record.tenancyother = "Some other tenancy type"
tenancy_validator.validate_other_tenancy_type(record)
expect(record.errors[other_field_label]).to be_empty
end
end
context "when tenancy type is not other" do
let(:field) { "validations.other_field_not_required" }
it "validates that other tenancy type is not provided" do
record.tenancy = "Assured"
record.tenancyother = "Some other tenancy type"
tenancy_validator.validate_other_tenancy_type(record)
expect(record.errors[other_field_label]).to include(match(expected_error))
end
it "expects that other tenancy type is not provided" do
record.tenancy = "Secure (including flexible)"
record.tenancyother = nil
tenancy_validator.validate_other_tenancy_type(record)
expect(record.errors[other_field_label]).to be_empty
end
end
end
end end

Loading…
Cancel
Save