mirror of
https://github.com/TheRealOwenRees/chess-endgame-trainer.git
synced 2026-11-18 03:32:06 +00:00
email signup up flow in dev mode, therefore in place for prod
This commit is contained in:
@@ -163,7 +163,7 @@ defmodule Chesstrainer.AccountsTest do
|
||||
assert "has already been taken" in errors_on(changeset).username
|
||||
end
|
||||
|
||||
test "registers users with a password, a confirmed account and default access" do
|
||||
test "registers users with a password, an unconfirmed account and default access" do
|
||||
email = unique_user_email()
|
||||
username = unique_username()
|
||||
|
||||
@@ -174,12 +174,70 @@ defmodule Chesstrainer.AccountsTest do
|
||||
assert user.username == username
|
||||
assert is_binary(user.hashed_password)
|
||||
assert is_nil(user.password)
|
||||
assert user.confirmed_at
|
||||
assert is_nil(user.confirmed_at)
|
||||
assert user.role == :member
|
||||
assert user.plan == :free
|
||||
end
|
||||
end
|
||||
|
||||
describe "deliver_user_confirmation_instructions/2" do
|
||||
setup do
|
||||
%{user: unconfirmed_user_fixture()}
|
||||
end
|
||||
|
||||
test "sends token through notification", %{user: user} do
|
||||
token =
|
||||
extract_user_token(fn url ->
|
||||
Accounts.deliver_user_confirmation_instructions(user, url)
|
||||
end)
|
||||
|
||||
{:ok, token} = Base.url_decode64(token, padding: false)
|
||||
assert user_token = Repo.get_by(UserToken, token: :crypto.hash(:sha256, token))
|
||||
assert user_token.user_id == user.id
|
||||
assert user_token.sent_to == user.email
|
||||
assert user_token.context == "confirm"
|
||||
end
|
||||
|
||||
test "does not send instructions when the account is already confirmed", %{user: user} do
|
||||
{:ok, user} = Repo.update(User.confirm_changeset(user))
|
||||
|
||||
assert Accounts.deliver_user_confirmation_instructions(user, fn _ -> "" end) ==
|
||||
{:error, :already_confirmed}
|
||||
end
|
||||
end
|
||||
|
||||
describe "confirm_user/1" do
|
||||
setup do
|
||||
user = unconfirmed_user_fixture()
|
||||
|
||||
token =
|
||||
extract_user_token(fn url ->
|
||||
Accounts.deliver_user_confirmation_instructions(user, url)
|
||||
end)
|
||||
|
||||
%{user: user, token: token}
|
||||
end
|
||||
|
||||
test "confirms the user with a valid token", %{user: user, token: token} do
|
||||
assert {:ok, %User{} = confirmed_user} = Accounts.confirm_user(token)
|
||||
assert confirmed_user.id == user.id
|
||||
assert confirmed_user.confirmed_at
|
||||
refute Repo.get_by(UserToken, user_id: user.id, context: "confirm")
|
||||
end
|
||||
|
||||
test "does not confirm with an invalid token", %{user: user} do
|
||||
assert Accounts.confirm_user("oops") == :error
|
||||
assert is_nil(Repo.get!(User, user.id).confirmed_at)
|
||||
end
|
||||
|
||||
test "does not confirm if the token expired", %{user: user, token: token} do
|
||||
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
|
||||
|
||||
assert Accounts.confirm_user(token) == :error
|
||||
assert is_nil(Repo.get!(User, user.id).confirmed_at)
|
||||
end
|
||||
end
|
||||
|
||||
describe "change_user_registration/3" do
|
||||
test "returns a user changeset" do
|
||||
assert %Ecto.Changeset{} = changeset = Accounts.change_user_registration(%User{})
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
defmodule ChesstrainerWeb.UserConfirmationControllerTest do
|
||||
use ChesstrainerWeb.ConnCase, async: true
|
||||
|
||||
import Chesstrainer.AccountsFixtures
|
||||
|
||||
alias Chesstrainer.Accounts
|
||||
alias Chesstrainer.Accounts.User
|
||||
alias Chesstrainer.Repo
|
||||
|
||||
setup do
|
||||
%{user: unconfirmed_user_fixture()}
|
||||
end
|
||||
|
||||
describe "GET /users/confirm/:token" do
|
||||
test "confirms the user and redirects to the login page", %{conn: conn, user: user} do
|
||||
token =
|
||||
extract_user_token(fn url ->
|
||||
Accounts.deliver_user_confirmation_instructions(user, url)
|
||||
end)
|
||||
|
||||
conn = get(conn, ~p"/users/confirm/#{token}")
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :info) == "User confirmed successfully."
|
||||
assert redirected_to(conn) == ~p"/users/log-in"
|
||||
assert Repo.get!(User, user.id).confirmed_at
|
||||
end
|
||||
|
||||
test "redirects with an error for an invalid token", %{conn: conn} do
|
||||
conn = get(conn, ~p"/users/confirm/oops")
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) ==
|
||||
"User not found or token is invalid."
|
||||
|
||||
assert redirected_to(conn) == ~p"/users/register"
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -2,6 +2,7 @@ defmodule ChesstrainerWeb.UserSessionControllerTest do
|
||||
use ChesstrainerWeb.ConnCase, async: true
|
||||
|
||||
import Chesstrainer.AccountsFixtures
|
||||
import Swoosh.TestAssertions
|
||||
|
||||
setup do
|
||||
%{user: user_fixture()}
|
||||
@@ -75,6 +76,25 @@ defmodule ChesstrainerWeb.UserSessionControllerTest do
|
||||
|
||||
assert redirected_to(conn) == ~p"/users/log-in"
|
||||
end
|
||||
|
||||
test "does not log in an unconfirmed user and resends confirmation instructions", %{
|
||||
conn: conn
|
||||
} do
|
||||
user = unconfirmed_user_fixture()
|
||||
|
||||
conn =
|
||||
post(conn, ~p"/users/log-in", %{
|
||||
"user" => %{"email" => user.email, "password" => valid_user_password()}
|
||||
})
|
||||
|
||||
refute get_session(conn, :user_token)
|
||||
|
||||
assert Phoenix.Flash.get(conn.assigns.flash, :error) ==
|
||||
"Invalid email, username or password"
|
||||
|
||||
assert redirected_to(conn) == ~p"/users/log-in"
|
||||
assert_email_sent(subject: "Confirmation instructions")
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /users/log-out" do
|
||||
|
||||
@@ -3,6 +3,7 @@ defmodule ChesstrainerWeb.UserLive.RegistrationTest do
|
||||
|
||||
import Phoenix.LiveViewTest
|
||||
import Chesstrainer.AccountsFixtures
|
||||
import Swoosh.TestAssertions
|
||||
|
||||
describe "Registration page" do
|
||||
test "renders registration page", %{conn: conn} do
|
||||
@@ -56,7 +57,10 @@ defmodule ChesstrainerWeb.UserLive.RegistrationTest do
|
||||
render_submit(form)
|
||||
|> follow_redirect(conn, ~p"/users/log-in")
|
||||
|
||||
assert html =~ "Account created successfully"
|
||||
assert html =~
|
||||
"An email with instructions for how to confirm your email address has been sent to your inbox."
|
||||
|
||||
assert_email_sent(subject: "Confirmation instructions")
|
||||
end
|
||||
|
||||
test "renders errors for duplicated email", %{conn: conn} do
|
||||
|
||||
@@ -22,6 +22,17 @@ defmodule Chesstrainer.AccountsFixtures do
|
||||
end
|
||||
|
||||
def user_fixture(attrs \\ %{}) do
|
||||
user = unconfirmed_user_fixture(attrs)
|
||||
|
||||
{:ok, user} =
|
||||
user
|
||||
|> Chesstrainer.Accounts.User.confirm_changeset()
|
||||
|> Chesstrainer.Repo.update()
|
||||
|
||||
user
|
||||
end
|
||||
|
||||
def unconfirmed_user_fixture(attrs \\ %{}) do
|
||||
{:ok, user} =
|
||||
attrs
|
||||
|> valid_user_attributes()
|
||||
|
||||
Reference in New Issue
Block a user