Browse Source

CLDC-3289 Avoid freezing time in tests (part 2) (#2432)

* Refactor tests #1

* Refactor model form tests

* Update tasklist helper tests

* Update task list helper

* Fix test

* Udate model/forms tests

* Update model/validations tests

* Update a few more model specs

* Update bulk request tests

* Update policy tests

* lint

* Refactor

* More fixes

* Rabase fixes

* Refactor

* lint
pull/2491/head
kosiakkatrina 9 months ago committed by GitHub
parent
commit
30b86f5a86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      spec/models/form/subsection_spec.rb
  2. 30
      spec/models/forms/bulk_upload_lettings/year_spec.rb
  3. 26
      spec/models/forms/bulk_upload_sales/year_spec.rb
  4. 38
      spec/models/location_deactivation_period_spec.rb
  5. 87
      spec/models/location_spec.rb
  6. 33
      spec/models/scheme_deactivation_period_spec.rb
  7. 42
      spec/models/scheme_spec.rb
  8. 9
      spec/models/validations/date_validations_spec.rb
  9. 90
      spec/models/validations/household_validations_spec.rb
  10. 42
      spec/models/validations/sales/financial_validations_spec.rb
  11. 98
      spec/models/validations/sales/household_validations_spec.rb
  12. 85
      spec/models/validations/sales/sale_information_validations_spec.rb
  13. 67
      spec/models/validations/sales/setup_validations_spec.rb
  14. 60
      spec/models/validations/setup_validations_spec.rb
  15. 81
      spec/models/validations/soft_validations_spec.rb
  16. 11
      spec/policies/location_policy_spec.rb
  17. 12
      spec/policies/scheme_policy_spec.rb
  18. 10
      spec/policies/user_policy_spec.rb
  19. 32
      spec/requests/bulk_upload_lettings_logs_controller_spec.rb
  20. 30
      spec/requests/bulk_upload_sales_logs_controller_spec.rb

2
spec/models/form/subsection_spec.rb

@ -34,7 +34,7 @@ RSpec.describe Form::Subsection, type: :model do
end end
context "with an in progress lettings log" do context "with an in progress lettings log" do
let(:lettings_log) { FactoryBot.build(:lettings_log, :in_progress) } let(:lettings_log) { FactoryBot.build(:lettings_log, :in_progress, tenancycode: 3, age1: 18) }
it "has a status" do it "has a status" do
expect(subsection.status(lettings_log)).to eq(:in_progress) expect(subsection.status(lettings_log)).to eq(:in_progress)

30
spec/models/forms/bulk_upload_lettings/year_spec.rb

@ -4,13 +4,15 @@ RSpec.describe Forms::BulkUploadLettings::Year do
subject(:form) { described_class.new } subject(:form) { described_class.new }
describe "#options" do describe "#options" do
context "when in a crossover period" do
before do before do
Timecop.freeze(2024, 4, 1) allow(FormHandler.instance).to receive(:lettings_forms).and_return({ "current_lettings" => instance_double(Form, start_date: Time.zone.local(2024, 4, 1)) })
allow(FormHandler.instance).to receive(:previous_lettings_form).and_return(instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))
allow(FormHandler.instance).to receive(:next_lettings_form).and_return(instance_double(Form, start_date: Time.zone.local(2025, 4, 1)))
end end
after do context "when in a crossover period" do
Timecop.return before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true)
end end
it "returns current and previous years" do it "returns current and previous years" do
@ -21,32 +23,24 @@ RSpec.describe Forms::BulkUploadLettings::Year do
context "when not in a crossover period" do context "when not in a crossover period" do
before do before do
Timecop.freeze(2024, 3, 1) allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(false)
end
after do
Timecop.return
end end
it "returns the current year" do it "returns the current year" do
expect(form.options.map(&:id)).to eql([2023]) expect(form.options.map(&:id)).to eql([2024])
expect(form.options.map(&:name)).to eql(%w[2023/2024]) expect(form.options.map(&:name)).to eql(%w[2024/2025])
end end
end end
context "when allow_future_form_use is toggled on" do context "when allow_future_form_use is toggled on" do
before do before do
Timecop.freeze(2024, 3, 1) allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(false)
allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true) allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true)
end end
after do
Timecop.return
end
it "returns current and next years" do it "returns current and next years" do
expect(form.options.map(&:id)).to eql([2023, 2024]) expect(form.options.map(&:id)).to eql([2024, 2025])
expect(form.options.map(&:name)).to eql(%w[2023/2024 2024/2025]) expect(form.options.map(&:name)).to eql(%w[2024/2025 2025/2026])
end end
end end
end end

26
spec/models/forms/bulk_upload_sales/year_spec.rb

@ -4,13 +4,15 @@ RSpec.describe Forms::BulkUploadSales::Year do
subject(:form) { described_class.new } subject(:form) { described_class.new }
describe "#options" do describe "#options" do
context "when in a crossover period" do
before do before do
Timecop.freeze(2024, 4, 1) allow(FormHandler.instance).to receive(:sales_forms).and_return({ "current_sales" => instance_double(Form, start_date: Time.zone.local(2024, 4, 1)) })
allow(FormHandler.instance).to receive(:previous_sales_form).and_return(instance_double(Form, start_date: Time.zone.local(2023, 4, 1)))
allow(FormHandler.instance).to receive(:next_sales_form).and_return(instance_double(Form, start_date: Time.zone.local(2025, 4, 1)))
end end
after do context "when in a crossover period" do
Timecop.return before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true)
end end
it "returns current and previous years" do it "returns current and previous years" do
@ -21,22 +23,18 @@ RSpec.describe Forms::BulkUploadSales::Year do
context "when not in a crossover period" do context "when not in a crossover period" do
before do before do
Timecop.freeze(2024, 3, 1) allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false)
end
after do
Timecop.return
end end
it "returns the current year" do it "returns the current year" do
expect(form.options.map(&:id)).to eql([2023]) expect(form.options.map(&:id)).to eql([2024])
expect(form.options.map(&:name)).to eql(%w[2023/2024]) expect(form.options.map(&:name)).to eql(%w[2024/2025])
end end
end end
context "when allow_future_form_use is toggled on" do context "when allow_future_form_use is toggled on" do
before do before do
Timecop.freeze(2024, 3, 1) allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false)
allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true) allow(FeatureToggle).to receive(:allow_future_form_use?).and_return(true)
end end
@ -45,8 +43,8 @@ RSpec.describe Forms::BulkUploadSales::Year do
end end
it "returns current and next years" do it "returns current and next years" do
expect(form.options.map(&:id)).to eql([2023, 2024]) expect(form.options.map(&:id)).to eql([2024, 2025])
expect(form.options.map(&:name)).to eql(%w[2023/2024 2024/2025]) expect(form.options.map(&:name)).to eql(%w[2024/2025 2025/2026])
end end
end end
end end

38
spec/models/location_deactivation_period_spec.rb

@ -2,31 +2,34 @@ require "rails_helper"
RSpec.describe LocationDeactivationPeriod do RSpec.describe LocationDeactivationPeriod do
let(:validator) { LocationDeactivationPeriodValidator.new } let(:validator) { LocationDeactivationPeriodValidator.new }
let(:location) { FactoryBot.create(:location, startdate: now - 2.years) } let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) }
let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: now, location:) } let(:current_collection_start_date) { Time.zone.local(2023, 4, 1) }
let(:location) { FactoryBot.create(:location, startdate: previous_collection_start_date - 2.years) }
let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: current_collection_start_date, location:) }
describe "#validate" do describe "#validate" do
around do |example| before do
Timecop.freeze(now) do allow(FormHandler.instance).to receive(:previous_collection_start_date).and_return(previous_collection_start_date)
example.run allow(FormHandler.instance).to receive(:current_collection_start_date).and_return(current_collection_start_date)
end
end end
context "when not in a crossover period" do context "when not in a crossover period" do
let(:now) { Time.utc(2023, 3, 1) } before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false)
end
context "with a deactivation date before the current collection period" do context "with a deactivation date before the current collection period" do
it "adds an error" do it "adds an error" do
record.deactivation_date = now - 1.year record.deactivation_date = current_collection_start_date - 1.year
location.location_deactivation_periods.clear location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to include "The date must be on or after the 1 April 2022" expect(record.errors[:deactivation_date]).to include "The date must be on or after the 1 April 2023"
end end
end end
context "with a deactivation date in the current collection period" do context "with a deactivation date in the current collection period" do
it "does not add an error" do it "does not add an error" do
record.deactivation_date = now - 1.day record.deactivation_date = current_collection_start_date + 1.day
location.location_deactivation_periods.clear location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors).to be_empty expect(record.errors).to be_empty
@ -35,11 +38,13 @@ RSpec.describe LocationDeactivationPeriod do
end end
context "when in a crossover period" do context "when in a crossover period" do
let(:now) { Time.utc(2023, 5, 1) } before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true)
end
context "with a deactivation date before the previous collection period" do context "with a deactivation date before the previous collection period" do
it "does not add an error" do it "does not add an error" do
record.deactivation_date = now - 2.years record.deactivation_date = previous_collection_start_date - 2.years
location.location_deactivation_periods.clear location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to include "The date must be on or after the 1 April 2022" expect(record.errors[:deactivation_date]).to include "The date must be on or after the 1 April 2022"
@ -48,7 +53,7 @@ RSpec.describe LocationDeactivationPeriod do
context "with a deactivation date in the previous collection period" do context "with a deactivation date in the previous collection period" do
it "does not add an error" do it "does not add an error" do
record.deactivation_date = now - 1.year record.deactivation_date = previous_collection_start_date + 1.day
location.location_deactivation_periods.clear location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors).to be_empty expect(record.errors).to be_empty
@ -57,7 +62,7 @@ RSpec.describe LocationDeactivationPeriod do
context "with a deactivation date in the current collection period" do context "with a deactivation date in the current collection period" do
it "does not add an error" do it "does not add an error" do
record.deactivation_date = now - 1.day record.deactivation_date = current_collection_start_date + 1.day
location.location_deactivation_periods.clear location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors).to be_empty expect(record.errors).to be_empty
@ -66,11 +71,12 @@ RSpec.describe LocationDeactivationPeriod do
context "but the location was created in the current collection period" do context "but the location was created in the current collection period" do
let(:location) { FactoryBot.create(:location, startdate:) } let(:location) { FactoryBot.create(:location, startdate:) }
let(:startdate) { now - 2.days } let(:startdate) { current_collection_start_date + 2.days }
let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: current_collection_start_date + 3.days, location:) }
context "with a deactivation date in the previous collection period" do context "with a deactivation date in the previous collection period" do
it "adds an error" do it "adds an error" do
record.deactivation_date = now - 1.year record.deactivation_date = previous_collection_start_date + 1.day
location.location_deactivation_periods.clear location.location_deactivation_periods.clear
validator.validate(record) validator.validate(record)
start_date = startdate.to_formatted_s(:govuk_date) start_date = startdate.to_formatted_s(:govuk_date)

87
spec/models/location_spec.rb

@ -99,11 +99,7 @@ RSpec.describe Location, type: :model do
let(:today) { Time.zone.local(2022, 4, 1) } let(:today) { Time.zone.local(2022, 4, 1) }
before do before do
Timecop.freeze(today) allow(Time).to receive(:now).and_return(today)
end
after do
Timecop.unfreeze
end end
it "returns a list of local authorities" do it "returns a list of local authorities" do
@ -425,11 +421,7 @@ RSpec.describe Location, type: :model do
let(:today) { Time.zone.local(2023, 5, 1) } let(:today) { Time.zone.local(2023, 5, 1) }
before do before do
Timecop.freeze(today) allow(Time).to receive(:now).and_return(today)
end
after do
Timecop.unfreeze
end end
it "returns a list of local authorities" do it "returns a list of local authorities" do
@ -842,15 +834,7 @@ RSpec.describe Location, type: :model do
end end
describe "status" do describe "status" do
let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:location) { FactoryBot.build(:location, startdate: Time.zone.today - 2.months) }
before do
Timecop.freeze(2022, 6, 7)
end
after do
Timecop.unfreeze
end
context "when location is not confirmed" do context "when location is not confirmed" do
it "returns incomplete " do it "returns incomplete " do
@ -865,7 +849,7 @@ RSpec.describe Location, type: :model do
end end
it "returns deactivating soon if deactivation_date is in the future" do it "returns deactivating soon if deactivation_date is in the future" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today + 2.months, location:)
location.save! location.save!
expect(location.status).to eq(:deactivating_soon) expect(location.status).to eq(:deactivating_soon)
end end
@ -876,25 +860,25 @@ RSpec.describe Location, type: :model do
end end
it "returns deactivated if deactivation_date is in the past" do it "returns deactivated if deactivation_date is in the past" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.yesterday, location:)
location.save! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
it "returns deactivated if deactivation_date is today" do it "returns deactivated if deactivation_date is today" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today, location:)
location.save! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
it "returns reactivating soon if the location has a future reactivation date" do it "returns reactivating soon if the location has a future reactivation date" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today, reactivation_date: Time.zone.tomorrow, location:)
location.save! location.save!
expect(location.status).to eq(:reactivating_soon) expect(location.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the location has a future startdate" do it "returns activating soon if the location has a future startdate" do
location.startdate = Time.zone.local(2022, 7, 7) location.startdate = Time.zone.today + 1.month
location.save! location.save!
expect(location.status).to eq(:activating_soon) expect(location.status).to eq(:activating_soon)
end end
@ -902,7 +886,7 @@ RSpec.describe Location, type: :model do
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
before do before do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 4), reactivation_date: Time.zone.local(2022, 6, 5), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today - 1.month, reactivation_date: Time.zone.today - 2.days, location:)
location.save! location.save!
end end
@ -911,39 +895,37 @@ RSpec.describe Location, type: :model do
end end
it "returns deactivating soon if deactivation_date is in the future" do it "returns deactivating soon if deactivation_date is in the future" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today + 1.month, location:)
location.save! location.save!
expect(location.status).to eq(:deactivating_soon) expect(location.status).to eq(:deactivating_soon)
end end
it "returns deactivated if deactivation_date is in the past" do it "returns deactivated if deactivation_date is in the past" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.yesterday, location:)
location.save! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
it "returns deactivated if deactivation_date is today" do it "returns deactivated if deactivation_date is today" do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today, location:)
location.save! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
it "returns reactivating soon if the location has a future reactivation date" do it "returns reactivating soon if the location has a future reactivation date" do
Timecop.freeze(2022, 6, 8) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.yesterday, reactivation_date: Time.zone.tomorrow, location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9), location:)
location.save! location.save!
expect(location.status).to eq(:reactivating_soon) expect(location.status).to eq(:reactivating_soon)
end end
it "returns reactivating soon if the location had a deactivation during another deactivation" do it "returns reactivating soon if the location had a deactivation during another deactivation" do
Timecop.freeze(2022, 6, 4) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today - 1.month, reactivation_date: Time.zone.today + 2.days, location:)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), location:)
location.save! location.save!
expect(location.status).to eq(:reactivating_soon) expect(location.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the location has a future startdate" do it "returns activating soon if the location has a future startdate" do
location.startdate = Time.zone.local(2022, 7, 7) location.startdate = Time.zone.tomorrow
location.save! location.save!
expect(location.status).to eq(:activating_soon) expect(location.status).to eq(:activating_soon)
end end
@ -951,24 +933,16 @@ RSpec.describe Location, type: :model do
end end
describe "status_at" do describe "status_at" do
let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:location) { FactoryBot.build(:location, startdate: Time.zone.today - 3.months) }
before do
Timecop.freeze(2022, 6, 7)
end
after do
Timecop.unfreeze
end
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
before do before do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 4), reactivation_date: Time.zone.local(2022, 6, 5), location:) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today - 1.month, reactivation_date: Time.zone.today - 2.days, location:)
location.save! location.save!
end end
it "returns active if the location has no relevant deactivation records" do it "returns active if the location has no relevant deactivation records" do
expect(location.status_at(Time.zone.local(2022, 4, 4))).to eq(:active) expect(location.status_at(Time.zone.today - 2.months)).to eq(:active)
end end
end end
end end
@ -977,28 +951,23 @@ RSpec.describe Location, type: :model do
let!(:deactivated_organisation) { FactoryBot.create(:organisation, active: false) } let!(:deactivated_organisation) { FactoryBot.create(:organisation, active: false) }
let!(:deactivated_by_organisation_scheme) { FactoryBot.create(:scheme, owning_organisation: deactivated_organisation) } let!(:deactivated_by_organisation_scheme) { FactoryBot.create(:scheme, owning_organisation: deactivated_organisation) }
let!(:deactivated_by_organisation_location) { FactoryBot.create(:location, scheme: deactivated_by_organisation_scheme) } let!(:deactivated_by_organisation_location) { FactoryBot.create(:location, scheme: deactivated_by_organisation_scheme) }
let!(:incomplete_location) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.local(2022, 4, 1)) } let!(:incomplete_location) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.today - 3.months) }
let!(:incomplete_location_with_nil_confirmed) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.local(2022, 4, 1), confirmed: nil) } let!(:incomplete_location_with_nil_confirmed) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.today - 3.months, confirmed: nil) }
let!(:active_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } let!(:active_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) }
let(:deactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:deactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) }
let(:deactivated_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:deactivated_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) }
let(:reactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } let(:reactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) }
let!(:activating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 7, 7)) } let!(:activating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today + 1.day) }
before do before do
Timecop.freeze(2022, 6, 7) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today + 1.month, location: deactivating_soon_location)
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), location: deactivating_soon_location)
deactivating_soon_location.save! deactivating_soon_location.save!
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), location: deactivated_location) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.yesterday, location: deactivated_location)
deactivated_location.save! deactivated_location.save!
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8), location: reactivating_soon_location) FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today, reactivation_date: Time.zone.tomorrow, location: reactivating_soon_location)
reactivating_soon_location.save! reactivating_soon_location.save!
end end
after do
Timecop.unfreeze
end
context "when filtering by incomplete status" do context "when filtering by incomplete status" do
it "returns only incomplete locations" do it "returns only incomplete locations" do
expect(described_class.filter_by_status(%w[incomplete]).count).to eq(2) expect(described_class.filter_by_status(%w[incomplete]).count).to eq(2)

33
spec/models/scheme_deactivation_period_spec.rb

@ -2,31 +2,34 @@ require "rails_helper"
RSpec.describe SchemeDeactivationPeriod do RSpec.describe SchemeDeactivationPeriod do
let(:validator) { SchemeDeactivationPeriodValidator.new } let(:validator) { SchemeDeactivationPeriodValidator.new }
let(:scheme) { FactoryBot.create(:scheme, created_at: now - 2.years) } let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) }
let(:record) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: now, scheme:) } let(:current_collection_start_date) { Time.zone.local(2023, 4, 1) }
let(:scheme) { FactoryBot.create(:scheme, created_at: previous_collection_start_date - 2.years) }
let(:record) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: current_collection_start_date, scheme:) }
describe "#validate" do describe "#validate" do
around do |example| before do
Timecop.freeze(now) do allow(FormHandler.instance).to receive(:previous_collection_start_date).and_return(previous_collection_start_date)
example.run allow(FormHandler.instance).to receive(:current_collection_start_date).and_return(current_collection_start_date)
end
end end
context "when not in a crossover period" do context "when not in a crossover period" do
let(:now) { Time.utc(2023, 3, 1) } before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false)
end
context "with a deactivation date before the current collection period" do context "with a deactivation date before the current collection period" do
it "adds an error" do it "adds an error" do
record.deactivation_date = now - 1.year record.deactivation_date = current_collection_start_date - 1.year
scheme.scheme_deactivation_periods.clear scheme.scheme_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to include("The date must be on or after the 1 April 2022") expect(record.errors[:deactivation_date]).to include("The date must be on or after the 1 April 2023")
end end
end end
context "with a deactivation date in the current collection period" do context "with a deactivation date in the current collection period" do
it "does not add an error" do it "does not add an error" do
record.deactivation_date = now - 1.day record.deactivation_date = current_collection_start_date + 1.day
scheme.scheme_deactivation_periods.clear scheme.scheme_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to be_empty expect(record.errors[:deactivation_date]).to be_empty
@ -35,11 +38,13 @@ RSpec.describe SchemeDeactivationPeriod do
end end
context "when in a crossover period" do context "when in a crossover period" do
let(:now) { Time.utc(2023, 5, 1) } before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true)
end
context "with a deactivation date before the previous collection period" do context "with a deactivation date before the previous collection period" do
it "does not add an error" do it "does not add an error" do
record.deactivation_date = now - 2.years record.deactivation_date = previous_collection_start_date - 2.years
scheme.scheme_deactivation_periods.clear scheme.scheme_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to include("The date must be on or after the 1 April 2022") expect(record.errors[:deactivation_date]).to include("The date must be on or after the 1 April 2022")
@ -48,7 +53,7 @@ RSpec.describe SchemeDeactivationPeriod do
context "with a deactivation date in the previous collection period" do context "with a deactivation date in the previous collection period" do
it "does not add an error" do it "does not add an error" do
record.deactivation_date = now - 1.year record.deactivation_date = previous_collection_start_date + 1.year
scheme.scheme_deactivation_periods.clear scheme.scheme_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to be_empty expect(record.errors[:deactivation_date]).to be_empty
@ -57,7 +62,7 @@ RSpec.describe SchemeDeactivationPeriod do
context "with a deactivation date in the current collection period" do context "with a deactivation date in the current collection period" do
it "does not add an error" do it "does not add an error" do
record.deactivation_date = now - 1.day record.deactivation_date = current_collection_start_date + 1.day
scheme.scheme_deactivation_periods.clear scheme.scheme_deactivation_periods.clear
validator.validate(record) validator.validate(record)
expect(record.errors[:deactivation_date]).to be_empty expect(record.errors[:deactivation_date]).to be_empty

42
spec/models/scheme_spec.rb

@ -216,11 +216,6 @@ RSpec.describe Scheme, type: :model do
before do before do
FactoryBot.create(:location, scheme:) FactoryBot.create(:location, scheme:)
Timecop.freeze(2022, 6, 7)
end
after do
Timecop.unfreeze
end end
context "when there have not been any previous deactivations" do context "when there have not been any previous deactivations" do
@ -229,7 +224,7 @@ RSpec.describe Scheme, type: :model do
end end
it "returns deactivating soon if deactivation_date is in the future" do it "returns deactivating soon if deactivation_date is in the future" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today + 2.weeks, scheme:)
scheme.reload scheme.reload
expect(scheme.status).to eq(:deactivating_soon) expect(scheme.status).to eq(:deactivating_soon)
end end
@ -240,34 +235,32 @@ RSpec.describe Scheme, type: :model do
end end
it "returns deactivated if deactivation_date is in the past" do it "returns deactivated if deactivation_date is in the past" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.yesterday, scheme:)
scheme.reload scheme.reload
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
it "returns deactivated if deactivation_date is today" do it "returns deactivated if deactivation_date is today" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today, scheme:)
scheme.reload scheme.reload
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
it "returns reactivating soon if the scheme has a future reactivation date" do it "returns reactivating soon if the scheme has a future reactivation date" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today, reactivation_date: Time.zone.tomorrow, scheme:)
scheme.save! scheme.save!
expect(scheme.status).to eq(:reactivating_soon) expect(scheme.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the scheme has a future startdate" do it "returns activating soon if the scheme has a future startdate" do
Timecop.freeze(2022, 6, 4) scheme.startdate = Time.zone.today + 2.weeks
scheme.startdate = Time.zone.local(2022, 7, 7)
scheme.save!
expect(scheme.status).to eq(:activating_soon) expect(scheme.status).to eq(:activating_soon)
end end
end end
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
before do before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 6, 5), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 3.days, reactivation_date: Time.zone.today - 2.days, scheme:)
end end
it "returns active if the scheme has no relevant deactivation records" do it "returns active if the scheme has no relevant deactivation records" do
@ -275,39 +268,37 @@ RSpec.describe Scheme, type: :model do
end end
it "returns deactivating soon if deactivation_date is in the future" do it "returns deactivating soon if deactivation_date is in the future" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today + 3.months, scheme:)
scheme.reload scheme.reload
expect(scheme.status).to eq(:deactivating_soon) expect(scheme.status).to eq(:deactivating_soon)
end end
it "returns deactivated if deactivation_date is in the past" do it "returns deactivated if deactivation_date is in the past" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.yesterday, scheme:)
scheme.reload scheme.reload
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
it "returns deactivated if deactivation_date is today" do it "returns deactivated if deactivation_date is today" do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today, scheme:)
scheme.reload scheme.reload
expect(scheme.status).to eq(:deactivated) expect(scheme.status).to eq(:deactivated)
end end
it "returns reactivating soon if the scheme has a future reactivation date" do it "returns reactivating soon if the scheme has a future reactivation date" do
Timecop.freeze(2022, 6, 8) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today, reactivation_date: Time.zone.tomorrow, scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9), scheme:)
scheme.save! scheme.save!
expect(scheme.status).to eq(:reactivating_soon) expect(scheme.status).to eq(:reactivating_soon)
end end
it "returns reactivating soon if the scheme had a deactivation during another deactivation" do it "returns reactivating soon if the scheme had a deactivation during another deactivation" do
Timecop.freeze(2022, 6, 4) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 2.months, reactivation_date: Time.zone.today + 2.days, scheme:)
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), scheme:)
scheme.save! scheme.save!
expect(scheme.status).to eq(:reactivating_soon) expect(scheme.status).to eq(:reactivating_soon)
end end
it "returns activating soon if the scheme has a future startdate" do it "returns activating soon if the scheme has a future startdate" do
scheme.startdate = Time.zone.local(2022, 7, 7) scheme.startdate = Time.zone.tomorrow
scheme.save! scheme.save!
expect(scheme.status).to eq(:activating_soon) expect(scheme.status).to eq(:activating_soon)
end end
@ -327,20 +318,15 @@ RSpec.describe Scheme, type: :model do
before do before do
FactoryBot.create(:location, scheme:) FactoryBot.create(:location, scheme:)
Timecop.freeze(2022, 6, 7)
end
after do
Timecop.unfreeze
end end
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
before do before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 6, 5), scheme:) FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 3.days, reactivation_date: Time.zone.today - 2.days, scheme:)
end end
it "returns active if the scheme has no relevant deactivation records" do it "returns active if the scheme has no relevant deactivation records" do
expect(scheme.status_at(Time.zone.local(2022, 5, 5))).to eq(:active) expect(scheme.status_at(Time.zone.today - 1.month)).to eq(:active)
end end
end end
end end

9
spec/models/validations/date_validations_spec.rb

@ -9,15 +9,6 @@ RSpec.describe Validations::DateValidations do
let(:scheme_no_end_date) { create(:scheme, end_date: nil) } let(:scheme_no_end_date) { create(:scheme, end_date: nil) }
describe "tenancy start date" do describe "tenancy start date" do
before do
Timecop.freeze(Time.zone.local(2023, 11, 10))
Singleton.__init__(FormHandler)
end
after do
Timecop.return
end
it "must be a valid date" do it "must be a valid date" do
record.startdate = Time.zone.local(0, 7, 1) record.startdate = Time.zone.local(0, 7, 1)
date_validator.validate_startdate(record) date_validator.validate_startdate(record)

90
spec/models/validations/household_validations_spec.rb

@ -4,16 +4,8 @@ RSpec.describe Validations::HouseholdValidations do
subject(:household_validator) { validator_class.new } subject(:household_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::HouseholdValidations } } let(:validator_class) { Class.new { include Validations::HouseholdValidations } }
let(:log_date) { Time.zone.now } let(:startdate) { Time.zone.now }
let(:record) { FactoryBot.create(:lettings_log, :setup_completed, startdate: log_date) } let(:record) { FactoryBot.build(:lettings_log, :setup_completed, startdate:, assigned_to: create(:user)) }
before do
Timecop.freeze(log_date + 1)
end
after do
Timecop.return
end
describe "reasonable preference validations" do describe "reasonable preference validations" do
context "when reasonable preference is not given" do context "when reasonable preference is not given" do
@ -59,7 +51,7 @@ RSpec.describe Validations::HouseholdValidations do
end end
context "when form year is before 2024" do context "when form year is before 2024" do
let(:log_date) { Time.zone.local(2024, 1, 1) } let(:startdate) { Time.zone.local(2024, 1, 1) }
it "does not validate the content of reasonother for phrases indicating homelessness" do it "does not validate the content of reasonother for phrases indicating homelessness" do
record.reason = 20 record.reason = 20
@ -70,7 +62,7 @@ RSpec.describe Validations::HouseholdValidations do
end end
context "when form year is >= 2024" do context "when form year is >= 2024" do
let(:log_date) { Time.zone.local(2024, 4, 1) } let(:startdate) { Time.zone.local(2024, 4, 1) }
context "when checking the content of reasonother" do context "when checking the content of reasonother" do
it "validates that the reason doesn't match phrase indicating homelessness" do it "validates that the reason doesn't match phrase indicating homelessness" do
@ -277,7 +269,7 @@ RSpec.describe Validations::HouseholdValidations do
end end
describe "#validate_partner_count" do describe "#validate_partner_count" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:startdate) { Time.zone.local(2023, 4, 1) }
it "validates that only 1 partner exists" do it "validates that only 1 partner exists" do
record.relat2 = "P" record.relat2 = "P"
@ -300,17 +292,7 @@ RSpec.describe Validations::HouseholdValidations do
describe "#validate_person_age_matches_relationship" do describe "#validate_person_age_matches_relationship" do
context "with 2023 logs" do context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:startdate) { Time.zone.local(2023, 4, 1) }
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "when the household contains a person under 16" do context "when the household contains a person under 16" do
it "validates that person must be a child of the tenant" do it "validates that person must be a child of the tenant" do
@ -334,17 +316,7 @@ RSpec.describe Validations::HouseholdValidations do
end end
context "with 2024 logs" do context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) } let(:startdate) { Time.zone.local(2024, 4, 1) }
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
it "does not add an error is person under 16 is a partner" do it "does not add an error is person under 16 is a partner" do
record.age2 = 14 record.age2 = 14
@ -366,17 +338,7 @@ RSpec.describe Validations::HouseholdValidations do
describe "#validate_person_age_matches_economic_status" do describe "#validate_person_age_matches_economic_status" do
context "with 2023 logs" do context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:startdate) { Time.zone.local(2023, 4, 1) }
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "when the household contains a person under 16" do context "when the household contains a person under 16" do
it "validates that person's economic status must be Child" do it "validates that person's economic status must be Child" do
@ -411,17 +373,7 @@ RSpec.describe Validations::HouseholdValidations do
end end
context "with 2024 logs" do context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) } let(:startdate) { Time.zone.local(2024, 4, 1) }
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
it "does not run the validation" do it "does not run the validation" do
record.age2 = 14 record.age2 = 14
@ -437,17 +389,7 @@ RSpec.describe Validations::HouseholdValidations do
describe "#validate_person_age_and_relationship_matches_economic_status" do describe "#validate_person_age_and_relationship_matches_economic_status" do
context "with 2023 logs" do context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:startdate) { Time.zone.local(2023, 4, 1) }
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "when the household contains a tenant’s child between the ages of 16 and 19" do context "when the household contains a tenant’s child between the ages of 16 and 19" do
it "validates that person's economic status must be full time student or refused" do it "validates that person's economic status must be full time student or refused" do
@ -519,17 +461,7 @@ RSpec.describe Validations::HouseholdValidations do
end end
context "with 2024 logs" do context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) } let(:startdate) { Time.zone.local(2024, 4, 1) }
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "when the household contains a tenant’s child between the ages of 16 and 19" do context "when the household contains a tenant’s child between the ages of 16 and 19" do
it "does not add an error" do it "does not add an error" do

42
spec/models/validations/sales/financial_validations_spec.rb

@ -6,7 +6,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
let(:validator_class) { Class.new { include Validations::Sales::FinancialValidations } } let(:validator_class) { Class.new { include Validations::Sales::FinancialValidations } }
describe "income validations for shared ownership" do describe "income validations for shared ownership" do
let(:record) { FactoryBot.create(:sales_log, ownershipsch: 1) } let(:record) { FactoryBot.build(:sales_log, ownershipsch: 1) }
context "when buying in a non london borough" do context "when buying in a non london borough" do
before do before do
@ -150,7 +150,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
describe "#validate_mortgage" do describe "#validate_mortgage" do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { FactoryBot.build(:sales_log) }
it "adds an error is the mortgage is zero" do it "adds an error is the mortgage is zero" do
record.mortgageused = 1 record.mortgageused = 1
@ -168,7 +168,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
describe "#validate_percentage_bought_not_greater_than_percentage_owned" do describe "#validate_percentage_bought_not_greater_than_percentage_owned" do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { FactoryBot.build(:sales_log) }
it "does not add an error if the percentage bought is less than the percentage owned" do it "does not add an error if the percentage bought is less than the percentage owned" do
record.stairbought = 20 record.stairbought = 20
@ -202,7 +202,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
describe "#validate_percentage_bought_not_equal_percentage_owned" do describe "#validate_percentage_bought_not_equal_percentage_owned" do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { FactoryBot.build(:sales_log) }
context "with 24/25 logs" do context "with 24/25 logs" do
before do before do
@ -249,7 +249,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
describe "#validate_monthly_leasehold_charges" do describe "#validate_monthly_leasehold_charges" do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { FactoryBot.build(:sales_log) }
it "does not add an error if monthly leasehold charges are positive" do it "does not add an error if monthly leasehold charges are positive" do
record.mscharge = 2345 record.mscharge = 2345
@ -265,7 +265,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
describe "#validate_percentage_bought_at_least_threshold" do describe "#validate_percentage_bought_at_least_threshold" do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { FactoryBot.build(:sales_log) }
it "adds an error to stairbought and type if the percentage bought is less than the threshold (which is 1% by default, but higher for some shared ownership types)" do it "adds an error to stairbought and type if the percentage bought is less than the threshold (which is 1% by default, but higher for some shared ownership types)" do
record.stairbought = 9 record.stairbought = 9
@ -307,7 +307,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
describe "#validate_child_income" do describe "#validate_child_income" do
let(:record) { FactoryBot.create(:sales_log) } let(:record) { FactoryBot.build(:sales_log) }
context "when buyer 2 is not a child" do context "when buyer 2 is not a child" do
before do before do
@ -352,17 +352,10 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
describe "#validate_equity_in_range_for_year_and_type" do describe "#validate_equity_in_range_for_year_and_type" do
let(:record) { FactoryBot.create(:sales_log, saledate: now) } let(:record) { FactoryBot.build(:sales_log, saledate:) }
around do |example|
Timecop.freeze(now) do
example.run
end
Timecop.return
end
context "with a log in the 22/23 collection year" do context "with a log in the 22/23 collection year" do
let(:now) { Time.zone.local(2023, 1, 1) } let(:saledate) { Time.zone.local(2023, 1, 1) }
it "adds an error for type 2, equity below min with the correct percentage" do it "adds an error for type 2, equity below min with the correct percentage" do
record.type = 2 record.type = 2
@ -397,7 +390,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
context "with a log in 23/24 collection year" do context "with a log in 23/24 collection year" do
let(:now) { Time.zone.local(2024, 1, 1) } let(:saledate) { Time.zone.local(2024, 1, 1) }
it "adds an error for type 2, equity below min with the correct percentage" do it "adds an error for type 2, equity below min with the correct percentage" do
record.type = 2 record.type = 2
@ -433,19 +426,10 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
describe "#validate_equity_less_than_staircase_difference" do describe "#validate_equity_less_than_staircase_difference" do
let(:record) { FactoryBot.create(:sales_log, saledate: now) } let(:record) { FactoryBot.build(:sales_log, saledate:) }
around do |example|
Timecop.freeze(now) do
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
Singleton.__init__(FormHandler)
end
context "with a log in the 23/24 collection year" do context "with a log in the 23/24 collection year" do
let(:now) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
it "does not add an error" do it "does not add an error" do
record.stairbought = 2 record.stairbought = 2
@ -457,7 +441,7 @@ RSpec.describe Validations::Sales::FinancialValidations do
end end
context "with a log in 24/25 collection year" do context "with a log in 24/25 collection year" do
let(:now) { Time.zone.local(2024, 4, 1) } let(:saledate) { Time.zone.local(2024, 4, 1) }
it "adds errors if equity is more than stairowned - stairbought for joint purchase" do it "adds errors if equity is more than stairowned - stairbought for joint purchase" do
record.stairbought = 2 record.stairbought = 2

98
spec/models/validations/sales/household_validations_spec.rb

@ -4,10 +4,12 @@ RSpec.describe Validations::Sales::HouseholdValidations do
subject(:household_validator) { validator_class.new } subject(:household_validator) { validator_class.new }
let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } } let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } }
let(:record) { build(:sales_log, saledate: log_date) } let(:record) { build(:sales_log, saledate:) }
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.now }
describe "#validate_partner_count" do describe "#validate_partner_count" do
let(:saledate) { Time.zone.local(2023, 4, 1) }
it "validates that only 1 partner exists" do it "validates that only 1 partner exists" do
record.relat2 = "P" record.relat2 = "P"
record.relat3 = "P" record.relat3 = "P"
@ -28,18 +30,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
describe "#validate_person_age_matches_relationship" do describe "#validate_person_age_matches_relationship" do
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "with 2023 logs" do context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
context "when the household contains a person under 16" do context "when the household contains a person under 16" do
it "expects that person is a child of the tenant" do it "expects that person is a child of the tenant" do
@ -73,7 +65,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
context "with 2024 logs" do context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) } let(:saledate) { Time.zone.local(2024, 4, 1) }
it "does not add error if person under 16 is a partner" do it "does not add error if person under 16 is a partner" do
record.age2 = 14 record.age2 = 14
@ -94,18 +86,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
describe "#validate_person_age_matches_economic_status" do describe "#validate_person_age_matches_economic_status" do
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "with 2023 logs" do context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
it "validates that person's economic status must be Child" do it "validates that person's economic status must be Child" do
record.age2 = 14 record.age2 = 14
@ -137,7 +119,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
context "with 2024 logs" do context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) } let(:saledate) { Time.zone.local(2024, 4, 1) }
it "does not run the validation" do it "does not run the validation" do
record.age2 = 14 record.age2 = 14
@ -152,18 +134,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
describe "#validate_child_12_years_younger" do describe "#validate_child_12_years_younger" do
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "with 2023 logs" do context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
it "validates the child is at least 12 years younger than buyer 1" do it "validates the child is at least 12 years younger than buyer 1" do
record.age1 = 30 record.age1 = 30
@ -190,7 +162,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
context "with 2024 logs" do context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) } let(:saledate) { Time.zone.local(2024, 4, 1) }
it "does not validate that child is at least 12 year younger than buyer" do it "does not validate that child is at least 12 year younger than buyer" do
record.age1 = 20 record.age1 = 20
@ -205,18 +177,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
describe "#validate_person_age_and_relationship_matches_economic_status" do describe "#validate_person_age_and_relationship_matches_economic_status" do
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "with 2023 logs" do context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
it "does not add an error for a person aged 16-19 who is a student but not a child of the buyer" do it "does not add an error for a person aged 16-19 who is a student but not a child of the buyer" do
record.age2 = 18 record.age2 = 18
@ -266,7 +228,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
context "with 2024 logs" do context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) } let(:saledate) { Time.zone.local(2024, 4, 1) }
context "when the household contains a tenant’s child between the ages of 16 and 19" do context "when the household contains a tenant’s child between the ages of 16 and 19" do
it "does not add an error" do it "does not add an error" do
@ -316,7 +278,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
describe "validating fields about buyers living in the property" do describe "validating fields about buyers living in the property" do
let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale_setup_complete, saledate: log_date, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale_setup_complete, saledate:, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) }
context "when buyers will live in the property and the sale is a joint purchase" do context "when buyers will live in the property and the sale is a joint purchase" do
let(:buylivein) { 1 } let(:buylivein) { 1 }
@ -349,7 +311,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
context "with 2023 logs" do context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
it "triggers a validation if buyer two will also not live in the property" do it "triggers a validation if buyer two will also not live in the property" do
sales_log.buy2livein = 2 sales_log.buy2livein = 2
@ -377,21 +339,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
describe "#validate_buyer1_previous_tenure" do describe "#validate_buyer1_previous_tenure" do
let(:record) { build(:sales_log) } let(:record) { build(:sales_log, saledate:, ownershipsch: 2) }
let(:now) { Time.zone.local(2024, 4, 4) }
before do
Timecop.freeze(now)
Singleton.__init__(FormHandler)
record.ownershipsch = 2
record.saledate = now
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
it "adds an error when previous tenure is not valid" do it "adds an error when previous tenure is not valid" do
[3, 4, 5, 6, 7, 9, 0].each do |prevten| [3, 4, 5, 6, 7, 9, 0].each do |prevten|
@ -437,7 +385,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
context "with 23/24 logs" do context "with 23/24 logs" do
let(:now) { Time.zone.local(2023, 4, 4) } let(:saledate) { Time.zone.local(2023, 4, 4) }
it "does not add an error for outright sale" do it "does not add an error for outright sale" do
record.ownershipsch = 2 record.ownershipsch = 2
@ -452,18 +400,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
describe "#validate_buyer_not_child" do describe "#validate_buyer_not_child" do
before do
Timecop.freeze(log_date)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
context "with 2023 logs" do context "with 2023 logs" do
let(:log_date) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
it "does not add an error if either buyer is a child" do it "does not add an error if either buyer is a child" do
record.jointpur = 1 record.jointpur = 1
@ -476,7 +414,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do
end end
context "with 2024 logs" do context "with 2024 logs" do
let(:log_date) { Time.zone.local(2024, 4, 1) } let(:saledate) { Time.zone.local(2024, 4, 1) }
it "validates buyer 1 isn't a child" do it "validates buyer 1 isn't a child" do
record.ecstat1 = 9 record.ecstat1 = 9

85
spec/models/validations/sales/sale_information_validations_spec.rb

@ -725,17 +725,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
describe "#validate_stairbought" do describe "#validate_stairbought" do
let(:now) { Time.zone.local(2024, 4, 4) } let(:saledate) { Time.zone.local(2024, 4, 4) }
before do
Timecop.freeze(now)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
Singleton.__init__(FormHandler)
end
[ [
["Shared Ownership (new model lease)", 30, 90], ["Shared Ownership (new model lease)", 30, 90],
@ -748,7 +738,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
["Older Persons Shared Ownership", 24, 50], ["Older Persons Shared Ownership", 24, 50],
].each do |label, type, max| ].each do |label, type, max|
context "when ownership type is #{label}" do context "when ownership type is #{label}" do
let(:record) { build(:sales_log, ownershipsch: 1, type:, saledate: now) } let(:record) { build(:sales_log, ownershipsch: 1, type:, saledate:) }
it "does not add an error if stairbought is under #{max}%" do it "does not add an error if stairbought is under #{max}%" do
record.stairbought = max - 1 record.stairbought = max - 1
@ -781,8 +771,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
end end
context "when the collection year is before 2024" do context "when the collection year is before 2024" do
let(:record) { build(:sales_log, ownershipsch: 1, type: 24, saledate: now, stairbought: 90) } let(:record) { build(:sales_log, ownershipsch: 1, type: 24, saledate:, stairbought: 90) }
let(:now) { Time.zone.local(2023, 4, 4) } let(:saledate) { Time.zone.local(2023, 4, 4) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_stairbought(record) sale_information_validator.validate_stairbought(record)
@ -793,15 +783,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
describe "#validate_discount_and_value" do describe "#validate_discount_and_value" do
let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 50, ownershipsch: 2, type: 9, saledate: now) } let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 50, ownershipsch: 2, type: 9, saledate:) }
let(:now) { Time.zone.local(2024, 4, 1) } let(:saledate) { Time.zone.local(2024, 4, 1) }
around do |example|
Timecop.freeze(now) do
example.run
end
Timecop.return
end
context "with a log in the 24/25 collection year" do context "with a log in the 24/25 collection year" do
context "when in London" do context "when in London" do
@ -870,19 +853,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
describe "#validate_non_staircasing_mortgage" do describe "#validate_non_staircasing_mortgage" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate:) }
around do |example|
Timecop.freeze(now) do
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
Singleton.__init__(FormHandler)
end
context "with a log in the 24/25 collection year" do context "with a log in the 24/25 collection year" do
let(:now) { Time.zone.local(2024, 4, 4) } let(:saledate) { Time.zone.local(2024, 4, 4) }
context "when MORTGAGE + DEPOSIT does not equal VALUE * EQUITY/100 " do context "when MORTGAGE + DEPOSIT does not equal VALUE * EQUITY/100 " do
context "and it is not a staircase transaction" do context "and it is not a staircase transaction" do
@ -952,7 +926,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when MORTGAGE + DEPOSIT equals VALUE * EQUITY/100" do context "when MORTGAGE + DEPOSIT equals VALUE * EQUITY/100" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 50, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 50, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_non_staircasing_mortgage(record) sale_information_validator.validate_non_staircasing_mortgage(record)
@ -966,7 +940,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when MORTGAGE + DEPOSIT is within 1£ tolerance of VALUE * EQUITY/100" do context "when MORTGAGE + DEPOSIT is within 1£ tolerance of VALUE * EQUITY/100" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 2, deposit: 50_000, value: 120_001, equity: 50, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 2, deposit: 50_000, value: 120_001, equity: 50, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_non_staircasing_mortgage(record) sale_information_validator.validate_non_staircasing_mortgage(record)
@ -1052,7 +1026,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when DEPOSIT equals VALUE * EQUITY/100" do context "when DEPOSIT equals VALUE * EQUITY/100" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 2, deposit: 15_000, value: 30_000, equity: 50, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 2, deposit: 15_000, value: 30_000, equity: 50, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_non_staircasing_mortgage(record) sale_information_validator.validate_non_staircasing_mortgage(record)
@ -1066,7 +1040,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when DEPOSIT is within 1£ tolerance of VALUE * EQUITY/100" do context "when DEPOSIT is within 1£ tolerance of VALUE * EQUITY/100" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 2, deposit: 15_000, value: 30_001, equity: 50, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 2, deposit: 15_000, value: 30_001, equity: 50, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_non_staircasing_mortgage(record) sale_information_validator.validate_non_staircasing_mortgage(record)
@ -1082,8 +1056,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when it is a 2023 log" do context "when it is a 2023 log" do
let(:now) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_non_staircasing_mortgage(record) sale_information_validator.validate_non_staircasing_mortgage(record)
@ -1098,19 +1072,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
describe "#validate_staircasing_mortgage" do describe "#validate_staircasing_mortgage" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) }
around do |example|
Timecop.freeze(now) do
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
Singleton.__init__(FormHandler)
end
context "with a log in the 24/25 collection year" do context "with a log in the 24/25 collection year" do
let(:now) { Time.zone.local(2024, 4, 4) } let(:saledate) { Time.zone.local(2024, 4, 4) }
context "when MORTGAGE + DEPOSIT does not equal STAIRBOUGHT/100 * VALUE" do context "when MORTGAGE + DEPOSIT does not equal STAIRBOUGHT/100 * VALUE" do
context "and it is a staircase transaction" do context "and it is a staircase transaction" do
@ -1180,7 +1145,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when MORTGAGE + DEPOSIT equals STAIRBOUGHT/100 * VALUE" do context "when MORTGAGE + DEPOSIT equals STAIRBOUGHT/100 * VALUE" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 50, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 50, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_staircasing_mortgage(record) sale_information_validator.validate_staircasing_mortgage(record)
@ -1194,7 +1159,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when MORTGAGE + DEPOSIT is within 1£ tolerance of STAIRBOUGHT/100 * VALUE" do context "when MORTGAGE + DEPOSIT is within 1£ tolerance of STAIRBOUGHT/100 * VALUE" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_001, stairbought: 50, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_001, stairbought: 50, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_staircasing_mortgage(record) sale_information_validator.validate_staircasing_mortgage(record)
@ -1209,8 +1174,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when it is a 2023 log" do context "when it is a 2023 log" do
let(:now) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_staircasing_mortgage(record) sale_information_validator.validate_staircasing_mortgage(record)
@ -1225,7 +1190,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
context "when mortgage is not used" do context "when mortgage is not used" do
context "with a log in the 24/25 collection year" do context "with a log in the 24/25 collection year" do
let(:now) { Time.zone.local(2024, 4, 4) } let(:saledate) { Time.zone.local(2024, 4, 4) }
before do before do
record.mortgageused = 2 record.mortgageused = 2
@ -1299,7 +1264,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when DEPOSIT equals STAIRBOUGHT/100 * VALUE" do context "when DEPOSIT equals STAIRBOUGHT/100 * VALUE" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 15_000, value: 30_000, stairbought: 50, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 15_000, value: 30_000, stairbought: 50, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_staircasing_mortgage(record) sale_information_validator.validate_staircasing_mortgage(record)
@ -1313,7 +1278,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when DEPOSIT is within 1£ tolerance of STAIRBOUGHT/100 * VALUE" do context "when DEPOSIT is within 1£ tolerance of STAIRBOUGHT/100 * VALUE" do
let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 15_000, value: 30_001, stairbought: 50, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 15_000, value: 30_001, stairbought: 50, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_staircasing_mortgage(record) sale_information_validator.validate_staircasing_mortgage(record)
@ -1328,8 +1293,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do
end end
context "when it is a 2023 log" do context "when it is a 2023 log" do
let(:now) { Time.zone.local(2023, 4, 1) } let(:saledate) { Time.zone.local(2023, 4, 1) }
let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate: now) } let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, staircase: 1, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) }
it "does not add an error" do it "does not add an error" do
sale_information_validator.validate_staircasing_mortgage(record) sale_information_validator.validate_staircasing_mortgage(record)

67
spec/models/validations/sales/setup_validations_spec.rb

@ -8,12 +8,7 @@ RSpec.describe Validations::Sales::SetupValidations do
describe "#validate_saledate_collection_year" do describe "#validate_saledate_collection_year" do
context "with sales_in_crossover_period == false" do context "with sales_in_crossover_period == false" do
before do before do
Timecop.freeze(Time.zone.local(2023, 1, 10)) allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false)
Singleton.__init__(FormHandler)
end
after do
Timecop.return
end end
context "when saledate is blank" do context "when saledate is blank" do
@ -26,8 +21,8 @@ RSpec.describe Validations::Sales::SetupValidations do
end end
end end
context "when saledate is in the 22/23 collection year" do context "when saledate is in the open collection year" do
let(:record) { build(:sales_log, saledate: Time.zone.local(2023, 1, 1)) } let(:record) { build(:sales_log, :saledate_today) }
it "does not add an error" do it "does not add an error" do
setup_validator.validate_saledate_collection_year(record) setup_validator.validate_saledate_collection_year(record)
@ -36,34 +31,30 @@ RSpec.describe Validations::Sales::SetupValidations do
end end
end end
context "when saledate is before the 22/23 collection year" do context "when saledate is before the open collection year" do
let(:record) { build(:sales_log, saledate: Time.zone.local(2020, 1, 1)) } let(:record) { build(:sales_log, saledate: Time.zone.today - 3.years) }
it "adds error" do it "adds error" do
setup_validator.validate_saledate_collection_year(record) setup_validator.validate_saledate_collection_year(record)
expect(record.errors[:saledate]).to include("Enter a date within the 22/23 collection year, which is between 1st April 2022 and 31st March 2023") expect(record.errors[:saledate]).to include(/Enter a date within the \d{2}\/\d{2} collection year, which is between 1st April \d{4} and 31st March \d{4}/)
end end
end end
context "when saledate is after the 22/23 collection year" do context "when saledate is after the open collection year" do
let(:record) { build(:sales_log, saledate: Time.zone.local(2025, 4, 1)) } let(:record) { build(:sales_log, saledate: Time.zone.today + 2.years) }
it "adds error" do it "adds error" do
setup_validator.validate_saledate_collection_year(record) setup_validator.validate_saledate_collection_year(record)
expect(record.errors[:saledate]).to include("Enter a date within the 22/23 collection year, which is between 1st April 2022 and 31st March 2023") expect(record.errors[:saledate]).to include(/Enter a date within the \d{2}\/\d{2} collection year, which is between 1st April \d{4} and 31st March \d{4}/)
end end
end end
end end
context "with sales_in_crossover_period == true" do context "with sales_in_crossover_period == true" do
around do |example| before do
Timecop.freeze(Time.zone.local(2024, 5, 1)) do allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true)
Singleton.__init__(FormHandler)
example.run
end
Timecop.return
end end
context "when saledate is blank" do context "when saledate is blank" do
@ -107,20 +98,19 @@ RSpec.describe Validations::Sales::SetupValidations do
end end
context "when current time is after the new logs end date but before edit end date for the previous period" do context "when current time is after the new logs end date but before edit end date for the previous period" do
let(:record) { build(:sales_log, saledate: Time.zone.local(2025, 4, 1)) } let(:record) { build(:sales_log, saledate: nil) }
before do before do
allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 8)) allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 8))
end end
it "cannot create new logs for the previous collection year" do it "cannot create new logs for the archived collection year" do
record.update!(saledate: nil) record.saledate = Time.zone.local(2023, 1, 1)
record.saledate = Time.zone.local(2024, 1, 1)
setup_validator.validate_saledate_collection_year(record) setup_validator.validate_saledate_collection_year(record)
expect(record.errors["saledate"]).to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025") expect(record.errors["saledate"]).to include(match "Enter a date within the 23/24 or 24/25 collection years, which is between 1st April 2023 and 31st March 2025")
end end
xit "can edit already created logs for the previous collection year" do it "can edit already created logs for the previous collection year" do
record.saledate = Time.zone.local(2024, 1, 2) record.saledate = Time.zone.local(2024, 1, 2)
record.save!(validate: false) record.save!(validate: false)
record.saledate = Time.zone.local(2024, 1, 1) record.saledate = Time.zone.local(2024, 1, 1)
@ -136,19 +126,19 @@ RSpec.describe Validations::Sales::SetupValidations do
allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 8)) allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 8))
end end
it "cannot create new logs for the previous collection year" do it "cannot create new logs for the archived collection year" do
record.update!(saledate: nil) record.update!(saledate: nil)
record.saledate = Time.zone.local(2024, 1, 1) record.saledate = Time.zone.local(2023, 1, 1)
setup_validator.validate_saledate_collection_year(record) setup_validator.validate_saledate_collection_year(record)
expect(record.errors["saledate"]).to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025") expect(record.errors["saledate"]).to include(match "Enter a date within the 23/24 or 24/25 collection years, which is between 1st April 2023 and 31st March 2025")
end end
it "cannot edit already created logs for the previous collection year" do it "cannot edit already created logs for the archived collection year" do
record.saledate = Time.zone.local(2024, 1, 2) record.saledate = Time.zone.local(2023, 1, 2)
record.save!(validate: false) record.save!(validate: false)
record.saledate = Time.zone.local(2024, 1, 1) record.saledate = Time.zone.local(2023, 1, 1)
setup_validator.validate_saledate_collection_year(record) setup_validator.validate_saledate_collection_year(record)
expect(record.errors["saledate"]).to include(match "Enter a date within the 24/25 collection year, which is between 1st April 2024 and 31st March 2025") expect(record.errors["saledate"]).to include(match "Enter a date within the 23/24 or 24/25 collection years, which is between 1st April 2023 and 31st March 2025")
end end
end end
end end
@ -191,12 +181,6 @@ RSpec.describe Validations::Sales::SetupValidations do
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") } let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") }
let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:merged_organisation) { create(:organisation, name: "Merged org") }
around do |example|
Timecop.freeze(Time.zone.local(2023, 5, 1))
example.run
Timecop.return
end
before do before do
merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2)) merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2))
end end
@ -249,11 +233,8 @@ RSpec.describe Validations::Sales::SetupValidations do
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") } let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org") }
let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:merged_organisation) { create(:organisation, name: "Merged org") }
around do |example| before do
Timecop.freeze(Time.zone.local(2023, 5, 1))
merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:) merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:)
example.run
Timecop.return
end end
context "and owning organisation is no longer active" do context "and owning organisation is no longer active" do

60
spec/models/validations/setup_validations_spec.rb

@ -143,7 +143,7 @@ RSpec.describe Validations::SetupValidations do
context "when attempted startdate is more than 14 days from the current date" do context "when attempted startdate is more than 14 days from the current date" do
before do before do
Timecop.freeze(2024, 3, 1) allow(Time).to receive(:now).and_return(Time.zone.local(2024, 3, 1))
end end
it "adds an error to startdate" do it "adds an error to startdate" do
@ -164,18 +164,13 @@ RSpec.describe Validations::SetupValidations do
end end
context "when organisations were merged" do context "when organisations were merged" do
around do |example|
Timecop.freeze(Time.zone.local(2023, 5, 1))
example.run
Timecop.return
end
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 1, 30, 4, 5, 6), available_from: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") } let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 1, 30, 4, 5, 6), available_from: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") }
let(:absorbing_organisation_2) { create(:organisation, created_at: Time.zone.local(2023, 1, 30), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org 2") } let(:absorbing_organisation_2) { create(:organisation, created_at: Time.zone.local(2023, 1, 30), available_from: Time.zone.local(2023, 2, 1), name: "Absorbing org 2") }
let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:merged_organisation) { create(:organisation, name: "Merged org") }
let(:merged_organisation_2) { create(:organisation, name: "Merged org 2") } let(:merged_organisation_2) { create(:organisation, name: "Merged org 2") }
before do before do
allow(Time).to receive(:now).and_return(Time.zone.local(2023, 5, 1))
merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2)) merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2))
merged_organisation_2.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2)) merged_organisation_2.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2))
end end
@ -431,9 +426,8 @@ RSpec.describe Validations::SetupValidations do
before do before do
create(:location, scheme:) create(:location, scheme:)
Timecop.freeze(Time.zone.local(2023, 11, 10)) scheme_deactivation_period = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:) scheme_deactivation_period.save!(validate: false)
Timecop.return
scheme.reload scheme.reload
end end
@ -461,9 +455,8 @@ RSpec.describe Validations::SetupValidations do
before do before do
create(:location, scheme:) create(:location, scheme:)
Timecop.freeze(Time.zone.local(2023, 11, 10)) scheme_deactivation_period = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) scheme_deactivation_period.save!(validate: false)
Timecop.return
scheme.reload scheme.reload
end end
@ -491,11 +484,12 @@ RSpec.describe Validations::SetupValidations do
before do before do
create(:location, scheme:) create(:location, scheme:)
Timecop.freeze(Time.zone.local(2023, 11, 10)) scheme_deactivation_period = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), scheme:) scheme_deactivation_period_2 = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) scheme_deactivation_period_3 = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:) scheme_deactivation_period.save!(validate: false)
Timecop.return scheme_deactivation_period_2.save!(validate: false)
scheme_deactivation_period_3.save!(validate: false)
scheme.reload scheme.reload
end end
@ -525,9 +519,8 @@ RSpec.describe Validations::SetupValidations do
let(:location) { create(:location, scheme:) } let(:location) { create(:location, scheme:) }
before do before do
Timecop.freeze(Time.zone.local(2023, 11, 10)) location_deactivation_period = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:)
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) location_deactivation_period.save!(validate: false)
Timecop.return
location.reload location.reload
end end
@ -555,9 +548,8 @@ RSpec.describe Validations::SetupValidations do
let(:location) { create(:location, scheme:) } let(:location) { create(:location, scheme:) }
before do before do
Timecop.freeze(Time.zone.local(2023, 11, 10)) location_deactivation_period = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:)
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) location_deactivation_period.save!(validate: false)
Timecop.return
location.reload location.reload
end end
@ -585,11 +577,12 @@ RSpec.describe Validations::SetupValidations do
let(:location) { create(:location, scheme:) } let(:location) { create(:location, scheme:) }
before do before do
Timecop.freeze(Time.zone.local(2023, 11, 10)) location_deactivation_period = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:)
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), location:) location_deactivation_period_2 = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:)
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) location_deactivation_period_3 = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 1), reactivation_date: Time.zone.local(2022, 9, 4), location:)
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:) location_deactivation_period.save!(validate: false)
Timecop.return location_deactivation_period_2.save!(validate: false)
location_deactivation_period_3.save!(validate: false)
location.reload location.reload
end end
@ -719,11 +712,10 @@ RSpec.describe Validations::SetupValidations do
let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1, 4, 5, 6), available_from: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") } let(:absorbing_organisation) { create(:organisation, created_at: Time.zone.local(2023, 2, 1, 4, 5, 6), available_from: Time.zone.local(2023, 2, 1, 4, 5, 6), name: "Absorbing org") }
let(:merged_organisation) { create(:organisation, name: "Merged org") } let(:merged_organisation) { create(:organisation, name: "Merged org") }
around do |example| before do
Timecop.freeze(Time.zone.local(2023, 5, 1)) merged_organisation.merge_date = Time.zone.local(2023, 2, 2)
merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:) merged_organisation.absorbing_organisation = absorbing_organisation
example.run merged_organisation.save!(validate: false)
Timecop.return
end end
context "and owning organisation is no longer active" do context "and owning organisation is no longer active" do

81
spec/models/validations/soft_validations_spec.rb

@ -1,17 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe Validations::SoftValidations do RSpec.describe Validations::SoftValidations do
let(:organisation) { FactoryBot.create(:organisation, provider_type: "PRP") } let(:organisation) { FactoryBot.build(:organisation, provider_type: "PRP", id: 123) }
let(:record) { FactoryBot.create(:lettings_log, owning_organisation: organisation) } let(:record) { FactoryBot.build(:lettings_log, owning_organisation: organisation) }
before do
Timecop.freeze(Time.zone.local(2021, 10, 10))
Singleton.__init__(FormHandler)
end
after do
Timecop.return
end
describe "rent min max validations" do describe "rent min max validations" do
before do before do
@ -32,7 +23,7 @@ RSpec.describe Validations::SoftValidations do
record.rent_type = 0 record.rent_type = 0
record.beds = 1 record.beds = 1
record.period = 1 record.period = 1
record.startdate = Time.zone.today record.startdate = Time.zone.local(2021, 10, 10)
end end
context "when validating soft min" do context "when validating soft min" do
@ -84,7 +75,8 @@ RSpec.describe Validations::SoftValidations do
describe "retirement soft validations" do describe "retirement soft validations" do
before do before do
record.update!(age1:, ecstat1:) record.age1 = age1
record.ecstat1 = ecstat1
end end
context "when the tenant is under the expected retirement age" do context "when the tenant is under the expected retirement age" do
@ -163,35 +155,57 @@ RSpec.describe Validations::SoftValidations do
describe "pregnancy soft validations" do describe "pregnancy soft validations" do
context "when there are no female tenants" do context "when there are no female tenants" do
it "shows the interruption screen" do it "shows the interruption screen" do
record.update!(age1: 43, sex1: "M", preg_occ: 1, hhmemb: 1, age1_known: 0) record.age1 = 43
record.sex1 = "M"
record.preg_occ = 1
record.hhmemb = 1
record.age1_known = 0
expect(record.no_females_in_a_pregnant_household?).to be true expect(record.no_females_in_a_pregnant_household?).to be true
end end
end end
context "when there are no female tenants and age of other tenants is unknown" do context "when there are no female tenants and age of other tenants is unknown" do
it "shows the interruption screen" do it "shows the interruption screen" do
record.update!(sex1: "M", preg_occ: 1, hhmemb: 1, age1_known: 1) record.sex1 = "M"
record.preg_occ = 1
record.hhmemb = 1
record.age1_known = 1
expect(record.no_females_in_a_pregnant_household?).to be true expect(record.no_females_in_a_pregnant_household?).to be true
end end
end end
context "when female tenants are under 16" do context "when female tenants are under 16" do
it "shows the interruption screen" do it "shows the interruption screen" do
record.update!(age2: 14, sex2: "F", preg_occ: 1, hhmemb: 2, details_known_2: 0, age2_known: 0, age1: 18, sex1: "M", age1_known: 0) record.age2 = 14
record.sex2 = "F"
record.preg_occ = 1
record.hhmemb = 2
record.details_known_2 = 0
record.age2_known = 0
record.age1 = 18
record.sex1 = "M"
record.age1_known = 0
expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true
end end
end end
context "when female tenants are over 50" do context "when female tenants are over 50" do
it "shows the interruption screen" do it "shows the interruption screen" do
record.update!(age1: 54, sex1: "F", preg_occ: 1, hhmemb: 1, age1_known: 0) record.age1 = 54
record.sex1 = "F"
record.preg_occ = 1
record.hhmemb = 1
record.age1_known = 0
expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true
end end
end end
context "when female tenants are outside of soft validation ranges" do context "when female tenants are outside of soft validation ranges" do
it "does not show the interruption screen" do it "does not show the interruption screen" do
record.update!(age1: 44, sex1: "F", preg_occ: 1, hhmemb: 1) record.age1 = 44
record.sex1 = "F"
record.preg_occ = 1
record.hhmemb = 1
expect(record.no_females_in_a_pregnant_household?).to be false expect(record.no_females_in_a_pregnant_household?).to be false
expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false
end end
@ -199,7 +213,8 @@ RSpec.describe Validations::SoftValidations do
context "when the information about the tenants is not given" do context "when the information about the tenants is not given" do
it "does not show the interruption screen" do it "does not show the interruption screen" do
record.update!(preg_occ: 1, hhmemb: 2) record.preg_occ = 1
record.hhmemb = 2
expect(record.no_females_in_a_pregnant_household?).to be false expect(record.no_females_in_a_pregnant_household?).to be false
expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false
end end
@ -207,48 +222,36 @@ RSpec.describe Validations::SoftValidations do
end end
describe "major repairs date soft validations" do describe "major repairs date soft validations" do
before do
Timecop.freeze(Time.zone.local(2022, 2, 1))
end
after do
Timecop.unfreeze
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.startdate = Time.zone.local(2022, 2, 1)
record.mrcdate = Time.zone.local(2013, 2, 1)
expect(record.major_repairs_date_in_soft_range?).to be true expect(record.major_repairs_date_in_soft_range?).to be true
end end
end end
context "when the major repairs date is less than 2 years before the tenancy start date" do context "when the major repairs date is less than 2 years before the tenancy start date" do
it "does not show the interruption screen" do it "does not show the interruption screen" do
record.update!(startdate: Time.zone.local(2022, 2, 1), mrcdate: Time.zone.local(2021, 2, 1)) record.startdate = Time.zone.local(2022, 2, 1)
record.mrcdate = Time.zone.local(2021, 2, 1)
expect(record.major_repairs_date_in_soft_range?).to be false expect(record.major_repairs_date_in_soft_range?).to be false
end end
end end
end end
describe "void date soft validations" do describe "void date soft validations" do
before do
Timecop.freeze(Time.zone.local(2022, 2, 1))
end
after do
Timecop.unfreeze
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.startdate = Time.zone.local(2022, 2, 1)
record.voiddate = Time.zone.local(2013, 2, 1)
expect(record.voiddate_in_soft_range?).to be true expect(record.voiddate_in_soft_range?).to be true
end end
end end
context "when the void date is less than 2 years before the tenancy start date" do context "when the void date is less than 2 years before the tenancy start date" do
it "does not show the interruption screen" do it "does not show the interruption screen" do
record.update!(startdate: Time.zone.local(2022, 2, 1), voiddate: Time.zone.local(2021, 2, 1)) record.startdate = Time.zone.local(2022, 2, 1)
record.voiddate = Time.zone.local(2021, 2, 1)
expect(record.voiddate_in_soft_range?).to be false expect(record.voiddate_in_soft_range?).to be false
end end
end end

11
spec/policies/location_policy_spec.rb

@ -44,18 +44,11 @@ RSpec.describe LocationPolicy do
context "with deactivated location" do context "with deactivated location" do
before do before do
location.location_deactivation_periods << create(:location_deactivation_period, deactivation_date: Time.zone.local(2024, 4, 10), location:) location.location_deactivation_periods << build(:location_deactivation_period, deactivation_date: Time.zone.today, location:)
location.save! log = build(:lettings_log, scheme: location.scheme, location:, startdate: Time.zone.today - 2.years)
Timecop.freeze(Time.utc(2024, 4, 10))
log = create(:lettings_log, scheme: location.scheme, location:)
log.startdate = Time.zone.local(2022, 10, 10)
log.save!(validate: false) log.save!(validate: false)
end end
after do
Timecop.unfreeze
end
context "and associated logs in editable collection period" do context "and associated logs in editable collection period" do
before do before do
create(:lettings_log, scheme: location.scheme, location:) create(:lettings_log, scheme: location.scheme, location:)

12
spec/policies/scheme_policy_spec.rb

@ -46,21 +46,15 @@ RSpec.describe SchemePolicy do
context "with deactivated scheme" do context "with deactivated scheme" do
before do before do
scheme.scheme_deactivation_periods << create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2024, 4, 10), scheme:) scheme.scheme_deactivation_periods << create(:scheme_deactivation_period, deactivation_date: Time.zone.today, scheme:)
scheme.save! scheme.save!
Timecop.freeze(Time.utc(2024, 4, 10)) log = build(:lettings_log, :sh, owning_organisation: scheme.owning_organisation, scheme:, startdate: Time.zone.today - 2.years)
log = create(:lettings_log, :sh, owning_organisation: scheme.owning_organisation, scheme:)
log.startdate = Time.zone.local(2022, 10, 10)
log.save!(validate: false) log.save!(validate: false)
end end
after do
Timecop.unfreeze
end
context "and associated logs in editable collection period" do context "and associated logs in editable collection period" do
before do before do
create(:lettings_log, :sh, owning_organisation: scheme.owning_organisation, scheme:, startdate: Time.zone.local(2024, 4, 9)) create(:lettings_log, :sh, owning_organisation: scheme.owning_organisation, scheme:, startdate: Time.zone.yesterday)
end end
it "does not allow deleting a scheme as a provider" do it "does not allow deleting a scheme as a provider" do

10
spec/policies/user_policy_spec.rb

@ -144,19 +144,13 @@ RSpec.describe UserPolicy do
let(:user) { create(:user, active: false) } let(:user) { create(:user, active: false) }
before do before do
Timecop.freeze(Time.utc(2024, 4, 10)) log = build(:lettings_log, owning_organisation: user.organisation, assigned_to: user, startdate: Time.zone.today - 2.years)
log = create(:lettings_log, owning_organisation: user.organisation, assigned_to: user)
log.startdate = Time.zone.local(2022, 10, 10)
log.save!(validate: false) log.save!(validate: false)
end end
after do
Timecop.unfreeze
end
context "and associated logs in editable collection period" do context "and associated logs in editable collection period" do
before do before do
create(:lettings_log, :sh, owning_organisation: user.organisation, assigned_to: user, startdate: Time.zone.local(2024, 4, 9)) create(:lettings_log, :sh, owning_organisation: user.organisation, assigned_to: user, startdate: Time.zone.yesterday)
end end
it "does not allow deleting a user as a provider" do it "does not allow deleting a user as a provider" do

32
spec/requests/bulk_upload_lettings_logs_controller_spec.rb

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe BulkUploadLettingsLogsController, type: :request do RSpec.describe BulkUploadLettingsLogsController, type: :request do
include CollectionTimeHelper
let(:user) { create(:user) } let(:user) { create(:user) }
let(:organisation) { user.organisation } let(:organisation) { user.organisation }
@ -21,49 +23,55 @@ RSpec.describe BulkUploadLettingsLogsController, type: :request do
end end
context "when not in crossover period" do context "when not in crossover period" do
let(:expected_year) { 2022 } let(:expected_year) { current_collection_start_year }
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(false)
end
it "redirects to /prepare-your-file" do it "redirects to /prepare-your-file" do
Timecop.freeze(2023, 1, 1) do
get "/lettings-logs/bulk-upload-logs/start", params: {} get "/lettings-logs/bulk-upload-logs/start", params: {}
expect(response).to redirect_to("/lettings-logs/bulk-upload-logs/prepare-your-file?form%5Byear%5D=#{expected_year}") expect(response).to redirect_to("/lettings-logs/bulk-upload-logs/prepare-your-file?form%5Byear%5D=#{expected_year}")
end end
end end
end
context "when in crossover period" do context "when in crossover period" do
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true)
end
it "redirects to /year" do it "redirects to /year" do
Timecop.freeze(2022, 6, 1) do
get "/lettings-logs/bulk-upload-logs/start", params: {} get "/lettings-logs/bulk-upload-logs/start", params: {}
expect(response).to redirect_to("/lettings-logs/bulk-upload-logs/year") expect(response).to redirect_to("/lettings-logs/bulk-upload-logs/year")
end end
end end
end end
end
describe "GET /lettings-logs/bulk-upload-logs/guidance" do describe "GET /lettings-logs/bulk-upload-logs/guidance" do
context "when not in crossover period" do context "when not in crossover period" do
let(:expected_year) { FormHandler.instance.forms["current_lettings"].start_date.year } before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(false)
end
it "shows guidance page with correct title" do it "shows guidance page with correct title" do
Timecop.freeze(2022, 1, 1) do get "/lettings-logs/bulk-upload-logs/guidance?form%5Byear%5D=#{current_collection_start_year}", params: {}
get "/lettings-logs/bulk-upload-logs/guidance?form%5Byear%5D=2022", params: {}
expect(response.body).to include("How to upload logs in bulk") expect(response.body).to include("How to upload logs in bulk")
end end
end end
end
context "when in crossover period" do context "when in crossover period" do
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true)
end
it "shows guidance page with correct title" do it "shows guidance page with correct title" do
Timecop.freeze(2023, 6, 1) do get "/lettings-logs/bulk-upload-logs/guidance?form%5Byear%5D=#{current_collection_start_year}", params: {}
get "/lettings-logs/bulk-upload-logs/guidance?form%5Byear%5D=2023", params: {}
expect(response.body).to include("How to upload logs in bulk") expect(response.body).to include("How to upload logs in bulk")
end end
end end
end end
end end
end

30
spec/requests/bulk_upload_sales_logs_controller_spec.rb

@ -1,6 +1,8 @@
require "rails_helper" require "rails_helper"
RSpec.describe BulkUploadSalesLogsController, type: :request do RSpec.describe BulkUploadSalesLogsController, type: :request do
include CollectionTimeHelper
let(:user) { create(:user) } let(:user) { create(:user) }
let(:organisation) { user.organisation } let(:organisation) { user.organisation }
@ -21,44 +23,51 @@ RSpec.describe BulkUploadSalesLogsController, type: :request do
end end
context "when not in crossover period" do context "when not in crossover period" do
let(:expected_year) { FormHandler.instance.forms["current_sales"].start_date.year } let(:expected_year) { current_collection_start_year }
before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false)
end
it "redirects to /prepare-your-file" do it "redirects to /prepare-your-file" do
Timecop.freeze(2022, 1, 1) do
get "/sales-logs/bulk-upload-logs/start", params: {} get "/sales-logs/bulk-upload-logs/start", params: {}
expect(response).to redirect_to("/sales-logs/bulk-upload-logs/prepare-your-file?form%5Byear%5D=#{expected_year}") expect(response).to redirect_to("/sales-logs/bulk-upload-logs/prepare-your-file?form%5Byear%5D=#{expected_year}")
end end
end end
end
context "when in crossover period" do context "when in crossover period" do
before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true)
end
it "redirects to /year" do it "redirects to /year" do
Timecop.freeze(2023, 6, 1) do
get "/sales-logs/bulk-upload-logs/start", params: {} get "/sales-logs/bulk-upload-logs/start", params: {}
expect(response).to redirect_to("/sales-logs/bulk-upload-logs/year") expect(response).to redirect_to("/sales-logs/bulk-upload-logs/year")
end end
end end
end end
end
describe "GET /sales-logs/bulk-upload-logs/guidance" do describe "GET /sales-logs/bulk-upload-logs/guidance" do
context "when not in crossover period" do context "when not in crossover period" do
let(:expected_year) { FormHandler.instance.forms["current_sales"].start_date.year } before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false)
end
it "shows guidance page with correct title" do it "shows guidance page with correct title" do
Timecop.freeze(2022, 1, 1) do get "/sales-logs/bulk-upload-logs/guidance?form%5Byear%5D=#{current_collection_start_year}", params: {}
get "/sales-logs/bulk-upload-logs/guidance?form%5Byear%5D=2022", params: {}
expect(response.body).to include("How to upload logs in bulk") expect(response.body).to include("How to upload logs in bulk")
end end
end end
end
context "when in crossover period" do context "when in crossover period" do
before do
allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true)
end
it "shows guidance page with correct title" do it "shows guidance page with correct title" do
Timecop.freeze(2023, 6, 1) do
get "/sales-logs/bulk-upload-logs/guidance?form%5Byear%5D=2023", params: {} get "/sales-logs/bulk-upload-logs/guidance?form%5Byear%5D=2023", params: {}
expect(response.body).to include("How to upload logs in bulk") expect(response.body).to include("How to upload logs in bulk")
@ -66,4 +75,3 @@ RSpec.describe BulkUploadSalesLogsController, type: :request do
end end
end end
end end
end

Loading…
Cancel
Save