Files
chess-endgame-trainer/test/chesstrainer/accounts_test.exs
T
owenrees 80cb033077 Add theme slugs, kinds and link provenance (#192)
* add function for upgrading a user's plan in interactive Elixir

* Add theme slugs, kinds and link provenance

Themes gain a stable slug, an optional description and a kind so the app can
tell material, motif and technique labels apart. Slugs are generated from the
name on first save and keep their value when the theme is renamed, so links to
a theme stay valid.

Endgame-to-theme links now record their source (derived, curated or community)
and creation time. Existing links are backfilled as curated. A derived pass can
therefore replace only its own links, leaving human work untouched.

The admin index shows each theme's kind and its coverage across the library.
Creating a theme name requires the manage_themes capability, and members get an
unauthorized error instead, ahead of the suggestion flow.

Closes #185
2026-11-17 20:46:43 +01:00

674 lines
22 KiB
Elixir

defmodule Chesstrainer.AccountsTest do
use Chesstrainer.DataCase
alias Chesstrainer.Accounts
import Chesstrainer.AccountsFixtures
import Chesstrainer.EndgamesFixtures
alias Chesstrainer.Accounts.{User, UserToken}
alias Chesstrainer.Attempts
alias Chesstrainer.Attempts.Attempt
alias Chesstrainer.Accounts.Scope
alias Chesstrainer.MailingList
alias Chesstrainer.MailingList.Subscriber
describe "get_user_by_email/1" do
test "does not return the user if the email does not exist" do
refute Accounts.get_user_by_email("unknown@example.com")
end
test "returns the user if the email exists" do
%{id: id} = user = user_fixture()
assert %User{id: ^id} = Accounts.get_user_by_email(user.email)
end
end
describe "get_user_by_login/1" do
test "returns the user by email" do
%{id: id} = user = user_fixture()
assert %User{id: ^id} = Accounts.get_user_by_login(user.email)
end
test "returns the user by username" do
%{id: id} = user = user_fixture()
assert %User{id: ^id} = Accounts.get_user_by_login(user.username)
end
test "ignores case for both email and username" do
user = user_fixture()
assert %User{id: id} = Accounts.get_user_by_login(String.upcase(user.email))
assert user.id == id
assert %User{id: ^id} = Accounts.get_user_by_login(String.upcase(user.username))
end
test "does not return the user if neither identifier exists" do
refute Accounts.get_user_by_login("unknown")
end
end
describe "get_user_by_login_and_password/2" do
test "does not return the user if the login does not exist" do
refute Accounts.get_user_by_login_and_password("unknown@example.com", "hello world!")
end
test "does not return the user if the password is not valid" do
user = user_fixture() |> set_password()
refute Accounts.get_user_by_login_and_password(user.email, "invalid")
end
test "returns the user if the email and password are valid" do
%{id: id} = user = user_fixture() |> set_password()
assert %User{id: ^id} =
Accounts.get_user_by_login_and_password(user.email, valid_user_password())
end
test "returns the user if the username and password are valid" do
%{id: id} = user = user_fixture() |> set_password()
assert %User{id: ^id} =
Accounts.get_user_by_login_and_password(user.username, valid_user_password())
end
end
describe "get_user!/1" do
test "raises if id is invalid" do
assert_raise Ecto.NoResultsError, fn ->
Accounts.get_user!("11111111-1111-1111-1111-111111111111")
end
end
test "returns the user with the given id" do
%{id: id} = user = user_fixture()
assert %User{id: ^id} = Accounts.get_user!(user.id)
end
end
describe "register_user/1" do
test "requires email, username and password to be set" do
{:error, changeset} = Accounts.register_user(%{})
assert %{
email: ["can't be blank"],
username: ["can't be blank"],
password: ["can't be blank"]
} = errors_on(changeset)
end
test "validates email when given" do
{:error, changeset} = Accounts.register_user(valid_user_attributes(email: "not valid"))
assert %{email: ["must have the @ sign and no spaces"]} = errors_on(changeset)
end
test "validates maximum values for email for security" do
too_long = String.duplicate("db", 100)
{:error, changeset} =
Accounts.register_user(valid_user_attributes(email: too_long))
assert "should be at most 160 character(s)" in errors_on(changeset).email
end
test "validates username format" do
{:error, changeset} =
Accounts.register_user(valid_user_attributes(username: "no spaces allowed"))
assert "only letters, numbers and underscores" in errors_on(changeset).username
end
test "validates username length" do
{:error, changeset} =
Accounts.register_user(valid_user_attributes(username: "ab"))
assert "should be at least 3 character(s)" in errors_on(changeset).username
{:error, changeset} =
Accounts.register_user(valid_user_attributes(username: String.duplicate("a", 31)))
assert "should be at most 30 character(s)" in errors_on(changeset).username
end
test "validates password confirmation when given" do
{:error, changeset} =
Accounts.register_user(valid_user_attributes(password_confirmation: "something else"))
assert %{password_confirmation: ["does not match password"]} = errors_on(changeset)
end
test "validates email uniqueness" do
%{email: email} = user_fixture()
{:error, changeset} = Accounts.register_user(valid_user_attributes(email: email))
assert "has already been taken" in errors_on(changeset).email
# Now try with the uppercased email too, to check that email case is ignored.
{:error, changeset} =
Accounts.register_user(valid_user_attributes(email: String.upcase(email)))
assert "has already been taken" in errors_on(changeset).email
end
test "validates username uniqueness, ignoring case" do
%{username: username} = user_fixture()
{:error, changeset} =
Accounts.register_user(valid_user_attributes(username: username))
assert "has already been taken" in errors_on(changeset).username
{:error, changeset} =
Accounts.register_user(valid_user_attributes(username: String.upcase(username)))
assert "has already been taken" in errors_on(changeset).username
end
test "registers users with a password, an unconfirmed account and default access" do
email = unique_user_email()
username = unique_username()
{:ok, user} =
Accounts.register_user(valid_user_attributes(email: email, username: username))
assert user.email == email
assert user.username == username
assert is_binary(user.hashed_password)
assert is_nil(user.password)
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{})
assert Enum.sort(changeset.required) == [:email, :password, :username]
end
test "does not cast the role or plan" do
changeset =
Accounts.change_user_registration(%User{}, %{
email: unique_user_email(),
username: unique_username(),
password: valid_user_password(),
role: :super_admin,
plan: :premium
})
refute Map.has_key?(changeset.changes, :role)
refute Map.has_key?(changeset.changes, :plan)
end
end
describe "update_user_role/2" do
test "changes the user's role" do
user = user_fixture()
assert {:ok, %User{role: :admin}} = Accounts.update_user_role(user, :admin)
end
test "rejects a role that is not in the enum" do
user = user_fixture()
assert {:error, changeset} = Accounts.update_user_role(user, :wizard)
assert %{role: ["is invalid"]} = errors_on(changeset)
end
end
describe "update_user_plan/2" do
test "changes the user's plan" do
user = user_fixture()
assert {:ok, %User{plan: :premium}} = Accounts.update_user_plan(user, :premium)
end
test "rejects a plan that is not in the enum" do
user = user_fixture()
assert {:error, changeset} = Accounts.update_user_plan(user, :platinum)
assert %{plan: ["is invalid"]} = errors_on(changeset)
end
end
describe "sudo_mode?/2" do
test "validates the authenticated_at time" do
now = DateTime.utc_now()
assert Accounts.sudo_mode?(%User{authenticated_at: DateTime.utc_now()})
assert Accounts.sudo_mode?(%User{authenticated_at: DateTime.add(now, -19, :minute)})
refute Accounts.sudo_mode?(%User{authenticated_at: DateTime.add(now, -21, :minute)})
# minute override
refute Accounts.sudo_mode?(
%User{authenticated_at: DateTime.add(now, -11, :minute)},
-10
)
# not authenticated
refute Accounts.sudo_mode?(%User{})
end
end
describe "change_user_email/3" do
test "returns a user changeset" do
assert %Ecto.Changeset{} = changeset = Accounts.change_user_email(%User{})
assert changeset.required == [:email]
end
end
describe "deliver_user_update_email_instructions/3" do
setup do
%{user: user_fixture()}
end
test "sends token through notification", %{user: user} do
token =
extract_user_token(fn url ->
Accounts.deliver_user_update_email_instructions(user, "current@example.com", 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 == "change:current@example.com"
end
end
describe "update_user_email/2" do
setup do
user = user_fixture()
email = unique_user_email()
token =
extract_user_token(fn url ->
Accounts.deliver_user_update_email_instructions(%{user | email: email}, user.email, url)
end)
%{user: user, token: token, email: email}
end
test "updates the email with a valid token", %{user: user, token: token, email: email} do
assert {:ok, %{email: ^email}} = Accounts.update_user_email(user, token)
changed_user = Repo.get!(User, user.id)
assert changed_user.email != user.email
assert changed_user.email == email
refute Repo.get_by(UserToken, user_id: user.id)
end
test "updates the linked mailing-list address", %{
user: user,
token: token,
email: email
} do
{:ok, _subscriber} = MailingList.subscribe_user(user)
MailingList.confirm_user_subscription(user)
assert {:ok, %{email: ^email}} = Accounts.update_user_email(user, token)
assert %Subscriber{email: ^email} = Repo.get_by(Subscriber, user_id: user.id)
end
test "does not update email with invalid token", %{user: user} do
assert Accounts.update_user_email(user, "oops") ==
{:error, :transaction_aborted}
assert Repo.get!(User, user.id).email == user.email
assert Repo.get_by(UserToken, user_id: user.id)
end
test "does not update email if user email changed", %{user: user, token: token} do
assert Accounts.update_user_email(%{user | email: "current@example.com"}, token) ==
{:error, :transaction_aborted}
assert Repo.get!(User, user.id).email == user.email
assert Repo.get_by(UserToken, user_id: user.id)
end
test "does not update email if 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.update_user_email(user, token) ==
{:error, :transaction_aborted}
assert Repo.get!(User, user.id).email == user.email
assert Repo.get_by(UserToken, user_id: user.id)
end
end
describe "change_user_password/3" do
test "returns a user changeset" do
assert %Ecto.Changeset{} = changeset = Accounts.change_user_password(%User{})
assert changeset.required == [:password]
end
test "allows fields to be set" do
changeset =
Accounts.change_user_password(
%User{},
%{
"password" => "new valid password"
},
hash_password: false
)
assert changeset.valid?
assert get_change(changeset, :password) == "new valid password"
assert is_nil(get_change(changeset, :hashed_password))
end
end
describe "update_user_password/2" do
setup do
%{user: user_fixture()}
end
test "validates password", %{user: user} do
{:error, changeset} =
Accounts.update_user_password(user, %{
password: "not valid",
password_confirmation: "another"
})
assert %{
password: ["should be at least 12 character(s)"],
password_confirmation: ["does not match password"]
} = errors_on(changeset)
end
test "validates maximum values for password for security", %{user: user} do
too_long = String.duplicate("db", 100)
{:error, changeset} =
Accounts.update_user_password(user, %{password: too_long})
assert "should be at most 72 character(s)" in errors_on(changeset).password
end
test "updates the password", %{user: user} do
{:ok, {user, expired_tokens}} =
Accounts.update_user_password(user, %{
password: "new valid password"
})
assert expired_tokens == []
assert is_nil(user.password)
assert Accounts.get_user_by_login_and_password(user.email, "new valid password")
end
test "deletes all tokens for the given user", %{user: user} do
_ = Accounts.generate_user_session_token(user)
{:ok, {_, _}} =
Accounts.update_user_password(user, %{
password: "new valid password"
})
refute Repo.get_by(UserToken, user_id: user.id)
end
end
describe "deliver_user_reset_password_instructions/2" do
setup do
%{user: user_fixture()}
end
test "sends token through notification", %{user: user} do
token =
extract_user_token(fn url ->
Accounts.deliver_user_reset_password_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 == "reset_password"
end
end
describe "get_user_by_reset_password_token/1" do
setup do
user = user_fixture()
token =
extract_user_token(fn url ->
Accounts.deliver_user_reset_password_instructions(user, url)
end)
%{user: user, token: token}
end
test "returns the user with a valid token", %{user: user, token: token} do
assert %User{id: id} = Accounts.get_user_by_reset_password_token(token)
assert id == user.id
end
test "returns nil with an invalid token" do
refute Accounts.get_user_by_reset_password_token("oops")
end
test "returns nil if the token expired", %{token: token} do
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
refute Accounts.get_user_by_reset_password_token(token)
end
end
describe "reset_user_password/2" do
setup do
%{user: user_fixture()}
end
test "updates the password", %{user: user} do
assert {:ok, {%User{} = updated_user, expired_tokens}} =
Accounts.reset_user_password(user, %{password: "new valid password"})
assert updated_user.id == user.id
assert expired_tokens == []
assert is_nil(updated_user.password)
assert Accounts.get_user_by_login_and_password(user.email, "new valid password")
refute Accounts.get_user_by_login_and_password(user.email, valid_user_password())
end
test "deletes all tokens for the given user", %{user: user} do
token =
extract_user_token(fn url ->
Accounts.deliver_user_reset_password_instructions(user, url)
end)
{:ok, {_, _}} = Accounts.reset_user_password(user, %{password: "new valid password"})
refute Repo.get_by(UserToken, user_id: user.id)
refute Accounts.get_user_by_reset_password_token(token)
end
test "validates the new password", %{user: user} do
assert {:error, changeset} =
Accounts.reset_user_password(user, %{
password: "not valid",
password_confirmation: "another"
})
assert %{
password: ["should be at least 12 character(s)"],
password_confirmation: ["does not match password"]
} = errors_on(changeset)
end
end
describe "generate_user_session_token/1" do
setup do
%{user: user_fixture()}
end
test "generates a token", %{user: user} do
token = Accounts.generate_user_session_token(user)
assert user_token = Repo.get_by(UserToken, token: token)
assert user_token.context == "session"
assert user_token.authenticated_at != nil
# Creating the same token for another user should fail
assert_raise Ecto.ConstraintError, fn ->
Repo.insert!(%UserToken{
token: user_token.token,
user_id: user_fixture().id,
context: "session"
})
end
end
test "duplicates the authenticated_at of given user in new token", %{user: user} do
user = %{user | authenticated_at: DateTime.add(DateTime.utc_now(:second), -3600)}
token = Accounts.generate_user_session_token(user)
assert user_token = Repo.get_by(UserToken, token: token)
assert user_token.authenticated_at == user.authenticated_at
assert DateTime.compare(user_token.inserted_at, user.authenticated_at) == :gt
end
end
describe "get_user_by_session_token/1" do
setup do
user = user_fixture()
token = Accounts.generate_user_session_token(user)
%{user: user, token: token}
end
test "returns user by token", %{user: user, token: token} do
assert {session_user, token_inserted_at} = Accounts.get_user_by_session_token(token)
assert session_user.id == user.id
assert session_user.authenticated_at != nil
assert token_inserted_at != nil
end
test "does not return user for invalid token" do
refute Accounts.get_user_by_session_token("oops")
end
test "does not return user for expired token", %{token: token} do
dt = ~N[2020-01-01 00:00:00]
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: dt, authenticated_at: dt])
refute Accounts.get_user_by_session_token(token)
end
end
describe "count_logged_in_users/0" do
test "counts distinct users with a valid session token" do
user = user_fixture()
_ = Accounts.generate_user_session_token(user)
_ = Accounts.generate_user_session_token(user)
_other = Accounts.generate_user_session_token(user_fixture())
assert Accounts.count_logged_in_users() == 2
end
test "ignores expired session tokens" do
user = user_fixture()
user_token = Accounts.generate_user_session_token(user)
offset_user_token(user_token, -15, :day)
assert Accounts.count_logged_in_users() == 0
end
end
describe "delete_user/1" do
test "deletes the user and their session tokens" do
user = user_fixture()
token = Accounts.generate_user_session_token(user)
assert {:ok, %User{}} = Accounts.delete_user(user)
refute Accounts.get_user_by_email(user.email)
refute Repo.get_by(UserToken, token: token)
end
test "deletes the user's attempts" do
user = user_fixture()
endgame = endgame_fixture()
assert {:ok, attempt} =
Attempts.record_attempt(Scope.for_user(user), endgame, :solved)
assert {:ok, %User{}} = Accounts.delete_user(user)
refute Repo.get(Attempt, attempt.id)
end
end
describe "delete_user_session_token/1" do
test "deletes the token" do
user = user_fixture()
token = Accounts.generate_user_session_token(user)
assert Accounts.delete_user_session_token(token) == :ok
refute Accounts.get_user_by_session_token(token)
end
end
describe "inspect/2 for the User module" do
test "does not include password" do
refute inspect(%User{password: "123456"}) =~ "password: \"123456\""
end
end
end