2021-03-05 19:23:32 -05:00
|
|
|
defmodule Shift73kWeb.UserConfirmationControllerTest do
|
|
|
|
use Shift73kWeb.ConnCase, async: true
|
2020-09-12 20:07:02 -04:00
|
|
|
|
2021-03-05 19:23:32 -05:00
|
|
|
alias Shift73k.Accounts
|
|
|
|
alias Shift73k.Repo
|
|
|
|
import Shift73k.AccountsFixtures
|
2020-09-12 20:07:02 -04: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)
|
2021-03-03 08:15:14 -05:00
|
|
|
assert response =~ "Resend confirmation instructions\n </h2>"
|
2020-09-12 20:07:02 -04:00
|
|
|
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!(Accounts.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!(Accounts.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(Accounts.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(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) == "/"
|
|
|
|
assert get_flash(conn, :info) =~ "Account confirmed successfully"
|
|
|
|
assert Accounts.get_user!(user.id).confirmed_at
|
|
|
|
refute get_session(conn, :user_token)
|
|
|
|
assert Repo.all(Accounts.UserToken) == []
|
|
|
|
|
|
|
|
conn = get(conn, Routes.user_confirmation_path(conn, :confirm, token))
|
|
|
|
assert redirected_to(conn) == "/"
|
|
|
|
assert get_flash(conn, :error) =~ "Confirmation link is invalid or it has expired"
|
|
|
|
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) == "/"
|
|
|
|
assert get_flash(conn, :error) =~ "Confirmation link is invalid or it has expired"
|
|
|
|
refute Accounts.get_user!(user.id).confirmed_at
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|