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 sure Pundit exception's query type is String #205

Merged
merged 2 commits into from
Jul 28, 2020
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
2 changes: 1 addition & 1 deletion lib/insights/api/common/custom_exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class CustomExceptions
def self.custom_message(exception)
case exception.class.to_s
when "Pundit::NotAuthorizedError"
"You are not authorized to #{exception.query.delete_suffix('?')} this #{exception.record.model_name.human.downcase}"
"You are not authorized to #{exception.query.to_s.delete_suffix('?')} this #{exception.record.model_name.human.downcase}"
end
end
end
Expand Down
20 changes: 16 additions & 4 deletions spec/lib/insights/api/common/custom_exceptions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
describe Insights::API::Common::CustomExceptions do
describe ".custom_message" do
context "when the exception is a Pundit::NotAuthorizedError" do
let(:record) { SourceType.new }
let(:exception) { double(:class => "Pundit::NotAuthorizedError", :query => "create?", :record => record) }
describe ".custom_message with Pundit::NotAuthorizedError exception" do
let(:record) { SourceType.new }
let(:exception) { double(:class => "Pundit::NotAuthorizedError", :query => query, :record => record) }

shared_examples_for "#test_message" do
it "returns a customized message" do
expect(Insights::API::Common::CustomExceptions.custom_message(exception)).to eq(
"You are not authorized to create this source type"
)
end
end

context "when the query is String" do
let(:query) { "create?" }

it_behaves_like "#test_message"
end

context "when the query is Symbol" do
let(:query) { :create? }

it_behaves_like "#test_message"
end
end
end