Browse Source

Add delete scheme button and update policy

pull/2286/head
Kat 2 years ago
parent
commit
6dbdb6f732
  1. 4
      app/helpers/schemes_helper.rb
  2. 4
      app/policies/scheme_policy.rb
  3. 4
      app/views/schemes/show.html.erb
  4. 83
      spec/requests/schemes_controller_spec.rb

4
app/helpers/schemes_helper.rb

@ -15,6 +15,10 @@ module SchemesHelper
return govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(scheme) if scheme.deactivated? return govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(scheme) if scheme.deactivated?
end end
def delete_scheme_link(scheme)
govuk_button_link_to "Delete this scheme", scheme_delete_confirmation_path(scheme), warning: true
end
def owning_organisation_options(current_user) def owning_organisation_options(current_user)
all_orgs = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) } all_orgs = Organisation.all.map { |org| OpenStruct.new(id: org.id, name: org.name) }
user_org = [OpenStruct.new(id: current_user.organisation_id, name: current_user.organisation.name)] user_org = [OpenStruct.new(id: current_user.organisation_id, name: current_user.organisation.name)]

4
app/policies/scheme_policy.rb

@ -66,11 +66,11 @@ class SchemePolicy
end end
def delete_confirmation? def delete_confirmation?
user.support? delete?
end end
def delete? def delete?
user.support? user.support? && (scheme.status == :incomplete || scheme.status == :deactivated)
end end
private private

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

@ -49,3 +49,7 @@
<% if SchemePolicy.new(current_user, @scheme).deactivate? %> <% if SchemePolicy.new(current_user, @scheme).deactivate? %>
<%= toggle_scheme_link(@scheme) %> <%= toggle_scheme_link(@scheme) %>
<% end %> <% end %>
<% if SchemePolicy.new(current_user, @scheme).delete? %>
<%= delete_scheme_link(@scheme) %>
<% end %>

83
spec/requests/schemes_controller_spec.rb

@ -577,6 +577,11 @@ RSpec.describe SchemesController, type: :request do
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/new-reactivation") expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/new-reactivation")
end end
it "does not render delete this scheme" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this scheme", href: "/schemes/#{scheme.id}/delete-confirmation")
end
end end
context "with scheme that's deactivating soon" do context "with scheme that's deactivating soon" do
@ -712,6 +717,70 @@ RSpec.describe SchemesController, type: :request do
expect(page).to have_content(specific_scheme.support_type) expect(page).to have_content(specific_scheme.support_type)
expect(page).to have_content(specific_scheme.intended_stay) expect(page).to have_content(specific_scheme.intended_stay)
end end
context "when looking at scheme details" do
let!(:scheme) { create(:scheme, owning_organisation: user.organisation) }
let(:add_deactivations) { scheme.scheme_deactivation_periods << scheme_deactivation_period }
before do
create(:location, scheme:)
Timecop.freeze(Time.utc(2022, 10, 10))
sign_in user
add_deactivations
scheme.save!
get "/schemes/#{scheme.id}"
end
after do
Timecop.unfreeze
end
context "with active scheme" do
let(:add_deactivations) {}
it "does not render delete this scheme" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this scheme", href: "/schemes/#{scheme.id}/delete-confirmation")
end
end
context "with deactivated scheme" do
let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), scheme:) }
it "renders delete this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Delete this scheme", href: "/schemes/#{scheme.id}/delete-confirmation")
end
end
context "with scheme that's deactivating soon" do
let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 12), scheme:) }
it "does not render delete this scheme" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this scheme", href: "/schemes/#{scheme.id}/delete-confirmation")
end
end
context "with scheme that's deactivating in more than 6 months" do
let(:scheme_deactivation_period) { create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2023, 5, 12), scheme:) }
it "does not render delete this scheme" do
expect(response).to have_http_status(:ok)
expect(page).not_to have_link("Delete this scheme", href: "/schemes/#{scheme.id}/delete-confirmation")
end
end
context "with incomplete scheme" do
let(:add_deactivations) {}
let!(:scheme) { create(:scheme, :incomplete, owning_organisation: user.organisation) }
it "renders delete this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Delete this scheme", href: "/schemes/#{scheme.id}/delete-confirmation")
end
end
end
end end
end end
@ -2646,9 +2715,16 @@ RSpec.describe SchemesController, type: :request do
let(:scheme) { create(:scheme, owning_organisation: user.organisation) } let(:scheme) { create(:scheme, owning_organisation: user.organisation) }
before do before do
Timecop.freeze(Time.utc(2022, 10, 10))
scheme.scheme_deactivation_periods << create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), scheme:)
scheme.save!
get "/schemes/#{scheme.id}/delete-confirmation" get "/schemes/#{scheme.id}/delete-confirmation"
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")
@ -2727,11 +2803,18 @@ RSpec.describe SchemesController, type: :request do
context "when signed in" do context "when signed in" do
before do before do
Timecop.freeze(Time.utc(2022, 10, 10))
scheme.scheme_deactivation_periods << create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9), scheme:)
scheme.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
delete "/schemes/#{scheme.id}/delete" delete "/schemes/#{scheme.id}/delete"
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) }

Loading…
Cancel
Save