Skip to content

Commit

Permalink
Use Phoenix.Flash module
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Oct 10, 2022
1 parent 25e962d commit b38b1d5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 11 deletions.
10 changes: 5 additions & 5 deletions guides/lock_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion guides/multitenancy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions test/pow/phoenix/controllers/plug_error_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/support/extensions/mocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion test/support/phoenix/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion test/support/phoenix/controller_assertions.ex
Original file line number Diff line number Diff line change
@@ -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}

Expand Down

0 comments on commit b38b1d5

Please sign in to comment.