From b38b1d54039835e7ca46114b88f8d08c631c105a Mon Sep 17 00:00:00 2001 From: Dan Schultzer <1254724+danschultzer@users.noreply.github.com> Date: Mon, 10 Oct 2022 08:48:22 +0200 Subject: [PATCH] Use Phoenix.Flash module --- guides/lock_users.md | 10 +++++----- guides/multitenancy.md | 2 +- .../phoenix/controllers/plug_error_handler_test.exs | 6 ++++-- test/support/extensions/mocks.ex | 3 ++- test/support/phoenix/conn_case.ex | 10 +++++++++- test/support/phoenix/controller_assertions.ex | 3 ++- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/guides/lock_users.md b/guides/lock_users.md index b1c3201f..d9df57fe 100644 --- a/guides/lock_users.md +++ b/guides/lock_users.md @@ -282,7 +282,7 @@ defmodule MyAppWeb.Admin.UserControllerTest do conn = post(conn, Routes.admin_user_path(conn, :lock, user.id)) - assert get_flash(conn, :info) == "User has been locked." + assert Phoenix.Flash.get(conn.assigns.flash, :info) == "User has been locked." assert redirected_to(conn) == "/" end @@ -291,7 +291,7 @@ defmodule MyAppWeb.Admin.UserControllerTest do conn = post(conn, Routes.admin_user_path(conn, :lock, user.id)) - assert get_flash(conn, :error) == "User couldn't be locked." + assert Phoenix.Flash.get(conn.assigns.flash, :error) == "User couldn't be locked." assert redirected_to(conn) == "/" end end @@ -345,7 +345,7 @@ defmodule MyAppWeb.EnsureUserNotLockedPlugTest do |> Pow.Plug.assign_current_user(@locked_user, @pow_config) |> EnsureUserNotLockedPlug.call(opts) - assert get_flash(conn, :error) == "Sorry, your account is locked." + assert Phoenix.Flash.get(conn.assigns.flash, :error) == "Sorry, your account is locked." assert redirected_to(conn) == Routes.pow_session_path(conn, :new) end @@ -377,7 +377,7 @@ defmodule MyAppWeb.ResetPasswordControllerTest do conn = post(conn, Routes.reset_password_path(conn, :create, @valid_params)) - assert get_flash(conn, :info) + assert Phoenix.Flash.get(conn.assigns.flash, :info) assert redirected_to(conn) == Routes.pow_session_path(conn, :new) assert count_reset_password_tokens_for_user(conn, user) == 1 @@ -388,7 +388,7 @@ defmodule MyAppWeb.ResetPasswordControllerTest do conn = post(conn, Routes.reset_password_path(conn, :create, @valid_params)) - assert get_flash(conn, :info) + assert Phoenix.Flash.get(conn.assigns.flash, :info) assert redirected_to(conn) == Routes.pow_session_path(conn, :new) assert count_reset_password_tokens_for_user(conn, user) == 0 diff --git a/guides/multitenancy.md b/guides/multitenancy.md index 3e15de0c..fa765686 100644 --- a/guides/multitenancy.md +++ b/guides/multitenancy.md @@ -311,7 +311,7 @@ defmodule MyAppWeb.AccountControllerTest do test "with valid params", %{conn: conn} do conn = post(conn, Routes.account_path(conn, :create, @valid_params)) - assert get_flash(conn, :info) == "Welcome!" + assert Phoenix.Flash.get(conn.assigns.flash, :info) == "Welcome!" assert redirected_to(conn) == Routes.page_path(conn, :index) assert Pow.Plug.current_user(conn) diff --git a/test/pow/phoenix/controllers/plug_error_handler_test.exs b/test/pow/phoenix/controllers/plug_error_handler_test.exs index 7e61075d..29969b79 100644 --- a/test/pow/phoenix/controllers/plug_error_handler_test.exs +++ b/test/pow/phoenix/controllers/plug_error_handler_test.exs @@ -2,7 +2,8 @@ defmodule Pow.Phoenix.PlugErrorHandlerTest do use ExUnit.Case doctest Pow.Phoenix.PlugErrorHandler - import Phoenix.ConnTest + import Phoenix.ConnTest, except: [get_flash: 2] + import Pow.Test.Phoenix.ConnCase, only: [get_flash: 2] alias Plug.Conn alias Pow.Phoenix.{Messages, PlugErrorHandler} @@ -17,7 +18,8 @@ defmodule Pow.Phoenix.PlugErrorHandlerTest do defp prepare_conn(conn) do conn |> Conn.put_private(:pow_config, messages_backend: Messages) - |> Conn.put_private(:phoenix_flash, %{}) + |> Conn.put_private(:phoenix_flash, %{}) # TODO: Remove when Phoenix 1.7 is required + |> Map.update(:assigns, %{}, & Map.put(&1, :flash, %{})) |> Conn.put_private(:phoenix_router, Pow.Test.Phoenix.Router) |> Conn.fetch_query_params() end diff --git a/test/support/extensions/mocks.ex b/test/support/extensions/mocks.ex index c9c83d72..7a126801 100644 --- a/test/support/extensions/mocks.ex +++ b/test/support/extensions/mocks.ex @@ -165,7 +165,8 @@ defmodule Pow.Test.ExtensionMocks do using do quote do import Plug.Conn - import Phoenix.ConnTest + import Phoenix.ConnTest, except: [get_flash: 2] + import Pow.Test.Phoenix.ConnCase, only: [get_flash: 2] import ControllerAssertions alias Router.Helpers, as: Routes diff --git a/test/support/phoenix/conn_case.ex b/test/support/phoenix/conn_case.ex index e5db0ae5..8dca49ba 100644 --- a/test/support/phoenix/conn_case.ex +++ b/test/support/phoenix/conn_case.ex @@ -8,7 +8,8 @@ defmodule Pow.Test.Phoenix.ConnCase do using do quote do import Plug.Conn - import Phoenix.ConnTest + import Phoenix.ConnTest, except: [get_flash: 2] + import unquote(__MODULE__), only: [get_flash: 2] import ControllerAssertions alias Router.Helpers, as: Routes @@ -22,4 +23,11 @@ defmodule Pow.Test.Phoenix.ConnCase do {:ok, conn: Phoenix.ConnTest.build_conn(), ets: EtsCacheMock} end + + # TODO: Remove when Phoenix 1.7 is required + if Code.ensure_loaded?(Phoenix.Flash) do + def get_flash(conn, key), do: Phoenix.Flash.get(conn.assigns.flash, key) + else + defdelegate get_flash(conn, key), to: Phoenix.Controller + end end diff --git a/test/support/phoenix/controller_assertions.ex b/test/support/phoenix/controller_assertions.ex index 369eb536..d8c7c2ce 100644 --- a/test/support/phoenix/controller_assertions.ex +++ b/test/support/phoenix/controller_assertions.ex @@ -1,6 +1,7 @@ defmodule Pow.Test.Phoenix.ControllerAssertions do @moduledoc false - import Phoenix.ConnTest + import Phoenix.ConnTest, except: [get_flash: 2] + import Pow.Test.Phoenix.ConnCase, only: [get_flash: 2] alias Pow.Phoenix.{Messages, Routes}