Browse Source

CLDC-1698 Update tenancy length validations (#975)

pull/977/head v0.2.19
David May-Miller 2 years ago committed by GitHub
parent
commit
ff788afcb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      app/models/validations/tenancy_validations.rb
  2. 4
      config/locales/en.yml
  3. 48
      spec/models/validations/tenancy_validations_spec.rb

16
app/models/validations/tenancy_validations.rb

@ -5,7 +5,7 @@ module Validations::TenancyValidations
def validate_fixed_term_tenancy(record)
is_present = record.tenancylength.present?
is_in_range = record.tenancylength.to_i.between?(2, 99)
is_in_range = record.tenancylength.to_i.between?(min_tenancy_length(record), 99)
conditions = [
{
condition: !(record.is_secure_tenancy? || record.is_assured_shorthold_tenancy?) && is_present,
@ -13,11 +13,17 @@ module Validations::TenancyValidations
},
{
condition: (record.is_assured_shorthold_tenancy? && !is_in_range) && is_present,
error: I18n.t("validations.tenancy.length.shorthold"),
error: I18n.t(
"validations.tenancy.length.shorthold",
min_tenancy_length: min_tenancy_length(record),
),
},
{
condition: record.is_secure_tenancy? && (!is_in_range && is_present),
error: I18n.t("validations.tenancy.length.secure"),
error: I18n.t(
"validations.tenancy.length.secure",
min_tenancy_length: min_tenancy_length(record),
),
},
]
@ -41,4 +47,8 @@ module Validations::TenancyValidations
record.errors.add :hhmemb, I18n.t("validations.tenancy.joint_more_than_one_member")
end
end
def min_tenancy_length(record)
record.is_supported_housing? || record.renttype == 3 ? 1 : 2
end
end

4
config/locales/en.yml

@ -301,8 +301,8 @@ en:
tenancy:
length:
fixed_term_not_required: "You must only answer the length of the tenancy if it's fixed-term"
shorthold: "Enter a tenancy length between 2 and 99 years for a Fixed Term – Assured Shorthold Tenancy (AST)"
secure: "Enter a tenancy length between 2 and 99 years (or don't specify the length) for a Secure (including flexible) tenancy"
shorthold: "Enter a tenancy length between %{min_tenancy_length} and 99 years for a tenancy of this type"
secure: "Enter a tenancy length between %{min_tenancy_length} and 99 years (or don't specify the length) for a tenancy of this type"
internal_transfer: "Answer must be secure tenancy as this tenancy is an internal transfer"
cannot_be_internal_transfer: "Answer cannot be internal transfer as this is not a secure tenancy"
not_joint: "This cannot be a joint tenancy as you've told us there's only one person in the household"

48
spec/models/validations/tenancy_validations_spec.rb

@ -4,7 +4,7 @@ RSpec.describe Validations::TenancyValidations do
subject(:tenancy_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::TenancyValidations } }
let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2021, 5, 1)) }
let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2021, 5, 1), needstype: 1, rent_type: 1) }
describe "fixed term tenancy validations" do
context "when fixed term tenancy" do
@ -21,11 +21,16 @@ RSpec.describe Validations::TenancyValidations do
end
context "when type of tenancy is assured shorthold" do
let(:expected_error) { I18n.t("validations.tenancy.length.shorthold") }
let(:expected_error) do
I18n.t(
"validations.tenancy.length.shorthold",
min_tenancy_length: 2,
)
end
before { record.tenancy = 4 }
context "when tenancy length is greater than 1" do
context "when tenancy length is less than 2" do
it "adds an error" do
record.tenancylength = 1
tenancy_validator.validate_fixed_term_tenancy(record)
@ -34,7 +39,7 @@ RSpec.describe Validations::TenancyValidations do
end
end
context "when tenancy length is less than 100" do
context "when tenancy length is greater than 99" do
it "adds an error" do
record.tenancylength = 100
tenancy_validator.validate_fixed_term_tenancy(record)
@ -64,11 +69,16 @@ RSpec.describe Validations::TenancyValidations do
context "when the collection start year is before 2022" do
context "when type of tenancy is secure" do
let(:expected_error) { I18n.t("validations.tenancy.length.secure") }
let(:expected_error) do
I18n.t(
"validations.tenancy.length.secure",
min_tenancy_length: 2,
)
end
before { record.tenancy = 1 }
context "when tenancy length is greater than 1" do
context "when tenancy length is less than 2" do
it "adds an error" do
record.tenancylength = 1
tenancy_validator.validate_fixed_term_tenancy(record)
@ -77,7 +87,7 @@ RSpec.describe Validations::TenancyValidations do
end
end
context "when tenancy length is less than 100" do
context "when tenancy length is greater than 99" do
it "adds an error" do
record.tenancylength = 100
tenancy_validator.validate_fixed_term_tenancy(record)
@ -107,14 +117,19 @@ RSpec.describe Validations::TenancyValidations do
end
context "when the collection start year is 2022 or later" do
let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 5, 1)) }
let(:record) { FactoryBot.create(:lettings_log, startdate: Time.zone.local(2022, 5, 1), needstype: 1, rent_type: 1) }
context "when type of tenancy is Secure - fixed term" do
let(:expected_error) { I18n.t("validations.tenancy.length.secure") }
let(:expected_error) do
I18n.t(
"validations.tenancy.length.secure",
min_tenancy_length: 2,
)
end
before { record.tenancy = 6 }
context "when tenancy length is greater than 1" do
context "when tenancy length is less than 2" do
it "adds an error" do
record.tenancylength = 1
tenancy_validator.validate_fixed_term_tenancy(record)
@ -123,7 +138,7 @@ RSpec.describe Validations::TenancyValidations do
end
end
context "when tenancy length is less than 100" do
context "when tenancy length is greater than 99" do
it "adds an error" do
record.tenancylength = 100
tenancy_validator.validate_fixed_term_tenancy(record)
@ -152,11 +167,16 @@ RSpec.describe Validations::TenancyValidations do
end
context "when type of tenancy is Secure - lifetime" do
let(:expected_error) { I18n.t("validations.tenancy.length.secure") }
let(:expected_error) do
I18n.t(
"validations.tenancy.length.secure",
min_tenancy_length: 2,
)
end
before { record.tenancy = 7 }
context "when tenancy length is greater than 1" do
context "when tenancy length is less than 2" do
it "adds an error" do
record.tenancylength = 1
tenancy_validator.validate_fixed_term_tenancy(record)
@ -165,7 +185,7 @@ RSpec.describe Validations::TenancyValidations do
end
end
context "when tenancy length is less than 100" do
context "when tenancy length is greater than 99" do
it "adds an error" do
record.tenancylength = 100
tenancy_validator.validate_fixed_term_tenancy(record)

Loading…
Cancel
Save