139 lines
3.9 KiB
Elixir
139 lines
3.9 KiB
Elixir
defmodule PrymnWeb.UserConfirmationLiveTest do
|
|
use PrymnWeb.ConnCase
|
|
|
|
import Phoenix.LiveViewTest
|
|
import Prymn.AccountsFixtures
|
|
|
|
alias Prymn.Accounts
|
|
alias Prymn.Repo
|
|
|
|
setup do
|
|
%{user: user_fixture()}
|
|
end
|
|
|
|
describe "Confirm user" do
|
|
test "confirms the given token once", %{conn: conn, user: user} do
|
|
token =
|
|
extract_user_token(fn url ->
|
|
Accounts.deliver_user_confirmation_instructions(user, url)
|
|
end)
|
|
|
|
{:ok, lv, _html} = live(conn, ~p"/auth/confirm/#{token}")
|
|
|
|
result =
|
|
lv
|
|
|> form("#confirmation_form")
|
|
|> render_submit()
|
|
|> follow_redirect(conn, "/")
|
|
|
|
assert {:ok, conn} = result
|
|
|
|
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
|
|
"Email confirmed successfully"
|
|
|
|
assert Accounts.get_user!(user.id).confirmed_at
|
|
refute get_session(conn, :user_token)
|
|
assert Repo.all(Accounts.UserToken) == []
|
|
|
|
# when not logged in
|
|
{:ok, lv, _html} = live(conn, ~p"/auth/confirm/#{token}")
|
|
|
|
result =
|
|
lv
|
|
|> form("#confirmation_form")
|
|
|> render_submit()
|
|
|> follow_redirect(conn, "/")
|
|
|
|
assert {:ok, conn} = result
|
|
|
|
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
|
|
"Email confirmation link is invalid or it has expired"
|
|
|
|
# when logged in
|
|
conn = build_conn()
|
|
|
|
{:ok, lv, _html} =
|
|
conn
|
|
|> log_in_user(user)
|
|
|> live(~p"/auth/confirm/#{token}")
|
|
|
|
result =
|
|
lv
|
|
|> form("#confirmation_form")
|
|
|> render_submit()
|
|
|> follow_redirect(conn, "/")
|
|
|
|
assert {:ok, conn} = result
|
|
refute Phoenix.Flash.get(conn.assigns.flash, :error)
|
|
end
|
|
|
|
test "does not confirm email with invalid token", %{conn: conn, user: user} do
|
|
{:ok, lv, _html} = live(conn, ~p"/auth/confirm/invalid-token")
|
|
|
|
{:ok, conn} =
|
|
lv
|
|
|> form("#confirmation_form")
|
|
|> render_submit()
|
|
|> follow_redirect(conn, ~p"/")
|
|
|
|
assert Phoenix.Flash.get(conn.assigns.flash, :error) =~
|
|
"Email confirmation link is invalid or it has expired"
|
|
|
|
refute Accounts.get_user!(user.id).confirmed_at
|
|
end
|
|
end
|
|
|
|
describe "Resend confirmation" do
|
|
test "renders the resend confirmation page", %{conn: conn} do
|
|
{:ok, _lv, html} = live(conn, ~p"/auth/confirm")
|
|
assert html =~ "Resend confirmation instructions"
|
|
end
|
|
|
|
test "sends a new confirmation token", %{conn: conn, user: user} do
|
|
{:ok, lv, _html} = live(conn, ~p"/auth/confirm")
|
|
|
|
{:ok, conn} =
|
|
lv
|
|
|> form("#confirmation_form", user: %{email: user.email})
|
|
|> render_submit()
|
|
|> follow_redirect(conn, ~p"/")
|
|
|
|
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
|
|
"If your email is in our system"
|
|
|
|
assert Repo.get_by!(Accounts.UserToken, user_id: user.id).context == "confirm"
|
|
end
|
|
|
|
test "does not send confirmation token if user is confirmed", %{conn: conn, user: user} do
|
|
Repo.update!(Accounts.User.confirm_changeset(user))
|
|
|
|
{:ok, lv, _html} = live(conn, ~p"/auth/confirm")
|
|
|
|
{:ok, conn} =
|
|
lv
|
|
|> form("#confirmation_form", user: %{email: user.email})
|
|
|> render_submit()
|
|
|> follow_redirect(conn, ~p"/")
|
|
|
|
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
|
|
"If your email is in our system"
|
|
|
|
refute Repo.get_by(Accounts.UserToken, user_id: user.id)
|
|
end
|
|
|
|
test "does not send confirmation token if email is invalid", %{conn: conn} do
|
|
{:ok, lv, _html} = live(conn, ~p"/auth/confirm")
|
|
|
|
{:ok, conn} =
|
|
lv
|
|
|> form("#confirmation_form", user: %{email: "unknown@example.com"})
|
|
|> render_submit()
|
|
|> follow_redirect(conn, ~p"/")
|
|
|
|
assert Phoenix.Flash.get(conn.assigns.flash, :info) =~
|
|
"If your email is in our system"
|
|
|
|
assert Repo.all(Accounts.UserToken) == []
|
|
end
|
|
end
|
|
end
|