Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support :focus for rspec permissions blocks #820

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Burgestrand marked this conversation as resolved.
Show resolved Hide resolved

```
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
30 changes: 30 additions & 0 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

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