Skip to content

Commit

Permalink
Merge pull request #88 from code0-tech/raise-for-invalid-ability-stub…
Browse files Browse the repository at this point in the history
…bing

Raise error when stubbing invalid abilities
  • Loading branch information
Taucher2003 authored Mar 1, 2024
2 parents 911be26 + 48c42cc commit 88afadd
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion app/models/team_role_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
class TeamRoleAbility < ApplicationRecord
ABILITIES = {
create_team_role: 1,
read_team_role: 2,
invite_member: 2,
assign_member_roles: 3,
}.with_indifferent_access

enum :ability, ABILITIES, prefix: :can
Expand Down
4 changes: 2 additions & 2 deletions spec/policies/concerns/customizable_permission_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

context 'when user has a role with a different ability' do
before do
create(:team_role_ability, team_role: team_role, ability: :read_team_role)
create(:team_role_ability, team_role: team_role, ability: :create_team_role)
end

it { is_expected.not_to be_allowed(:create_team_role) }
it { is_expected.not_to be_allowed(:invite_member) }
end

it { is_expected.not_to be_allowed(:create_team_role) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
before do
create(:team_member, team: team, user: current_user)
stub_allowed_ability(TeamPolicy, :invite_member, user: current_user, subject: team)
stub_allowed_ability(TeamPolicy, :read_team_member, user: current_user, subject: team)
end

it 'creates team member' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
before do
create(:team_member, team: team, user: current_user)
stub_allowed_ability(TeamPolicy, :create_team_role, user: current_user, subject: team)
stub_allowed_ability(TeamPolicy, :read_team_role, user: current_user, subject: team)
end

it 'creates team role' do
Expand Down
4 changes: 4 additions & 0 deletions spec/support/helpers/stub_ability.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# frozen_string_literal: true

module StubAbility
InvalidAbility = Class.new(StandardError)

def stub_allowed_ability(policy_class, ability, user: nil, subject: nil)
raise InvalidAbility, "Ability #{ability} does not exist" unless TeamRoleAbility::ABILITIES.key?(ability)

# rubocop:disable RSpec/AnyInstance -- policy instances are per user and subject
allow_any_instance_of(policy_class)
.to receive(:user_has_ability?)
Expand Down

0 comments on commit 88afadd

Please sign in to comment.