From 22a3087c77d700cc98df4cccecb5840e727387e8 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 25 Oct 2021 12:03:23 +0100 Subject: [PATCH] Add fixed term tenancy validation --- app/models/case_log.rb | 14 ++++++++ spec/fixtures/complete_case_log.json | 2 +- spec/models/case_log_spec.rb | 51 ++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 99285ca3f..3de903caf 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -74,6 +74,20 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_fixed_term_tenancy(record) + if !(record.tenancy_type == "Fixed term – Secure" || record.tenancy_type == "Fixed term – Assured Shorthold Tenancy (AST)") && record.fixed_term_tenancy.present? + record.errors.add :fixed_term_tenancy, "You must only answer the fixed term tenancy length question if the tenancy type is fixed term" + end + + if record.tenancy_type == "Fixed term – Assured Shorthold Tenancy (AST)" && !record.fixed_term_tenancy.to_i.between?(2, 99) + record.errors.add :fixed_term_tenancy, "Fixed term – Assured Shorthold Tenancy (AST) should be between 2 and 99 years" + end + + if record.tenancy_type == "Fixed term – Secure" && (!record.fixed_term_tenancy.to_i.between?(2, 99) && record.fixed_term_tenancy.present?) + record.errors.add :fixed_term_tenancy, "Fixed term – Secure should be between 2 and 99 years or not specified" + end + end + def validate(record) # 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 diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index 541c63970..9238bba6a 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -51,7 +51,7 @@ "tenancy_code": "BZ757", "tenancy_start_date": "12/03/2019", "starter_tenancy": "No", - "fixed_term_tenancy": "No", + "fixed_term_tenancy": "5", "tenancy_type": "Fixed term – Secure", "letting_type": "Affordable Rent - General Needs", "letting_provider": "This landlord", diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index ae85b2fd1..e8241bd14 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -82,14 +82,14 @@ RSpec.describe Form, type: :model do end 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 { CaseLog.create!(armed_forces: "Yes - a regular", armed_forces_injured: nil) }.to raise_error(ActiveRecord::RecordInvalid) 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 { CaseLog.create!(armed_forces: "No", armed_forces_injured: "Yes") @@ -116,6 +116,53 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) 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 describe "status" do