cannery/test/cannery_web/controllers/user_confirmation_controller_test.exs

102 lines
3.4 KiB
Elixir
Raw Normal View History

2021-03-11 21:12:55 -05:00
defmodule CanneryWeb.UserConfirmationControllerTest do
2022-02-16 21:02:21 -05:00
@moduledoc """
Tests user confirmation
"""
2021-03-11 21:12:55 -05:00
2022-02-16 21:02:21 -05:00
use CanneryWeb.ConnCase, async: true
2022-03-28 23:05:12 -04:00
alias Cannery.{Accounts, Repo}
2022-02-16 21:02:21 -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
conn = get(conn, Routes.user_confirmation_path(conn, :new))
response = html_response(conn, 200)
2023-03-28 21:57:29 -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
conn =
2023-03-28 21:57:29 -04:00
post(conn, Routes.user_confirmation_path(conn, :create), %{user: %{email: user.email}})
2021-03-11 21:12:55 -05:00
assert redirected_to(conn) == "/"
2022-02-16 21:20:04 -05:00
assert get_flash(conn, :info) =~
2023-03-28 21:57:29 -04:00
"If your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
2022-02-16 21:20:04 -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))
conn =
2023-03-28 21:57:29 -04:00
post(conn, Routes.user_confirmation_path(conn, :create), %{user: %{email: user.email}})
2021-03-11 21:12:55 -05:00
assert redirected_to(conn) == "/"
2022-02-16 21:20:04 -05:00
assert get_flash(conn, :info) =~
2023-03-28 21:57:29 -04:00
"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
conn =
post(conn, Routes.user_confirmation_path(conn, :create), %{
2023-03-28 21:57:29 -04:00
user: %{email: "unknown@example.com"}
2021-03-11 21:12:55 -05:00
})
assert redirected_to(conn) == "/"
2022-02-16 21:20:04 -05:00
assert get_flash(conn, :info) =~
2023-03-28 21:57:29 -04:00
"If your email is in our system and it has not been confirmed yet, you will receive an email with instructions shortly."
2022-02-16 21:20:04 -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)
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
assert redirected_to(conn) == "/"
2023-03-28 21:57:29 -04:00
assert get_flash(conn, :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
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
assert redirected_to(conn) == "/"
2022-02-16 21:20:04 -05:00
2023-03-28 21:57:29 -04:00
assert get_flash(conn, :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)
|> get(Routes.user_confirmation_path(conn, :confirm, token))
assert redirected_to(conn) == "/"
refute get_flash(conn, :error)
end
test "does not confirm email with invalid token", %{conn: conn, user: user} do
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, "oops"))
assert redirected_to(conn) == "/"
2023-03-28 21:57:29 -04:00
assert get_flash(conn, :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