Skip to content

Commit

Permalink
Add authorise user constraint system spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Avlonitis committed Apr 24, 2024
1 parent a44f0bf commit dded52d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
8 changes: 4 additions & 4 deletions lib/gds-sso/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module GDS
module SSO
class Railtie < Rails::Railtie
config.action_dispatch.rescue_responses.merge!(
"GDS::SSO::PermissionDeniedError" => :forbidden,
)

initializer "gds-sso.initializer" do
GDS::SSO.config do |config|
config.cache = Rails.cache
config.api_only = Rails.configuration.api_only
end
OmniAuth.config.logger = Rails.logger

config.action_dispatch.rescue_responses.merge!(
"GDS::SSO::PermissionDeniedError" => :forbidden,
)
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion spec/internal/app/controllers/example_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class ExampleController < ApplicationController
before_action :authenticate_user!, except: :not_restricted
before_action -> { authorise_user!("execute") }, only: :this_requires_execute_permission

def not_restricted
render body: "jabberwocky"
end
Expand All @@ -13,4 +12,8 @@ def restricted
def this_requires_execute_permission
render body: "you have execute permission"
end

def constraint_restricted
render body: "constraint restricted"
end
end
4 changes: 4 additions & 0 deletions spec/internal/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
get "/not-restricted" => "example#not_restricted"
get "/restricted" => "example#restricted"
get "/this-requires-execute-permission" => "example#this_requires_execute_permission"

constraints(GDS::SSO::AuthorisedUserConstraint.new("execute")) do
get "/constraint-restricted" => "example#constraint_restricted"
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

Combustion.initialize! :all do
config.cache_store = :null_store
config.action_dispatch.show_exceptions = :all
end

require "rspec/rails"
Expand Down
21 changes: 21 additions & 0 deletions spec/system/authentication_and_authorisation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@
end
end

context "when accessing a route that is restricted by the authorised user constraint" do
it "allows access when an authenticated user has correct permissions" do
stub_signon_authenticated(permissions: %w[execute])
visit "/constraint-restricted"
expect(page).to have_content("constraint restricted")
end

it "redirects an unauthenticated request to signon" do
visit "/constraint-restricted"
expect(page.response_headers["Location"]).to match("/auth/gds")
visit page.response_headers["Location"]
expect(page.response_headers["Location"]).to match("http://signon/oauth/authorize")
end

it "restricts access when an authenticated user does not have the correct permissions" do
stub_signon_authenticated(permissions: %w[no-access])
visit "/constraint-restricted"
expect(page.status_code).to eq(403)
end
end

def stub_signon_authenticated(permissions: [])
# visit restricted page to trigger redirect URL to record state attribute
visit "/auth/gds"
Expand Down

0 comments on commit dded52d

Please sign in to comment.