Skip to content

Commit

Permalink
Merge pull request #108 from punditcommunity/bugfix/fix-composability
Browse files Browse the repository at this point in the history
Add back composability support
  • Loading branch information
chrisalley authored Jun 25, 2023
2 parents b1942be + 2b346e5 commit ec7d6a6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Pundit Matchers

## 3.1.1 (unreleased)

- Fix composability support

## 3.1.0 (2023-06-19)

- Add `*_attribute(s)` matchers for uniformity with `*_action(s)`
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
pundit-matchers (3.1.0)
pundit-matchers (3.1.1)
rspec-core (~> 3.12)
rspec-expectations (~> 3.12)
rspec-mocks (~> 3.12)
Expand Down
2 changes: 2 additions & 0 deletions lib/pundit/matchers/base_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Pundit
module Matchers
# This is the base class for all matchers in the Pundit Matchers library.
class BaseMatcher
include RSpec::Matchers::Composable

# Error message when an ambiguous negated matcher is used.
AMBIGUOUS_NEGATED_MATCHER_ERROR = <<~MSG
`expect().not_to %<name>s` is not supported since it creates ambiguity.
Expand Down
2 changes: 1 addition & 1 deletion pundit-matchers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = 'pundit-matchers'
s.version = '3.1.0'
s.version = '3.1.1'
s.summary = 'RSpec matchers for Pundit policies'
s.description = 'A set of RSpec matchers for testing Pundit authorisation ' \
'policies'
Expand Down
8 changes: 8 additions & 0 deletions spec/pundit/matchers/permit_actions_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@
it { is_expected.to permit_actions(:poke) }
end

it 'supports composability' do
policy = policy_factory(test1?: true, test2?: false)

expect(policy)
.to permit_actions(:test1)
.and forbid_actions(:test2)
end

context 'when expectation is met' do
subject(:policy) { policy_factory(test?: true) }

Expand Down
10 changes: 10 additions & 0 deletions spec/pundit/matchers/permit_attributes_matcher_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe Pundit::Matchers::PermitAttributesMatcher do
it_behaves_like 'a composable matcher'

it 'initializes expected attributes with a single attribute' do
expect(described_class.new(:test).send(:expected_attributes)).to eq %i[test]
end
Expand Down Expand Up @@ -214,6 +216,14 @@
"but permitted the mass assignment of [:test] for 'user'"
end

it 'supports composability' do
policy = policy_factory(permitted_attributes: %i[test1])

expect(policy)
.to permit_attribute(:test1)
.and forbid_attribute(:test2)
end

context 'with for_action chain' do
subject(:policy) do
policy_factory(permitted_attributes: %i[test1 test2],
Expand Down
8 changes: 8 additions & 0 deletions spec/shared_examples.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# frozen_string_literal: true

RSpec.shared_examples_for 'a composable matcher' do
it 'is composable' do
expect(described_class).to include(RSpec::Matchers::Composable)
end
end

RSpec.shared_examples_for 'an actions matcher' do
it_behaves_like 'a composable matcher'

it 'initializes expected actions with a single action' do
expect(described_class.new(:test).send(:expected_actions)).to eq %i[test]
end
Expand Down

0 comments on commit ec7d6a6

Please sign in to comment.