From 8c3f852fbcd067edf379c745fc118a768662a28d Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Tue, 15 Nov 2022 15:09:34 +0000 Subject: [PATCH] test: update tests to new flow --- app/models/location.rb | 5 +-- app/models/scheme.rb | 9 +++--- config/locales/en.yml | 4 +-- spec/models/scheme_spec.rb | 4 +++ spec/requests/locations_controller_spec.rb | 6 ++-- spec/requests/schemes_controller_spec.rb | 36 ++++++++++++++-------- 6 files changed, 41 insertions(+), 23 deletions(-) diff --git a/app/models/location.rb b/app/models/location.rb index 3b4a113ba..a877ce549 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -375,7 +375,8 @@ class Location < ApplicationRecord def status return :active if deactivation_date.blank? return :deactivating_soon if Time.zone.now < deactivation_date - return :deactivated if Time.zone.now >= deactivation_date + + :deactivated end def active? @@ -395,7 +396,7 @@ class Location < ApplicationRecord collection_start_date = FormHandler.instance.current_collection_start_date if [day, month, year].any?(&:blank?) - errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.not_entered")) + errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) elsif !Date.valid_date?(year.to_i, month.to_i, day.to_i) errors.add(:deactivation_date, message: I18n.t("validations.location.deactivation_date.invalid")) elsif !Date.new(year.to_i, month.to_i, day.to_i).between?(collection_start_date, Date.new(2200, 1, 1)) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index c7ac918b9..9efc820a9 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -218,25 +218,26 @@ class Scheme < ApplicationRecord def status return :active if deactivation_date.blank? return :deactivating_soon if Time.zone.now < deactivation_date - return :deactivated if Time.zone.now >= deactivation_date + + :deactivated end def active? status == :active end - def run_deactivation_validations + def implicit_run_deactivation_validations deactivation_date.present? || @run_deactivation_validations end def deactivation_date_errors - return unless run_deactivation_validations + return unless implicit_run_deactivation_validations collection_start_date = FormHandler.instance.current_collection_start_date if deactivation_date_type.blank? errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected")) elsif deactivation_date.blank? - errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.not_entered")) + errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation_date.invalid")) elsif !deactivation_date.between?(collection_start_date, Date.new(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))) end diff --git a/config/locales/en.yml b/config/locales/en.yml index 9247ebc93..a1c18330a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -315,13 +315,13 @@ en: scheme: deactivation_date: not_selected: "Select one of the options" - not_entered: "Enter a valid day, month and year" + invalid: "Enter a valid day, month and year" out_of_range: "The date must be on or after the %{date}" location: deactivation_date: not_selected: "Select one of the options" - not_entered: "Enter a valid day, month and year" + invalid: "Enter a valid day, month and year" out_of_range: "The date must be on or after the %{date}" soft_validations: diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index b69629ba5..cb6ef2c8d 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -101,24 +101,28 @@ RSpec.describe Scheme, type: :model do it "returns active if the scheme is not deactivated" do scheme.deactivation_date = nil + scheme.deactivation_date_type = nil scheme.save! expect(scheme.status).to eq(:active) end it "returns deactivating soon if deactivation_date is in the future" do scheme.deactivation_date = Time.zone.local(2022, 8, 8) + scheme.deactivation_date_type = "other" scheme.save! expect(scheme.status).to eq(:deactivating_soon) end it "returns deactivated if deactivation_date is in the past" do scheme.deactivation_date = Time.zone.local(2022, 4, 8) + scheme.deactivation_date_type = "other" scheme.save! expect(scheme.status).to eq(:deactivated) end it "returns deactivated if deactivation_date is today" do scheme.deactivation_date = Time.zone.local(2022, 6, 7) + scheme.deactivation_date_type = "other" scheme.save! expect(scheme.status).to eq(:deactivated) end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 361128f5b..33a082018 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1310,7 +1310,7 @@ RSpec.describe LocationsController, 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.location.deactivation_date.not_entered")) + expect(page).to have_content(I18n.t("validations.location.deactivation_date.invalid")) end end @@ -1319,7 +1319,7 @@ RSpec.describe LocationsController, 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.location.deactivation_date.not_entered")) + expect(page).to have_content(I18n.t("validations.location.deactivation_date.invalid")) end end @@ -1328,7 +1328,7 @@ RSpec.describe LocationsController, 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.location.deactivation_date.not_entered")) + expect(page).to have_content(I18n.t("validations.location.deactivation_date.invalid")) end end end diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 2c7556673..5ad18663e 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -251,21 +251,24 @@ RSpec.describe SchemesController, type: :request do Timecop.freeze(Time.utc(2022, 10, 10)) sign_in user scheme.deactivation_date = deactivation_date + scheme.deactivation_date_type = deactivation_date_type scheme.save! get "/schemes/#{scheme.id}" end context "with active scheme" do let(:deactivation_date) { nil } + let(:deactivation_date_type) { nil } it "renders deactivate this scheme" do expect(response).to have_http_status(:ok) - expect(page).to have_link("Deactivate this scheme", href: "/schemes/#{scheme.id}/deactivate") + expect(page).to have_link("Deactivate this scheme", href: "/schemes/#{scheme.id}/new-deactivation") end end context "with deactivated scheme" do let(:deactivation_date) { Time.utc(2022, 10, 9) } + let(:deactivation_date_type) { "other" } it "renders reactivate this scheme" do expect(response).to have_http_status(:ok) @@ -275,6 +278,7 @@ RSpec.describe SchemesController, type: :request do context "with scheme that's deactivating soon" do let(:deactivation_date) { Time.utc(2022, 10, 12) } + let(:deactivation_date_type) { "other" } it "renders reactivate this scheme" do expect(response).to have_http_status(:ok) @@ -1742,7 +1746,7 @@ RSpec.describe SchemesController, type: :request do describe "#deactivate" do context "when not signed in" do it "redirects to the sign in page" do - patch "/schemes/1/deactivate" + patch "/schemes/1/new-deactivation" expect(response).to redirect_to("/account/sign-in") end end @@ -1752,7 +1756,7 @@ RSpec.describe SchemesController, type: :request do before do sign_in user - patch "/schemes/1/deactivate" + patch "/schemes/1/new-deactivation" end it "returns 401 unauthorized" do @@ -1770,29 +1774,37 @@ RSpec.describe SchemesController, type: :request do before do Timecop.freeze(Time.utc(2022, 10, 10)) sign_in user - patch "/schemes/#{scheme.id}/deactivate", params: + patch "/schemes/#{scheme.id}/new-deactivation", params: end context "with default date" do - let(:params) { { scheme: { deactivation_date_type: "default" } } } + let(:params) { { scheme: { deactivation_date_type: "default", deactivation_date: } } } - it "renders the confirmation page" do + it "redirects to the confirmation page" do + follow_redirect! expect(response).to have_http_status(:ok) expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} logs") end end context "with other date" do - let(:params) { { scheme: { deactivation_date: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "10", "deactivation_date(1i)": "2022" } } } + let(:params) { { scheme: { deactivation_date_type: "other", "deactivation_date(3i)": "10", "deactivation_date(2i)": "10", "deactivation_date(1i)": "2022" } } } - it "renders the confirmation page" do + it "redirects to the confirmation page" do + follow_redirect! expect(response).to have_http_status(:ok) expect(page).to have_content("This change will affect #{scheme.lettings_logs.count} logs") end end context "when confirming deactivation" do - let(:params) { { scheme: { deactivation_date:, confirm: true } } } + let(:params) { { scheme: { deactivation_date:, confirm: true, deactivation_date_type: "other" } } } + + before do + Timecop.freeze(Time.utc(2022, 10, 10)) + sign_in user + patch "/schemes/#{scheme.id}/deactivate", params: + end it "updates existing scheme with valid deactivation date and renders scheme page" do follow_redirect! @@ -1836,7 +1848,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.not_entered")) + expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.invalid")) end end @@ -1845,7 +1857,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.not_entered")) + expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.invalid")) end end @@ -1854,7 +1866,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.not_entered")) + expect(page).to have_content(I18n.t("validations.scheme.deactivation_date.invalid")) end end end