memEx/test/memex_web/controllers/user_confirmation_controller_test.exs

93 lines
3.2 KiB
Elixir
Raw Permalink Normal View History

2022-07-25 19:31:54 -04:00
defmodule MemexWeb.UserConfirmationControllerTest do
2022-02-25 21:53:15 -05:00
@moduledoc """
Tests user confirmation
"""
2021-03-11 21:12:55 -05:00
2022-07-25 19:31:54 -04:00
use MemexWeb.ConnCase, async: true
2023-02-04 17:36:27 -05:00
alias Memex.{Accounts, Repo}
2022-02-25 21:53:15 -05:00
@moduletag :user_confirmation_controller_test
2021-03-11 21:12:55 -05:00
setup do
%{user: user_fixture()}
end
describe "GET /users/confirm" do
test "renders the confirmation page", %{conn: conn} do
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/confirm")
2021-03-11 21:12:55 -05:00
response = html_response(conn, 200)
2023-03-22 22:08:37 -04:00
assert response =~ "Resend confirmation instructions"
2021-03-11 21:12:55 -05:00
end
end
describe "POST /users/confirm" do
@tag :capture_log
test "sends a new confirmation token", %{conn: conn, user: user} do
2023-04-14 20:08:03 -04:00
conn = post(conn, ~p"/users/confirm", %{user: %{email: user.email}})
2023-04-14 19:51:14 -04:00
assert redirected_to(conn) == ~p"/"
2022-02-25 21:53:15 -05:00
2023-04-15 21:40:57 -04:00
assert conn.assigns.flash["info"] =~
"if your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
2022-02-25 21:53:15 -05:00
2021-03-11 21:12:55 -05:00
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))
2023-04-14 20:08:03 -04:00
conn = post(conn, ~p"/users/confirm", %{user: %{email: user.email}})
2023-04-14 19:51:14 -04:00
assert redirected_to(conn) == ~p"/"
2022-02-25 21:53:15 -05:00
2023-04-15 21:40:57 -04:00
assert conn.assigns.flash["info"] =~
"if your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
2021-03-11 21:12:55 -05:00
end
test "does not send confirmation token if email is invalid", %{conn: conn} do
2023-04-14 20:08:03 -04:00
conn = post(conn, ~p"/users/confirm", %{user: %{email: "unknown@example.com"}})
2023-04-14 19:51:14 -04:00
assert redirected_to(conn) == ~p"/"
2022-02-25 21:53:15 -05:00
2023-04-15 21:40:57 -04:00
assert conn.assigns.flash["info"] =~
"if your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
2022-02-25 21:53:15 -05:00
2021-03-11 21:12:55 -05:00
assert Repo.all(Accounts.UserToken) == []
end
end
describe "GET /users/confirm/:token" 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)
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/confirm/#{token}")
2023-04-14 19:51:14 -04:00
assert redirected_to(conn) == ~p"/"
2023-04-15 21:40:57 -04:00
assert conn.assigns.flash["info"] =~ "#{user.email} confirmed successfully"
2021-03-11 21:12:55 -05:00
assert Accounts.get_user!(user.id).confirmed_at
refute get_session(conn, :user_token)
assert Repo.all(Accounts.UserToken) == []
# When not logged in
2023-04-13 23:29:29 -04:00
conn = get(conn, ~p"/users/confirm/#{token}")
2023-04-14 19:51:14 -04:00
assert redirected_to(conn) == ~p"/"
2023-04-15 21:40:57 -04:00
assert conn.assigns.flash["error"] =~ "user confirmation link is invalid or it has expired"
2021-03-11 21:12:55 -05:00
# When logged in
conn =
build_conn()
|> log_in_user(user)
2023-04-13 23:29:29 -04:00
|> get(~p"/users/confirm/#{token}")
2021-03-11 21:12:55 -05:00
2023-04-14 19:51:14 -04:00
assert redirected_to(conn) == ~p"/"
2023-04-13 23:29:29 -04:00
refute conn.assigns.flash["error"]
2021-03-11 21:12:55 -05:00
end
test "does not confirm email with invalid token", %{conn: conn, user: user} do
2023-04-15 21:40:57 -04:00
conn = get(conn, ~p"/users/confirm/oops")
2023-04-14 19:51:14 -04:00
assert redirected_to(conn) == ~p"/"
2023-04-15 21:40:57 -04:00
assert conn.assigns.flash["error"] =~ "user confirmation link is invalid or it has expired"
2021-03-11 21:12:55 -05:00
refute Accounts.get_user!(user.id).confirmed_at
end
end
end