From e0c53ad60683dc7716eacd6eef23ebf0a8585644 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 11 Nov 2022 07:51:23 +0000 Subject: [PATCH] Add reactivate ocation button and path --- app/controllers/locations_controller.rb | 4 ++ app/views/locations/show.html.erb | 6 +- app/views/locations/toggle_active.html.erb | 2 +- config/routes.rb | 1 + spec/requests/locations_controller_spec.rb | 64 ++++++++++++++++++++++ 5 files changed, 75 insertions(+), 2 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 82f96e9ca..dfd2e3435 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -39,6 +39,10 @@ class LocationsController < ApplicationController end end + def reactivate + render "toggle_active", locals: { action: "reactivate" } + end + def create if date_params_missing?(location_params) || valid_date_params?(location_params) @location = Location.new(location_params) diff --git a/app/views/locations/show.html.erb b/app/views/locations/show.html.erb index fdceb77ad..829b56942 100644 --- a/app/views/locations/show.html.erb +++ b/app/views/locations/show.html.erb @@ -24,5 +24,9 @@ <% if FeatureToggle.location_toggle_enabled? %> - <%= govuk_button_link_to "Deactivate this location", scheme_location_deactivate_path(scheme_id: @scheme.id, location_id: @location.id), warning: true %> + <% if @location.status == :active %> + <%= govuk_button_link_to "Deactivate this location", scheme_location_deactivate_path(scheme_id: @scheme.id, location_id: @location.id), warning: true %> + <% else %> + <%= govuk_button_link_to "Reactivate this location", scheme_location_reactivate_path(scheme_id: @scheme.id, location_id: @location.id) %> + <% end%> <% end %> diff --git a/app/views/locations/toggle_active.html.erb b/app/views/locations/toggle_active.html.erb index fe5b665a5..90638f34c 100644 --- a/app/views/locations/toggle_active.html.erb +++ b/app/views/locations/toggle_active.html.erb @@ -1,4 +1,4 @@ -<% title = "Deactivate #{@location.postcode}" %> +<% title = "#{action.humanize} #{@location.postcode}" %> <% content_for :title, title %> <% content_for :before_content do %> diff --git a/config/routes.rb b/config/routes.rb index 22650185d..8bd78d6e6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -54,6 +54,7 @@ Rails.application.routes.draw do get "edit-name", to: "locations#edit_name" get "edit-local-authority", to: "locations#edit_local_authority" get "deactivate", to: "locations#deactivate" + get "reactivate", to: "locations#reactivate" patch "deactivate", to: "locations#deactivate" end end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 0895c54e6..f4ec9f159 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1345,4 +1345,68 @@ RSpec.describe LocationsController, type: :request do end end end + + describe "#show" do + context "when not signed in" do + it "redirects to the sign in page" do + get "/schemes/1/locations/1" + 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 + get "/schemes/1/locations/1" + 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:) } + + before do + Timecop.freeze(Time.utc(2022, 10, 10)) + sign_in user + location.deactivation_date = deactivation_date + location.save! + get "/schemes/#{scheme.id}/locations/#{location.id}" + end + + context "with active location" do + let(:deactivation_date) { nil } + + it "renders deactivate this location" do + expect(response).to have_http_status(:ok) + expect(page).to have_link("Deactivate this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/deactivate") + end + end + + context "with deactivated location" do + let(:deactivation_date) { Time.utc(2022, 10, 9) } + + it "renders reactivate this location" do + expect(response).to have_http_status(:ok) + expect(page).to have_link("Reactivate this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/reactivate") + end + end + + context "with location that's deactivating soon" do + let(:deactivation_date) { Time.utc(2022, 10, 12) } + + it "renders reactivate this location" do + expect(response).to have_http_status(:ok) + expect(page).to have_link("Reactivate this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/reactivate") + end + end + end + end end