Skip to content

Commit 095a0f8

Browse files
committed
Add tests for simultaneous requests
1 parent 8862763 commit 095a0f8

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

test/extensions/persistent_session/phoenix/controllers/controller_callbacks_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule PowPersistentSession.Phoenix.ControllerCallbacksTest do
3232
end
3333

3434
describe "Pow.Phoenix.SessionController.delete/2" do
35-
test "generates cookie", %{conn: conn, ets: ets} do
35+
test "expires cookie", %{conn: conn, ets: ets} do
3636
conn = post conn, Routes.pow_session_path(conn, :create, %{"user" => @valid_params})
3737

3838
assert %{value: id} = conn.resp_cookies[@cookie_key]

test/extensions/persistent_session/plug/cookie_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ defmodule PowPersistentSession.Plug.CookieTest do
6060
assert PersistentSessionCache.get([backend: ets], new_id) == {[id: 1], []}
6161
end
6262

63+
test "call/2 assigns user from cookie and doesn't expire with simultanous request", %{conn: conn, ets: ets} do
64+
user = %User{id: 1}
65+
id = "test"
66+
opts = Cookie.init([])
67+
conn = store_persistent(conn, ets, id, {[id: user.id], []})
68+
69+
first_conn =
70+
conn
71+
|> Cookie.call(opts)
72+
|> Conn.resp(200, "")
73+
74+
assert Plug.current_user(first_conn) == user
75+
assert %{value: _id, max_age: @max_age, path: "/"} = first_conn.resp_cookies[@cookie_key]
76+
77+
second_conn =
78+
conn
79+
|> Cookie.call(opts)
80+
|> Conn.resp(200, "")
81+
82+
refute Plug.current_user(second_conn) == user
83+
refute second_conn.resp_cookies[@cookie_key]
84+
end
85+
6386
test "call/2 assigns user from cookie passing fingerprint to the session metadata", %{conn: conn, ets: ets} do
6487
user = %User{id: 1}
6588
id = "test"

test/pow/plug/session_test.exs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,43 @@ defmodule Pow.Plug.SessionTest do
120120
assert conn.private[:pow_session_metadata][:fingerprint] == "fingerprint"
121121
end
122122

123+
test "call/2 creates new session when :session_renewal_ttl reached and doesn't delete with simultanous request", %{conn: new_conn} do
124+
ttl = 100
125+
id = "token"
126+
config = Keyword.put(@default_opts, :session_ttl_renewal, ttl)
127+
stale_timestamp = :os.system_time(:millisecond) - ttl - 1
128+
opts = Session.init(config)
129+
130+
CredentialsCache.put(@store_config, id, {@user, inserted_at: stale_timestamp})
131+
132+
conn =
133+
new_conn
134+
|> Conn.fetch_session()
135+
|> Conn.put_session(config[:session_key], id)
136+
|> Conn.send_resp(200, "")
137+
138+
conn = Test.recycle_cookies(new_conn, conn)
139+
140+
first_conn =
141+
conn
142+
|> Session.call(opts)
143+
|> Conn.send_resp(200, "")
144+
145+
assert Plug.current_user(first_conn) == @user
146+
assert %{value: _id} = first_conn.resp_cookies["foobar"]
147+
assert new_id = first_conn.private[:plug_session][config[:session_key]]
148+
refute new_id == id
149+
assert {@user, _metadata} = CredentialsCache.get(@store_config, new_id)
150+
151+
second_conn =
152+
conn
153+
|> Session.call(opts)
154+
|> Conn.send_resp(200, "")
155+
156+
refute second_conn.resp_cookies["foobar"]
157+
refute second_conn.private[:plug_session]
158+
end
159+
123160
test "call/2 with prepended `:otp_app` session key", %{conn: conn} do
124161
CredentialsCache.put(@store_config, "token", {@user, inserted_at: :os.system_time(:millisecond)})
125162

0 commit comments

Comments
 (0)