Browse Source

Update policy

pull/2459/head
Kat 2 years ago committed by kosiakkatrina
parent
commit
5382e6d389
  1. 15
      app/policies/organisation_policy.rb
  2. 108
      spec/policies/organisation_policy_spec.rb

15
app/policies/organisation_policy.rb

@ -19,6 +19,19 @@ class OrganisationPolicy
end end
def delete? def delete?
user.support? && (organisation.status == :deactivated || organisation.status == :merged) return false unless user.support?
return false unless organisation.status == :deactivated || organisation.status == :merged
!has_any_logs_in_editable_collection_period
end
def has_any_logs_in_editable_collection_period
editable_from_date = FormHandler.instance.earliest_open_for_editing_collection_start_date
editable_lettings_logs = organisation.lettings_logs.visible.after_date(editable_from_date)
return true if organisation.lettings_logs.visible.where(startdate: nil).any? || editable_lettings_logs.any?
editable_sales_logs = organisation.sales_logs.visible.after_date(editable_from_date)
organisation.sales_logs.visible.where(saledate: nil).any? || editable_sales_logs.any?
end end
end end

108
spec/policies/organisation_policy_spec.rb

@ -63,4 +63,112 @@ RSpec.describe OrganisationPolicy do
expect(policy).not_to permit(support, organisation) expect(policy).not_to permit(support, organisation)
end end
end end
permissions :delete? do
let(:organisation) { FactoryBot.create(:organisation) }
context "with active organisation" do
it "does not allow deleting a organisation as a provider" do
expect(policy).not_to permit(data_provider, organisation)
end
it "does not allow allows deleting a organisation as a coordinator" do
expect(policy).not_to permit(data_coordinator, organisation)
end
it "does not allow deleting a organisation as a support user" do
expect(policy).not_to permit(support, organisation)
end
end
context "with deactivated organisation" do
before do
organisation.update!(active: false)
end
it "does not allow deleting a organisation as a provider" do
expect(policy).not_to permit(data_provider, organisation)
end
it "does not allow allows deleting a organisation as a coordinator" do
expect(policy).not_to permit(data_coordinator, organisation)
end
it "allows deleting a organisation as a support user" do
expect(policy).to permit(support, organisation)
end
context "and associated lettings logs in editable collection period" do
before do
create(:lettings_log, owning_organisation: organisation)
end
it "does not allow deleting a organisation as a provider" do
expect(policy).not_to permit(data_provider, organisation)
end
it "does not allow allows deleting a organisation as a coordinator" do
expect(policy).not_to permit(data_coordinator, organisation)
end
it "does not allow deleting a organisation as a support user" do
expect(policy).not_to permit(support, organisation)
end
end
context "and deleted associated lettings logs in editable collection period" do
before do
create(:lettings_log, owning_organisation: organisation, discarded_at: Time.zone.yesterday)
end
it "does not allow deleting a organisation as a provider" do
expect(policy).not_to permit(data_provider, organisation)
end
it "does not allow allows deleting a organisation as a coordinator" do
expect(policy).not_to permit(data_coordinator, organisation)
end
it "allows deleting a organisation as a support user" do
expect(policy).to permit(support, organisation)
end
end
context "and associated sales logs in editable collection period" do
before do
create(:sales_log, owning_organisation: organisation)
end
it "does not allow deleting a organisation as a provider" do
expect(policy).not_to permit(data_provider, organisation)
end
it "does not allow allows deleting a organisation as a coordinator" do
expect(policy).not_to permit(data_coordinator, organisation)
end
it "does not allow deleting a organisation as a support user" do
expect(policy).not_to permit(support, organisation)
end
end
context "and deleted associated sales logs in editable collection period" do
before do
create(:sales_log, owning_organisation: organisation, discarded_at: Time.zone.yesterday)
end
it "does not allow deleting a organisation as a provider" do
expect(policy).not_to permit(data_provider, organisation)
end
it "does not allow allows deleting a organisation as a coordinator" do
expect(policy).not_to permit(data_coordinator, organisation)
end
it "allows deleting a organisation as a support user" do
expect(policy).to permit(support, organisation)
end
end
end
end
end end

Loading…
Cancel
Save