philomena/test/philomena_web/controllers/confirmation_controller_test.exs

91 lines
2.9 KiB
Elixir
Raw Normal View History

defmodule PhilomenaWeb.ConfirmationControllerTest do
use PhilomenaWeb.ConnCase, async: true
alias Philomena.Users
alias Philomena.Repo
2023-05-18 16:23:17 +02:00
alias Phoenix.Flash
import Philomena.UsersFixtures
setup do
%{user: user_fixture()}
end
describe "GET /confirmations/new" do
test "renders the confirmation page", %{conn: conn} do
2024-06-06 22:28:35 +02:00
conn = get(conn, ~p"/confirmations/new")
response = html_response(conn, 200)
assert response =~ "<h1>Resend confirmation instructions</h1>"
end
end
describe "POST /confirmations" do
@tag :capture_log
test "sends a new confirmation token", %{conn: conn, user: user} do
conn =
2024-06-06 22:28:35 +02:00
post(conn, ~p"/confirmations", %{
"user" => %{"email" => user.email}
})
assert redirected_to(conn) == "/"
2024-06-13 00:27:08 +02:00
assert Flash.get(conn.assigns.flash, :alert) =~ "If your email is in our system"
assert Repo.get_by!(Users.UserToken, user_id: user.id).context == "confirm"
end
test "does not send confirmation token if account is confirmed", %{conn: conn, user: user} do
Repo.update!(Users.User.confirm_changeset(user))
conn =
2024-06-06 22:28:35 +02:00
post(conn, ~p"/confirmations", %{
"user" => %{"email" => user.email}
})
assert redirected_to(conn) == "/"
2024-06-13 00:27:08 +02:00
assert Flash.get(conn.assigns.flash, :alert) =~ "If your email is in our system"
refute Repo.get_by(Users.UserToken, user_id: user.id)
end
test "does not send confirmation token if email is invalid", %{conn: conn} do
conn =
2024-06-06 22:28:35 +02:00
post(conn, ~p"/confirmations", %{
"user" => %{"email" => "unknown@example.com"}
})
assert redirected_to(conn) == "/"
2024-06-13 00:27:08 +02:00
assert Flash.get(conn.assigns.flash, :alert) =~ "If your email is in our system"
assert Repo.all(Users.UserToken) == []
end
end
describe "GET /confirmations/:id" do
test "confirms the given token once", %{conn: conn, user: user} do
token =
extract_user_token(fn url ->
Users.deliver_user_confirmation_instructions(user, url)
end)
2024-06-06 22:28:35 +02:00
conn = get(conn, ~p"/confirmations/#{token}")
assert redirected_to(conn) == "/"
2023-05-18 16:23:17 +02:00
assert Flash.get(conn.assigns.flash, :info) =~ "Account confirmed successfully"
assert Users.get_user!(user.id).confirmed_at
refute get_session(conn, :user_token)
assert Repo.all(Users.UserToken) == []
2024-06-06 22:28:35 +02:00
conn = get(conn, ~p"/confirmations/#{token}")
assert redirected_to(conn) == "/"
2023-05-18 16:23:17 +02:00
2024-06-13 00:27:08 +02:00
assert Flash.get(conn.assigns.flash, :warning) =~
2023-05-18 16:23:17 +02:00
"Confirmation link is invalid or it has expired"
end
test "does not confirm email with invalid token", %{conn: conn, user: user} do
2024-06-06 22:28:35 +02:00
conn = get(conn, ~p"/confirmations/oops")
assert redirected_to(conn) == "/"
2023-05-18 16:23:17 +02:00
2024-06-13 00:27:08 +02:00
assert Flash.get(conn.assigns.flash, :warning) =~
2023-05-18 16:23:17 +02:00
"Confirmation link is invalid or it has expired"
refute Users.get_user!(user.id).confirmed_at
end
end
end