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

Improved NotAuthorizedError message when record is a class #647

Closed
Closed
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
8 changes: 7 additions & 1 deletion lib/pundit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ def initialize(options = {})
@policy = options[:policy]
@reason = options[:reason]

message = options.fetch(:message) { "not allowed to #{query} this #{record.class}" }
name = if record.is_a?(Class)
record.name
else
"this #{record.class.name}"
end

message = options.fetch(:message) { "not allowed to #{query} #{name}" }
end

super(message)
Expand Down
10 changes: 10 additions & 0 deletions spec/pundit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@
it "raises an error with a invalid policy constructor" do
expect { controller.authorize(wiki, :destroy?) }.to raise_error(Pundit::InvalidConstructorError)
end

it "raises an error with class name when passed Class" do
expect { controller.authorize(Post, :index?) }
.to raise_error(Pundit::NotAuthorizedError, "not allowed to index? Post")
end

it "raises an error with class name when passed record" do
expect { controller.authorize(post, :destroy?) }
.to raise_error(Pundit::NotAuthorizedError, "not allowed to destroy? this Post")
end
end

describe "#skip_authorization" do
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def show?
true
end

def index?
false
end

def permitted_attributes
if post.user == user
%i[title votes]
Expand Down