Browse Source

Update location policy

pull/2285/head
Kat 2 years ago
parent
commit
b598a3af20
  1. 12
      app/policies/location_policy.rb
  2. 92
      spec/policies/location_policy_spec.rb

12
app/policies/location_policy.rb

@ -31,7 +31,10 @@ class LocationPolicy
end end
def delete? def delete?
user.support? && (location.status == :incomplete || location.status == :deactivated) return false unless user.support?
return false unless location.status == :incomplete || location.status == :deactivated
!has_any_logs_in_editable_collection_period
end end
%w[ %w[
@ -83,4 +86,11 @@ private
def scheme_owned_by_user_org_or_stock_owner def scheme_owned_by_user_org_or_stock_owner
scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id) scheme&.owning_organisation == user.organisation || user.organisation.stock_owners.exists?(scheme&.owning_organisation_id)
end end
def has_any_logs_in_editable_collection_period
editable_from_date = FormHandler.instance.earliest_open_for_editing_collection_start_date
editable_logs = LettingsLog.where(location_id: location.id).after_date(editable_from_date)
LettingsLog.where(location_id: location.id, startdate: nil).any? || editable_logs.any?
end
end end

92
spec/policies/location_policy_spec.rb

@ -0,0 +1,92 @@
require "rails_helper"
RSpec.describe LocationPolicy do
subject(:policy) { described_class }
let(:data_provider) { FactoryBot.create(:user, :data_provider) }
let(:data_coordinator) { FactoryBot.create(:user, :data_coordinator) }
let(:support) { FactoryBot.create(:user, :support) }
permissions :delete? do
let(:location) { FactoryBot.create(:location) }
context "with active location" do
it "does not allow deleting a location as a provider" do
expect(policy).not_to permit(data_provider, location)
end
it "does not allow allows deleting a location as a coordinator" do
expect(policy).not_to permit(data_coordinator, location)
end
it "does not allow deleting a location as a support user" do
expect(policy).not_to permit(support, location)
end
end
context "with incomplete location" do
before do
location.update!(units: nil)
end
it "does not allow deleting a location as a provider" do
expect(policy).not_to permit(data_provider, location)
end
it "does not allow allows deleting a location as a coordinator" do
expect(policy).not_to permit(data_coordinator, location)
end
it "allows deleting a location as a support user" do
expect(policy).to permit(support, location)
end
end
context "with deactivated location" do
before do
location.location_deactivation_periods << create(:location_deactivation_period, deactivation_date: Time.zone.local(2024, 4, 10), location:)
location.save!
Timecop.freeze(Time.utc(2024, 4, 10))
log = create(:lettings_log, scheme: location.scheme, location:)
log.startdate = Time.zone.local(2022, 10, 10)
log.save!(validate: false)
end
after do
Timecop.unfreeze
end
context "and associated logs in editable collection period" do
before do
create(:lettings_log, scheme: location.scheme, location:)
end
it "does not allow deleting a location as a provider" do
expect(policy).not_to permit(data_provider, location)
end
it "does not allow allows deleting a location as a coordinator" do
expect(policy).not_to permit(data_coordinator, location)
end
it "does not allow deleting a location as a support user" do
expect(policy).not_to permit(support, location)
end
end
context "and no associated logs in editable collection period" do
it "does not allow deleting a location as a provider" do
expect(policy).not_to permit(data_provider, location)
end
it "does not allow allows deleting a location as a coordinator" do
expect(policy).not_to permit(data_coordinator, location)
end
it "allows deleting a location as a support user" do
expect(policy).to permit(support, location)
end
end
end
end
end
Loading…
Cancel
Save