forked from iisaphd/enroll
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pundit policy to plan design organization controller (#2710)
* adds pundit policy to plan design origanization controller * includes pundit in application controller * adds specs * fixes rubocop errors * fixes typo on the file name * replace the file --------- Co-authored-by: Sri Harsha <sriharsha.poosa@gmail.com>
- Loading branch information
1 parent
4268210
commit c4233ea
Showing
6 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...benefits/app/policies/sponsored_benefits/organizations/plan_design_organization_policy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module SponsoredBenefits | ||
module Organizations | ||
class PlanDesignOrganizationPolicy < ::ApplicationPolicy | ||
|
||
def edit? | ||
plan_design_organization_access? | ||
end | ||
|
||
def update? | ||
plan_design_organization_access? | ||
end | ||
|
||
def destroy? | ||
plan_design_organization_access? | ||
end | ||
|
||
private | ||
|
||
def plan_design_organization_access? | ||
return true if user.has_hbx_staff_role? | ||
return false unless user.person | ||
|
||
person = user.person | ||
|
||
return true if broker_owns_plan_design_organization_via_broker_agency?(person) | ||
|
||
true if broker_staff_owns_plan_design_organization_via_broker_agency?(person) | ||
end | ||
|
||
def broker_staff_owns_plan_design_organization_via_broker_agency?(person) | ||
broker_agency_staff_roles = person.broker_agency_staff_roles&.active | ||
return false if broker_agency_staff_roles.blank? | ||
|
||
broker_agency_staff_roles.any? do |basr| | ||
basr.benefit_sponsors_broker_agency_profile_id == record.owner_profile_id | ||
end | ||
end | ||
|
||
def broker_owns_plan_design_organization_via_broker_agency?(person) | ||
return false unless person.broker_role&.active? | ||
|
||
person.broker_role.benefit_sponsors_broker_agency_profile_id == record.owner_profile_id | ||
end | ||
end | ||
end | ||
end |
68 changes: 68 additions & 0 deletions
68
...onsored_benefits/organizations/plan_design_organizations_controller_authorization_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require "#{SponsoredBenefits::Engine.root}/spec/shared_contexts/sponsored_benefits" | ||
|
||
module SponsoredBenefits | ||
RSpec.describe Organizations::PlanDesignOrganizationsController, type: :controller, dbclean: :around_each do | ||
include_context "set up broker agency profile for BQT, by using configuration settings" | ||
|
||
routes { SponsoredBenefits::Engine.routes } | ||
include Rails.application.routes.url_helpers | ||
|
||
context "when the logged-in user is not authorized to access" do | ||
let(:fake_user) { FactoryBot.create(:user, person: fake_person) } | ||
let(:fake_person) do | ||
FactoryBot.create(:person, :with_broker_role).tap do |person| | ||
person.broker_role.update_attributes(broker_agency_profile_id: broker_agency_profile.id.to_s) | ||
end | ||
end | ||
let(:valid_attributes) do | ||
{ | ||
"legal_name" => "Some Name", | ||
"dba" => "", | ||
"entity_kind" => "", | ||
"sic_code" => "0116" | ||
} | ||
end | ||
|
||
it "redirects to the root path and displays an error message" do | ||
sign_in(fake_user) | ||
|
||
get :edit, params: { id: prospect_plan_design_organization.to_param } | ||
expect(response).to redirect_to(root_path) | ||
expect(flash[:error]).to eq("Access not allowed for edit?, (Pundit policy)") | ||
end | ||
|
||
it "redirects to the root path and displays an error message" do | ||
sign_in(fake_user) | ||
|
||
delete :destroy, params: {:id => prospect_plan_design_organization.to_param } | ||
expect(response).to redirect_to(root_path) | ||
expect(flash[:error]).to eq("Access not allowed for destroy?, (Pundit policy)") | ||
end | ||
|
||
it "redirects to the root path and displays an error message" do | ||
sign_in(fake_user) | ||
|
||
patch :update, params: { organization: valid_attributes, id: prospect_plan_design_organization.id } | ||
expect(flash[:error]).to eq("Access not allowed for update?, (Pundit policy)") | ||
end | ||
|
||
it "redirects to the root path and displays an error message" do | ||
sign_in(fake_user) | ||
|
||
get :new, params: { plan_design_organization_id: prospect_plan_design_organization.id, broker_agency_id: broker_agency_profile.id } | ||
expect(response).to redirect_to(root_path) | ||
expect(flash[:error]).to eq("Access not allowed for plan_design_org_new?, (Pundit policy)") | ||
end | ||
|
||
it "redirects to the root path and displays an error message" do | ||
sign_in(fake_user) | ||
|
||
post :create, params: { organization: valid_attributes, broker_agency_id: broker_agency_profile.id, format: 'js'} | ||
expect(flash[:error]).to eq("Access not allowed for plan_design_org_create?, (Pundit policy)") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters