Hash :
e800e02b
Author :
Thomas de Grivel
Date :
2021-12-08T16:24:53
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
defmodule KmxgitWeb.UserConfirmationControllerTest do
use KmxgitWeb.ConnCase, async: true
alias Kmxgit.UserManager
alias Kmxgit.Repo
import Kmxgit.UserManagerFixtures
setup do
%{user: user_fixture()}
end
describe "GET /users/confirm" do
test "renders the resend confirmation page", %{conn: conn} do
conn = get(conn, Routes.user_confirmation_path(conn, :new))
response = html_response(conn, 200)
assert response =~ "<h1>Resend confirmation instructions</h1>"
end
end
describe "POST /users/confirm" do
@tag :capture_log
test "sends a new confirmation token", %{conn: conn, user: user} do
conn =
post(conn, Routes.user_confirmation_path(conn, :create), %{
"user" => %{"email" => user.email}
})
assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ "If your email is in our system"
assert Repo.get_by!(UserManager.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!(UserManager.User.confirm_changeset(user))
conn =
post(conn, Routes.user_confirmation_path(conn, :create), %{
"user" => %{"email" => user.email}
})
assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ "If your email is in our system"
refute Repo.get_by(UserManager.UserToken, user_id: user.id)
end
test "does not send confirmation token if email is invalid", %{conn: conn} do
conn =
post(conn, Routes.user_confirmation_path(conn, :create), %{
"user" => %{"email" => "unknown@example.com"}
})
assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ "If your email is in our system"
assert Repo.all(UserManager.UserToken) == []
end
end
describe "GET /users/confirm/:token" do
test "renders the confirmation page", %{conn: conn} do
conn = get(conn, Routes.user_confirmation_path(conn, :edit, "some-token"))
response = html_response(conn, 200)
assert response =~ "<h1>Confirm account</h1>"
form_action = Routes.user_confirmation_path(conn, :update, "some-token")
assert response =~ "action=\"#{form_action}\""
end
end
describe "POST /users/confirm/:token" do
test "confirms the given token once", %{conn: conn, user: user} do
token =
extract_user_token(fn url ->
UserManager.deliver_user_confirmation_instructions(user, url)
end)
conn = post(conn, Routes.user_confirmation_path(conn, :update, token))
assert redirected_to(conn) == "/"
assert get_flash(conn, :info) =~ "User confirmed successfully"
assert UserManager.get_user!(user.id).confirmed_at
refute get_session(conn, :user_token)
assert Repo.all(UserManager.UserToken) == []
# When not logged in
conn = post(conn, Routes.user_confirmation_path(conn, :update, token))
assert redirected_to(conn) == "/"
assert get_flash(conn, :error) =~ "User confirmation link is invalid or it has expired"
# When logged in
conn =
build_conn()
|> log_in_user(user)
|> post(Routes.user_confirmation_path(conn, :update, 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 = post(conn, Routes.user_confirmation_path(conn, :update, "oops"))
assert redirected_to(conn) == "/"
assert get_flash(conn, :error) =~ "User confirmation link is invalid or it has expired"
refute UserManager.get_user!(user.id).confirmed_at
end
end
end