diff --git a/spec/factories/scheme_deactivation_period.rb b/spec/factories/scheme_deactivation_period.rb index c073bc68a..95e33d9ce 100644 --- a/spec/factories/scheme_deactivation_period.rb +++ b/spec/factories/scheme_deactivation_period.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory :scheme_deactivation_period do + deactivation_date { Time.zone.local(2022, 4, 1) } reactivation_date { nil } end end diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index d95d867dc..3ca9837df 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -125,11 +125,18 @@ RSpec.describe Scheme, type: :model do scheme.save! expect(scheme.status).to eq(:deactivated) end + + it "returns reactivating soon if the scheme has a future reactivation date" do + scheme.scheme_deactivation_periods << FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8)) + scheme.save! + expect(scheme.status).to eq(:reactivating_soon) + end end context "when there have been previous deactivations" do before do scheme.scheme_deactivation_periods << FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 6, 5)) + scheme.save! end it "returns active if the scheme has no relevant deactivation records" do @@ -154,6 +161,13 @@ RSpec.describe Scheme, type: :model do expect(scheme.status).to eq(:deactivated) end end + + it "returns reactivating soon if the scheme has a future reactivation date" do + Timecop.freeze(2022, 6, 8) + scheme.scheme_deactivation_periods << FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9)) + scheme.save! + expect(scheme.status).to eq(:reactivating_soon) + end end describe "with deactivation_date (but no deactivation_date_type)" do diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 59d044050..7734a41aa 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -274,7 +274,7 @@ RSpec.describe SchemesController, type: :request do it "renders reactivate this scheme" do expect(response).to have_http_status(:ok) - expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/reactivate") + expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/new-reactivation") end end @@ -283,7 +283,7 @@ RSpec.describe SchemesController, type: :request do it "renders reactivate this scheme" do expect(response).to have_http_status(:ok) - expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/reactivate") + expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/new-reactivation") end end end @@ -314,6 +314,142 @@ 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) { FactoryBot.create(:user) } + + before do + sign_in user + patch "/schemes/1/reactivate" + end + + it "returns 401 unauthorized" do + request + expect(response).to have_http_status(:unauthorized) + end + end + + context "when signed in as a data coordinator" do + let(:user) { FactoryBot.create(:user, :data_coordinator) } + let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } + let!(:location) { FactoryBot.create(:location, scheme:) } + let(:deactivation_date) { Time.zone.local(2022, 10, 10) } + let(:startdate) { Time.utc(2022, 10, 11) } + + before do + Timecop.freeze(Time.utc(2022, 10, 10)) + sign_in user + scheme.scheme_deactivation_periods << FactoryBot.create(:scheme_deactivation_period, deactivation_date:) + scheme.save! + patch "/schemes/#{scheme.id}/reactivate", params: + end + + after do + Timecop.unfreeze + end + + context "with default date" do + let(:params) { { scheme: { reactivation_date_type: "default" } } } + + it "redirects to the scheme page and displays a success banner" do + expect(response).to redirect_to("/schemes/#{scheme.id}/details") + follow_redirect! + follow_redirect! + expect(response).to have_http_status(:ok) + expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success") + expect(page).to have_content("#{scheme.service_name} has been reactivated") + 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(2022, 4, 1)) + end + end + + context "with other date" do + let(:params) { { scheme: { reactivation_date_type: "other", "reactivation_date(3i)": "14", "reactivation_date(2i)": "10", "reactivation_date(1i)": "2022" } } } + + it "redirects to the scheme page and displays a success banner" do + expect(response).to redirect_to("/schemes/#{scheme.id}/details") + follow_redirect! + follow_redirect! + expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success") + expect(page).to have_content("#{scheme.service_name} has been reactivated") + 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(2022, 10, 14)) + end + end + + context "when the date is not selected" do + let(:params) { { scheme: { "reactivation_date": "" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.scheme.reactivation_date.not_selected")) + end + end + + context "when invalid date is entered" do + let(:params) { { scheme: { reactivation_date_type: "other", "reactivation_date(3i)": "10", "reactivation_date(2i)": "44", "reactivation_date(1i)": "2022" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.scheme.reactivation_date.invalid")) + end + end + + context "when the date is entered is before the beginning of current collection window" do + let(:params) { { scheme: { reactivation_date_type: "other", "reactivation_date(3i)": "10", "reactivation_date(2i)": "4", "reactivation_date(1i)": "2020" } } } + + it "displays the new page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.scheme.reactivation_date.out_of_range", date: "1 April 2022")) + end + end + + context "when the day is not entered" do + let(:params) { { scheme: { reactivation_date_type: "other", "reactivation_date(3i)": "", "reactivation_date(2i)": "2", "reactivation_date(1i)": "2022" } } } + + it "displays page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.scheme.reactivation_date.invalid")) + end + end + + context "when the month is not entered" do + let(:params) { { scheme: { reactivation_date_type: "other", "reactivation_date(3i)": "2", "reactivation_date(2i)": "", "reactivation_date(1i)": "2022" } } } + + it "displays page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.scheme.reactivation_date.invalid")) + end + end + + context "when the year is not entered" do + let(:params) { { scheme: { reactivation_date_type: "other", "reactivation_date(3i)": "2", "reactivation_date(2i)": "2", "reactivation_date(1i)": "" } } } + + it "displays page with an error message" do + expect(response).to have_http_status(:unprocessable_entity) + expect(page).to have_content(I18n.t("validations.scheme.reactivation_date.invalid")) + end + end + end + end + describe "#new" do context "when not signed in" do it "redirects to the sign in page" do