Browse Source

Add delete location button and update policy

pull/2285/head
Kat 2 years ago
parent
commit
d9be2fd406
  1. 4
      app/helpers/locations_helper.rb
  2. 4
      app/policies/location_policy.rb
  3. 4
      app/views/locations/show.html.erb
  4. 99
      spec/requests/locations_controller_spec.rb

4
app/helpers/locations_helper.rb

@ -73,6 +73,10 @@ module LocationsHelper
return govuk_button_link_to "Reactivate this location", scheme_location_new_reactivation_path(location.scheme, location) if location.deactivated? return govuk_button_link_to "Reactivate this location", scheme_location_new_reactivation_path(location.scheme, location) if location.deactivated?
end end
def delete_location_link(location)
govuk_button_link_to "Delete this location", scheme_location_delete_confirmation_path(location.scheme, location), warning: true
end
def location_creation_success_notice(location) def location_creation_success_notice(location)
if location.confirmed if location.confirmed
"#{location.postcode} #{location.startdate.blank? || location.startdate.before?(Time.zone.now) ? 'has been' : 'will be'} added to this scheme" "#{location.postcode} #{location.startdate.blank? || location.startdate.before?(Time.zone.now) ? 'has been' : 'will be'} added to this scheme"

4
app/policies/location_policy.rb

@ -27,11 +27,11 @@ class LocationPolicy
end end
def delete_confirmation? def delete_confirmation?
user.support? delete?
end end
def delete? def delete?
user.support? user.support? && (location.status == :incomplete || location.status == :deactivated)
end end
%w[ %w[

4
app/views/locations/show.html.erb

@ -45,3 +45,7 @@
<% if LocationPolicy.new(current_user, @location).deactivate? %> <% if LocationPolicy.new(current_user, @location).deactivate? %>
<%= toggle_location_link(@location) %> <%= toggle_location_link(@location) %>
<% end %> <% end %>
<% if LocationPolicy.new(current_user, @location).delete? %>
<%= delete_location_link(@location) %>
<% end %>

99
spec/requests/locations_controller_spec.rb

@ -1825,6 +1825,11 @@ RSpec.describe LocationsController, type: :request do
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(page).to have_link("Reactivate this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/new-reactivation") expect(page).to have_link("Reactivate this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/new-reactivation")
end end
it "does not render delete this location" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation")
end
end end
context "with location that's deactivating soon" do context "with location that's deactivating soon" do
@ -1883,6 +1888,86 @@ RSpec.describe LocationsController, type: :request do
end end
end end
end end
context "when signed in as a support user" do
let(:user) { create(:user, :support) }
let(:scheme) { create(:scheme) }
let(:location) { create(:location, scheme:) }
let(:add_deactivations) { location.location_deactivation_periods << location_deactivation_period }
before do
Timecop.freeze(Time.utc(2022, 10, 10))
allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user
add_deactivations
location.save!
get "/schemes/#{scheme.id}/locations/#{location.id}"
end
after do
Timecop.unfreeze
end
context "with active location" do
let(:add_deactivations) {}
it "does not render delete this location" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation")
end
end
context "with deactivated location" do
let(:location_deactivation_period) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), location:) }
it "renders delete this location" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation")
end
end
context "with incomplete location" do
let(:add_deactivations) {}
before do
location.update!(units: nil)
get "/schemes/#{scheme.id}/locations/#{location.id}"
end
it "renders delete this location" do
expect(location.reload.status).to eq(:incomplete)
expect(response).to have_http_status(:ok)
expect(page).to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation")
end
end
context "with location that's deactivating soon" do
let(:location_deactivation_period) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 12), location:) }
it "does not render delete this location" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation")
end
end
context "with location that's deactivating in more than 6 months" do
let(:location_deactivation_period) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2023, 6, 12), location:) }
it "does not render delete this location" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation")
end
end
context "with location that's reactivating soon" do
let(:location_deactivation_period) { create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 12), reactivation_date: Time.zone.local(2022, 10, 12), location:) }
it "does not render delete this location" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation")
end
end
end
end end
describe "#reactivate" do describe "#reactivate" do
@ -2057,11 +2142,18 @@ RSpec.describe LocationsController, type: :request do
context "when signed in" do context "when signed in" do
before do before do
Timecop.freeze(Time.utc(2022, 10, 10))
location.location_deactivation_periods << create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), location:)
location.save!
allow(user).to receive(:need_two_factor_authentication?).and_return(false) allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user sign_in user
get "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation" get "/schemes/#{scheme.id}/locations/#{location.id}/delete-confirmation"
end end
after do
Timecop.unfreeze
end
context "with a data provider user" do context "with a data provider user" do
let(:user) { create(:user) } let(:user) { create(:user) }
@ -2116,9 +2208,16 @@ RSpec.describe LocationsController, type: :request do
let(:location) { create(:location, scheme:, name: "Location to delete", created_at: Time.zone.local(2022, 4, 1)) } let(:location) { create(:location, scheme:, name: "Location to delete", created_at: Time.zone.local(2022, 4, 1)) }
before do before do
Timecop.freeze(Time.utc(2022, 10, 10))
location.location_deactivation_periods << create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), location:)
location.save!
delete "/schemes/#{scheme.id}/locations/#{location.id}/delete" delete "/schemes/#{scheme.id}/locations/#{location.id}/delete"
end end
after do
Timecop.unfreeze
end
context "when not signed in" do context "when not signed in" do
it "redirects to the sign in page" do it "redirects to the sign in page" do
expect(response).to redirect_to("/account/sign-in") expect(response).to redirect_to("/account/sign-in")

Loading…
Cancel
Save