pass accounts test

This commit is contained in:
shibao 2022-02-16 20:29:47 -05:00
parent e200cf1b84
commit eede6c225d

View File

@ -1,11 +1,15 @@
defmodule Cannery.AccountsTest do defmodule Cannery.AccountsTest do
use Cannery.DataCase @moduledoc """
This tests the accounts module
"""
use Cannery.DataCase
alias Cannery.Accounts alias Cannery.Accounts
import Cannery.AccountsFixtures
alias Cannery.Accounts.{User, UserToken} alias Cannery.Accounts.{User, UserToken}
alias Ecto.Changeset alias Ecto.Changeset
@moduletag :accounts_test
describe "get_user_by_email/1" do describe "get_user_by_email/1" do
test "does not return the user if the email does not exist" do test "does not return the user if the email does not exist" do
refute Accounts.get_user_by_email("unknown@example.com") refute Accounts.get_user_by_email("unknown@example.com")
@ -59,7 +63,8 @@ defmodule Cannery.AccountsTest do
end end
test "validates email and password when given" do test "validates email and password when given" do
{:error, changeset} = Accounts.register_user(%{email: "not valid", password: "not valid"}) {:error, changeset} =
Accounts.register_user(%{"email" => "not valid", "password" => "not valid"})
assert %{ assert %{
email: ["must have the @ sign and no spaces"], email: ["must have the @ sign and no spaces"],
@ -69,24 +74,27 @@ defmodule Cannery.AccountsTest do
test "validates maximum values for email and password for security" do test "validates maximum values for email and password for security" do
too_long = String.duplicate("db", 100) too_long = String.duplicate("db", 100)
{:error, changeset} = Accounts.register_user(%{email: too_long, password: too_long}) {:error, changeset} = Accounts.register_user(%{"email" => too_long, "password" => too_long})
assert "should be at most 160 character(s)" in errors_on(changeset).email assert "should be at most 160 character(s)" in errors_on(changeset).email
assert "should be at most 80 character(s)" in errors_on(changeset).password assert "should be at most 80 character(s)" in errors_on(changeset).password
end end
test "validates email uniqueness" do test "validates email uniqueness" do
%{email: email} = user_fixture() %{email: email} = user_fixture()
{:error, changeset} = Accounts.register_user(%{email: email}) {:error, changeset} = Accounts.register_user(%{"email" => email})
assert "has already been taken" in errors_on(changeset).email assert "has already been taken" in errors_on(changeset).email
# Now try with the upper cased email too, to check that email case is ignored. # Now try with the upper cased email too, to check that email case is ignored.
{:error, changeset} = Accounts.register_user(%{email: String.upcase(email)}) {:error, changeset} = Accounts.register_user(%{"email" => String.upcase(email)})
assert "has already been taken" in errors_on(changeset).email assert "has already been taken" in errors_on(changeset).email
end end
test "registers users with a hashed password" do test "registers users with a hashed password" do
email = unique_user_email() email = unique_user_email()
{:ok, user} = Accounts.register_user(%{email: email, password: valid_user_password()})
{:ok, user} =
Accounts.register_user(%{"email" => email, "password" => valid_user_password()})
assert user.email == email assert user.email == email
assert is_binary(user.hashed_password) assert is_binary(user.hashed_password)
assert is_nil(user.confirmed_at) assert is_nil(user.confirmed_at)
@ -133,7 +141,7 @@ defmodule Cannery.AccountsTest do
test "validates email", %{user: user} do test "validates email", %{user: user} do
{:error, changeset} = {:error, changeset} =
Accounts.apply_user_email(user, valid_user_password(), %{email: "not valid"}) Accounts.apply_user_email(user, valid_user_password(), %{"email" => "not valid"})
assert %{email: ["must have the @ sign and no spaces"]} = errors_on(changeset) assert %{email: ["must have the @ sign and no spaces"]} = errors_on(changeset)
end end
@ -142,7 +150,7 @@ defmodule Cannery.AccountsTest do
too_long = String.duplicate("db", 100) too_long = String.duplicate("db", 100)
{:error, changeset} = {:error, changeset} =
Accounts.apply_user_email(user, valid_user_password(), %{email: too_long}) Accounts.apply_user_email(user, valid_user_password(), %{"email" => too_long})
assert "should be at most 160 character(s)" in errors_on(changeset).email assert "should be at most 160 character(s)" in errors_on(changeset).email
end end
@ -151,21 +159,21 @@ defmodule Cannery.AccountsTest do
%{email: email} = user_fixture() %{email: email} = user_fixture()
{:error, changeset} = {:error, changeset} =
Accounts.apply_user_email(user, valid_user_password(), %{email: email}) Accounts.apply_user_email(user, valid_user_password(), %{"email" => email})
assert "has already been taken" in errors_on(changeset).email assert "has already been taken" in errors_on(changeset).email
end end
test "validates current password", %{user: user} do test "validates current password", %{user: user} do
{:error, changeset} = {:error, changeset} =
Accounts.apply_user_email(user, "invalid", %{email: unique_user_email()}) Accounts.apply_user_email(user, "invalid", %{"email" => unique_user_email()})
assert %{current_password: ["is not valid"]} = errors_on(changeset) assert %{current_password: ["is not valid"]} = errors_on(changeset)
end end
test "applies the email without persisting it", %{user: user} do test "applies the email without persisting it", %{user: user} do
email = unique_user_email() email = unique_user_email()
{:ok, user} = Accounts.apply_user_email(user, valid_user_password(), %{email: email}) {:ok, user} = Accounts.apply_user_email(user, valid_user_password(), %{"email" => email})
assert user.email == email assert user.email == email
assert Accounts.get_user!(user.id).email != email assert Accounts.get_user!(user.id).email != email
end end
@ -259,8 +267,8 @@ defmodule Cannery.AccountsTest do
test "validates password", %{user: user} do test "validates password", %{user: user} do
{:error, changeset} = {:error, changeset} =
Accounts.update_user_password(user, valid_user_password(), %{ Accounts.update_user_password(user, valid_user_password(), %{
password: "not valid", "password" => "not valid",
password_confirmation: "another" "password_confirmation" => "another"
}) })
assert %{ assert %{
@ -273,14 +281,14 @@ defmodule Cannery.AccountsTest do
too_long = String.duplicate("db", 100) too_long = String.duplicate("db", 100)
{:error, changeset} = {:error, changeset} =
Accounts.update_user_password(user, valid_user_password(), %{password: too_long}) Accounts.update_user_password(user, valid_user_password(), %{"password" => too_long})
assert "should be at most 80 character(s)" in errors_on(changeset).password assert "should be at most 80 character(s)" in errors_on(changeset).password
end end
test "validates current password", %{user: user} do test "validates current password", %{user: user} do
{:error, changeset} = {:error, changeset} =
Accounts.update_user_password(user, "invalid", %{password: valid_user_password()}) Accounts.update_user_password(user, "invalid", %{"password" => valid_user_password()})
assert %{current_password: ["is not valid"]} = errors_on(changeset) assert %{current_password: ["is not valid"]} = errors_on(changeset)
end end
@ -288,7 +296,7 @@ defmodule Cannery.AccountsTest do
test "updates the password", %{user: user} do test "updates the password", %{user: user} do
{:ok, user} = {:ok, user} =
Accounts.update_user_password(user, valid_user_password(), %{ Accounts.update_user_password(user, valid_user_password(), %{
password: "new valid password" "password" => "new valid password"
}) })
assert is_nil(user.password) assert is_nil(user.password)
@ -300,7 +308,7 @@ defmodule Cannery.AccountsTest do
{:ok, _} = {:ok, _} =
Accounts.update_user_password(user, valid_user_password(), %{ Accounts.update_user_password(user, valid_user_password(), %{
password: "new valid password" "password" => "new valid password"
}) })
refute Repo.get_by(UserToken, user_id: user.id) refute Repo.get_by(UserToken, user_id: user.id)
@ -468,8 +476,8 @@ defmodule Cannery.AccountsTest do
test "validates password", %{user: user} do test "validates password", %{user: user} do
{:error, changeset} = {:error, changeset} =
Accounts.reset_user_password(user, %{ Accounts.reset_user_password(user, %{
password: "not valid", "password" => "not valid",
password_confirmation: "another" "password_confirmation" => "another"
}) })
assert %{ assert %{
@ -480,19 +488,21 @@ defmodule Cannery.AccountsTest do
test "validates maximum values for password for security", %{user: user} do test "validates maximum values for password for security", %{user: user} do
too_long = String.duplicate("db", 100) too_long = String.duplicate("db", 100)
{:error, changeset} = Accounts.reset_user_password(user, %{password: too_long}) {:error, changeset} = Accounts.reset_user_password(user, %{"password" => too_long})
assert "should be at most 80 character(s)" in errors_on(changeset).password assert "should be at most 80 character(s)" in errors_on(changeset).password
end end
test "updates the password", %{user: user} do test "updates the password", %{user: user} do
{:ok, updated_user} = Accounts.reset_user_password(user, %{password: "new valid password"}) {:ok, updated_user} =
Accounts.reset_user_password(user, %{"password" => "new valid password"})
assert is_nil(updated_user.password) assert is_nil(updated_user.password)
assert Accounts.get_user_by_email_and_password(user.email, "new valid password") assert Accounts.get_user_by_email_and_password(user.email, "new valid password")
end end
test "deletes all tokens for the given user", %{user: user} do test "deletes all tokens for the given user", %{user: user} do
_ = Accounts.generate_user_session_token(user) _ = Accounts.generate_user_session_token(user)
{:ok, _} = Accounts.reset_user_password(user, %{password: "new valid password"}) {:ok, _} = Accounts.reset_user_password(user, %{"password" => "new valid password"})
refute Repo.get_by(UserToken, user_id: user.id) refute Repo.get_by(UserToken, user_id: user.id)
end end
end end