-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-use authorisation logic in the controller_methods class
- Loading branch information
Alex Avlonitis
committed
Apr 23, 2024
1 parent
da351b0
commit acec73d
Showing
2 changed files
with
15 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,24 @@ | ||
require "spec_helper" | ||
|
||
RSpec.describe GDS::SSO::ControllerMethods, "#authorise_user!" do | ||
let(:current_user) { double } | ||
let(:expected_error) { GDS::SSO::ControllerMethods::PermissionDeniedException } | ||
RSpec.describe GDS::SSO::ControllerMethods do | ||
describe "#authorise_user!" do | ||
let(:current_user) { double } | ||
let(:expected_error) { GDS::SSO::PermissionDeniedError } | ||
|
||
context "with a single string permission argument" do | ||
it "permits users with the required permission" do | ||
allow(current_user).to receive(:has_permission?).with("good").and_return(true) | ||
context "when the user is authorised" do | ||
it "does not raise an error" do | ||
allow(current_user).to receive(:has_permission?).with("good").and_return(true) | ||
|
||
expect { ControllerSpy.new(current_user).authorise_user!("good") }.not_to raise_error | ||
expect { ControllerSpy.new(current_user).authorise_user!("good") }.not_to raise_error | ||
end | ||
end | ||
|
||
it "does not permit the users without the required permission" do | ||
allow(current_user).to receive(:has_permission?).with("good").and_return(false) | ||
context "when the user is not authorised" do | ||
it "raises an error" do | ||
allow(current_user).to receive(:has_permission?).with("bad").and_return(false) | ||
|
||
expect { ControllerSpy.new(current_user).authorise_user!("good") }.to raise_error(expected_error) | ||
end | ||
end | ||
|
||
context "with the `all_of` option" do | ||
it "permits users with all of the required permissions" do | ||
allow(current_user).to receive(:has_permission?).with("good").and_return(true) | ||
allow(current_user).to receive(:has_permission?).with("bad").and_return(true) | ||
|
||
expect { ControllerSpy.new(current_user).authorise_user!(all_of: %w[good bad]) }.not_to raise_error | ||
end | ||
|
||
it "does not permit users without all of the required permissions" do | ||
allow(current_user).to receive(:has_permission?).with("good").and_return(false) | ||
allow(current_user).to receive(:has_permission?).with("bad").and_return(true) | ||
|
||
expect { ControllerSpy.new(current_user).authorise_user!(all_of: %w[good bad]) }.to raise_error(expected_error) | ||
end | ||
end | ||
|
||
context "with the `any_of` option" do | ||
it "permits users with any of the required permissions" do | ||
allow(current_user).to receive(:has_permission?).with("good").and_return(true) | ||
allow(current_user).to receive(:has_permission?).with("bad").and_return(false) | ||
|
||
expect { ControllerSpy.new(current_user).authorise_user!(any_of: %w[good bad]) }.not_to raise_error | ||
end | ||
|
||
it "does not permit users without any of the required permissions" do | ||
allow(current_user).to receive(:has_permission?).and_return(false) | ||
|
||
expect { ControllerSpy.new(current_user).authorise_user!(any_of: %w[good bad]) }.to raise_error(expected_error) | ||
end | ||
end | ||
|
||
context "with none of `any_of` or `all_of`" do | ||
it "raises an `ArgumentError`" do | ||
expect { ControllerSpy.new(current_user).authorise_user!(whoops: "bad") }.to raise_error(ArgumentError) | ||
expect { ControllerSpy.new(current_user).authorise_user!("bad") }.to raise_error(expected_error) | ||
end | ||
end | ||
end | ||
end |