Skip to content

Commit

Permalink
Support filter_run_when_matching :focus with permissions helper
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenElberger committed Jul 25, 2024
1 parent e3b4408 commit 92c900d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Improve the `NotAuthorizedError` message to include the policy class.
Furthermore, in the case where the record passed is a class instead of an instance, the class name is given. (#812)
- Add support for filter_run_when_matching :focus with permissions helper.

## 2.3.2 (2024-05-08)

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,12 @@ which may be desirable when distributing policy specs as documentation.
An alternative approach to Pundit policy specs is scoping them to a user context as outlined in this
[excellent post](http://thunderboltlabs.com/blog/2013/03/27/testing-pundit-policies-with-rspec/) and implemented in the third party [pundit-matchers](https://github.com/punditcommunity/pundit-matchers) gem.

If your RSpec config has `filter_run_when_matching :focus`, you may tag the `permissions` helper like so:

```
permissions :show?, :focus do
```

### Scope Specs

Pundit does not provide a DSL for testing scopes. Test them like you would a regular Ruby class!
Expand Down
10 changes: 9 additions & 1 deletion lib/pundit/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ def permissions

module DSL
def permissions(*list, &block)
describe(list.to_sentence, permissions: list, caller: caller) { instance_eval(&block) }
metadata = { permissions: list, caller: caller }

if list.last == :focus
list.pop
metadata[:focus] = true
end

description = list.to_sentence
describe(description, metadata) { instance_eval(&block) }
end
end

Expand Down
29 changes: 29 additions & 0 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require "spec_helper"

RSpec.describe "Pundit RSpec DSL" do
before do
RSpec.configure do |config|
config.include ::Pundit::RSpec::DSL
end
end

let(:block) { proc { "block content" } }

it "calls describe with the correct metadata and without :focus" do
expected_metadata = { permissions: %i[:item1, :item2], caller: instance_of(Array) }
expect(self).to receive(:describe).with("item1 and item2", hash_including(expected_metadata)) do |&block|
expect(block.call).to eq("block content")
end

permissions(:item1, :item2, &block)
end

it "calls describe with the correct metadata and with :focus" do
expected_metadata = { permissions: %i[:item1, :item2], caller: instance_of(Array), focus: true }
expect(self).to receive(:describe).with("item1 and item2", hash_including(expected_metadata)) do |&block|
expect(block.call).to eq("block content")
end

permissions(:item1, :item2, :focus, &block)
end
end

0 comments on commit 92c900d

Please sign in to comment.