From ea2152e235152c8596e8ac65ef6bbe42dc5af9d6 Mon Sep 17 00:00:00 2001 From: Dan Schultzer Date: Sun, 5 Aug 2018 12:46:37 -0700 Subject: [PATCH] Refactor EtsCacheMock handling --- .../controllers/controller_callbacks_test.exs | 9 ++-- .../persistent_session/plug/cookie_test.exs | 43 +++++++++++-------- .../reset_password_controller_test.exs | 21 +++++---- test/pow/plug/session_test.exs | 37 ++++++++-------- test/pow/plug_test.exs | 33 +++++++------- test/pow/store/credentials_cache_test.exs | 19 ++++---- test/support/extensions/mock.ex | 8 ++-- test/support/phoenix/conn_case.ex | 12 +++--- 8 files changed, 96 insertions(+), 86 deletions(-) diff --git a/test/extensions/persistent_session/phoenix/controllers/controller_callbacks_test.exs b/test/extensions/persistent_session/phoenix/controllers/controller_callbacks_test.exs index 56855cda..0ba2cdaf 100644 --- a/test/extensions/persistent_session/phoenix/controllers/controller_callbacks_test.exs +++ b/test/extensions/persistent_session/phoenix/controllers/controller_callbacks_test.exs @@ -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" => "test@example.com", "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 @@ -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 diff --git a/test/extensions/persistent_session/plug/cookie_test.exs b/test/extensions/persistent_session/plug/cookie_test.exs index 1e745532..f1735a0b 100644 --- a/test/extensions/persistent_session/plug/cookie_test.exs +++ b/test/extensions/persistent_session/plug/cookie_test.exs @@ -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 @@ -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 diff --git a/test/extensions/reset_password/phoenix/controllers/reset_password_controller_test.exs b/test/extensions/reset_password/phoenix/controllers/reset_password_controller_test.exs index ae01e62d..10a08dee 100644 --- a/test/extensions/reset_password/phoenix/controllers/reset_password_controller_test.exs +++ b/test/extensions/reset_password/phoenix/controllers/reset_password_controller_test.exs @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/test/pow/plug/session_test.exs b/test/pow/plug/session_test.exs index d7bf5b10..5d24baea 100644 --- a/test/pow/plug/session_test.exs +++ b/test/pow/plug/session_test.exs @@ -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 @@ -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 = @@ -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 = @@ -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) @@ -76,7 +77,7 @@ 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) @@ -84,7 +85,7 @@ defmodule Pow.Plug.SessionTest do 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) @@ -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 = @@ -102,7 +103,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 @@ -110,16 +111,16 @@ defmodule Pow.Plug.SessionTest do 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 = @@ -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 @@ -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 diff --git a/test/pow/plug_test.exs b/test/pow/plug_test.exs index 2ff1d4f1..47e68a48 100644 --- a/test/pow/plug_test.exs +++ b/test/pow/plug_test.exs @@ -4,18 +4,21 @@ defmodule Pow.PlugTest do alias Plug.Conn alias Pow.{Config, Config.ConfigError, Plug, Plug.Session} - alias Pow.Test.{ConnHelpers, ContextMock, - Ecto.Users.User, EtsCacheMock} + alias Pow.Test.{ConnHelpers, ContextMock, Ecto.Users.User} + @ets Pow.Test.EtsCacheMock @default_config [ current_user_assigns_key: :current_user, users_context: ContextMock, - cache_store_backend: EtsCacheMock, + cache_store_backend: @ets, user: User ] @admin_config Config.put(@default_config, :current_user_assigns_key, :current_admin_user) + setup do + {:ok, ets: @ets} + end test "current_user/1" do assert_raise ConfigError, "Pow configuration not found. Please set the Pow.Plug.Session plug beforehand.", fn -> @@ -48,8 +51,8 @@ defmodule Pow.PlugTest do assert Plug.assign_current_user(conn, %{id: 1}, @admin_config) == %Conn{assigns: %{current_admin_user: user}} end - test "authenticate_user/2" do - EtsCacheMock.init() + test "authenticate_user/2", %{ets: ets} do + ets.init() opts = Session.init(@default_config) conn = @@ -85,8 +88,8 @@ defmodule Pow.PlugTest do end end - test "clear_authenticated_user/1" do - EtsCacheMock.init() + test "clear_authenticated_user/1", %{ets: ets} do + ets.init() opts = Session.init(@default_config) conn = @@ -98,12 +101,12 @@ defmodule Pow.PlugTest do assert user.id == 1 assert Plug.current_user(conn) == user assert session_id = conn.private[:plug_session]["auth"] - assert {^user, _timestamp} = EtsCacheMock.get(nil, session_id) + assert {^user, _timestamp} = ets.get(nil, session_id) {:ok, conn} = Plug.clear_authenticated_user(conn) refute Plug.current_user(conn) refute conn.private[:plug_session]["auth"] - assert EtsCacheMock.get(nil, session_id) == :not_found + assert ets.get(nil, session_id) == :not_found end test "change_user/2" do @@ -115,8 +118,8 @@ defmodule Pow.PlugTest do assert changeset.data.id == 1 end - test "create_user/2" do - EtsCacheMock.init() + test "create_user/2", %{ets: ets} do + ets.init() opts = Session.init(@default_config) conn = @@ -133,8 +136,8 @@ defmodule Pow.PlugTest do assert conn.private[:plug_session]["auth"] end - test "update_user/2" do - EtsCacheMock.init() + test "update_user/2", %{ets: ets} do + ets.init() opts = Session.init(@default_config) conn = @@ -156,8 +159,8 @@ defmodule Pow.PlugTest do refute conn.private[:plug_session]["auth"] == session_id end - test "delete_user/2" do - EtsCacheMock.init() + test "delete_user/2", %{ets: ets} do + ets.init() opts = Session.init(@default_config) conn = diff --git a/test/pow/store/credentials_cache_test.exs b/test/pow/store/credentials_cache_test.exs index c833c653..d4e1d1ec 100644 --- a/test/pow/store/credentials_cache_test.exs +++ b/test/pow/store/credentials_cache_test.exs @@ -1,22 +1,23 @@ defmodule Pow.Store.CredentialsCacheTest do use ExUnit.Case - doctest Pow.Store.Base + doctest Pow.Store.CredentialsCache alias Pow.Store.{Backend.EtsCache, CredentialsCache} alias Pow.Test.Ecto.Users.{User, UsernameUser} - alias Pow.Test.EtsCacheMock + + @ets Pow.Test.EtsCacheMock setup do - EtsCacheMock.init() + @ets.init() - :ok + {:ok, ets: @ets} end - test "stores sessions" do + test "stores sessions", %{ets: ets} do user_1 = %User{id: 1} user_2 = %User{id: 2} user_3 = %UsernameUser{id: 1} - config = [backend: EtsCacheMock] + config = [backend: ets] backend_config = [] CredentialsCache.put(config, backend_config, "key_1", user_1) @@ -33,7 +34,7 @@ defmodule Pow.Store.CredentialsCacheTest do assert CredentialsCache.sessions(config, backend_config, user_2) == ["key_3"] assert CredentialsCache.sessions(config, backend_config, user_3) == ["key_4"] - assert EtsCacheMock.get(config, "#{Macro.underscore(User)}_sessions_1") == %{user: user_1, sessions: ["key_1", "key_2"]} + assert ets.get(config, "#{Macro.underscore(User)}_sessions_1") == %{user: user_1, sessions: ["key_1", "key_2"]} CredentialsCache.put(config, backend_config, "key_2", %{user_1 | email: :updated}) assert CredentialsCache.get(config, backend_config, "key_1") == %{user_1 | email: :updated} @@ -45,7 +46,7 @@ defmodule Pow.Store.CredentialsCacheTest do CredentialsCache.delete(config, backend_config, "key_2") assert CredentialsCache.sessions(config, backend_config, user_1) == [] - assert EtsCacheMock.get(config, "#{Macro.underscore(User)}_sessions_1") == :not_found + assert ets.get(config, "#{Macro.underscore(User)}_sessions_1") == :not_found end test "handles purged values" do @@ -75,6 +76,6 @@ defmodule Pow.Store.CredentialsCacheTest do assert CredentialsCache.get(config, backend_config, "key_1") == :not_found assert CredentialsCache.get(config, backend_config, "key_2") == :not_found assert CredentialsCache.sessions(config, backend_config, user_1) == [] - assert EtsCacheMock.get(config, "#{Macro.underscore(User)}_sessions_1") == :not_found + assert EtsCache.get(config, "#{Macro.underscore(User)}_sessions_1") == :not_found end end diff --git a/test/support/extensions/mock.ex b/test/support/extensions/mock.ex index 561710c7..0ecb252f 100644 --- a/test/support/extensions/mock.ex +++ b/test/support/extensions/mock.ex @@ -82,7 +82,7 @@ defmodule Pow.Test.ExtensionMocks do module = Module.concat([web_module, Phoenix.ConnCase]) quoted = quote do use ExUnit.CaseTemplate - alias Pow.Test.{EtsCacheMock, Phoenix.ControllerAssertions} + alias Pow.Test.Phoenix.ControllerAssertions alias unquote(web_module).Phoenix.{Endpoint, Router} using do @@ -96,9 +96,11 @@ defmodule Pow.Test.ExtensionMocks do end end + @ets Application.get_env(unquote(web_module), :pow)[:cache_store_backend] + setup _tags do - EtsCacheMock.init() - {:ok, conn: Phoenix.ConnTest.build_conn()} + @ets.init() + {:ok, conn: Phoenix.ConnTest.build_conn(), ets: @ets} end end Module.create(module, quoted, Macro.Env.location(__ENV__)) diff --git a/test/support/phoenix/conn_case.ex b/test/support/phoenix/conn_case.ex index f36a3533..ab724947 100644 --- a/test/support/phoenix/conn_case.ex +++ b/test/support/phoenix/conn_case.ex @@ -1,10 +1,7 @@ defmodule Pow.Test.Phoenix.ConnCase do @moduledoc false use ExUnit.CaseTemplate - alias Pow.Test.{EtsCacheMock, - Phoenix.ControllerAssertions, - Phoenix.Endpoint, - Phoenix.Router} + alias Pow.Test.Phoenix.{ControllerAssertions, Endpoint, Router} using do quote do @@ -17,8 +14,11 @@ defmodule Pow.Test.Phoenix.ConnCase do end end + @ets Pow.Test.EtsCacheMock + setup _tags do - EtsCacheMock.init() - {:ok, conn: Phoenix.ConnTest.build_conn()} + @ets.init() + + {:ok, conn: Phoenix.ConnTest.build_conn(), ets: @ets} end end