Skip to content

Commit

Permalink
Refactor EtsCacheMock handling
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Aug 5, 2018
1 parent ce52bc6 commit ea2152e
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
defmodule PowPersistentSession.Phoenix.ControllerCallbacksTest do
use PowPersistentSession.TestWeb.Phoenix.ConnCase

alias Pow.Test.EtsCacheMock
alias PowPersistentSession.Store.PersistentSessionCache

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

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

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

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

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

assert %{max_age: -1, path: "/", value: ""} = conn.resp_cookies["persistent_session_cookie"]
assert PersistentSessionCache.get([backend: EtsCacheMock], id) == :not_found
assert PersistentSessionCache.get([backend: ets], id) == :not_found
end
end
end
43 changes: 24 additions & 19 deletions test/extensions/persistent_session/plug/cookie_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@ defmodule PowPersistentSession.Plug.CookieTest do
doctest PowPersistentSession.Plug.Cookie

alias Pow.{Plug, Plug.Session}
alias Pow.Test.{ConnHelpers, EtsCacheMock}
alias Pow.Test.ConnHelpers
alias PowPersistentSession.{Plug.Cookie, Store.PersistentSessionCache}
alias PowPersistentSession.Test.Users.User

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

setup do
EtsCacheMock.init()
config = [otp_app: PowPersistentSession.TestWeb]
ets = Pow.Config.get(config, :cache_store_backend, nil)

ets.init()

conn =
:get
|> ConnHelpers.conn("/")
|> ConnHelpers.init_session()
|> Session.call([otp_app: PowPersistentSession.TestWeb])
|> Session.call(config)

{:ok, %{conn: conn}}
{:ok, %{conn: conn, config: config, ets: ets}}
end

defp store_persistent(conn, id, user) do
PersistentSessionCache.put([backend: EtsCacheMock], id, user.id)
defp store_persistent(conn, ets, id, user) do
PersistentSessionCache.put([backend: ets], id, user.id)
persistent_cookie(conn, id)
end

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

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

assert {Cookie, [mod: Session, otp_app: PowPersistentSession.TestWeb]} = conn.private[:pow_persistent_session]
assert {Cookie, ^expected_config} = conn.private[:pow_persistent_session]
refute conn.resp_cookies["persistent_session_cookie"]
end

test "call/2 assigns user from cookie", %{conn: conn} do
test "call/2 assigns user from cookie", %{conn: conn, ets: ets} do
user = %User{id: 1}
id = "test"
conn =
conn
|> store_persistent(id, user)
|> store_persistent(ets, id, user)
|> Cookie.call(Cookie.init([]))

assert Plug.current_user(conn) == user
assert %{value: new_id, max_age: @max_age, path: "/"} = conn.resp_cookies["persistent_session_cookie"]
refute new_id == id
assert PersistentSessionCache.get([backend: EtsCacheMock], id) == :not_found
assert PersistentSessionCache.get([backend: EtsCacheMock], new_id) == 1
assert PersistentSessionCache.get([backend: ets], id) == :not_found
assert PersistentSessionCache.get([backend: ets], new_id) == 1
end

test "call/2 when user already assigned", %{conn: conn} do
test "call/2 when user already assigned", %{conn: conn, ets: ets} do
user = %User{id: 1}
id = "test"
conn =
conn
|> store_persistent(id, user)
|> store_persistent(ets, id, user)
|> Plug.assign_current_user(:user, [])
|> Cookie.call(Cookie.init([]))

assert %{value: new_id, max_age: @max_age, path: "/"} = conn.resp_cookies["persistent_session_cookie"]
assert new_id == id
assert PersistentSessionCache.get([backend: EtsCacheMock], id) == 1
assert PersistentSessionCache.get([backend: ets], id) == 1
end

test "call/2 when user doesn't exist in database", %{conn: conn} do
test "call/2 when user doesn't exist in database", %{conn: conn, ets: ets} do
user = %User{id: -1}
id = "test"
conn =
conn
|> store_persistent(id, user)
|> store_persistent(ets, id, user)
|> Cookie.call(Cookie.init([]))

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

test "call/2 when persistent session cache doesn't have credentials", %{conn: conn} do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule PowResetPassword.Phoenix.ResetPasswordControllerTest do
use PowResetPassword.TestWeb.Phoenix.ConnCase

alias Pow.Test.EtsCacheMock
alias PowResetPassword.Store.ResetTokenCache
alias PowResetPassword.Test.Users.User

Expand Down Expand Up @@ -65,10 +64,10 @@ defmodule PowResetPassword.Phoenix.ResetPasswordControllerTest do
@valid_token "valid"
@invalid_token "invalid"

setup %{conn: conn} do
ResetTokenCache.put([backend: EtsCacheMock], @valid_token, @user)
setup %{conn: conn, ets: ets} do
ResetTokenCache.put([backend: ets], @valid_token, @user)

{:ok, conn: conn}
{:ok, conn: conn, ets: ets}
end

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

setup %{conn: conn} do
ResetTokenCache.put([backend: EtsCacheMock], @valid_token, @user)
setup %{conn: conn, ets: ets} do
ResetTokenCache.put([backend: ets], @valid_token, @user)

{:ok, conn: conn}
{:ok, conn: conn, ets: ets}
end

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

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

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

assert ResetTokenCache.get([backend: EtsCacheMock], @valid_token) == :not_found
assert ResetTokenCache.get([backend: ets], @valid_token) == :not_found
end

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

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

assert ResetTokenCache.get([backend: EtsCacheMock], @valid_token) == @user
assert ResetTokenCache.get([backend: ets], @valid_token) == @user
end
end
end
37 changes: 19 additions & 18 deletions test/pow/plug/session_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ defmodule Pow.Plug.SessionTest do

alias Plug.Conn
alias Pow.{Config, Plug, Plug.Session, Store.CredentialsCache}
alias Pow.Test.{ConnHelpers, EtsCacheMock}
alias Pow.Test.ConnHelpers

@ets Pow.Test.EtsCacheMock
@default_opts [
current_user_assigns_key: :current_user,
session_key: "auth",
cache_store_backend: EtsCacheMock
cache_store_backend: @ets
]

setup do
EtsCacheMock.init()
@ets.init()
conn =
:get
|> ConnHelpers.conn("/")
|> ConnHelpers.init_session()

{:ok, %{conn: conn}}
{:ok, %{conn: conn, ets: @ets}}
end

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

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

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

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

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

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

EtsCacheMock.put(nil, "token", {"cached", timestamp})
ets.put(nil, "token", {"cached", timestamp})

opts = Session.init(config)
conn = Session.call(init_conn, opts)
session_id = get_session_id(conn)

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

EtsCacheMock.put(nil, "token", {"cached", stale_timestamp})
ets.put(nil, "token", {"cached", stale_timestamp})

conn = Session.call(init_conn, opts)

Expand All @@ -93,7 +94,7 @@ defmodule Pow.Plug.SessionTest do
assert new_session_id != session_id
end

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

session_id = get_session_id(conn)
{etc_user, _inserted_at} = EtsCacheMock.get(nil, session_id)
{etc_user, _inserted_at} = ets.get(nil, session_id)

assert is_binary(session_id)
assert etc_user == user
assert Plug.current_user(conn) == user

conn = Session.do_create(conn, user)
new_session_id = get_session_id(conn)
{etc_user, _inserted_at} = EtsCacheMock.get(nil, new_session_id)
{etc_user, _inserted_at} = ets.get(nil, new_session_id)

assert is_binary(session_id)
assert new_session_id != session_id
assert EtsCacheMock.get(nil, session_id) == :not_found
assert ets.get(nil, session_id) == :not_found
assert etc_user == user
assert Plug.current_user(conn) == user
end

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

session_id = get_session_id(conn)
{etc_user, _inserted_at} = EtsCacheMock.get(nil, session_id)
{etc_user, _inserted_at} = ets.get(nil, session_id)

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

refute new_session_id = get_session_id(conn)
assert is_nil(new_session_id)
assert EtsCacheMock.get(nil, session_id) == :not_found
assert ets.get(nil, session_id) == :not_found
assert is_nil(Plug.current_user(conn))
end

Expand Down
Loading

0 comments on commit ea2152e

Please sign in to comment.