Browse Source

test deactivation period related behaviour

pull/2995/head
Carolyn 2 months ago
parent
commit
63d270d3a0
  1. 72
      spec/models/location_deactivation_period_spec.rb
  2. 10
      spec/models/location_spec.rb
  3. 80
      spec/models/scheme_deactivation_period_spec.rb
  4. 10
      spec/models/scheme_spec.rb
  5. 41
      spec/models/validations/setup_validations_spec.rb
  6. 23
      spec/requests/locations_controller_spec.rb
  7. 247
      spec/requests/schemes_controller_spec.rb

72
spec/models/location_deactivation_period_spec.rb

@ -85,5 +85,77 @@ RSpec.describe LocationDeactivationPeriod do
end
end
end
context "when there is an open deactivation period less than six months in the future" do # validate_reactivation
let!(:location) { FactoryBot.build(:location, created_at: previous_collection_start_date - 2.years) }
let!(:existing_period) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 5.months, location:) }
context "when reactivation date is nil" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now, location:) }
it "adds an error" do
validator.validate(record)
expect(record.errors.count).to eq(1)
expect(record.errors[:reactivation_date_type]).to include("Select one of the options.")
end
end
context "when reactivation date is present" do
context "when reactivation date is before the existing period's start" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, location:) }
it "adds an error" do
validator.validate(record)
expect(record.errors[:reactivation_date].count).to eq(1)
expect(record.errors[:reactivation_date][0]).to match("The reactivation date must be on or after deactivation date.")
end
end
context "when reactivation date is after the existing period's start" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 6.months, location:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
end
end
context "when there is not an open deactivation period within six months" do # validate_deactivation
let!(:location) { FactoryBot.create(:location, created_at: previous_collection_start_date - 2.years) }
let!(:existing_period_1) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 7.months, reactivation_date: Time.zone.now + 8.months, location:) }
let!(:existing_period_2) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 1.month, reactivation_date: Time.zone.now + 2.months, location:) }
let!(:existing_period_3) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.now + 9.months, location:) }
context "when reactivation date is nil" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now, location:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
context "when reactivation date is present" do
context "when deactivation date is less than six months in the future" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, location:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
context "when deactivation date is more than six months in the future" do
let(:record) { FactoryBot.build(:location_deactivation_period, deactivation_date: Time.zone.now + 9.months, reactivation_date: Time.zone.now + 10.months, location:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
end
end
end
end

10
spec/models/location_spec.rb

@ -1082,6 +1082,16 @@ RSpec.describe Location, type: :model do
it "returns active if the location has no relevant deactivation records" do
expect(location.status_at(Time.zone.today - 2.months)).to eq(:active)
end
context "when the most recently created deactivation is not the current one" do
before do
FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.today - 80.days, reactivation_date: Time.zone.today - 70.days, location:)
end
it "returns reactivating_soon" do
expect(location.status_at(Time.zone.today - 3.days)).to eq(:reactivating_soon)
end
end
end
end

80
spec/models/scheme_deactivation_period_spec.rb

@ -4,8 +4,6 @@ RSpec.describe SchemeDeactivationPeriod do
let(:validator) { SchemeDeactivationPeriodValidator.new }
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
before do
@ -14,6 +12,9 @@ RSpec.describe SchemeDeactivationPeriod do
end
context "when not in a crossover period" do
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:) }
before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(false)
end
@ -38,6 +39,9 @@ RSpec.describe SchemeDeactivationPeriod do
end
context "when in a crossover period" do
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:) }
before do
allow(FormHandler.instance).to receive(:in_edit_crossover_period?).and_return(true)
end
@ -69,5 +73,77 @@ RSpec.describe SchemeDeactivationPeriod do
end
end
end
context "when there is an open deactivation period less than six months in the future" do # validate_reactivation
let!(:scheme) { FactoryBot.build(:scheme, created_at: previous_collection_start_date - 2.years) }
let!(:existing_period) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.now + 5.months, scheme:) }
context "when reactivation date is nil" do
let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now, scheme:) }
it "adds an error" do
validator.validate(record)
expect(record.errors.count).to eq(1)
expect(record.errors[:reactivation_date_type]).to include("Select one of the options.")
end
end
context "when reactivation date is present" do
context "when reactivation date is before the existing period's start" do
let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, scheme:) }
it "adds an error" do
validator.validate(record)
expect(record.errors[:reactivation_date].count).to eq(1)
expect(record.errors[:reactivation_date][0]).to match("The reactivation date must be on or after deactivation date.")
end
end
context "when reactivation date is after the existing period's start" do
let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 6.months, scheme:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
end
end
context "when there is not an open deactivation period within six months" do # validate_deactivation
let!(:scheme) { FactoryBot.create(:scheme, created_at: previous_collection_start_date - 2.years) }
let!(:existing_period_1) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.now + 7.months, reactivation_date: Time.zone.now + 8.months, scheme:) }
let!(:existing_period_2) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.now + 1.month, reactivation_date: Time.zone.now + 2.months, scheme:) }
let!(:existing_period_3) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.now + 9.months, scheme:) }
context "when reactivation date is nil" do
let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now, scheme:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
context "when reactivation date is present" do
context "when deactivation date is less than six months in the future" do
let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now + 3.months, reactivation_date: Time.zone.now + 4.months, scheme:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
context "when deactivation date is more than six months in the future" do
let(:record) { FactoryBot.build(:scheme_deactivation_period, deactivation_date: Time.zone.now + 9.months, reactivation_date: Time.zone.now + 10.months, scheme:) }
it "does not add an error" do
validator.validate(record)
expect(record.errors).to be_empty
end
end
end
end
end
end

10
spec/models/scheme_spec.rb

@ -470,6 +470,16 @@ RSpec.describe Scheme, type: :model do
it "returns active if the scheme has no relevant deactivation records" do
expect(scheme.status_at(Time.zone.today - 1.month)).to eq(:active)
end
context "when the most recently created deactivation is not the current one" do
before do
FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.today - 300.days, reactivation_date: Time.zone.today - 200.days, scheme:)
end
it "returns reactivating_soon" do
expect(scheme.status_at(Time.zone.today - 3.days)).to eq(:reactivating_soon)
end
end
end
end

41
spec/models/validations/setup_validations_spec.rb

@ -512,6 +512,47 @@ RSpec.describe Validations::SetupValidations do
end
end
context "with a scheme whose chronologically latest deactivation period is not most recently created" do
let(:scheme) { create(:scheme) }
before do
create(:location, scheme:)
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, 2, 4), reactivation_date: Time.zone.local(2022, 3, 4), scheme:)
scheme_deactivation_period.save!(validate: false)
scheme_deactivation_period_2.save!(validate: false)
scheme.reload
end
it "produces error when tenancy start date is during later deactivated scheme period" do
record.startdate = Time.zone.local(2022, 7, 5)
record.scheme = scheme
setup_validator.validate_scheme(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 August 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.lettings.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 August 2022"))
end
it "produces error when tenancy start date is during earlier deactivated scheme period" do
record.startdate = Time.zone.local(2022, 2, 5)
record.scheme = scheme
setup_validator.validate_scheme(record)
expect(record.errors["startdate"])
.to include(match I18n.t("validations.lettings.setup.startdate.scheme.reactivating_soon.startdate", name: scheme.service_name, date: "4 August 2022"))
expect(record.errors["scheme_id"])
.to include(match I18n.t("validations.lettings.setup.startdate.scheme.reactivating_soon.scheme_id", name: scheme.service_name, date: "4 August 2022"))
end
it "produces no error when tenancy start date is during an active scheme period" do
record.startdate = Time.zone.local(2022, 10, 1)
record.scheme = scheme
setup_validator.validate_scheme(record)
expect(record.errors["startdate"]).to be_empty
expect(record.errors["scheme_id"]).to be_empty
end
end
context "with a scheme with no locations active on the start date & no location set" do
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:) }

23
spec/requests/locations_controller_spec.rb

@ -2169,6 +2169,29 @@ RSpec.describe LocationsController, type: :request do
expect(page).to have_content(I18n.t("validations.location.reactivation.before_deactivation", date: "10 October 2022"))
end
end
context "when there is no open deactivation period" do
let(:params) { { location_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "8/9/2022" } } }
before do
location.location_deactivation_periods.clear
create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 5, 1), reactivation_date: Time.zone.local(2022, 7, 5), updated_at: Time.zone.local(2000, 1, 1), location:)
create(:location_deactivation_period, deactivation_date: Time.zone.local(2023, 1, 1), reactivation_date: Time.zone.local(2023, 4, 5), updated_at: Time.zone.local(2000, 1, 1), location:)
location.save!
patch "/schemes/#{scheme.id}/locations/#{location.id}/reactivate", params:
end
it "renders not found" do
expect(response).to have_http_status(:not_found)
end
it "does not update deactivation periods" do
location.reload
expect(location.location_deactivation_periods.count).to eq(2)
expect(location.location_deactivation_periods[0].updated_at).to eq(Time.zone.local(2000, 1, 1))
expect(location.location_deactivation_periods[1].updated_at).to eq(Time.zone.local(2000, 1, 1))
end
end
end
end

247
spec/requests/schemes_controller_spec.rb

@ -2850,15 +2850,13 @@ RSpec.describe SchemesController, type: :request do
end
end
context "and there already is a deactivation period" do
let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) }
context "and there already is an open deactivation period" do
before do
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:)
patch "/schemes/#{scheme.id}/deactivate", params:
end
it "updates existing scheme with valid deactivation date and renders scheme page" do
it "updates existing period with valid deactivation date and renders scheme page" do
follow_redirect!
follow_redirect!
expect(response).to have_http_status(:ok)
@ -2868,10 +2866,37 @@ RSpec.describe SchemesController, type: :request do
expect(scheme.scheme_deactivation_periods.first.deactivation_date).to eq(deactivation_date)
end
it "clears the scheme and scheme answers" do
it "clears the scheme answer" do
expect(lettings_log.scheme).to eq(scheme)
lettings_log.reload
expect(lettings_log.scheme).to eq(nil)
end
it "marks log as needing attention" do
expect(lettings_log.unresolved).to eq(nil)
lettings_log.reload
expect(lettings_log.unresolved).to eq(true)
end
end
context "and there already is a closed deactivation period" do
before do
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: Time.zone.local(2023, 5, 5), scheme:)
patch "/schemes/#{scheme.id}/deactivate", params:
end
it "creates new deactivation period with valid deactivation date and renders scheme page" do
follow_redirect!
follow_redirect!
expect(response).to have_http_status(:ok)
scheme.reload
expect(scheme.scheme_deactivation_periods.count).to eq(2)
expect(scheme.scheme_deactivation_periods.map(&:deactivation_date)).to include(deactivation_date)
end
it "clears the scheme answer" do
expect(lettings_log.scheme).to eq(scheme)
lettings_log.reload
expect(lettings_log.scheme).to eq(nil)
end
@ -2967,7 +2992,7 @@ RSpec.describe SchemesController, type: :request do
end
context "when there is a later open deactivation" do
let(:deactivation_date) { Time.zone.local(2022, 10, 10) }
# let(:deactivation_date) { Time.zone.local(2022, 10, 10) }
let(:params) { { scheme_deactivation_period: { deactivation_date_type: "other", "deactivation_date": "8/9/2022" } } }
let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 5), reactivation_date: nil, scheme:) }
@ -2980,6 +3005,216 @@ RSpec.describe SchemesController, type: :request do
end
end
describe "#reactivate" do
context "when not signed in" do
it "redirects to the sign in page" do
patch "/schemes/1/reactivate"
expect(response).to redirect_to("/account/sign-in")
end
end
context "when signed in as a data provider" do
let(:user) { create(:user) }
let(:scheme) { create(:scheme, owning_organisation: user.organisation) }
before do
sign_in user
patch "/schemes/#{scheme.id}/reactivate"
end
it "returns 401 unauthorized" do
expect(response).to be_unauthorized
end
end
context "when signed in as a data coordinator" do
let(:user) { create(:user, :data_coordinator) }
let!(:scheme) { create(:scheme, owning_organisation: user.organisation, created_at: Time.zone.local(2023, 10, 11)) }
let(:deactivation_date) { Time.utc(2022, 10, 10) }
let(:startdate) { Time.utc(2022, 10, 11) }
let(:params) { { scheme_deactivation_period: { reactivation_date: "5/8/2023", reactivation_date_type: "other" } } }
let(:add_deactivations) {}
before do
allow(FormHandler.instance).to receive(:lettings_in_crossover_period?).and_return(true)
Timecop.freeze(Time.utc(2023, 10, 10))
# Singleton.__init__(FormHandler)
sign_in user
add_deactivations
scheme.save!
get "/schemes/#{scheme.id}/new-reactivation"
end
after do
Timecop.unfreeze
# Singleton.__init__(FormHandler)
end
context "when there is no open deactivation period" do
let(:add_deactivations) do
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 4, 1), reactivation_date: Time.zone.local(2023, 5, 5), updated_at: Time.zone.local(2000, 1, 1), scheme:)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2024, 1, 1), reactivation_date: Time.zone.local(2026, 4, 5), updated_at: Time.zone.local(2000, 1, 1), scheme:)
end
it "renders not found" do
expect(response).to have_http_status(:not_found)
end
it "does not update deactivation periods" do
scheme.reload
expect(scheme.scheme_deactivation_periods.count).to eq(2)
expect(scheme.scheme_deactivation_periods[0].updated_at).to eq(Time.zone.local(2000, 1, 1))
expect(scheme.scheme_deactivation_periods[1].updated_at).to eq(Time.zone.local(2000, 1, 1))
end
end
context "when there is an open deactivation period starting after reactivation date" do
let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 9, 15), reactivation_date: nil, updated_at: Time.zone.local(2000, 1, 1), scheme:) }
before do
patch "/schemes/#{scheme.id}/reactivate", params:
end
it "shows an unprocessable content error" do
expect(response).to have_http_status(:unprocessable_content)
expect(page).to have_content(I18n.t("validations.scheme.reactivation.before_deactivation", date: "15 September 2023"))
end
it "does not update the deactivation period" do
scheme.reload
expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods[0].updated_at).to eq(Time.zone.local(2000, 1, 1))
end
end
context "when there is an open deactivation period starting before reactivation date" do
let(:add_deactivations) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 7, 15), reactivation_date: nil, scheme:) }
before do
patch "/schemes/#{scheme.id}/reactivate", params:
end
it "redirects to scheme page" do
follow_redirect!
follow_redirect!
expect(response).to have_http_status(:ok)
expect(path).to match("/schemes/#{scheme.id}")
end
it "ends the existing deactivation period" do
scheme.reload
expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date).to eq(Time.zone.local(2023, 7, 15))
expect(scheme.scheme_deactivation_periods.first.reactivation_date).to eq(Time.zone.local(2023, 8, 5))
end
context "with default date" do
let(:add_deactivations) {}
let(:params) { { scheme_deactivation_period: { reactivation_date_type: "default" } } }
before do
allow(FormHandler.instance).to receive(:current_collection_start_year).and_return(2023)
create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 9, 15), reactivation_date: nil, scheme:)
allow(FormHandler.instance).to receive(:current_collection_start_year).and_return(2024)
patch "/schemes/#{scheme.id}/reactivate", params:
end
it "redirects to the scheme details page" do
expect(response).to redirect_to("/schemes/#{scheme.id}/details")
follow_redirect!
follow_redirect!
expect(response).to have_http_status(:ok)
end
it "updates existing scheme deactivations with valid reactivation date" do
follow_redirect!
scheme.reload
expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.reactivation_date).to eq(Time.zone.local(2024, 4, 1))
end
end
context "with other date" do
let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "10/9/2023" } } }
it "redirects to the scheme details page" do
expect(response).to redirect_to("/schemes/#{scheme.id}/details")
end
it "updates existing scheme deactivations with valid reactivation date" do
follow_redirect!
scheme.reload
expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.reactivation_date).to eq(Time.zone.local(2023, 9, 10))
end
end
context "with other future date" do
let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "14/12/2099" } } }
it "redirects to the scheme details page" do
expect(response).to redirect_to("/schemes/#{scheme.id}/details")
end
end
context "when the date is not selected" do
let(:params) { { scheme_deactivation_period: { "reactivation_date": "" } } }
it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_content)
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.not_selected"))
end
end
context "when invalid date is entered" do
let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "10/44/2022" } } }
it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_content)
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid"))
end
end
context "when the date is entered is before the beginning of current collection window" do
let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "10/4/2020" } } }
it "displays the new page with an error message" do
expect(response).to have_http_status(:unprocessable_content)
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.out_of_range", date: "1 April 2022"))
end
end
context "when the day is not entered" do
let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "/2/2022" } } }
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content)
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid"))
end
end
context "when the month is not entered" do
let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "2//2022" } } }
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content)
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid"))
end
end
context "when the year is not entered" do
let(:params) { { scheme_deactivation_period: { reactivation_date_type: "other", "reactivation_date": "2/2/" } } }
it "displays page with an error message" do
expect(response).to have_http_status(:unprocessable_content)
expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid"))
end
end
end
end
end
describe "#delete-confirmation" do
let(:scheme) { create(:scheme, owning_organisation: user.organisation) }

Loading…
Cancel
Save