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. 7
      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

7
spec/features/form/validations_spec.rb

@ -2,6 +2,11 @@ require "rails_helper"
require_relative "helpers" require_relative "helpers"
RSpec.describe "validations" do RSpec.describe "validations" do
before do
allow(Time.zone).to receive(:now).and_return(Time.zone.local(2022, 5, 1))
end
context "tests" do
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let(:lettings_log) do let(:lettings_log) do
@ -163,3 +168,5 @@ RSpec.describe "validations" do
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