-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from eclarizio/update_custom_pundit_exceptions
Use custom error_message if a policy exposes it
- Loading branch information
Showing
4 changed files
with
31 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,41 @@ | ||
describe Insights::API::Common::CustomExceptions do | ||
describe ".custom_message with Pundit::NotAuthorizedError exception" do | ||
let(:record) { SourceType.new } | ||
let(:exception) { double(:class => "Pundit::NotAuthorizedError", :query => query, :record => record) } | ||
let(:exception) { double(:class => "Pundit::NotAuthorizedError", :query => query, :record => record, :policy => policy) } | ||
|
||
context "when a custom error message exists on the policy" do | ||
let(:query) { "create?" } | ||
let(:policy) { double(:error_message => "This custom error message says 'no', you can't do that") } | ||
|
||
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" | ||
"This custom error message says 'no', you can't do that" | ||
) | ||
end | ||
end | ||
|
||
context "when the query is String" do | ||
let(:query) { "create?" } | ||
context "when a custom error message does not exist on the policy" do | ||
let(:policy) { nil } | ||
|
||
it_behaves_like "#test_message" | ||
end | ||
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 perform the create action for this source type" | ||
) | ||
end | ||
end | ||
|
||
context "when the query is Symbol" do | ||
let(:query) { :create? } | ||
context "when the query is String" do | ||
let(:query) { "create?" } | ||
|
||
it_behaves_like "#test_message" | ||
it_behaves_like "#test_message" | ||
end | ||
|
||
context "when the query is Symbol" do | ||
let(:query) { :create? } | ||
|
||
it_behaves_like "#test_message" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters