diff --git a/spec/models/form/subsection_spec.rb b/spec/models/form/subsection_spec.rb index d02f8099b..8a4b9d3d7 100644 --- a/spec/models/form/subsection_spec.rb +++ b/spec/models/form/subsection_spec.rb @@ -34,7 +34,7 @@ RSpec.describe Form::Subsection, type: :model do end 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 expect(subsection.status(lettings_log)).to eq(:in_progress) diff --git a/spec/models/forms/bulk_upload_lettings/year_spec.rb b/spec/models/forms/bulk_upload_lettings/year_spec.rb index 241a0a918..70d6af4bf 100644 --- a/spec/models/forms/bulk_upload_lettings/year_spec.rb +++ b/spec/models/forms/bulk_upload_lettings/year_spec.rb @@ -4,13 +4,15 @@ RSpec.describe Forms::BulkUploadLettings::Year do subject(:form) { described_class.new } describe "#options" do + before do + 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 + context "when in a crossover period" do before do - Timecop.freeze(2024, 4, 1) - end - - after do - Timecop.return + allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true) end 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 before do - Timecop.freeze(2024, 3, 1) - end - - after do - Timecop.return + allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(false) end it "returns the current year" do - expect(form.options.map(&:id)).to eql([2023]) - expect(form.options.map(&:name)).to eql(%w[2023/2024]) + expect(form.options.map(&:id)).to eql([2024]) + expect(form.options.map(&:name)).to eql(%w[2024/2025]) end end context "when allow_future_form_use is toggled on" 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) end - after do - Timecop.return - end - it "returns current and next years" do - expect(form.options.map(&:id)).to eql([2023, 2024]) - expect(form.options.map(&:name)).to eql(%w[2023/2024 2024/2025]) + expect(form.options.map(&:id)).to eql([2024, 2025]) + expect(form.options.map(&:name)).to eql(%w[2024/2025 2025/2026]) end end end diff --git a/spec/models/forms/bulk_upload_sales/year_spec.rb b/spec/models/forms/bulk_upload_sales/year_spec.rb index 654a5760e..2cfa8babc 100644 --- a/spec/models/forms/bulk_upload_sales/year_spec.rb +++ b/spec/models/forms/bulk_upload_sales/year_spec.rb @@ -4,13 +4,15 @@ RSpec.describe Forms::BulkUploadSales::Year do subject(:form) { described_class.new } describe "#options" do + before do + 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 + context "when in a crossover period" do before do - Timecop.freeze(2024, 4, 1) - end - - after do - Timecop.return + allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true) end 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 before do - Timecop.freeze(2024, 3, 1) - end - - after do - Timecop.return + allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false) end it "returns the current year" do - expect(form.options.map(&:id)).to eql([2023]) - expect(form.options.map(&:name)).to eql(%w[2023/2024]) + expect(form.options.map(&:id)).to eql([2024]) + expect(form.options.map(&:name)).to eql(%w[2024/2025]) end end context "when allow_future_form_use is toggled on" 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) end @@ -45,8 +43,8 @@ RSpec.describe Forms::BulkUploadSales::Year do end it "returns current and next years" do - expect(form.options.map(&:id)).to eql([2023, 2024]) - expect(form.options.map(&:name)).to eql(%w[2023/2024 2024/2025]) + expect(form.options.map(&:id)).to eql([2024, 2025]) + expect(form.options.map(&:name)).to eql(%w[2024/2025 2025/2026]) end end end diff --git a/spec/models/location_deactivation_period_spec.rb b/spec/models/location_deactivation_period_spec.rb index 4e0bb3384..e2cfd5758 100644 --- a/spec/models/location_deactivation_period_spec.rb +++ b/spec/models/location_deactivation_period_spec.rb @@ -2,31 +2,34 @@ require "rails_helper" RSpec.describe LocationDeactivationPeriod do let(:validator) { LocationDeactivationPeriodValidator.new } - let(:location) { FactoryBot.create(:location, startdate: now - 2.years) } - let(:record) { FactoryBot.create(:location_deactivation_period, deactivation_date: now, location:) } + let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) } + 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 - around do |example| - Timecop.freeze(now) do - example.run - end + before do + allow(FormHandler.instance).to receive(:previous_collection_start_date).and_return(previous_collection_start_date) + allow(FormHandler.instance).to receive(:current_collection_start_date).and_return(current_collection_start_date) end 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 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 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 context "with a deactivation date in the current collection period" 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 validator.validate(record) expect(record.errors).to be_empty @@ -35,11 +38,13 @@ RSpec.describe LocationDeactivationPeriod do end 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 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 validator.validate(record) 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 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 validator.validate(record) 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 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 validator.validate(record) 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 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 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 validator.validate(record) start_date = startdate.to_formatted_s(:govuk_date) diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index 50dc4b4f4..44118bcc6 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -99,11 +99,7 @@ RSpec.describe Location, type: :model do let(:today) { Time.zone.local(2022, 4, 1) } before do - Timecop.freeze(today) - end - - after do - Timecop.unfreeze + allow(Time).to receive(:now).and_return(today) end 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) } before do - Timecop.freeze(today) - end - - after do - Timecop.unfreeze + allow(Time).to receive(:now).and_return(today) end it "returns a list of local authorities" do @@ -842,15 +834,7 @@ RSpec.describe Location, type: :model do end describe "status" do - let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 1)) } - - before do - Timecop.freeze(2022, 6, 7) - end - - after do - Timecop.unfreeze - end + let(:location) { FactoryBot.build(:location, startdate: Time.zone.today - 2.months) } context "when location is not confirmed" do it "returns incomplete " do @@ -865,7 +849,7 @@ RSpec.describe Location, type: :model do end 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! expect(location.status).to eq(:deactivating_soon) end @@ -876,25 +860,25 @@ RSpec.describe Location, type: :model do end 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! expect(location.status).to eq(:deactivated) end 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! expect(location.status).to eq(:deactivated) end 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! expect(location.status).to eq(:reactivating_soon) end 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! expect(location.status).to eq(:activating_soon) end @@ -902,7 +886,7 @@ RSpec.describe Location, type: :model do context "when there have been previous deactivations" 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! end @@ -911,39 +895,37 @@ RSpec.describe Location, type: :model do end 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! expect(location.status).to eq(:deactivating_soon) end 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! expect(location.status).to eq(:deactivated) end 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! expect(location.status).to eq(:deactivated) end 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.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.yesterday, reactivation_date: Time.zone.tomorrow, location:) location.save! expect(location.status).to eq(:reactivating_soon) end 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.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), location:) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today - 1.month, reactivation_date: Time.zone.today + 2.days, location:) location.save! expect(location.status).to eq(:reactivating_soon) end 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! expect(location.status).to eq(:activating_soon) end @@ -951,24 +933,16 @@ RSpec.describe Location, type: :model do end describe "status_at" do - let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 4, 1)) } - - before do - Timecop.freeze(2022, 6, 7) - end - - after do - Timecop.unfreeze - end + let(:location) { FactoryBot.build(:location, startdate: Time.zone.today - 3.months) } context "when there have been previous deactivations" 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! end 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 @@ -977,28 +951,23 @@ RSpec.describe Location, type: :model do let!(:deactivated_organisation) { FactoryBot.create(:organisation, active: false) } 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!(:incomplete_location) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.local(2022, 4, 1)) } - let!(:incomplete_location_with_nil_confirmed) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.local(2022, 4, 1), confirmed: nil) } - let!(:active_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } - let(:deactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } - let(:deactivated_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } - let(:reactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) } - let!(:activating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 7, 7)) } + 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.today - 3.months, confirmed: nil) } + let!(:active_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) } + let(:deactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) } + let(:deactivated_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) } + let(:reactivating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today - 3.months) } + let!(:activating_soon_location) { FactoryBot.create(:location, startdate: Time.zone.today + 1.day) } before do - Timecop.freeze(2022, 6, 7) - FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8), location: deactivating_soon_location) + FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today + 1.month, location: deactivating_soon_location) 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! - 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! end - after do - Timecop.unfreeze - end - context "when filtering by incomplete status" do it "returns only incomplete locations" do expect(described_class.filter_by_status(%w[incomplete]).count).to eq(2) diff --git a/spec/models/scheme_deactivation_period_spec.rb b/spec/models/scheme_deactivation_period_spec.rb index eb46ee62f..9e59399e9 100644 --- a/spec/models/scheme_deactivation_period_spec.rb +++ b/spec/models/scheme_deactivation_period_spec.rb @@ -2,31 +2,34 @@ require "rails_helper" RSpec.describe SchemeDeactivationPeriod do let(:validator) { SchemeDeactivationPeriodValidator.new } - let(:scheme) { FactoryBot.create(:scheme, created_at: now - 2.years) } - let(:record) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: now, scheme:) } + let(:previous_collection_start_date) { Time.zone.local(2022, 4, 1) } + 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 - around do |example| - Timecop.freeze(now) do - example.run - end + before do + allow(FormHandler.instance).to receive(:previous_collection_start_date).and_return(previous_collection_start_date) + allow(FormHandler.instance).to receive(:current_collection_start_date).and_return(current_collection_start_date) end 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 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 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 context "with a deactivation date in the current collection period" 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 validator.validate(record) expect(record.errors[:deactivation_date]).to be_empty @@ -35,11 +38,13 @@ RSpec.describe SchemeDeactivationPeriod do end 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 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 validator.validate(record) 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 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 validator.validate(record) 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 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 validator.validate(record) expect(record.errors[:deactivation_date]).to be_empty diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index c84be444f..75fe66833 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -216,11 +216,6 @@ RSpec.describe Scheme, type: :model do before do FactoryBot.create(:location, scheme:) - Timecop.freeze(2022, 6, 7) - end - - after do - Timecop.unfreeze end context "when there have not been any previous deactivations" do @@ -229,7 +224,7 @@ RSpec.describe Scheme, type: :model do end 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 expect(scheme.status).to eq(:deactivating_soon) end @@ -240,34 +235,32 @@ RSpec.describe Scheme, type: :model do end 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 expect(scheme.status).to eq(:deactivated) end 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 expect(scheme.status).to eq(:deactivated) end 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! expect(scheme.status).to eq(:reactivating_soon) end it "returns activating soon if the scheme has a future startdate" do - Timecop.freeze(2022, 6, 4) - scheme.startdate = Time.zone.local(2022, 7, 7) - scheme.save! + scheme.startdate = Time.zone.today + 2.weeks expect(scheme.status).to eq(:activating_soon) end end context "when there have been previous deactivations" 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 it "returns active if the scheme has no relevant deactivation records" do @@ -275,39 +268,37 @@ RSpec.describe Scheme, type: :model do end 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 expect(scheme.status).to eq(:deactivating_soon) end 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 expect(scheme.status).to eq(:deactivated) end 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 expect(scheme.status).to eq(:deactivated) end 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.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today, reactivation_date: Time.zone.tomorrow, scheme:) scheme.save! expect(scheme.status).to eq(:reactivating_soon) end 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.local(2022, 5, 5), reactivation_date: Time.zone.local(2022, 6, 2), scheme:) + FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 2.months, reactivation_date: Time.zone.today + 2.days, scheme:) scheme.save! expect(scheme.status).to eq(:reactivating_soon) end 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! expect(scheme.status).to eq(:activating_soon) end @@ -327,20 +318,15 @@ RSpec.describe Scheme, type: :model do before do FactoryBot.create(:location, scheme:) - Timecop.freeze(2022, 6, 7) - end - - after do - Timecop.unfreeze end context "when there have been previous deactivations" 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 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 diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index 5b9d1a436..75907b5bc 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/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) } 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 record.startdate = Time.zone.local(0, 7, 1) date_validator.validate_startdate(record) diff --git a/spec/models/validations/household_validations_spec.rb b/spec/models/validations/household_validations_spec.rb index 4254ede6d..7c8e023d0 100644 --- a/spec/models/validations/household_validations_spec.rb +++ b/spec/models/validations/household_validations_spec.rb @@ -4,16 +4,8 @@ RSpec.describe Validations::HouseholdValidations do subject(:household_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::HouseholdValidations } } - let(:log_date) { Time.zone.now } - let(:record) { FactoryBot.create(:lettings_log, :setup_completed, startdate: log_date) } - - before do - Timecop.freeze(log_date + 1) - end - - after do - Timecop.return - end + let(:startdate) { Time.zone.now } + let(:record) { FactoryBot.build(:lettings_log, :setup_completed, startdate:, assigned_to: create(:user)) } describe "reasonable preference validations" do context "when reasonable preference is not given" do @@ -59,7 +51,7 @@ RSpec.describe Validations::HouseholdValidations do end 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 record.reason = 20 @@ -70,7 +62,7 @@ RSpec.describe Validations::HouseholdValidations do end 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 it "validates that the reason doesn't match phrase indicating homelessness" do @@ -277,7 +269,7 @@ RSpec.describe Validations::HouseholdValidations do end 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 record.relat2 = "P" @@ -300,17 +292,7 @@ RSpec.describe Validations::HouseholdValidations do describe "#validate_person_age_matches_relationship" do context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2023, 4, 1) } context "when the household contains a person under 16" do it "validates that person must be a child of the tenant" do @@ -334,17 +316,7 @@ RSpec.describe Validations::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2024, 4, 1) } it "does not add an error is person under 16 is a partner" do record.age2 = 14 @@ -366,17 +338,7 @@ RSpec.describe Validations::HouseholdValidations do describe "#validate_person_age_matches_economic_status" do context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2023, 4, 1) } context "when the household contains a person under 16" do it "validates that person's economic status must be Child" do @@ -411,17 +373,7 @@ RSpec.describe Validations::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2024, 4, 1) } it "does not run the validation" do record.age2 = 14 @@ -437,17 +389,7 @@ RSpec.describe Validations::HouseholdValidations do describe "#validate_person_age_and_relationship_matches_economic_status" do context "with 2023 logs" do - let(:log_date) { Time.zone.local(2023, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2023, 4, 1) } 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 @@ -519,17 +461,7 @@ RSpec.describe Validations::HouseholdValidations do end context "with 2024 logs" do - let(:log_date) { Time.zone.local(2024, 4, 1) } - - before do - Timecop.freeze(log_date) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:startdate) { Time.zone.local(2024, 4, 1) } context "when the household contains a tenant’s child between the ages of 16 and 19" do it "does not add an error" do diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 316c2d175..6ae34c3df 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/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 } } 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 before do @@ -150,7 +150,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end 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 record.mortgageused = 1 @@ -168,7 +168,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end 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 record.stairbought = 20 @@ -202,7 +202,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end 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 before do @@ -249,7 +249,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end 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 record.mscharge = 2345 @@ -265,7 +265,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end 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 record.stairbought = 9 @@ -307,7 +307,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end 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 before do @@ -352,17 +352,10 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_equity_in_range_for_year_and_type" do - let(:record) { FactoryBot.create(:sales_log, saledate: now) } - - around do |example| - Timecop.freeze(now) do - example.run - end - Timecop.return - end + let(:record) { FactoryBot.build(:sales_log, saledate:) } 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 record.type = 2 @@ -397,7 +390,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end 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 record.type = 2 @@ -433,19 +426,10 @@ RSpec.describe Validations::Sales::FinancialValidations do end describe "#validate_equity_less_than_staircase_difference" do - let(:record) { FactoryBot.create(:sales_log, saledate: now) } - - around do |example| - Timecop.freeze(now) do - Singleton.__init__(FormHandler) - example.run - end - Timecop.return - Singleton.__init__(FormHandler) - end + let(:record) { FactoryBot.build(:sales_log, saledate:) } 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 record.stairbought = 2 @@ -457,7 +441,7 @@ RSpec.describe Validations::Sales::FinancialValidations do end 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 record.stairbought = 2 diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 89680fc1a..c12d7c5b3 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -4,10 +4,12 @@ RSpec.describe Validations::Sales::HouseholdValidations do subject(:household_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } } - let(:record) { build(:sales_log, saledate: log_date) } - let(:log_date) { Time.zone.local(2023, 4, 1) } + let(:record) { build(:sales_log, saledate:) } + let(:saledate) { Time.zone.now } describe "#validate_partner_count" do + let(:saledate) { Time.zone.local(2023, 4, 1) } + it "validates that only 1 partner exists" do record.relat2 = "P" record.relat3 = "P" @@ -28,18 +30,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 - 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 it "expects that person is a child of the tenant" do @@ -73,7 +65,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 record.age2 = 14 @@ -94,18 +86,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 - 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 record.age2 = 14 @@ -137,7 +119,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 record.age2 = 14 @@ -152,18 +134,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 - 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 record.age1 = 30 @@ -190,7 +162,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 record.age1 = 20 @@ -205,18 +177,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 - 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 record.age2 = 18 @@ -266,7 +228,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 it "does not add an error" do @@ -316,7 +278,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 let(:buylivein) { 1 } @@ -349,7 +311,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 sales_log.buy2livein = 2 @@ -377,21 +339,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end describe "#validate_buyer1_previous_tenure" do - let(:record) { build(:sales_log) } - - 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 + let(:record) { build(:sales_log, saledate:, ownershipsch: 2) } it "adds an error when previous tenure is not valid" do [3, 4, 5, 6, 7, 9, 0].each do |prevten| @@ -437,7 +385,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 record.ownershipsch = 2 @@ -452,18 +400,8 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 - 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 record.jointpur = 1 @@ -476,7 +414,7 @@ RSpec.describe Validations::Sales::HouseholdValidations do end 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 record.ecstat1 = 9 diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 123122a0c..44245f73f 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -725,17 +725,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end describe "#validate_stairbought" do - let(:now) { Time.zone.local(2024, 4, 4) } - - before do - Timecop.freeze(now) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - Singleton.__init__(FormHandler) - end + let(:saledate) { Time.zone.local(2024, 4, 4) } [ ["Shared Ownership (new model lease)", 30, 90], @@ -748,7 +738,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do ["Older Persons Shared Ownership", 24, 50], ].each do |label, type, max| 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 record.stairbought = max - 1 @@ -781,8 +771,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end context "when the collection year is before 2024" do - let(:record) { build(:sales_log, ownershipsch: 1, type: 24, saledate: now, stairbought: 90) } - let(:now) { Time.zone.local(2023, 4, 4) } + let(:record) { build(:sales_log, ownershipsch: 1, type: 24, saledate:, stairbought: 90) } + let(:saledate) { Time.zone.local(2023, 4, 4) } it "does not add an error" do sale_information_validator.validate_stairbought(record) @@ -793,15 +783,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end describe "#validate_discount_and_value" do - let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 50, ownershipsch: 2, type: 9, saledate: now) } - let(:now) { Time.zone.local(2024, 4, 1) } - - around do |example| - Timecop.freeze(now) do - example.run - end - Timecop.return - end + let(:record) { FactoryBot.build(:sales_log, value: 200_000, discount: 50, ownershipsch: 2, type: 9, saledate:) } + let(:saledate) { Time.zone.local(2024, 4, 1) } context "with a log in the 24/25 collection year" do context "when in London" do @@ -870,19 +853,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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) } - - around do |example| - Timecop.freeze(now) do - Singleton.__init__(FormHandler) - example.run - end - Timecop.return - Singleton.__init__(FormHandler) - end + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate:) } 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 "and it is not a staircase transaction" do @@ -952,7 +926,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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 sale_information_validator.validate_non_staircasing_mortgage(record) @@ -966,7 +940,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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 sale_information_validator.validate_non_staircasing_mortgage(record) @@ -1052,7 +1026,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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 sale_information_validator.validate_non_staircasing_mortgage(record) @@ -1066,7 +1040,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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 sale_information_validator.validate_non_staircasing_mortgage(record) @@ -1082,8 +1056,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when it is a 2023 log" do - let(:now) { 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(: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:) } it "does not add an error" do sale_information_validator.validate_non_staircasing_mortgage(record) @@ -1098,19 +1072,10 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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) } - - around do |example| - Timecop.freeze(now) do - Singleton.__init__(FormHandler) - example.run - end - Timecop.return - Singleton.__init__(FormHandler) - end + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, mortgage: 10_000, deposit: 5_000, value: 30_000, stairbought: 28, ownershipsch: 1, type: 30, saledate:) } 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 "and it is a staircase transaction" do @@ -1180,7 +1145,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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 sale_information_validator.validate_staircasing_mortgage(record) @@ -1194,7 +1159,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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 sale_information_validator.validate_staircasing_mortgage(record) @@ -1209,8 +1174,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when it is a 2023 log" do - let(:now) { 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(: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:) } it "does not add an error" do 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 "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 record.mortgageused = 2 @@ -1299,7 +1264,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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 sale_information_validator.validate_staircasing_mortgage(record) @@ -1313,7 +1278,7 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end 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 sale_information_validator.validate_staircasing_mortgage(record) @@ -1328,8 +1293,8 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end context "when it is a 2023 log" do - let(:now) { 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(: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:) } it "does not add an error" do sale_information_validator.validate_staircasing_mortgage(record) diff --git a/spec/models/validations/sales/setup_validations_spec.rb b/spec/models/validations/sales/setup_validations_spec.rb index 526024ae3..005adfaaf 100644 --- a/spec/models/validations/sales/setup_validations_spec.rb +++ b/spec/models/validations/sales/setup_validations_spec.rb @@ -8,12 +8,7 @@ RSpec.describe Validations::Sales::SetupValidations do describe "#validate_saledate_collection_year" do context "with sales_in_crossover_period == false" do before do - Timecop.freeze(Time.zone.local(2023, 1, 10)) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return + allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(false) end context "when saledate is blank" do @@ -26,8 +21,8 @@ RSpec.describe Validations::Sales::SetupValidations do end end - context "when saledate is in the 22/23 collection year" do - let(:record) { build(:sales_log, saledate: Time.zone.local(2023, 1, 1)) } + context "when saledate is in the open collection year" do + let(:record) { build(:sales_log, :saledate_today) } it "does not add an error" do setup_validator.validate_saledate_collection_year(record) @@ -36,34 +31,30 @@ RSpec.describe Validations::Sales::SetupValidations do end end - context "when saledate is before the 22/23 collection year" do - let(:record) { build(:sales_log, saledate: Time.zone.local(2020, 1, 1)) } + context "when saledate is before the open collection year" do + let(:record) { build(:sales_log, saledate: Time.zone.today - 3.years) } it "adds error" do 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 - context "when saledate is after the 22/23 collection year" do - let(:record) { build(:sales_log, saledate: Time.zone.local(2025, 4, 1)) } + context "when saledate is after the open collection year" do + let(:record) { build(:sales_log, saledate: Time.zone.today + 2.years) } it "adds error" do 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 context "with sales_in_crossover_period == true" do - around do |example| - Timecop.freeze(Time.zone.local(2024, 5, 1)) do - Singleton.__init__(FormHandler) - example.run - end - Timecop.return + before do + allow(FormHandler.instance).to receive(:sales_in_crossover_period?).and_return(true) end context "when saledate is blank" do @@ -107,20 +98,19 @@ RSpec.describe Validations::Sales::SetupValidations do end 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 allow(Time).to receive(:now).and_return(Time.zone.local(2025, 1, 8)) end - it "cannot create new logs for the previous collection year" do - record.update!(saledate: nil) - record.saledate = Time.zone.local(2024, 1, 1) + it "cannot create new logs for the archived collection year" do + record.saledate = Time.zone.local(2023, 1, 1) 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 - 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.save!(validate: false) 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)) 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(2024, 1, 1) + record.saledate = Time.zone.local(2023, 1, 1) 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 - it "cannot edit already created logs for the previous collection year" do - record.saledate = Time.zone.local(2024, 1, 2) + it "cannot edit already created logs for the archived collection year" do + record.saledate = Time.zone.local(2023, 1, 2) 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) - 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 @@ -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(: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 merged_organisation.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2)) 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(:merged_organisation) { create(:organisation, name: "Merged org") } - around do |example| - Timecop.freeze(Time.zone.local(2023, 5, 1)) + before do merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:) - example.run - Timecop.return end context "and owning organisation is no longer active" do diff --git a/spec/models/validations/setup_validations_spec.rb b/spec/models/validations/setup_validations_spec.rb index aca3eea7d..5e7705c6d 100644 --- a/spec/models/validations/setup_validations_spec.rb +++ b/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 before do - Timecop.freeze(2024, 3, 1) + allow(Time).to receive(:now).and_return(Time.zone.local(2024, 3, 1)) end it "adds an error to startdate" do @@ -164,18 +164,13 @@ RSpec.describe Validations::SetupValidations do end 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_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_2) { create(:organisation, name: "Merged org 2") } 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_2.update!(absorbing_organisation:, merge_date: Time.zone.local(2023, 2, 2)) end @@ -431,9 +426,8 @@ RSpec.describe Validations::SetupValidations do before do create(:location, scheme:) - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:) - Timecop.return + scheme_deactivation_period = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), scheme:) + scheme_deactivation_period.save!(validate: false) scheme.reload end @@ -461,9 +455,8 @@ RSpec.describe Validations::SetupValidations do before do create(:location, scheme:) - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) - Timecop.return + scheme_deactivation_period = build(: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) scheme.reload end @@ -491,11 +484,12 @@ RSpec.describe Validations::SetupValidations do before do create(:location, scheme:) - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(: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, 4), reactivation_date: Time.zone.local(2022, 8, 4), scheme:) - create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), scheme:) - Timecop.return + scheme_deactivation_period = build(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), 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:) + 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:) + scheme_deactivation_period.save!(validate: false) + scheme_deactivation_period_2.save!(validate: false) + scheme_deactivation_period_3.save!(validate: false) scheme.reload end @@ -525,9 +519,8 @@ RSpec.describe Validations::SetupValidations do let(:location) { create(:location, scheme:) } before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) - Timecop.return + location_deactivation_period = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), location:) + location_deactivation_period.save!(validate: false) location.reload end @@ -555,9 +548,8 @@ RSpec.describe Validations::SetupValidations do let(:location) { create(:location, scheme:) } before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) - Timecop.return + location_deactivation_period = build(: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) location.reload end @@ -585,11 +577,12 @@ RSpec.describe Validations::SetupValidations do let(:location) { create(:location, scheme:) } before do - Timecop.freeze(Time.zone.local(2023, 11, 10)) - create(: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, 4), reactivation_date: Time.zone.local(2022, 8, 4), location:) - create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), location:) - Timecop.return + location_deactivation_period = build(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 2), reactivation_date: Time.zone.local(2022, 8, 3), 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:) + 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:) + location_deactivation_period.save!(validate: false) + location_deactivation_period_2.save!(validate: false) + location_deactivation_period_3.save!(validate: false) location.reload 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(:merged_organisation) { create(:organisation, name: "Merged org") } - around do |example| - Timecop.freeze(Time.zone.local(2023, 5, 1)) - merged_organisation.update!(merge_date: Time.zone.local(2023, 2, 2), absorbing_organisation:) - example.run - Timecop.return + before do + merged_organisation.merge_date = Time.zone.local(2023, 2, 2) + merged_organisation.absorbing_organisation = absorbing_organisation + merged_organisation.save!(validate: false) end context "and owning organisation is no longer active" do diff --git a/spec/models/validations/soft_validations_spec.rb b/spec/models/validations/soft_validations_spec.rb index 601c5640e..6a3d9e051 100644 --- a/spec/models/validations/soft_validations_spec.rb +++ b/spec/models/validations/soft_validations_spec.rb @@ -1,17 +1,8 @@ require "rails_helper" RSpec.describe Validations::SoftValidations do - let(:organisation) { FactoryBot.create(:organisation, provider_type: "PRP") } - let(:record) { FactoryBot.create(:lettings_log, owning_organisation: organisation) } - - before do - Timecop.freeze(Time.zone.local(2021, 10, 10)) - Singleton.__init__(FormHandler) - end - - after do - Timecop.return - end + let(:organisation) { FactoryBot.build(:organisation, provider_type: "PRP", id: 123) } + let(:record) { FactoryBot.build(:lettings_log, owning_organisation: organisation) } describe "rent min max validations" do before do @@ -32,7 +23,7 @@ RSpec.describe Validations::SoftValidations do record.rent_type = 0 record.beds = 1 record.period = 1 - record.startdate = Time.zone.today + record.startdate = Time.zone.local(2021, 10, 10) end context "when validating soft min" do @@ -84,7 +75,8 @@ RSpec.describe Validations::SoftValidations do describe "retirement soft validations" do before do - record.update!(age1:, ecstat1:) + record.age1 = age1 + record.ecstat1 = ecstat1 end 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 context "when there are no female tenants" 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 end end context "when there are no female tenants and age of other tenants is unknown" 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 end end context "when female tenants are under 16" 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 end end context "when female tenants are over 50" 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 end end context "when female tenants are outside of soft validation ranges" 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.female_in_pregnant_household_in_soft_validation_range?).to be false end @@ -199,7 +213,8 @@ RSpec.describe Validations::SoftValidations do context "when the information about the tenants is not given" 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.female_in_pregnant_household_in_soft_validation_range?).to be false end @@ -207,48 +222,36 @@ RSpec.describe Validations::SoftValidations do end 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 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 end end 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 - 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 end end end 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 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 end end context "when the void date is less than 2 years before the tenancy start date" 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 end end diff --git a/spec/policies/location_policy_spec.rb b/spec/policies/location_policy_spec.rb index efc56978b..f7a86d9af 100644 --- a/spec/policies/location_policy_spec.rb +++ b/spec/policies/location_policy_spec.rb @@ -44,18 +44,11 @@ RSpec.describe LocationPolicy do context "with deactivated location" do before do - location.location_deactivation_periods << create(:location_deactivation_period, deactivation_date: Time.zone.local(2024, 4, 10), location:) - location.save! - Timecop.freeze(Time.utc(2024, 4, 10)) - log = create(:lettings_log, scheme: location.scheme, location:) - log.startdate = Time.zone.local(2022, 10, 10) + location.location_deactivation_periods << build(:location_deactivation_period, deactivation_date: Time.zone.today, location:) + log = build(:lettings_log, scheme: location.scheme, location:, startdate: Time.zone.today - 2.years) log.save!(validate: false) end - after do - Timecop.unfreeze - end - context "and associated logs in editable collection period" do before do create(:lettings_log, scheme: location.scheme, location:) diff --git a/spec/policies/scheme_policy_spec.rb b/spec/policies/scheme_policy_spec.rb index c22be81bb..311208efb 100644 --- a/spec/policies/scheme_policy_spec.rb +++ b/spec/policies/scheme_policy_spec.rb @@ -46,21 +46,15 @@ RSpec.describe SchemePolicy do context "with deactivated scheme" 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! - Timecop.freeze(Time.utc(2024, 4, 10)) - log = create(:lettings_log, :sh, owning_organisation: scheme.owning_organisation, scheme:) - log.startdate = Time.zone.local(2022, 10, 10) + log = build(:lettings_log, :sh, owning_organisation: scheme.owning_organisation, scheme:, startdate: Time.zone.today - 2.years) log.save!(validate: false) end - after do - Timecop.unfreeze - end - context "and associated logs in editable collection period" 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 it "does not allow deleting a scheme as a provider" do diff --git a/spec/policies/user_policy_spec.rb b/spec/policies/user_policy_spec.rb index 22bec7fc9..e35e078e4 100644 --- a/spec/policies/user_policy_spec.rb +++ b/spec/policies/user_policy_spec.rb @@ -144,19 +144,13 @@ RSpec.describe UserPolicy do let(:user) { create(:user, active: false) } before do - Timecop.freeze(Time.utc(2024, 4, 10)) - log = create(:lettings_log, owning_organisation: user.organisation, assigned_to: user) - log.startdate = Time.zone.local(2022, 10, 10) + log = build(:lettings_log, owning_organisation: user.organisation, assigned_to: user, startdate: Time.zone.today - 2.years) log.save!(validate: false) end - after do - Timecop.unfreeze - end - context "and associated logs in editable collection period" 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 it "does not allow deleting a user as a provider" do diff --git a/spec/requests/bulk_upload_lettings_logs_controller_spec.rb b/spec/requests/bulk_upload_lettings_logs_controller_spec.rb index afc5577c3..d9a9a508d 100644 --- a/spec/requests/bulk_upload_lettings_logs_controller_spec.rb +++ b/spec/requests/bulk_upload_lettings_logs_controller_spec.rb @@ -1,6 +1,8 @@ require "rails_helper" RSpec.describe BulkUploadLettingsLogsController, type: :request do + include CollectionTimeHelper + let(:user) { create(:user) } let(:organisation) { user.organisation } @@ -21,48 +23,54 @@ RSpec.describe BulkUploadLettingsLogsController, type: :request do end 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 - 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}") - end + expect(response).to redirect_to("/lettings-logs/bulk-upload-logs/prepare-your-file?form%5Byear%5D=#{expected_year}") end end 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 - 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") - end + expect(response).to redirect_to("/lettings-logs/bulk-upload-logs/year") end end end describe "GET /lettings-logs/bulk-upload-logs/guidance" 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 - Timecop.freeze(2022, 1, 1) do - get "/lettings-logs/bulk-upload-logs/guidance?form%5Byear%5D=2022", params: {} + get "/lettings-logs/bulk-upload-logs/guidance?form%5Byear%5D=#{current_collection_start_year}", params: {} - expect(response.body).to include("How to upload logs in bulk") - end + expect(response.body).to include("How to upload logs in bulk") end end 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 - Timecop.freeze(2023, 6, 1) do - get "/lettings-logs/bulk-upload-logs/guidance?form%5Byear%5D=2023", params: {} + get "/lettings-logs/bulk-upload-logs/guidance?form%5Byear%5D=#{current_collection_start_year}", params: {} - expect(response.body).to include("How to upload logs in bulk") - end + expect(response.body).to include("How to upload logs in bulk") end end end diff --git a/spec/requests/bulk_upload_sales_logs_controller_spec.rb b/spec/requests/bulk_upload_sales_logs_controller_spec.rb index c603ec821..6fcf7c15d 100644 --- a/spec/requests/bulk_upload_sales_logs_controller_spec.rb +++ b/spec/requests/bulk_upload_sales_logs_controller_spec.rb @@ -1,6 +1,8 @@ require "rails_helper" RSpec.describe BulkUploadSalesLogsController, type: :request do + include CollectionTimeHelper + let(:user) { create(:user) } let(:organisation) { user.organisation } @@ -21,48 +23,54 @@ RSpec.describe BulkUploadSalesLogsController, type: :request do end 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 - 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}") - end + expect(response).to redirect_to("/sales-logs/bulk-upload-logs/prepare-your-file?form%5Byear%5D=#{expected_year}") end end 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 - 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") - end + expect(response).to redirect_to("/sales-logs/bulk-upload-logs/year") end end end describe "GET /sales-logs/bulk-upload-logs/guidance" 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 - Timecop.freeze(2022, 1, 1) do - get "/sales-logs/bulk-upload-logs/guidance?form%5Byear%5D=2022", params: {} + get "/sales-logs/bulk-upload-logs/guidance?form%5Byear%5D=#{current_collection_start_year}", params: {} - expect(response.body).to include("How to upload logs in bulk") - end + expect(response.body).to include("How to upload logs in bulk") end end 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 - 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") - end + expect(response.body).to include("How to upload logs in bulk") end end end