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 10, 2024
1 parent 85679c9 commit 3c529a8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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 customizable permit matcher description (#806)
- 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 @@ -821,6 +821,14 @@ PostPolicy
is expected to permit user with role admin to access record with ID 130
```

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

RSpec.describe "Pundit RSpec DSL" do
let(:fake_rspec) do
double = class_double(RSpec::ExampleGroups)
double.extend(::Pundit::RSpec::DSL)
double
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(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
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 3c529a8

Please sign in to comment.