Skip to content

Commit

Permalink
Merge pull request #836 from varvet/kbs/add-useful-error-to-permissio…
Browse files Browse the repository at this point in the history
…ns-dsl

Make error message from `permit` more useful when you forget `permissions`
  • Loading branch information
Burgestrand authored Nov 21, 2024
2 parents d0d9f2b + d1c3d90 commit a885c79
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Deprecated `Pundit::SUFFIX`, moved it to `Pundit::PolicyFinder::SUFFIX` (#835)
- Explicitly require less of `active_support` (#837)
- Using `permit` matcher without a surrouding `permissions` block now raises a useful error. (#836)

## 2.4.0 (2024-08-26)

Expand Down
6 changes: 5 additions & 1 deletion lib/pundit/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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

0 comments on commit a885c79

Please sign in to comment.