Skip to content

Commit ea2152e

Browse files
committed
Refactor EtsCacheMock handling
1 parent ce52bc6 commit ea2152e

File tree

8 files changed

+96
-86
lines changed

8 files changed

+96
-86
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
defmodule PowPersistentSession.Phoenix.ControllerCallbacksTest do
22
use PowPersistentSession.TestWeb.Phoenix.ConnCase
33

4-
alias Pow.Test.EtsCacheMock
54
alias PowPersistentSession.Store.PersistentSessionCache
65

76
@valid_params %{"email" => "[email protected]", "password" => "secret1234"}
87
@max_age Integer.floor_div(:timer.hours(30) * 24, 1000)
98

109
describe "Pow.Phoenix.SessionController.create/2" do
11-
test "generates cookie", %{conn: conn} do
10+
test "generates cookie", %{conn: conn, ets: ets} do
1211
conn = post conn, Routes.pow_session_path(conn, :create, %{"user" => @valid_params})
1312

1413
assert %{max_age: @max_age, path: "/", value: id} = conn.resp_cookies["persistent_session_cookie"]
15-
assert PersistentSessionCache.get([backend: EtsCacheMock], id) == 1
14+
assert PersistentSessionCache.get([backend: ets], id) == 1
1615
end
1716

1817
test "with persistent_session param set to false", %{conn: conn} do
@@ -32,13 +31,13 @@ defmodule PowPersistentSession.Phoenix.ControllerCallbacksTest do
3231
end
3332

3433
describe "Pow.Phoenix.SessionController.delete/2" do
35-
test "generates cookie", %{conn: conn} do
34+
test "generates cookie", %{conn: conn, ets: ets} do
3635
conn = post conn, Routes.pow_session_path(conn, :create, %{"user" => @valid_params})
3736
%{value: id} = conn.resp_cookies["persistent_session_cookie"]
3837
conn = delete conn, Routes.pow_session_path(conn, :delete)
3938

4039
assert %{max_age: -1, path: "/", value: ""} = conn.resp_cookies["persistent_session_cookie"]
41-
assert PersistentSessionCache.get([backend: EtsCacheMock], id) == :not_found
40+
assert PersistentSessionCache.get([backend: ets], id) == :not_found
4241
end
4342
end
4443
end

test/extensions/persistent_session/plug/cookie_test.exs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,29 @@ defmodule PowPersistentSession.Plug.CookieTest do
33
doctest PowPersistentSession.Plug.Cookie
44

55
alias Pow.{Plug, Plug.Session}
6-
alias Pow.Test.{ConnHelpers, EtsCacheMock}
6+
alias Pow.Test.ConnHelpers
77
alias PowPersistentSession.{Plug.Cookie, Store.PersistentSessionCache}
88
alias PowPersistentSession.Test.Users.User
99

1010
@max_age Integer.floor_div(:timer.hours(24) * 30, 1000)
1111

1212
setup do
13-
EtsCacheMock.init()
13+
config = [otp_app: PowPersistentSession.TestWeb]
14+
ets = Pow.Config.get(config, :cache_store_backend, nil)
15+
16+
ets.init()
17+
1418
conn =
1519
:get
1620
|> ConnHelpers.conn("/")
1721
|> ConnHelpers.init_session()
18-
|> Session.call([otp_app: PowPersistentSession.TestWeb])
22+
|> Session.call(config)
1923

20-
{:ok, %{conn: conn}}
24+
{:ok, %{conn: conn, config: config, ets: ets}}
2125
end
2226

23-
defp store_persistent(conn, id, user) do
24-
PersistentSessionCache.put([backend: EtsCacheMock], id, user.id)
27+
defp store_persistent(conn, ets, id, user) do
28+
PersistentSessionCache.put([backend: ets], id, user.id)
2529
persistent_cookie(conn, id)
2630
end
2731

@@ -30,53 +34,54 @@ defmodule PowPersistentSession.Plug.CookieTest do
3034
%{conn | req_cookies: cookies, cookies: cookies}
3135
end
3236

33-
test "call/2 sets pow_persistent_session_mod in conn", %{conn: conn} do
34-
conn = Cookie.call(conn, Cookie.init([]))
37+
test "call/2 sets pow_persistent_session_mod in conn", %{conn: conn, config: config} do
38+
conn = Cookie.call(conn, Cookie.init([]))
39+
expected_config = [mod: Session] ++ config
3540

36-
assert {Cookie, [mod: Session, otp_app: PowPersistentSession.TestWeb]} = conn.private[:pow_persistent_session]
41+
assert {Cookie, ^expected_config} = conn.private[:pow_persistent_session]
3742
refute conn.resp_cookies["persistent_session_cookie"]
3843
end
3944

40-
test "call/2 assigns user from cookie", %{conn: conn} do
45+
test "call/2 assigns user from cookie", %{conn: conn, ets: ets} do
4146
user = %User{id: 1}
4247
id = "test"
4348
conn =
4449
conn
45-
|> store_persistent(id, user)
50+
|> store_persistent(ets, id, user)
4651
|> Cookie.call(Cookie.init([]))
4752

4853
assert Plug.current_user(conn) == user
4954
assert %{value: new_id, max_age: @max_age, path: "/"} = conn.resp_cookies["persistent_session_cookie"]
5055
refute new_id == id
51-
assert PersistentSessionCache.get([backend: EtsCacheMock], id) == :not_found
52-
assert PersistentSessionCache.get([backend: EtsCacheMock], new_id) == 1
56+
assert PersistentSessionCache.get([backend: ets], id) == :not_found
57+
assert PersistentSessionCache.get([backend: ets], new_id) == 1
5358
end
5459

55-
test "call/2 when user already assigned", %{conn: conn} do
60+
test "call/2 when user already assigned", %{conn: conn, ets: ets} do
5661
user = %User{id: 1}
5762
id = "test"
5863
conn =
5964
conn
60-
|> store_persistent(id, user)
65+
|> store_persistent(ets, id, user)
6166
|> Plug.assign_current_user(:user, [])
6267
|> Cookie.call(Cookie.init([]))
6368

6469
assert %{value: new_id, max_age: @max_age, path: "/"} = conn.resp_cookies["persistent_session_cookie"]
6570
assert new_id == id
66-
assert PersistentSessionCache.get([backend: EtsCacheMock], id) == 1
71+
assert PersistentSessionCache.get([backend: ets], id) == 1
6772
end
6873

69-
test "call/2 when user doesn't exist in database", %{conn: conn} do
74+
test "call/2 when user doesn't exist in database", %{conn: conn, ets: ets} do
7075
user = %User{id: -1}
7176
id = "test"
7277
conn =
7378
conn
74-
|> store_persistent(id, user)
79+
|> store_persistent(ets, id, user)
7580
|> Cookie.call(Cookie.init([]))
7681

7782
refute Plug.current_user(conn)
7883
assert conn.resp_cookies["persistent_session_cookie"] == %{max_age: -1, path: "/", value: ""}
79-
assert PersistentSessionCache.get([backend: EtsCacheMock], id) == :not_found
84+
assert PersistentSessionCache.get([backend: ets], id) == :not_found
8085
end
8186

8287
test "call/2 when persistent session cache doesn't have credentials", %{conn: conn} do

test/extensions/reset_password/phoenix/controllers/reset_password_controller_test.exs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
defmodule PowResetPassword.Phoenix.ResetPasswordControllerTest do
22
use PowResetPassword.TestWeb.Phoenix.ConnCase
33

4-
alias Pow.Test.EtsCacheMock
54
alias PowResetPassword.Store.ResetTokenCache
65
alias PowResetPassword.Test.Users.User
76

@@ -65,10 +64,10 @@ defmodule PowResetPassword.Phoenix.ResetPasswordControllerTest do
6564
@valid_token "valid"
6665
@invalid_token "invalid"
6766

68-
setup %{conn: conn} do
69-
ResetTokenCache.put([backend: EtsCacheMock], @valid_token, @user)
67+
setup %{conn: conn, ets: ets} do
68+
ResetTokenCache.put([backend: ets], @valid_token, @user)
7069

71-
{:ok, conn: conn}
70+
{:ok, conn: conn, ets: ets}
7271
end
7372

7473
test "already signed in", %{conn: conn} do
@@ -105,10 +104,10 @@ defmodule PowResetPassword.Phoenix.ResetPasswordControllerTest do
105104
@valid_params %{"user" => %{"password" => @password, "confirm_password" => @password}}
106105
@invalid_params %{"user" => %{"password" => @password, "confirm_password" => "invalid"}}
107106

108-
setup %{conn: conn} do
109-
ResetTokenCache.put([backend: EtsCacheMock], @valid_token, @user)
107+
setup %{conn: conn, ets: ets} do
108+
ResetTokenCache.put([backend: ets], @valid_token, @user)
110109

111-
{:ok, conn: conn}
110+
{:ok, conn: conn, ets: ets}
112111
end
113112

114113
test "already signed in", %{conn: conn} do
@@ -127,16 +126,16 @@ defmodule PowResetPassword.Phoenix.ResetPasswordControllerTest do
127126
assert get_flash(conn, :error) == "The reset token has expired."
128127
end
129128

130-
test "with valid params", %{conn: conn} do
129+
test "with valid params", %{conn: conn, ets: ets} do
131130
conn = put conn, Routes.pow_reset_password_reset_password_path(conn, :update, @valid_token, @valid_params)
132131

133132
assert redirected_to(conn) == Routes.pow_session_path(conn, :new)
134133
assert get_flash(conn, :info) == "The password has been updated."
135134

136-
assert ResetTokenCache.get([backend: EtsCacheMock], @valid_token) == :not_found
135+
assert ResetTokenCache.get([backend: ets], @valid_token) == :not_found
137136
end
138137

139-
test "with invalid params", %{conn: conn} do
138+
test "with invalid params", %{conn: conn, ets: ets} do
140139
conn = put conn, Routes.pow_reset_password_reset_password_path(conn, :update, @valid_token, @invalid_params)
141140

142141
assert html = html_response(conn, 200)
@@ -146,7 +145,7 @@ defmodule PowResetPassword.Phoenix.ResetPasswordControllerTest do
146145
assert errors = conn.assigns[:changeset].errors
147146
assert errors[:confirm_password]
148147

149-
assert ResetTokenCache.get([backend: EtsCacheMock], @valid_token) == @user
148+
assert ResetTokenCache.get([backend: ets], @valid_token) == @user
150149
end
151150
end
152151
end

test/pow/plug/session_test.exs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@ defmodule Pow.Plug.SessionTest do
44

55
alias Plug.Conn
66
alias Pow.{Config, Plug, Plug.Session, Store.CredentialsCache}
7-
alias Pow.Test.{ConnHelpers, EtsCacheMock}
7+
alias Pow.Test.ConnHelpers
88

9+
@ets Pow.Test.EtsCacheMock
910
@default_opts [
1011
current_user_assigns_key: :current_user,
1112
session_key: "auth",
12-
cache_store_backend: EtsCacheMock
13+
cache_store_backend: @ets
1314
]
1415

1516
setup do
16-
EtsCacheMock.init()
17+
@ets.init()
1718
conn =
1819
:get
1920
|> ConnHelpers.conn("/")
2021
|> ConnHelpers.init_session()
2122

22-
{:ok, %{conn: conn}}
23+
{:ok, %{conn: conn, ets: @ets}}
2324
end
2425

2526
test "call/2 sets mod in :pow_config", %{conn: conn} do
@@ -40,8 +41,8 @@ defmodule Pow.Plug.SessionTest do
4041
assert conn.assigns[:current_user] == "assigned"
4142
end
4243

43-
test "call/2 with stored current_user", %{conn: conn} do
44-
EtsCacheMock.put(nil, "token", {"cached", :os.system_time(:millisecond)})
44+
test "call/2 with stored current_user", %{conn: conn, ets: ets} do
45+
ets.put(nil, "token", {"cached", :os.system_time(:millisecond)})
4546

4647
opts = Session.init(@default_opts)
4748
conn =
@@ -53,8 +54,8 @@ defmodule Pow.Plug.SessionTest do
5354
assert conn.assigns[:current_user] == "cached"
5455
end
5556

56-
test "call/2 with non existing cached key", %{conn: conn} do
57-
EtsCacheMock.put(nil, "token", "cached")
57+
test "call/2 with non existing cached key", %{conn: conn, ets: ets} do
58+
ets.put(nil, "token", "cached")
5859

5960
opts = Session.init(@default_opts)
6061
conn =
@@ -66,7 +67,7 @@ defmodule Pow.Plug.SessionTest do
6667
assert is_nil(conn.assigns[:current_user])
6768
end
6869

69-
test "call/2 creates new session when :session_renewal_ttl reached", %{conn: conn} do
70+
test "call/2 creates new session when :session_renewal_ttl reached", %{conn: conn, ets: ets} do
7071
ttl = 100
7172
config = Keyword.put(@default_opts, :session_ttl_renewal, ttl)
7273
timestamp = :os.system_time(:millisecond)
@@ -76,15 +77,15 @@ defmodule Pow.Plug.SessionTest do
7677
|> Conn.fetch_session()
7778
|> Conn.put_session(config[:session_key], "token")
7879

79-
EtsCacheMock.put(nil, "token", {"cached", timestamp})
80+
ets.put(nil, "token", {"cached", timestamp})
8081

8182
opts = Session.init(config)
8283
conn = Session.call(init_conn, opts)
8384
session_id = get_session_id(conn)
8485

8586
assert conn.assigns[:current_user] == "cached"
8687

87-
EtsCacheMock.put(nil, "token", {"cached", stale_timestamp})
88+
ets.put(nil, "token", {"cached", stale_timestamp})
8889

8990
conn = Session.call(init_conn, opts)
9091

@@ -93,7 +94,7 @@ defmodule Pow.Plug.SessionTest do
9394
assert new_session_id != session_id
9495
end
9596

96-
test "create/2 creates new session id", %{conn: conn} do
97+
test "create/2 creates new session id", %{conn: conn, ets: ets} do
9798
user = %{id: 1}
9899
opts = Session.init(@default_opts)
99100
conn =
@@ -102,24 +103,24 @@ defmodule Pow.Plug.SessionTest do
102103
|> Session.do_create(user)
103104

104105
session_id = get_session_id(conn)
105-
{etc_user, _inserted_at} = EtsCacheMock.get(nil, session_id)
106+
{etc_user, _inserted_at} = ets.get(nil, session_id)
106107

107108
assert is_binary(session_id)
108109
assert etc_user == user
109110
assert Plug.current_user(conn) == user
110111

111112
conn = Session.do_create(conn, user)
112113
new_session_id = get_session_id(conn)
113-
{etc_user, _inserted_at} = EtsCacheMock.get(nil, new_session_id)
114+
{etc_user, _inserted_at} = ets.get(nil, new_session_id)
114115

115116
assert is_binary(session_id)
116117
assert new_session_id != session_id
117-
assert EtsCacheMock.get(nil, session_id) == :not_found
118+
assert ets.get(nil, session_id) == :not_found
118119
assert etc_user == user
119120
assert Plug.current_user(conn) == user
120121
end
121122

122-
test "delete/1 removes session id", %{conn: conn} do
123+
test "delete/1 removes session id", %{conn: conn, ets: ets} do
123124
user = %{id: 1}
124125
opts = Session.init(@default_opts)
125126
conn =
@@ -128,7 +129,7 @@ defmodule Pow.Plug.SessionTest do
128129
|> Session.do_create(user)
129130

130131
session_id = get_session_id(conn)
131-
{etc_user, _inserted_at} = EtsCacheMock.get(nil, session_id)
132+
{etc_user, _inserted_at} = ets.get(nil, session_id)
132133

133134
assert is_binary(session_id)
134135
assert etc_user == user
@@ -138,7 +139,7 @@ defmodule Pow.Plug.SessionTest do
138139

139140
refute new_session_id = get_session_id(conn)
140141
assert is_nil(new_session_id)
141-
assert EtsCacheMock.get(nil, session_id) == :not_found
142+
assert ets.get(nil, session_id) == :not_found
142143
assert is_nil(Plug.current_user(conn))
143144
end
144145

0 commit comments

Comments
 (0)