@@ -8,6 +8,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
8
8
alias PowPersistentSession . { Plug.Cookie , Store.PersistentSessionCache }
9
9
alias PowPersistentSession.Test.Users.User
10
10
11
+ @ cookie_key "persistent_session"
11
12
@ max_age Integer . floor_div ( :timer . hours ( 24 ) * 30 , 1000 )
12
13
@ custom_cookie_opts [ domain: "domain.com" , max_age: 1 , path: "/path" , http_only: false , secure: true , extra: "SameSite=Lax" ]
13
14
@@ -26,7 +27,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
26
27
{ :ok , % { conn: conn , config: config , ets: ets } }
27
28
end
28
29
29
- defp store_persistent ( conn , ets , id , value , cookie_key \\ "persistent_session_cookie" ) do
30
+ defp store_persistent ( conn , ets , id , value , cookie_key \\ @ cookie_key ) do
30
31
PersistentSessionCache . put ( [ backend: ets ] , id , value )
31
32
persistent_cookie ( conn , cookie_key , id )
32
33
end
@@ -41,7 +42,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
41
42
expected_config = [ mod: Session , plug: Session ] ++ config
42
43
43
44
assert { Cookie , ^ expected_config } = conn . private [ :pow_persistent_session ]
44
- refute conn . resp_cookies [ "persistent_session_cookie" ]
45
+ refute conn . resp_cookies [ @ cookie_key ]
45
46
end
46
47
47
48
test "call/2 assigns user from cookie" , % { conn: conn , ets: ets } do
@@ -53,7 +54,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
53
54
|> Cookie . call ( Cookie . init ( [ ] ) )
54
55
55
56
assert Plug . current_user ( conn ) == user
56
- assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ "persistent_session_cookie" ]
57
+ assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ @ cookie_key ]
57
58
refute new_id == id
58
59
assert PersistentSessionCache . get ( [ backend: ets ] , id ) == :not_found
59
60
assert PersistentSessionCache . get ( [ backend: ets ] , new_id ) == { [ id: 1 ] , [ ] }
@@ -68,7 +69,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
68
69
|> Cookie . call ( Cookie . init ( [ ] ) )
69
70
70
71
assert Plug . current_user ( conn ) == user
71
- assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ "persistent_session_cookie" ]
72
+ assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ @ cookie_key ]
72
73
refute new_id == id
73
74
assert PersistentSessionCache . get ( [ backend: ets ] , id ) == :not_found
74
75
assert PersistentSessionCache . get ( [ backend: ets ] , new_id ) == { [ id: 1 ] , session_metadata: [ fingerprint: "fingerprint" ] }
@@ -85,7 +86,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
85
86
|> Cookie . call ( Cookie . init ( [ ] ) )
86
87
87
88
assert Plug . current_user ( conn ) == user
88
- assert % { value: id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ "persistent_session_cookie" ]
89
+ assert % { value: id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ @ cookie_key ]
89
90
assert PersistentSessionCache . get ( [ backend: ets ] , id ) == { [ id: 1 ] , session_metadata: [ fingerprint: "new_fingerprint" , b: 2 , a: 2 ] }
90
91
assert [ inserted_at: _ , b: 2 , a: 3 , fingerprint: "new_fingerprint" ] = conn . private [ :pow_session_metadata ]
91
92
end
@@ -97,11 +98,11 @@ defmodule PowPersistentSession.Plug.CookieTest do
97
98
|> ConnHelpers . conn ( "/" )
98
99
|> ConnHelpers . init_session ( )
99
100
|> Session . call ( config ++ [ otp_app: :test_app ] )
100
- |> store_persistent ( ets , "test_app_test" , { [ id: user . id ] , [ ] } , "test_app_persistent_session_cookie" )
101
+ |> store_persistent ( ets , "test_app_test" , { [ id: user . id ] , [ ] } , "test_app_" <> @ cookie_key )
101
102
|> Cookie . call ( Cookie . init ( config ) )
102
103
103
104
assert Plug . current_user ( conn ) == user
104
- assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ "test_app_persistent_session_cookie" ]
105
+ assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ "test_app_" <> @ cookie_key ]
105
106
assert String . starts_with? ( new_id , "test_app" )
106
107
assert PersistentSessionCache . get ( [ backend: ets ] , new_id ) == { [ id: 1 ] , [ ] }
107
108
end
@@ -115,8 +116,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
115
116
|> Plug . assign_current_user ( :user , [ ] )
116
117
|> Cookie . call ( Cookie . init ( [ ] ) )
117
118
118
- assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ "persistent_session_cookie" ]
119
- assert new_id == id
119
+ refute conn . resp_cookies [ @ cookie_key ]
120
120
assert PersistentSessionCache . get ( [ backend: ets ] , id ) == { [ id: 1 ] , [ ] }
121
121
end
122
122
@@ -129,29 +129,29 @@ defmodule PowPersistentSession.Plug.CookieTest do
129
129
|> Cookie . call ( Cookie . init ( [ ] ) )
130
130
131
131
refute Plug . current_user ( conn )
132
- assert conn . resp_cookies [ "persistent_session_cookie" ] == % { max_age: 10 , path: "/" , value: "test" }
132
+ assert conn . resp_cookies [ @ cookie_key ] == % { max_age: 10 , path: "/" , value: "test" }
133
133
assert PersistentSessionCache . get ( [ backend: ets ] , id ) == :not_found
134
134
end
135
135
136
136
test "call/2 when persistent session cache doesn't have credentials" , % { conn: conn } do
137
137
conn =
138
138
conn
139
- |> persistent_cookie ( "persistent_session_cookie" , "test" )
139
+ |> persistent_cookie ( @ cookie_key , "test" )
140
140
|> Cookie . call ( Cookie . init ( [ ] ) )
141
141
142
142
refute Plug . current_user ( conn )
143
- assert conn . resp_cookies [ "persistent_session_cookie" ] == % { max_age: 10 , path: "/" , value: "test" }
143
+ assert conn . resp_cookies [ @ cookie_key ] == % { max_age: 10 , path: "/" , value: "test" }
144
144
end
145
145
146
146
test "call/2 when persistent session cache doesn't have credentials with custom cookie options" , % { conn: conn , config: config } do
147
147
config = Keyword . merge ( config , persistent_session_cookie_opts: @ custom_cookie_opts , persistent_session_cookie_expiration_timeout: 20 )
148
148
conn =
149
149
conn
150
- |> persistent_cookie ( "persistent_session_cookie" , "test" )
150
+ |> persistent_cookie ( @ cookie_key , "test" )
151
151
|> Cookie . call ( Cookie . init ( config ) )
152
152
153
153
refute Plug . current_user ( conn )
154
- assert conn . resp_cookies [ "persistent_session_cookie" ] == % {
154
+ assert conn . resp_cookies [ @ cookie_key ] == % {
155
155
domain: "domain.com" ,
156
156
extra: "SameSite=Lax" ,
157
157
http_only: false ,
@@ -183,7 +183,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
183
183
|> Cookie . call ( Cookie . init ( [ ] ) )
184
184
185
185
assert Plug . current_user ( conn ) == user
186
- assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ "persistent_session_cookie" ]
186
+ assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ @ cookie_key ]
187
187
refute new_id == id
188
188
assert PersistentSessionCache . get ( [ backend: ets ] , id ) == :not_found
189
189
assert PersistentSessionCache . get ( [ backend: ets ] , new_id ) == { [ id: 1 ] , [ ] }
@@ -199,7 +199,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
199
199
|> Cookie . call ( Cookie . init ( [ ] ) )
200
200
201
201
assert Plug . current_user ( conn ) == user
202
- assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ "persistent_session_cookie" ]
202
+ assert % { value: new_id , max_age: @ max_age , path: "/" } = conn . resp_cookies [ @ cookie_key ]
203
203
refute new_id == id
204
204
assert PersistentSessionCache . get ( [ backend: ets ] , id ) == :not_found
205
205
assert PersistentSessionCache . get ( [ backend: ets ] , new_id ) == { [ id: 1 ] , session_metadata: [ fingerprint: "fingerprint" ] }
@@ -213,7 +213,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
213
213
assert_received { :ets , :put , [ { _key , _value } | _rest ] , config }
214
214
assert config [ :ttl ] == 1000
215
215
216
- assert % { max_age: 1 , path: "/" } = conn . resp_cookies [ "persistent_session_cookie" ]
216
+ assert % { max_age: 1 , path: "/" } = conn . resp_cookies [ @ cookie_key ]
217
217
end
218
218
219
219
test "create/3 with custom cookie options" , % { conn: conn , config: config } do
@@ -227,7 +227,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
227
227
max_age: 1 ,
228
228
path: "/path" ,
229
229
secure: true
230
- } = conn . resp_cookies [ "persistent_session_cookie" ]
230
+ } = conn . resp_cookies [ @ cookie_key ]
231
231
end
232
232
233
233
test "create/3 deletes previous persistent session" , % { conn: conn , config: config , ets: ets } do
@@ -265,7 +265,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
265
265
|> Cookie . delete ( config )
266
266
267
267
refute Plug . current_user ( conn )
268
- assert conn . resp_cookies [ "persistent_session_cookie" ] == % { max_age: - 1 , path: "/" , value: "" }
268
+ assert conn . resp_cookies [ @ cookie_key ] == % { max_age: - 1 , path: "/" , value: "" }
269
269
assert PersistentSessionCache . get ( [ backend: ets ] , id ) == :not_found
270
270
end
271
271
@@ -278,7 +278,7 @@ defmodule PowPersistentSession.Plug.CookieTest do
278
278
|> Cookie . delete ( config )
279
279
280
280
refute Plug . current_user ( conn )
281
- assert conn . resp_cookies [ "persistent_session_cookie" ] == % {
281
+ assert conn . resp_cookies [ @ cookie_key ] == % {
282
282
domain: "domain.com" ,
283
283
extra: "SameSite=Lax" ,
284
284
http_only: false ,
0 commit comments