Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(policy): Fix scope in policies #2478

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions app/policies/archive_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ def create?
def show? = create?

def destroy? = create?

def resolve
Archive.where(organisation_id: pundit_user.organisations.pluck(:id))
end
end
2 changes: 1 addition & 1 deletion app/policies/category_configuration_policy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CategoryConfigurationPolicy < ApplicationPolicy
class Scope < Scope
def resolve
CategoryConfiguration.where(organisation: pundit_user.organisations)
scope.where(organisation_id: pundit_user.organisation_ids)
end
end
end
2 changes: 1 addition & 1 deletion app/policies/department_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def parcours?

class Scope < Scope
def resolve
Department.where(id: pundit_user.organisations.pluck(:department_id))
scope.where(id: pundit_user.organisations.pluck(:department_id))
end
end
end
2 changes: 1 addition & 1 deletion app/policies/messages_configuration_policy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class MessagesConfigurationPolicy < ApplicationPolicy
class Scope < Scope
def resolve
MessagesConfiguration.where(organisation_id: pundit_user.admin_organisations_ids)
scope.where(organisation_id: pundit_user.admin_organisations_ids)
end
end
end
2 changes: 1 addition & 1 deletion app/policies/organisation_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def export_csv?

class Scope < Scope
def resolve
pundit_user.organisations
scope.where(id: pundit_user.organisation_ids)
end
end
end
15 changes: 6 additions & 9 deletions spec/controllers/invitations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
let!(:other_org) { create(:organisation, department: department) }
let!(:other_category_configuration) { create(:category_configuration, organisation: other_org, motif_category:) }

let!(:organisations) { Organisation.where(id: organisation.id) }
let!(:agent) { create(:agent, organisations: organisations) }
let!(:agent) { create(:agent, organisations: [organisation]) }
let!(:user) do
create(
:user,
first_name: "JANE", last_name: "DOE", title: "madame",
id: user_id, organisations: organisations
id: user_id, organisations: [organisation]
)
end
let!(:motif_category) { create(:motif_category, short_name: "rsa_orientation") }
Expand Down Expand Up @@ -42,20 +41,19 @@
sign_in(agent)
travel_to(Time.zone.parse("2022-05-04 12:30"))
allow(InviteUser).to receive(:call)
.with(user:, organisations:, invitation_attributes:, motif_category_attributes:)
.and_return(OpenStruct.new(success?: true, invitation:))
end

context "organisation level" do
it "calls the invite user service" do
expect(InviteUser).to receive(:call)
.with(user:, organisations:, invitation_attributes:, motif_category_attributes:)
.with(user:, organisations: [organisation], invitation_attributes:, motif_category_attributes:)
post :create, params: create_params
end
end

context "department level" do
let!(:organisations) { Organisation.where(id: [organisation.id, other_org.id]) }
let!(:agent) { create(:agent, organisations: [organisation, other_org]) }
let!(:create_params) do
{
department_id: department.id,
Expand All @@ -74,19 +72,18 @@

it "calls the service" do
expect(InviteUser).to receive(:call)
.with(user:, organisations:, invitation_attributes:, motif_category_attributes:)
.with(user:, organisations: [organisation, other_org], invitation_attributes:, motif_category_attributes:)
post :create, params: create_params
end

context "when an org does not have a configuration for this category" do
let!(:other_category_configuration) do
create(:category_configuration, organisation: other_org, motif_category: create(:motif_category))
end
let!(:organisations) { Organisation.where(id: [organisation.id]) }

it "calls the service with only the orgs handling the category" do
expect(InviteUser).to receive(:call)
.with(user:, organisations:, invitation_attributes:, motif_category_attributes:)
.with(user:, organisations: [organisation], invitation_attributes:, motif_category_attributes:)
post :create, params: create_params
end
end
Expand Down
9 changes: 7 additions & 2 deletions spec/features/agent_can_change_navigation_level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
let!(:department) { create(:department) }
let!(:organisation) { create(:organisation, department: department) }
let!(:organisation2) { create(:organisation, department: department) }
let!(:agent) { create(:agent, organisations: [organisation2]) }
let!(:admin_agent_role) { create(:agent_role, organisation: organisation, agent: agent, access_level: "admin") }
let!(:other_department) { create(:department) }
let!(:other_department_organisation) { create(:organisation, department: other_department) }
let!(:agent) do
create(:agent, admin_role_in_organisations: [organisation, organisation2, other_department_organisation])
end
let!(:motif_category) { create(:motif_category, short_name: "rsa_orientation", name: "RSA orientation") }
let!(:motif_category2) { create(:motif_category, short_name: "rsa_accompagnement", name: "RSA accompagnement") }
let!(:category_configuration) do
Expand Down Expand Up @@ -31,6 +34,8 @@
)
expect(page).to have_link(organisation.name, href: organisation_users_path(organisation))
expect(page).to have_link(organisation2.name, href: organisation_users_path(organisation2))
expect(page).to have_no_link(other_department_organisation.name,
href: organisation_users_path(other_department_organisation))
end
end
end
Expand Down