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 Aug 8, 2024
1 parent e3b4408 commit 36dd293
Show file tree
Hide file tree
Showing 4 changed files with 51 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. (#820)

## 2.3.2 (2024-05-08)

Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,14 @@ 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.

### Focus Support

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
33 changes: 33 additions & 0 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "spec_helper"

RSpec.describe "Pundit RSpec DSL" do
let(:fake_rspec) do
double = class_double(RSpec::ExampleGroups)
double.extend(::Pundit::RSpec::DSL)
allow(double).to receive(:describe).and_yield
double
end
let(:block) { proc { "block content" } }

it "calls describe with the correct metadata and without :focus" do
expect { |block| fake_rspec.permissions(:item1, :item2, &block) }.to yield_control

expected_metadata = { permissions: %i[item1 item2], caller: instance_of(Array) }
expect(fake_rspec).to receive(:describe).with("item1 and item2", match(expected_metadata)) do |&block|
expect(block.call).to eq("block content")
end

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

it "calls describe with the correct metadata and with :focus" do
expect { |block| fake_rspec.permissions(&block) }.to yield_control

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

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

0 comments on commit 36dd293

Please sign in to comment.