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

Make error message from permit more useful when you forget permissions #836

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

- Using `permit` matcher without a surrouding `permissions` block now raises a useful error. (#836)

## 2.4.0 (2024-08-26)

## Changed
Expand Down
6 changes: 5 additions & 1 deletion lib/pundit/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ def current_example
end

def permissions
current_example.metadata[:permissions]
current_example.metadata.fetch(:permissions) do
raise KeyError, <<~ERROR.strip
No permissions in example metadata, did you forget to wrap with `permissions :show?, ...`?
ERROR
end
end
end
# rubocop:enable Metrics/BlockLength
Expand Down
10 changes: 10 additions & 0 deletions spec/rspec_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
end

describe "#permit" do
context "when not appropriately wrapped in permissions" do
it "raises a descriptive error" do
expect do
expect(policy).to permit(user, post)
end.to raise_error(KeyError, <<~MSG.strip)
No permissions in example metadata, did you forget to wrap with `permissions :show?, ...`?
MSG
end
end

permissions :edit?, :update? do
it "succeeds when action is permitted" do
expect(policy).to permit(user, post)
Expand Down