From 59ccbf9011a32dfe7791a7098e7edc5b1c37c1b1 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 18 Nov 2022 10:46:59 +0000 Subject: [PATCH] feat: add new features from locations branch --- app/controllers/schemes_controller.rb | 7 +- app/models/scheme.rb | 10 +- app/views/schemes/show.html.erb | 2 +- config/locales/en.yml | 2 +- spec/factories/scheme_deactivation_period.rb | 1 + spec/models/scheme_spec.rb | 14 ++ spec/requests/schemes_controller_spec.rb | 151 ++++++++++++++++++- 7 files changed, 173 insertions(+), 14 deletions(-) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 03e675d77..a4ab90766 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -317,7 +317,12 @@ private end def reactivate_success_notice - "#{@scheme.service_name} has been reactivated" + case @scheme.status + when :active + "#{@scheme.service_name} has been reactivated" + when :reactivating_soon + "#{@scheme.service_name} will reactivate on #{reactivation_date.to_time.to_formatted_s(:govuk_date)}" + end end def deactivation_date diff --git a/app/models/scheme.rb b/app/models/scheme.rb index ac3226161..09fe504f1 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -231,6 +231,10 @@ class Scheme < ApplicationRecord status == :active end + def reactivating_soon? + status == :reactivating_soon + end + def run_deactivation_validations! @run_deactivation_validations = true end @@ -244,14 +248,14 @@ class Scheme < ApplicationRecord if deactivation_date.blank? if deactivation_date_type.blank? - errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected")) + errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.toggle_date.not_selected")) elsif deactivation_date_type == "other" - errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid")) + errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.invalid")) end else collection_start_date = FormHandler.instance.current_collection_start_date unless deactivation_date.between?(collection_start_date, Time.zone.local(2200, 1, 1)) - errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) + errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: collection_start_date.to_formatted_s(:govuk_date))) end end end diff --git a/app/views/schemes/show.html.erb b/app/views/schemes/show.html.erb index 82b9899be..e0b229af4 100644 --- a/app/views/schemes/show.html.erb +++ b/app/views/schemes/show.html.erb @@ -26,7 +26,7 @@ <% end %> <% if FeatureToggle.scheme_toggle_enabled? %> - <% if @scheme.active? %> + <% if @scheme.active? || @scheme.reactivating_soon? %> <%= govuk_button_link_to "Deactivate this scheme", scheme_new_deactivation_path(@scheme), warning: true %> <% else %> <%= govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(@scheme) %> diff --git a/config/locales/en.yml b/config/locales/en.yml index c53c495b8..88ad711be 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -313,7 +313,7 @@ en: missing: "You must show the DLUHC privacy notice to the tenant before you can submit this log." scheme: - deactivation_date: + toggle_date: not_selected: "Select one of the options" invalid: "Enter a valid day, month and year" out_of_range: "The date must be on or after the %{date}" 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..50b4e7fad 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,141 @@ 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(: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 @@ -1855,7 +1990,7 @@ RSpec.describe SchemesController, type: :request do 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.deactivation_date.not_selected")) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.not_selected")) end end @@ -1864,7 +1999,7 @@ RSpec.describe SchemesController, type: :request do 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.deactivation_date.invalid")) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid")) end end @@ -1873,7 +2008,7 @@ RSpec.describe SchemesController, type: :request do 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.deactivation_date.out_of_range", date: "1 April 2022")) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.out_of_range", date: "1 April 2022")) end end @@ -1882,7 +2017,7 @@ RSpec.describe SchemesController, type: :request do 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.deactivation_date.invalid")) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid")) end end @@ -1891,7 +2026,7 @@ RSpec.describe SchemesController, type: :request do 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.deactivation_date.invalid")) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid")) end end @@ -1900,7 +2035,7 @@ RSpec.describe SchemesController, type: :request do 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.deactivation_date.invalid")) + expect(page).to have_content(I18n.t("validations.scheme.toggle_date.invalid")) end end end