1
1
require "support/base_controller_double"
2
+ require "support/using_cache"
2
3
3
4
module Kracken
4
5
class TokenAuthController < BaseControllerDouble
@@ -19,43 +20,16 @@ def authenticate_or_request_with_http_token(realm = nil)
19
20
end
20
21
21
22
RSpec . describe Controllers ::TokenAuthenticatable do
22
- describe "authenticating via a token" do
23
- context "on a cache hit" do
24
- it "munges the request headers to support parameterized tokens"
25
- it "leaves the request header unchange when with no parameterized token"
26
- it "uses the exising cache to bypass the authentication process"
27
- it "returns the auth info"
28
- it "exposes the auth info via the `current_` helpers"
29
- it "lazy loads the current user"
30
- end
23
+ describe "authenticating via a token" , :using_cache do
24
+ shared_examples "the authorization request headers" do |token_helper |
25
+ let ( :expected_token ) { public_send token_helper }
31
26
32
- context "on a cache miss with an invalid token" do
33
- it "munges the request headers to support parameterized tokens"
34
- it "leaves the request header unchange when with no parameterized token"
35
- it "follows the token authentication process"
36
- it "returns nil"
37
- it "doesn't cache invalid tokens"
38
- end
39
-
40
- context "on a cache miss with a valid token" do
41
- before do
42
- allow ( Authenticator ) . to receive ( :user_with_token )
43
- end
44
-
45
- it "follows the token authentication process"
46
- it "returns the auth info"
47
- it "exposes the auth info via the `current_` helpers"
48
- it "sets the auth info as the cache value"
49
- it "sets the cache expiration to one minute by default"
50
- it "sets the cache expiration to the environment setting `KRACKEN_TOKEN_TTL` when available"
51
- it "eager loads the current user"
52
-
53
- it "munges the request headers to support parameterized tokens" do
27
+ specify "are munged to include a provided parameterized token" do
54
28
controller = TokenAuthController . new
55
29
controller . request . env = {
56
30
'HTTP_AUTHORIZATION' => 'Token token="header token"'
57
31
}
58
- controller . params = { token : "param token" }
32
+ controller . params = { token : expected_token }
59
33
60
34
expect {
61
35
controller . authenticate_user_with_token!
@@ -64,27 +38,79 @@ def authenticate_or_request_with_http_token(realm = nil)
64
38
} . from (
65
39
'HTTP_AUTHORIZATION' => 'Token token="header token"'
66
40
) . to (
67
- 'HTTP_AUTHORIZATION' => ' Token token="param token"'
41
+ 'HTTP_AUTHORIZATION' => " Token token=\" #{ expected_token } \" "
68
42
)
69
43
end
70
44
71
- it "leaves the request header unchange when with no parameterized token" do
45
+ specify "are not modified when no parameterized token provided " do
72
46
controller = TokenAuthController . new
73
47
controller . request . env = {
74
- 'HTTP_AUTHORIZATION' => ' Token token="any token"'
48
+ 'HTTP_AUTHORIZATION' => " Token token=\" #{ expected_token } \" "
75
49
}
76
50
77
51
expect {
78
52
controller . authenticate_user_with_token!
79
53
} . not_to change { controller . request . env } . from (
80
- 'HTTP_AUTHORIZATION' => ' Token token="any token"'
54
+ 'HTTP_AUTHORIZATION' => " Token token=\" #{ expected_token } \" "
81
55
)
82
56
end
57
+ end
83
58
84
- it "authenticates the current user via the token" do
85
- a_user = instance_double ( User )
86
- allow ( Authenticator ) . to receive ( :user_with_token ) . with ( "any token" )
59
+ context "on a cache hit" do
60
+ let ( :cached_token ) { "any token" }
61
+ let ( :cache_key ) { "auth/token/any token" }
62
+
63
+ before do
64
+ Rails . cache . write ( cache_key , "auth info" )
65
+ end
66
+
67
+ include_examples "the authorization request headers" , :cached_token
68
+
69
+ it "uses the exising cache to bypass the authentication process"
70
+ it "returns the auth info"
71
+ it "exposes the auth info via the `current_` helpers"
72
+ it "lazy loads the current user"
73
+ end
74
+
75
+ context "on a cache miss with an invalid token" do
76
+ let ( :invalid_token ) { "any token" }
77
+
78
+ before do
79
+ allow ( Authenticator ) . to receive ( :user_with_token ) . with ( invalid_token )
80
+ . and_return ( nil )
81
+ end
82
+
83
+ include_examples "the authorization request headers" , :invalid_token
84
+
85
+ it "follows the token authentication process"
86
+ it "returns nil"
87
+ it "doesn't cache invalid tokens"
88
+ end
89
+
90
+ context "on a cache miss with a valid token" do
91
+ let ( :a_user ) {
92
+ instance_double ( User , id : user_id , team_ids : some_team_ids )
93
+ }
94
+ let ( :some_team_ids ) { [ :some , :team , :ids ] }
95
+ let ( :user_id ) { :any_id }
96
+ let ( :valid_token ) { "any token" }
97
+
98
+ before do
99
+ allow ( Authenticator ) . to receive ( :user_with_token ) . with ( valid_token )
87
100
. and_return ( a_user )
101
+ end
102
+
103
+ include_examples "the authorization request headers" , :valid_token
104
+
105
+ it "follows the token authentication process"
106
+ it "returns the auth info"
107
+ it "exposes the auth info via the `current_` helpers"
108
+ it "sets the auth info as the cache value"
109
+ it "sets the cache expiration to one minute by default"
110
+ it "sets the cache expiration to the environment setting `KRACKEN_TOKEN_TTL` when available"
111
+ it "eager loads the current user"
112
+
113
+ it "authenticates the current user via the token" do
88
114
controller = TokenAuthController . new
89
115
controller . request . env = {
90
116
'HTTP_AUTHORIZATION' => 'Token token="any token"'
0 commit comments