Browse Source

CLDC-466: Add fixed term tenancy validation (#66)

* Add fixed term tenancy validation

* refactor validate_fixed_term_tenancy

* add fixed_term_tenancy pattern to api docs and make it dynamically not required
pull/67/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
c39271d486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      app/models/case_log.rb
  2. 3
      docs/api/DLUHC-CORE-Data.v1.json
  3. 2
      spec/fixtures/complete_case_log.json
  4. 51
      spec/models/case_log_spec.rb

18
app/models/case_log.rb

@ -74,6 +74,20 @@ class CaseLogValidator < ActiveModel::Validator
end end
end end
def validate_fixed_term_tenancy(record)
is_present = record.fixed_term_tenancy.present?
is_in_range = record.fixed_term_tenancy.to_i.between?(2, 99)
is_secure = record.tenancy_type == "Fixed term – Secure"
is_ast = record.tenancy_type == "Fixed term – Assured Shorthold Tenancy (AST)"
conditions = [
{ condition: !(is_secure || is_ast) && is_present, error: "You must only answer the fixed term tenancy length question if the tenancy type is fixed term" },
{ condition: is_ast && !is_in_range, error: "Fixed term – Assured Shorthold Tenancy (AST) should be between 2 and 99 years" },
{ condition: is_secure && (!is_in_range && is_present), error: "Fixed term – Secure should be between 2 and 99 years or not specified" },
]
conditions.each { |condition| condition[:condition] ? (record.errors.add :fixed_term_tenancy, condition[:error]) : nil }
end
def validate(record) def validate(record)
# If we've come from the form UI we only want to validate the specific fields # If we've come from the form UI we only want to validate the specific fields
# that have just been submitted. If we're submitting a log via API or Bulk Upload # that have just been submitted. If we're submitting a log via API or Bulk Upload
@ -172,6 +186,10 @@ private
dynamically_not_required << "net_income_frequency" dynamically_not_required << "net_income_frequency"
end end
if tenancy_type == "Fixed term – Secure"
dynamically_not_required << "fixed_term_tenancy"
end
required.delete_if { |key, _value| dynamically_not_required.include?(key) } required.delete_if { |key, _value| dynamically_not_required.include?(key) }
end end
end end

3
docs/api/DLUHC-CORE-Data.v1.json

@ -873,7 +873,8 @@
}, },
"fixed_term_tenancy": { "fixed_term_tenancy": {
"type": "string", "type": "string",
"minLength": 1 "minLength": 1,
"pattern": "((?!1|0)([0-9][0-9]))+"
}, },
"tenancy_type": { "tenancy_type": {
"type": "string", "type": "string",

2
spec/fixtures/complete_case_log.json vendored

@ -51,7 +51,7 @@
"tenancy_code": "BZ757", "tenancy_code": "BZ757",
"tenancy_start_date": "12/03/2019", "tenancy_start_date": "12/03/2019",
"starter_tenancy": "No", "starter_tenancy": "No",
"fixed_term_tenancy": "No", "fixed_term_tenancy": "5",
"tenancy_type": "Fixed term – Secure", "tenancy_type": "Fixed term – Secure",
"letting_type": "Affordable Rent - General Needs", "letting_type": "Affordable Rent - General Needs",
"letting_provider": "This landlord", "letting_provider": "This landlord",

51
spec/models/case_log_spec.rb

@ -82,14 +82,14 @@ RSpec.describe Form, type: :model do
end end
context "armed forces injured validation" do context "armed forces injured validation" do
it "must be anwered if tenant was a regular or reserve in armed forces" do it "must be answered if tenant was a regular or reserve in armed forces" do
expect { expect {
CaseLog.create!(armed_forces: "Yes - a regular", CaseLog.create!(armed_forces: "Yes - a regular",
armed_forces_injured: nil) armed_forces_injured: nil)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "must be anwered if tenant was not a regular or reserve in armed forces" do it "must be answered if tenant was not a regular or reserve in armed forces" do
expect { expect {
CaseLog.create!(armed_forces: "No", CaseLog.create!(armed_forces: "No",
armed_forces_injured: "Yes") armed_forces_injured: "Yes")
@ -116,6 +116,53 @@ RSpec.describe Form, type: :model do
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
context "fixed term tenancy length" do
it "Must not be completed if Type of main tenancy is not responded with either Secure or Assured shorthold " do
expect {
CaseLog.create!(tenancy_type: "Other",
fixed_term_tenancy: 10)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Must be completed and between 2 and 99 if type of tenancy is Assured shorthold" do
expect {
CaseLog.create!(tenancy_type: "Fixed term – Assured Shorthold Tenancy (AST)",
fixed_term_tenancy: 1)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Assured Shorthold Tenancy (AST)",
fixed_term_tenancy: nil)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Assured Shorthold Tenancy (AST)",
fixed_term_tenancy: 2)
}.not_to raise_error
end
it "Must be empty or between 2 and 99 if type of tenancy is Secure" do
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: 1)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: 100)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: nil)
}.not_to raise_error
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: 2)
}.not_to raise_error
end
end
end end
describe "status" do describe "status" do

Loading…
Cancel
Save