From 677b0aeb1fc8ca8212c95d359c28940421bb3267 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Tue, 28 Feb 2023 17:32:51 +0000 Subject: [PATCH] feat: use collection time helper and start fixing tests --- app/models/validations/setup_validations.rb | 37 +-- spec/features/form/validations_spec.rb | 273 +++++++++--------- spec/mailers/bulk_upload_mailer_spec.rb | 1 + .../validations/setup_validations_spec.rb | 16 +- .../validations/soft_validations_spec.rb | 8 + 5 files changed, 167 insertions(+), 168 deletions(-) diff --git a/app/models/validations/setup_validations.rb b/app/models/validations/setup_validations.rb index 4dd400eb2..ff17706bb 100644 --- a/app/models/validations/setup_validations.rb +++ b/app/models/validations/setup_validations.rb @@ -1,16 +1,11 @@ module Validations::SetupValidations include Validations::SharedValidations + include CollectionTimeHelper def validate_startdate_setup(record) return unless record.startdate && date_valid?("startdate", record) - created_at = record.created_at || Time.zone.now - - if created_at >= previous_collection_end_date && !record.startdate.between?(current_collection_start_date, next_collection_start_date) - record.errors.add :startdate, validation_error_message - end - - if created_at < previous_collection_end_date && !record.startdate.between?(previous_collection_start_date, next_collection_start_date) + unless record.startdate.between?(active_collection_start_date, current_collection_end_date) record.errors.add :startdate, validation_error_message end end @@ -41,6 +36,14 @@ module Validations::SetupValidations private + def active_collection_start_date + if FormHandler.instance.lettings_in_crossover_period? + previous_collection_start_date + else + current_collection_start_date + end + end + def validation_error_message current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y") @@ -64,26 +67,6 @@ private end end - def previous_collection_start_date - FormHandler.instance.lettings_forms["previous_lettings"].start_date - end - - def previous_collection_end_date - FormHandler.instance.lettings_forms["previous_lettings"].end_date - end - - def current_collection_start_date - FormHandler.instance.lettings_forms["current_lettings"].start_date - end - - def current_collection_end_date - FormHandler.instance.lettings_forms["current_lettings"].end_date - end - - def next_collection_start_date - FormHandler.instance.lettings_forms["next_lettings"].start_date - end - def intermediate_product_rent_type?(record) record.rent_type == 5 end diff --git a/spec/features/form/validations_spec.rb b/spec/features/form/validations_spec.rb index 24b2816ce..e8eef1a7b 100644 --- a/spec/features/form/validations_spec.rb +++ b/spec/features/form/validations_spec.rb @@ -2,164 +2,171 @@ require "rails_helper" require_relative "helpers" RSpec.describe "validations" do - let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } - let(:user) { FactoryBot.create(:user) } - let(:lettings_log) do - FactoryBot.create( - :lettings_log, - :in_progress, - created_by: user, - renewal: 0, - ) - end - let(:empty_lettings_log) do - FactoryBot.create( - :lettings_log, - created_by: user, - ) - end - let(:completed_without_declaration) do - FactoryBot.create( - :lettings_log, - :completed, - created_by: user, - status: 1, - declaration: nil, - startdate: Time.zone.local(2021, 5, 1), - ) - end - let(:id) { lettings_log.id } - before do - allow(fake_2021_2022_form).to receive(:end_date).and_return(Time.zone.today + 1.day) - allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) - sign_in user - allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) + allow(Time.zone).to receive(:now).and_return(Time.zone.local(2022, 5, 1)) end - include Helpers - - describe "Question validation" do - context "when the tenant age is invalid" do - it "shows validation for under 0" do - visit("/lettings-logs/#{id}/person-1-age") - fill_in_number_question(empty_lettings_log.id, "age1", -5, "person-1-age") - expect(page).to have_selector("#error-summary-title") - expect(page).to have_selector("#lettings-log-age1-error") - expect(page).to have_selector("#lettings-log-age1-field-error") - expect(page).to have_title("Error") - end - - it "shows validation for over 120" do - visit("/lettings-logs/#{id}/person-1-age") - fill_in_number_question(empty_lettings_log.id, "age1", 121, "person-1-age") - expect(page).to have_selector("#error-summary-title") - expect(page).to have_selector("#lettings-log-age1-error") - expect(page).to have_selector("#lettings-log-age1-field-error") - expect(page).to have_title("Error") - end + context "tests" do + let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } + let(:user) { FactoryBot.create(:user) } + let(:lettings_log) do + FactoryBot.create( + :lettings_log, + :in_progress, + created_by: user, + renewal: 0, + ) end - end - - describe "date validation", js: true do - def fill_in_date(lettings_log_id, question, day, month, year, path) - visit("/lettings-logs/#{lettings_log_id}/#{path}") - fill_in("lettings_log[#{question}(1i)]", with: year) - fill_in("lettings_log[#{question}(2i)]", with: month) - fill_in("lettings_log[#{question}(3i)]", with: day) + let(:empty_lettings_log) do + FactoryBot.create( + :lettings_log, + created_by: user, + ) end + let(:completed_without_declaration) do + FactoryBot.create( + :lettings_log, + :completed, + created_by: user, + status: 1, + declaration: nil, + startdate: Time.zone.local(2021, 5, 1), + ) + end + let(:id) { lettings_log.id } - it "does not allow out of range dates to be submitted" do - fill_in_date(id, "mrcdate", 3100, 12, 2000, "property-major-repairs") - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") - - fill_in_date(id, "mrcdate", 12, 1, 20_000, "property-major-repairs") - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") - - fill_in_date(id, "mrcdate", 13, 100, 2020, "property-major-repairs") - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") - - fill_in_date(id, "mrcdate", 21, 11, 2020, "property-major-repairs") - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{id}/local-authority/check-answers") + before do + allow(fake_2021_2022_form).to receive(:end_date).and_return(Time.zone.today + 1.day) + allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day) + sign_in user + allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end - it "does not allow non numeric inputs to be submitted" do - fill_in_date(id, "mrcdate", "abc", "de", "ff", "property-major-repairs") - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") + include Helpers + + describe "Question validation" do + context "when the tenant age is invalid" do + it "shows validation for under 0" do + visit("/lettings-logs/#{id}/person-1-age") + fill_in_number_question(empty_lettings_log.id, "age1", -5, "person-1-age") + expect(page).to have_selector("#error-summary-title") + expect(page).to have_selector("#lettings-log-age1-error") + expect(page).to have_selector("#lettings-log-age1-field-error") + expect(page).to have_title("Error") + end + + it "shows validation for over 120" do + visit("/lettings-logs/#{id}/person-1-age") + fill_in_number_question(empty_lettings_log.id, "age1", 121, "person-1-age") + expect(page).to have_selector("#error-summary-title") + expect(page).to have_selector("#lettings-log-age1-error") + expect(page).to have_selector("#lettings-log-age1-field-error") + expect(page).to have_title("Error") + end + end end - it "does not allow partial inputs to be submitted" do - fill_in_date(id, "mrcdate", 21, 12, nil, "property-major-repairs") - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") + describe "date validation", js: true do + def fill_in_date(lettings_log_id, question, day, month, year, path) + visit("/lettings-logs/#{lettings_log_id}/#{path}") + fill_in("lettings_log[#{question}(1i)]", with: year) + fill_in("lettings_log[#{question}(2i)]", with: month) + fill_in("lettings_log[#{question}(3i)]", with: day) + end - fill_in_date(id, "mrcdate", 12, nil, 2000, "property-major-repairs") - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") + it "does not allow out of range dates to be submitted" do + fill_in_date(id, "mrcdate", 3100, 12, 2000, "property-major-repairs") + click_button("Save and continue") + expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") - fill_in_date(id, "mrcdate", nil, 10, 2020, "property-major-repairs") - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") - end + fill_in_date(id, "mrcdate", 12, 1, 20_000, "property-major-repairs") + click_button("Save and continue") + expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") - it "allows valid inputs to be submitted" do - fill_in_date(id, "mrcdate", 21, 11, 2020, "property-major-repairs") - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{id}/local-authority/check-answers") - end - end + fill_in_date(id, "mrcdate", 13, 100, 2020, "property-major-repairs") + click_button("Save and continue") + expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") - describe "Soft Validation" do - context "when a weekly net income is above the expected amount for the given economic status but below the hard max" do - let(:lettings_log) do - FactoryBot.create( - :lettings_log, - :in_progress, - ecstat1: 1, - created_by: user, - ) + fill_in_date(id, "mrcdate", 21, 11, 2020, "property-major-repairs") + click_button("Save and continue") + expect(page).to have_current_path("/lettings-logs/#{id}/local-authority/check-answers") end - let(:income_over_soft_limit) { 750 } - let(:income_under_soft_limit) { 700 } - it "prompts the user to confirm the value is correct with an interruption screen" do - visit("/lettings-logs/#{lettings_log.id}/net-income") - fill_in("lettings-log-earnings-field", with: income_over_soft_limit) - choose("lettings-log-incfreq-1-field", allow_label_click: true) - click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{lettings_log.id}/net-income-value-check") - expect(page).to have_content("Net income is outside the expected range based on the lead tenant’s working situation") - expect(page).to have_content("You told us the lead tenant’s working situation is: full-time – 30 hours or more") - expect(page).to have_content("The household income you have entered is £750.00 every week") - choose("lettings-log-net-income-value-check-0-field", allow_label_click: true) + it "does not allow non numeric inputs to be submitted" do + fill_in_date(id, "mrcdate", "abc", "de", "ff", "property-major-repairs") click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{lettings_log.id}/net-income-uc-proportion") + expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") end - it "returns the user to the previous question if they do not confirm the value as correct on the interruption screen" do - visit("/lettings-logs/#{lettings_log.id}/net-income") - fill_in("lettings-log-earnings-field", with: income_over_soft_limit) - choose("lettings-log-incfreq-1-field", allow_label_click: true) + it "does not allow partial inputs to be submitted" do + fill_in_date(id, "mrcdate", 21, 12, nil, "property-major-repairs") click_button("Save and continue") - choose("lettings-log-net-income-value-check-1-field", allow_label_click: true) + expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") + + fill_in_date(id, "mrcdate", 12, nil, 2000, "property-major-repairs") + click_button("Save and continue") + expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") + + fill_in_date(id, "mrcdate", nil, 10, 2020, "property-major-repairs") click_button("Save and continue") - expect(page).to have_current_path("/lettings-logs/#{lettings_log.id}/net-income") + expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") end - end - end - describe "Submission validation" do - context "when tenant has not seen the privacy notice" do - it "shows a warning" do - visit("/lettings-logs/#{completed_without_declaration.id}/declaration") + it "allows valid inputs to be submitted" do + fill_in_date(id, "mrcdate", 21, 11, 2020, "property-major-repairs") click_button("Save and continue") - expect(page).to have_content("You must show the DLUHC privacy notice to the tenant") + expect(page).to have_current_path("/lettings-logs/#{id}/local-authority/check-answers") + end + end + + describe "Soft Validation" do + context "when a weekly net income is above the expected amount for the given economic status but below the hard max" do + let(:lettings_log) do + FactoryBot.create( + :lettings_log, + :in_progress, + ecstat1: 1, + created_by: user, + ) + end + let(:income_over_soft_limit) { 750 } + let(:income_under_soft_limit) { 700 } + + it "prompts the user to confirm the value is correct with an interruption screen" do + visit("/lettings-logs/#{lettings_log.id}/net-income") + fill_in("lettings-log-earnings-field", with: income_over_soft_limit) + choose("lettings-log-incfreq-1-field", allow_label_click: true) + click_button("Save and continue") + expect(page).to have_current_path("/lettings-logs/#{lettings_log.id}/net-income-value-check") + expect(page).to have_content("Net income is outside the expected range based on the lead tenant’s working situation") + expect(page).to have_content("You told us the lead tenant’s working situation is: full-time – 30 hours or more") + expect(page).to have_content("The household income you have entered is £750.00 every week") + choose("lettings-log-net-income-value-check-0-field", allow_label_click: true) + click_button("Save and continue") + expect(page).to have_current_path("/lettings-logs/#{lettings_log.id}/net-income-uc-proportion") + end + + it "returns the user to the previous question if they do not confirm the value as correct on the interruption screen" do + visit("/lettings-logs/#{lettings_log.id}/net-income") + fill_in("lettings-log-earnings-field", with: income_over_soft_limit) + choose("lettings-log-incfreq-1-field", allow_label_click: true) + click_button("Save and continue") + choose("lettings-log-net-income-value-check-1-field", allow_label_click: true) + click_button("Save and continue") + expect(page).to have_current_path("/lettings-logs/#{lettings_log.id}/net-income") + end + end + end + + describe "Submission validation" do + context "when tenant has not seen the privacy notice" do + it "shows a warning" do + visit("/lettings-logs/#{completed_without_declaration.id}/declaration") + click_button("Save and continue") + expect(page).to have_content("You must show the DLUHC privacy notice to the tenant") + end end end end + end diff --git a/spec/mailers/bulk_upload_mailer_spec.rb b/spec/mailers/bulk_upload_mailer_spec.rb index d0217bfaf..471f91baf 100644 --- a/spec/mailers/bulk_upload_mailer_spec.rb +++ b/spec/mailers/bulk_upload_mailer_spec.rb @@ -10,6 +10,7 @@ RSpec.describe BulkUploadMailer do before do allow(Notifications::Client).to receive(:new).and_return(notify_client) allow(notify_client).to receive(:send_email).and_return(true) + allow(Time).to receive(:now).and_return(Time.zone.local(2021, 5, 1)) end describe "#send_bulk_upload_complete_mail" do diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index 7bb00790a..55aa94a04 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/spec/models/validations/setup_validations_spec.rb @@ -17,13 +17,13 @@ RSpec.describe Validations::SetupValidations do it "cannot be before the first collection window start date" do record.startdate = Time.zone.local(2021, 1, 1) setup_validator.validate_startdate_setup(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 1st July 2023") + expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 31st March 2023") end it "cannot be after the second collection window end date" do record.startdate = Time.zone.local(2023, 7, 1, 6) setup_validator.validate_startdate_setup(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 1st July 2023") + expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 31st March 2023") end end @@ -36,13 +36,13 @@ RSpec.describe Validations::SetupValidations do it "cannot be before the first collection window start date" do record.startdate = Time.zone.local(2022, 1, 1) setup_validator.validate_startdate_setup(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 1st July 2023") + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023") end it "cannot be after the second collection window end date" do record.startdate = Time.zone.local(2023, 7, 1, 6) setup_validator.validate_startdate_setup(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 1st July 2023") + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023") end end end @@ -57,13 +57,13 @@ RSpec.describe Validations::SetupValidations do it "cannot be before the first collection window start date" do record.startdate = Time.zone.local(2022, 1, 1) setup_validator.validate_startdate_setup(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 9th July 2024") + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 31st March 2024") end it "cannot be after the second collection window end date" do record.startdate = Time.zone.local(2024, 7, 1, 6) setup_validator.validate_startdate_setup(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 9th July 2024") + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 31st March 2024") end end @@ -76,13 +76,13 @@ RSpec.describe Validations::SetupValidations do it "cannot be before the first collection window start date" do record.startdate = Time.zone.local(2023, 1, 1) setup_validator.validate_startdate_setup(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 9th July 2024") + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 31st March 2024") end it "cannot be after the second collection window end date" do record.startdate = Time.zone.local(2024, 7, 1, 6) setup_validator.validate_startdate_setup(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 9th July 2024") + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 31st March 2024") end end end diff --git a/spec/models/validations/soft_validations_spec.rb b/spec/models/validations/soft_validations_spec.rb index 9ca9436fb..e4e67481c 100644 --- a/spec/models/validations/soft_validations_spec.rb +++ b/spec/models/validations/soft_validations_spec.rb @@ -207,6 +207,10 @@ RSpec.describe Validations::SoftValidations do end describe "major repairs date soft validations" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2022, 2, 1)) + end + context "when the major repairs date is within 10 years of the tenancy start date" do it "shows the interruption screen" do record.update!(startdate: Time.zone.local(2022, 2, 1), mrcdate: Time.zone.local(2013, 2, 1)) @@ -223,6 +227,10 @@ RSpec.describe Validations::SoftValidations do end describe "void date soft validations" do + before do + allow(Time).to receive(:now).and_return(Time.zone.local(2022, 2, 1)) + end + context "when the void date is within 10 years of the tenancy start date" do it "shows the interruption screen" do record.update!(startdate: Time.zone.local(2022, 2, 1), voiddate: Time.zone.local(2013, 2, 1))