Browse Source

feat: use collection time helper and start fixing tests

pull/1318/head
natdeanlewissoftwire 2 years ago
parent
commit
677b0aeb1f
  1. 37
      app/models/validations/setup_validations.rb
  2. 273
      spec/features/form/validations_spec.rb
  3. 1
      spec/mailers/bulk_upload_mailer_spec.rb
  4. 16
      spec/models/validations/setup_validations_spec.rb
  5. 8
      spec/models/validations/soft_validations_spec.rb

37
app/models/validations/setup_validations.rb

@ -1,16 +1,11 @@
module Validations::SetupValidations module Validations::SetupValidations
include Validations::SharedValidations include Validations::SharedValidations
include CollectionTimeHelper
def validate_startdate_setup(record) def validate_startdate_setup(record)
return unless record.startdate && date_valid?("startdate", record) return unless record.startdate && date_valid?("startdate", record)
created_at = record.created_at || Time.zone.now unless record.startdate.between?(active_collection_start_date, current_collection_end_date)
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)
record.errors.add :startdate, validation_error_message record.errors.add :startdate, validation_error_message
end end
end end
@ -41,6 +36,14 @@ module Validations::SetupValidations
private 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 def validation_error_message
current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y") current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y")
@ -64,26 +67,6 @@ private
end end
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) def intermediate_product_rent_type?(record)
record.rent_type == 5 record.rent_type == 5
end end

273
spec/features/form/validations_spec.rb

@ -2,164 +2,171 @@ require "rails_helper"
require_relative "helpers" require_relative "helpers"
RSpec.describe "validations" do 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 before do
allow(fake_2021_2022_form).to receive(:end_date).and_return(Time.zone.today + 1.day) allow(Time.zone).to receive(:now).and_return(Time.zone.local(2022, 5, 1))
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 end
include Helpers context "tests" do
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
describe "Question validation" do let(:user) { FactoryBot.create(:user) }
context "when the tenant age is invalid" do let(:lettings_log) do
it "shows validation for under 0" do FactoryBot.create(
visit("/lettings-logs/#{id}/person-1-age") :lettings_log,
fill_in_number_question(empty_lettings_log.id, "age1", -5, "person-1-age") :in_progress,
expect(page).to have_selector("#error-summary-title") created_by: user,
expect(page).to have_selector("#lettings-log-age1-error") renewal: 0,
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
end let(:empty_lettings_log) do
FactoryBot.create(
describe "date validation", js: true do :lettings_log,
def fill_in_date(lettings_log_id, question, day, month, year, path) created_by: user,
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 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 before do
fill_in_date(id, "mrcdate", 3100, 12, 2000, "property-major-repairs") allow(fake_2021_2022_form).to receive(:end_date).and_return(Time.zone.today + 1.day)
click_button("Save and continue") allow(lettings_log.form).to receive(:end_date).and_return(Time.zone.today + 1.day)
expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") sign_in user
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
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")
end end
it "does not allow non numeric inputs to be submitted" do include Helpers
fill_in_date(id, "mrcdate", "abc", "de", "ff", "property-major-repairs")
click_button("Save and continue") describe "Question validation" do
expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") 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 end
it "does not allow partial inputs to be submitted" do describe "date validation", js: true do
fill_in_date(id, "mrcdate", 21, 12, nil, "property-major-repairs") def fill_in_date(lettings_log_id, question, day, month, year, path)
click_button("Save and continue") visit("/lettings-logs/#{lettings_log_id}/#{path}")
expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") 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") it "does not allow out of range dates to be submitted" do
click_button("Save and continue") fill_in_date(id, "mrcdate", 3100, 12, 2000, "property-major-repairs")
expect(page).to have_current_path("/lettings-logs/#{id}/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") fill_in_date(id, "mrcdate", 12, 1, 20_000, "property-major-repairs")
click_button("Save and continue") click_button("Save and continue")
expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs") expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs")
end
it "allows valid inputs to be submitted" do fill_in_date(id, "mrcdate", 13, 100, 2020, "property-major-repairs")
fill_in_date(id, "mrcdate", 21, 11, 2020, "property-major-repairs") click_button("Save and continue")
click_button("Save and continue") expect(page).to have_current_path("/lettings-logs/#{id}/property-major-repairs")
expect(page).to have_current_path("/lettings-logs/#{id}/local-authority/check-answers")
end
end
describe "Soft Validation" do fill_in_date(id, "mrcdate", 21, 11, 2020, "property-major-repairs")
context "when a weekly net income is above the expected amount for the given economic status but below the hard max" do click_button("Save and continue")
let(:lettings_log) do expect(page).to have_current_path("/lettings-logs/#{id}/local-authority/check-answers")
FactoryBot.create(
:lettings_log,
:in_progress,
ecstat1: 1,
created_by: user,
)
end 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 it "does not allow non numeric inputs to be submitted" do
visit("/lettings-logs/#{lettings_log.id}/net-income") fill_in_date(id, "mrcdate", "abc", "de", "ff", "property-major-repairs")
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") 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 end
it "returns the user to the previous question if they do not confirm the value as correct on the interruption screen" do it "does not allow partial inputs to be submitted" do
visit("/lettings-logs/#{lettings_log.id}/net-income") fill_in_date(id, "mrcdate", 21, 12, nil, "property-major-repairs")
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") 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") 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
end
describe "Submission validation" do it "allows valid inputs to be submitted" do
context "when tenant has not seen the privacy notice" do fill_in_date(id, "mrcdate", 21, 11, 2020, "property-major-repairs")
it "shows a warning" do
visit("/lettings-logs/#{completed_without_declaration.id}/declaration")
click_button("Save and continue") 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
end end
end end

1
spec/mailers/bulk_upload_mailer_spec.rb

@ -10,6 +10,7 @@ RSpec.describe BulkUploadMailer do
before do before do
allow(Notifications::Client).to receive(:new).and_return(notify_client) allow(Notifications::Client).to receive(:new).and_return(notify_client)
allow(notify_client).to receive(:send_email).and_return(true) allow(notify_client).to receive(:send_email).and_return(true)
allow(Time).to receive(:now).and_return(Time.zone.local(2021, 5, 1))
end end
describe "#send_bulk_upload_complete_mail" do describe "#send_bulk_upload_complete_mail" do

16
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 it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2021, 1, 1) record.startdate = Time.zone.local(2021, 1, 1)
setup_validator.validate_startdate_setup(record) 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
it "cannot be after the second collection window end date" do it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2023, 7, 1, 6) record.startdate = Time.zone.local(2023, 7, 1, 6)
setup_validator.validate_startdate_setup(record) 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
end end
@ -36,13 +36,13 @@ RSpec.describe Validations::SetupValidations do
it "cannot be before the first collection window start date" do it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2022, 1, 1) record.startdate = Time.zone.local(2022, 1, 1)
setup_validator.validate_startdate_setup(record) 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
it "cannot be after the second collection window end date" do it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2023, 7, 1, 6) record.startdate = Time.zone.local(2023, 7, 1, 6)
setup_validator.validate_startdate_setup(record) 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 end
end end
@ -57,13 +57,13 @@ RSpec.describe Validations::SetupValidations do
it "cannot be before the first collection window start date" do it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2022, 1, 1) record.startdate = Time.zone.local(2022, 1, 1)
setup_validator.validate_startdate_setup(record) 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
it "cannot be after the second collection window end date" do it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2024, 7, 1, 6) record.startdate = Time.zone.local(2024, 7, 1, 6)
setup_validator.validate_startdate_setup(record) 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
end end
@ -76,13 +76,13 @@ RSpec.describe Validations::SetupValidations do
it "cannot be before the first collection window start date" do it "cannot be before the first collection window start date" do
record.startdate = Time.zone.local(2023, 1, 1) record.startdate = Time.zone.local(2023, 1, 1)
setup_validator.validate_startdate_setup(record) 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
it "cannot be after the second collection window end date" do it "cannot be after the second collection window end date" do
record.startdate = Time.zone.local(2024, 7, 1, 6) record.startdate = Time.zone.local(2024, 7, 1, 6)
setup_validator.validate_startdate_setup(record) 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 end
end end

8
spec/models/validations/soft_validations_spec.rb

@ -207,6 +207,10 @@ RSpec.describe Validations::SoftValidations do
end end
describe "major repairs date soft validations" do 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 context "when the major repairs date is within 10 years of the tenancy start date" do
it "shows the interruption screen" do it "shows the interruption screen" do
record.update!(startdate: Time.zone.local(2022, 2, 1), mrcdate: Time.zone.local(2013, 2, 1)) 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 end
describe "void date soft validations" do 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 context "when the void date is within 10 years of the tenancy start date" do
it "shows the interruption screen" do it "shows the interruption screen" do
record.update!(startdate: Time.zone.local(2022, 2, 1), voiddate: Time.zone.local(2013, 2, 1)) record.update!(startdate: Time.zone.local(2022, 2, 1), voiddate: Time.zone.local(2013, 2, 1))

Loading…
Cancel
Save