-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Tom de Grunt <[email protected]>
- Loading branch information
Showing
11 changed files
with
185 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test/dummy/**/* |
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "passwordless/controller_helpers" | ||
|
||
module Passwordless | ||
# A class the constraint routes to authenticated records | ||
class Constraint | ||
include ControllerHelpers | ||
|
||
attr_reader :authenticatable_type, :predicate, :session | ||
|
||
# @param [Class] authenticatable_type Authenticatable class | ||
# @option options [Proc] :if A lambda that takes an authenticatable and returns a boolean | ||
def initialize(authenticatable_type, **options) | ||
@authenticatable_type = authenticatable_type | ||
# `if' is a keyword but so we do this instead of keyword arguments | ||
@predicate = options.fetch(:if) { -> (_) { true } } | ||
end | ||
|
||
def matches?(request) | ||
# used in authenticate_by_session | ||
@session = request.session | ||
authenticatable = authenticate_by_session(authenticatable_type) | ||
!!(authenticatable && predicate.call(authenticatable)) | ||
end | ||
end | ||
|
||
# A class the constraint routes to NOT authenticated records | ||
class ConstraintNot < Constraint | ||
# @param [Class] authenticatable_type Authenticatable class | ||
# @option options [Proc] :if A lambda that takes an authenticatable and returns a boolean | ||
def initialize(authenticatable_type, **options) | ||
super | ||
end | ||
|
||
def matches?(request) | ||
!super | ||
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
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
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "passwordless/test_helpers" | ||
|
||
module Passwordless | ||
class ConstraintTest < ActionDispatch::IntegrationTest | ||
fixtures :users | ||
|
||
test("restricts to users") do | ||
assert_raises(ActionController::RoutingError) do | ||
get "/constraint/only-user" | ||
end | ||
|
||
alice = users(:alice) | ||
passwordless_sign_in(alice) | ||
|
||
get "/constraint/only-user" | ||
assert_response :success | ||
end | ||
|
||
test("restricts with predicate") do | ||
alice = users(:alice) | ||
passwordless_sign_in(alice) | ||
|
||
assert_raises(ActionController::RoutingError) do | ||
get "/constraint/only-john" | ||
end | ||
|
||
john = users(:john) | ||
passwordless_sign_in(john) | ||
|
||
get "/constraint/only-john" | ||
assert_response :success | ||
end | ||
|
||
test("negative version") do | ||
get "/constraint/not-user" | ||
# redirect because not signed in but route is still matched | ||
assert_response :redirect | ||
end | ||
|
||
test("negative version with predicate") do | ||
passwordless_sign_in(users(:alice)) | ||
assert_raises(ActionController::RoutingError) do | ||
get "/constraint/not-user" | ||
end | ||
|
||
get "/constraint/not-john" | ||
assert_response :success | ||
|
||
passwordless_sign_in(users(:john)) | ||
assert_raises(ActionController::RoutingError) do | ||
get "/constraint/not-john" | ||
end | ||
end | ||
end | ||
end |