From 4520b3a1ccebd761e92c0e56b1dcf7b61ea4be7f Mon Sep 17 00:00:00 2001 From: magicmilo Date: Tue, 26 Oct 2021 16:03:48 +0100 Subject: [PATCH 1/8] added armed forces active response validation --- app/models/case_log.rb | 11 ++++++++++ spec/helpers/check_answers_helper_spec.rb | 6 ++++-- spec/models/case_log_spec.rb | 25 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index aad4d23b0..f56f20e2f 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -55,6 +55,17 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_armed_forces_active_response(record) + # binding.pry + if record.armed_forces == "Yes - a regular" && record.armed_forces_active.blank? + record.errors.add :armed_forces_active, "You must answer the armed forces active question if the tenant has served as a regular in the armed forces" + end + + if record.armed_forces != "Yes - a regular" && !record.armed_forces_active.blank? + record.errors.add :armed_forces_active, "You must not answer the armed forces active question if the tenant has not served as a regular in the armed forces" + 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/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index cc619a041..e53c54957 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -11,7 +11,9 @@ RSpec.describe CheckAnswersHelper do ) end let(:case_log_with_met_radio_condition) do - FactoryBot.create(:case_log, armed_forces: "Yes - a regular", armed_forces_injured: "No") + FactoryBot.create(:case_log, armed_forces: "Yes - a regular", + armed_forces_injured: "No", + armed_forces_active: "Yes") end let(:subsection) { "income_and_benefits" } let(:subsection_with_numeric_conditionals) { "household_characteristics" } @@ -56,7 +58,7 @@ RSpec.describe CheckAnswersHelper do subsection_with_radio_conditionals, case_log_with_met_radio_condition, form, - )).to equal(3) + )).to equal(4) end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 265296e40..5fcb41bec 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -96,6 +96,31 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end end + + context "armed forces active validation" do + it "must be answered if ever served in the forces as a regular" do + expect { + CaseLog.create!(armed_forces: "Yes - a regular", + armed_forces_active: nil) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "must not be answered if not ever served as a regular" do + expect { + CaseLog.create!(armed_forces: "No", + armed_forces_active: "Yes") + }.to raise_error() + end + + #Crossover over tests here as injured must be answered as well for no error + it "must be answered if ever served in the forces as a regular" do + expect { + CaseLog.create!(armed_forces: "Yes - a regular", + armed_forces_active: "Yes", + armed_forces_injured: "Yes") + }.not_to raise_error() + end + end end describe "status" do From ede51029e9c6a40c61bbfcf346aeb4cbd0e2a8e4 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Tue, 26 Oct 2021 16:06:56 +0100 Subject: [PATCH 2/8] rubocop --- app/models/case_log.rb | 2 +- spec/helpers/check_answers_helper_spec.rb | 4 ++-- spec/models/case_log_spec.rb | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index f56f20e2f..9e5f4e453 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -61,7 +61,7 @@ class CaseLogValidator < ActiveModel::Validator record.errors.add :armed_forces_active, "You must answer the armed forces active question if the tenant has served as a regular in the armed forces" end - if record.armed_forces != "Yes - a regular" && !record.armed_forces_active.blank? + if record.armed_forces != "Yes - a regular" && record.armed_forces_active.present? record.errors.add :armed_forces_active, "You must not answer the armed forces active question if the tenant has not served as a regular in the armed forces" end end diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb index e53c54957..cac582f93 100644 --- a/spec/helpers/check_answers_helper_spec.rb +++ b/spec/helpers/check_answers_helper_spec.rb @@ -12,8 +12,8 @@ RSpec.describe CheckAnswersHelper do end let(:case_log_with_met_radio_condition) do FactoryBot.create(:case_log, armed_forces: "Yes - a regular", - armed_forces_injured: "No", - armed_forces_active: "Yes") + armed_forces_injured: "No", + armed_forces_active: "Yes") end let(:subsection) { "income_and_benefits" } let(:subsection_with_numeric_conditionals) { "household_characteristics" } diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 5fcb41bec..3907a8176 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -109,16 +109,16 @@ RSpec.describe Form, type: :model do expect { CaseLog.create!(armed_forces: "No", armed_forces_active: "Yes") - }.to raise_error() + }.to raise_error end - #Crossover over tests here as injured must be answered as well for no error + # Crossover over tests here as injured must be answered as well for no error it "must be answered if ever served in the forces as a regular" do expect { CaseLog.create!(armed_forces: "Yes - a regular", armed_forces_active: "Yes", armed_forces_injured: "Yes") - }.not_to raise_error() + }.not_to raise_error end end end From 3d2699e557525e960fadf11e4ccae00ef95dee81 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Wed, 27 Oct 2021 09:33:58 +0100 Subject: [PATCH 3/8] added simple validation for outstanding rent --- app/models/case_log.rb | 9 +++++++++ spec/models/case_log_spec.rb | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index aad4d23b0..ef4655a7a 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -55,6 +55,15 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_outstanding_rent_amount(record) + if record.outstanding_rent_or_charges == "Yes" && record.outstanding_amount.blank? + record.errors.add :outstanding_amount, "You must answer the oustanding amout question if you have outstanding rent or charges." + end + if record.outstanding_rent_or_charges == "No" && !record.outstanding_amount.blank? + record.errors.add :outstanding_amount, "You must not answer the oustanding amout question if you don't have outstanding rent or charges." + 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/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 265296e40..aaa7f8c84 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -96,6 +96,22 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end end + + context "outstanding rent or charges validation" do + it "must be anwered if answered yes to outstanding rent or charges" do + expect { + CaseLog.create!(outstanding_rent_or_charges: "Yes", + outstanding_amount: nil) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "must be not be anwered if answered no to outstanding rent or charges" do + expect { + CaseLog.create!(outstanding_rent_or_charges: "No", + outstanding_amount: 99) + }.to raise_error(ActiveRecord::RecordInvalid) + end + end end describe "status" do From d4497883aecb2690cfac8445121336e227ab09b7 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Wed, 27 Oct 2021 11:25:48 +0100 Subject: [PATCH 4/8] rubocop --- app/models/case_log.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index ef4655a7a..b7e7e1c0f 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -59,7 +59,7 @@ class CaseLogValidator < ActiveModel::Validator if record.outstanding_rent_or_charges == "Yes" && record.outstanding_amount.blank? record.errors.add :outstanding_amount, "You must answer the oustanding amout question if you have outstanding rent or charges." end - if record.outstanding_rent_or_charges == "No" && !record.outstanding_amount.blank? + if record.outstanding_rent_or_charges == "No" && record.outstanding_amount.present? record.errors.add :outstanding_amount, "You must not answer the oustanding amout question if you don't have outstanding rent or charges." end end From 096cb8fae71aa4518d582fba632d960023da605b Mon Sep 17 00:00:00 2001 From: magicmilo Date: Wed, 27 Oct 2021 14:14:21 +0100 Subject: [PATCH 5/8] replace tenant to person_1 in examples and required of API Json --- docs/api/DLUHC-CORE-Data.v1.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/api/DLUHC-CORE-Data.v1.json b/docs/api/DLUHC-CORE-Data.v1.json index a62d1f184..81081b52c 100644 --- a/docs/api/DLUHC-CORE-Data.v1.json +++ b/docs/api/DLUHC-CORE-Data.v1.json @@ -109,7 +109,7 @@ "Invalid Age": { "value": { "errors": { - "tenant_age": [ + "person_1_age": [ "Tenant age must be between 0 and 120" ] } @@ -219,7 +219,7 @@ "reasonable_preference_reason": [ "If reasonable preference is Yes, a reason must be given" ], - "tenant_age": [ + "person_1_age": [ "Tenant age must be between 0 and 120" ] } @@ -268,8 +268,8 @@ "x-examples": { "example-1": { "tenant_code": "T657", - "tenant_age": 35, - "tenant_gender": "Female", + "person_1_age": 35, + "person_1_gender": "Female", "tenant_ethnic_group": "White: English/Scottish/Welsh/Northern Irish/British", "tenant_nationality": "UK national resident in UK", "previous_housing_situation": "Private sector tenancy", From 512e3a5a82e949c185db336336df970f8cccef87 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Wed, 27 Oct 2021 14:21:05 +0100 Subject: [PATCH 6/8] added required armed forces fields to example --- docs/api/DLUHC-CORE-Data.v1.json | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/api/DLUHC-CORE-Data.v1.json b/docs/api/DLUHC-CORE-Data.v1.json index 64d5ca517..35a028ace 100644 --- a/docs/api/DLUHC-CORE-Data.v1.json +++ b/docs/api/DLUHC-CORE-Data.v1.json @@ -1207,6 +1207,17 @@ ] } }, - "securitySchemes": {} + "securitySchemes": { + "API Key - 1": { + "name": "API Key", + "type": "apiKey", + "in": "query" + }, + "API Key - 2": { + "name": "API Key", + "type": "apiKey", + "in": "query" + } + } } -} +} \ No newline at end of file From 073ccfe633b668d162f994f5dc65b0c7e35c72c0 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Wed, 27 Oct 2021 14:47:10 +0100 Subject: [PATCH 7/8] rubocop --- Gemfile | 2 +- app/models/case_log.rb | 2 +- spec/models/case_log_spec.rb | 8 ++++---- spec/rails_helper.rb | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Gemfile b/Gemfile index 0084b7546..c249007c2 100644 --- a/Gemfile +++ b/Gemfile @@ -49,10 +49,10 @@ end group :test do gem "capybara", require: false + gem "database_cleaner-active_record", require: false gem "factory_bot_rails" gem "selenium-webdriver", require: false gem "simplecov", require: false - gem "database_cleaner-active_record", require: false %w[rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support].each do |lib| gem lib, git: "https://github.com/rspec/#{lib}.git", branch: "main", require: false end diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 556013773..2c818c736 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -54,7 +54,7 @@ class CaseLogValidator < ActiveModel::Validator record.errors.add :armed_forces_injured, "You must not answer the armed forces injury question if the tenant has not served in the armed forces or prefer not to say was chosen" end end - + EMPLOYED_STATUSES = ["Full-time - 30 hours or more", "Part-time - Less than 30 hours"].freeze def validate_net_income_uc_proportion(record) (1..8).any? do |n| diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index cad16431f..e0d03ed53 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -115,7 +115,7 @@ 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 { @@ -163,7 +163,7 @@ RSpec.describe Form, type: :model do }.not_to raise_error end end - + context "armed forces active validation" do it "must be answered if ever served in the forces as a regular" do expect { @@ -176,7 +176,7 @@ RSpec.describe Form, type: :model do expect { CaseLog.create!(armed_forces: "No", armed_forces_active: "Yes") - }.to raise_error + }.to raise_error(ActiveRecord::RecordInvalid) end # Crossover over tests here as injured must be answered as well for no error @@ -185,7 +185,7 @@ RSpec.describe Form, type: :model do CaseLog.create!(armed_forces: "Yes - a regular", armed_forces_active: "Yes", armed_forces_injured: "Yes") - }.not_to raise_error + }.not_to raise_error end end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index b873a5929..7a6a17442 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -6,7 +6,7 @@ require File.expand_path("../config/environment", __dir__) abort("The Rails environment is running in production mode!") if Rails.env.production? require "rspec/rails" require "capybara/rspec" -require 'database_cleaner/active_record' +require "database_cleaner/active_record" # Comment to run `js: true specs` with visible browser interaction Capybara.javascript_driver = :selenium_headless @@ -58,6 +58,7 @@ RSpec.configure do |config| uncommitted transaction data setup over the spec's database connection. MSG end + DatabaseCleaner.clean_with(:truncation) end From fba24747474fc2e9d110e70666a81a6c046f3891 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Wed, 27 Oct 2021 14:51:39 +0100 Subject: [PATCH 8/8] rubocop --- app/models/case_log.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 1446c967c..7ec3ae1bc 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -63,7 +63,7 @@ class CaseLogValidator < ActiveModel::Validator record.errors.add :outstanding_amount, "You must not answer the oustanding amout question if you don't have outstanding rent or charges." end end - + EMPLOYED_STATUSES = ["Full-time - 30 hours or more", "Part-time - Less than 30 hours"].freeze def validate_net_income_uc_proportion(record) (1..8).any? do |n|