add accounts doctests

This commit is contained in:
shibao 2023-01-29 14:30:42 -05:00 committed by oliviasculley
parent 6dbadc58ae
commit 737484c36e
11 changed files with 245 additions and 153 deletions

View File

@ -16,14 +16,15 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> get_user_by_email("foo@example.com") iex> register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> with %User{} <- get_user_by_email("foo@example.com"), do: :passed
:passed
iex> get_user_by_email("unknown@example.com") iex> get_user_by_email("unknown@example.com")
nil nil
""" """
@spec get_user_by_email(String.t()) :: User.t() | nil @spec get_user_by_email(email :: String.t()) :: User.t() | nil
def get_user_by_email(email) when is_binary(email), do: Repo.get_by(User, email: email) def get_user_by_email(email) when is_binary(email), do: Repo.get_by(User, email: email)
@doc """ @doc """
@ -31,14 +32,15 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> get_user_by_email_and_password("foo@example.com", "correct_password") iex> register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> with %User{} <- get_user_by_email_and_password("foo@example.com", "valid_password"), do: :passed
:passed
iex> get_user_by_email_and_password("foo@example.com", "invalid_password") iex> get_user_by_email_and_password("foo@example.com", "invalid_password")
nil nil
""" """
@spec get_user_by_email_and_password(String.t(), String.t()) :: @spec get_user_by_email_and_password(email :: String.t(), password :: String.t()) ::
User.t() | nil User.t() | nil
def get_user_by_email_and_password(email, password) def get_user_by_email_and_password(email, password)
when is_binary(email) and is_binary(password) do when is_binary(email) and is_binary(password) do
@ -53,10 +55,11 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> get_user!(123) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> get_user!(user.id)
user
iex> get_user!(456) > get_user!()
** (Ecto.NoResultsError) ** (Ecto.NoResultsError)
""" """
@ -68,13 +71,15 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> list_users_by_role(%User{id: 123, role: :admin}) iex> {:ok, user1} = register_user(%{email: "foo1@example.com", password: "valid_password"})
[admin: [%User{}], user: [%User{}, %User{}]] iex> {:ok, user2} = register_user(%{email: "foo2@example.com", password: "valid_password"})
iex> with %{admin: [^user1], user: [^user2]} <- list_all_users_by_role(user1), do: :passed
:passed
""" """
@spec list_all_users_by_role(User.t()) :: %{String.t() => [User.t()]} @spec list_all_users_by_role(User.t()) :: %{User.role() => [User.t()]}
def list_all_users_by_role(%User{role: :admin}) do def list_all_users_by_role(%User{role: :admin}) do
Repo.all(from u in User, order_by: u.email) |> Enum.group_by(fn user -> user.role end) Repo.all(from u in User, order_by: u.email) |> Enum.group_by(fn %{role: role} -> role end)
end end
@doc """ @doc """
@ -82,13 +87,13 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> list_users_by_role(%User{id: 123, role: :admin}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
[%User{}] iex> with [^user] <- list_users_by_role(:admin), do: :passed
:passed
""" """
@spec list_users_by_role(:admin | :user) :: [User.t()] @spec list_users_by_role(:admin) :: [User.t()]
def list_users_by_role(role) do def list_users_by_role(:admin = role) do
role = role |> to_string()
Repo.all(from u in User, where: u.role == ^role, order_by: u.email) Repo.all(from u in User, where: u.role == ^role, order_by: u.email)
end end
@ -99,22 +104,30 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> register_user(%{field: value}) iex> with {:ok, %User{email: "foo@example.com"}} <-
{:ok, %User{}} ...> register_user(%{email: "foo@example.com", password: "valid_password"}),
...> do: :passed
:passed
iex> register_user(%{field: bad_value}) iex> with {:error, %Changeset{}} <- register_user(%{email: "foo@example"}), do: :passed
{:error, %Changeset{}} :passed
""" """
@spec register_user(map()) :: {:ok, User.t()} | {:error, Changeset.t(User.new_user())} @spec register_user(attrs :: map()) :: {:ok, User.t()} | {:error, User.changeset()}
def register_user(attrs) do def register_user(attrs) do
Multi.new()
|> Multi.one(:users_count, from(u in User, select: count(u.id), distinct: true))
|> Multi.insert(:add_user, fn %{users_count: count} ->
# if no registered users, make first user an admin # if no registered users, make first user an admin
role = role = if count == 0, do: :admin, else: :user
if Repo.one!(from u in User, select: count(u.id), distinct: true) == 0,
do: "admin",
else: "user"
%User{} |> User.registration_changeset(attrs |> Map.put("role", role)) |> Repo.insert() User.registration_changeset(attrs) |> User.role_changeset(role)
end)
|> Repo.transaction()
|> case do
{:ok, %{add_user: user}} -> {:ok, user}
{:error, :add_user, changeset, _changes_so_far} -> {:error, changeset}
end
end end
@doc """ @doc """
@ -122,16 +135,17 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> change_user_registration(user) iex> with %Changeset{} <- change_user_registration(), do: :passed
%Changeset{data: %User{}} :passed
iex> with %Changeset{} <- change_user_registration(%{password: "hi"}), do: :passed
:passed
""" """
@spec change_user_registration(User.t() | User.new_user()) :: @spec change_user_registration() :: User.changeset()
Changeset.t(User.t() | User.new_user()) @spec change_user_registration(attrs :: map()) :: User.changeset()
@spec change_user_registration(User.t() | User.new_user(), map()) :: def change_user_registration(attrs \\ %{}),
Changeset.t(User.t() | User.new_user()) do: User.registration_changeset(attrs, hash_password: false)
def change_user_registration(user, attrs \\ %{}),
do: User.registration_changeset(user, attrs, hash_password: false)
## Settings ## Settings
@ -140,11 +154,12 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> change_user_email(user) iex> with %Changeset{} <- change_user_email(%User{email: "foo@example.com"}), do: :passed
%Changeset{data: %User{}} :passed
""" """
@spec change_user_email(User.t(), map()) :: Changeset.t(User.t()) @spec change_user_email(User.t()) :: User.changeset()
@spec change_user_email(User.t(), attrs :: map()) :: User.changeset()
def change_user_email(user, attrs \\ %{}), do: User.email_changeset(user, attrs) def change_user_email(user, attrs \\ %{}), do: User.email_changeset(user, attrs)
@doc """ @doc """
@ -152,11 +167,11 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> change_user_role(user) iex> with %Changeset{} <- change_user_role(%User{}, :user), do: :passed
%Changeset{data: %User{}} :passed
""" """
@spec change_user_role(User.t(), atom()) :: Changeset.t(User.t()) @spec change_user_role(User.t(), User.role()) :: User.changeset()
def change_user_role(user, role), do: User.role_changeset(user, role) def change_user_role(user, role), do: User.role_changeset(user, role)
@doc """ @doc """
@ -165,15 +180,21 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> apply_user_email(user, "valid password", %{email: ...}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %User{}} iex> with {:ok, %User{}} <-
...> apply_user_email(user, "valid_password", %{email: "new_email@account.com"}),
...> do: :passed
:passed
iex> apply_user_email(user, "invalid password", %{email: ...}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:error, %Changeset{}} iex> with {:error, %Changeset{}} <-
...> apply_user_email(user, "invalid password", %{email: "new_email@account"}),
...> do: :passed
:passed
""" """
@spec apply_user_email(User.t(), String.t(), map()) :: @spec apply_user_email(User.t(), email :: String.t(), attrs :: map()) ::
{:ok, User.t()} | {:error, Changeset.t(User.t())} {:ok, User.t()} | {:error, User.changeset()}
def apply_user_email(user, password, attrs) do def apply_user_email(user, password, attrs) do
user user
|> User.email_changeset(attrs) |> User.email_changeset(attrs)
@ -187,7 +208,7 @@ defmodule Lokal.Accounts do
If the token matches, the user email is updated and the token is deleted. If the token matches, the user email is updated and the token is deleted.
The confirmed_at date is also updated to the current time. The confirmed_at date is also updated to the current time.
""" """
@spec update_user_email(User.t(), String.t()) :: :ok | :error @spec update_user_email(User.t(), token :: String.t()) :: :ok | :error
def update_user_email(user, token) do def update_user_email(user, token) do
context = "change:#{user.email}" context = "change:#{user.email}"
@ -196,11 +217,11 @@ defmodule Lokal.Accounts do
{:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do {:ok, _} <- Repo.transaction(user_email_multi(user, email, context)) do
:ok :ok
else else
_ -> :error _error_tuple -> :error
end end
end end
@spec user_email_multi(User.t(), String.t(), String.t()) :: Multi.t() @spec user_email_multi(User.t(), email :: String.t(), context :: String.t()) :: Multi.t()
defp user_email_multi(user, email, context) do defp user_email_multi(user, email, context) do
changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset() changeset = user |> User.email_changeset(%{email: email}) |> User.confirm_changeset()
@ -214,11 +235,16 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> deliver_update_email_instructions(user, current_email, &Routes.user_update_email_url(conn, :edit, &1)) iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %{to: ..., body: ...}} iex> with %Oban.Job{
...> args: %{email: :update_email, user_id: ^user_id, attrs: %{url: "example url"}}
...> } <- deliver_update_email_instructions(user, "new_foo@example.com", fn _token -> "example url" end),
...> do: :passed
:passed
""" """
@spec deliver_update_email_instructions(User.t(), String.t(), function) :: Job.t() @spec deliver_update_email_instructions(User.t(), current_email :: String.t(), function) ::
Job.t()
def deliver_update_email_instructions(user, current_email, update_email_url_fun) def deliver_update_email_instructions(user, current_email, update_email_url_fun)
when is_function(update_email_url_fun, 1) do when is_function(update_email_url_fun, 1) do
{encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}") {encoded_token, user_token} = UserToken.build_email_token(user, "change:#{current_email}")
@ -231,11 +257,11 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> change_user_password(user) iex> with %Changeset{} <- change_user_password(%User{}), do: :passed
%Changeset{data: %User{}} :passed
""" """
@spec change_user_password(User.t(), map()) :: Changeset.t(User.t()) @spec change_user_password(User.t(), attrs :: map()) :: User.changeset()
def change_user_password(user, attrs \\ %{}), def change_user_password(user, attrs \\ %{}),
do: User.password_changeset(user, attrs, hash_password: false) do: User.password_changeset(user, attrs, hash_password: false)
@ -244,15 +270,24 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> update_user_password(user, "valid password", %{password: ...}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %User{}} iex> with {:ok, %User{}} <-
...> reset_user_password(user, %{
...> password: "new password",
...> password_confirmation: "new password"
...> }),
...> do: :passed
:passed
iex> update_user_password(user, "invalid password", %{password: ...}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:error, %Changeset{}} iex> with {:error, %Changeset{}} <-
...> update_user_password(user, "invalid password", %{password: "123"}),
...> do: :passed
:passed
""" """
@spec update_user_password(User.t(), String.t(), map()) :: @spec update_user_password(User.t(), String.t(), attrs :: map()) ::
{:ok, User.t()} | {:error, Changeset.t(User.t())} {:ok, User.t()} | {:error, User.changeset()}
def update_user_password(user, password, attrs) do def update_user_password(user, password, attrs) do
changeset = changeset =
user user
@ -265,20 +300,20 @@ defmodule Lokal.Accounts do
|> Repo.transaction() |> Repo.transaction()
|> case do |> case do
{:ok, %{user: user}} -> {:ok, user} {:ok, %{user: user}} -> {:ok, user}
{:error, :user, changeset, _} -> {:error, changeset} {:error, :user, changeset, _changes_so_far} -> {:error, changeset}
end end
end end
@doc """ @doc """
Returns an `%Changeset{}` for changing the user locale. Returns an `Ecto.Changeset.t()` for changing the user locale.
## Examples ## Examples
iex> change_user_locale(user) iex> with %Changeset{} <- change_user_locale(%User{}), do: :passed
%Changeset{data: %User{}} :passed
""" """
@spec change_user_locale(User.t()) :: Changeset.t(User.t()) @spec change_user_locale(User.t()) :: User.changeset()
def change_user_locale(%{locale: locale} = user), do: User.locale_changeset(user, locale) def change_user_locale(%{locale: locale} = user), do: User.locale_changeset(user, locale)
@doc """ @doc """
@ -286,15 +321,13 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> update_user_locale(user, "valid locale") iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %User{}} iex> with {:ok, %User{}} <- update_user_locale(user, "en_US"), do: :passed
:passed
iex> update_user_password(user, "invalid locale")
{:error, %Changeset{}}
""" """
@spec update_user_locale(User.t(), locale :: String.t()) :: @spec update_user_locale(User.t(), locale :: String.t()) ::
{:ok, User.t()} | {:error, Changeset.t(User.t())} {:ok, User.t()} | {:error, User.changeset()}
def update_user_locale(user, locale), def update_user_locale(user, locale),
do: user |> User.locale_changeset(locale) |> Repo.update() do: user |> User.locale_changeset(locale) |> Repo.update()
@ -303,14 +336,16 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> delete_user!(user_to_delete, %User{id: 123, role: :admin}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> with %User{} <- delete_user!(user, %User{id: 123, role: :admin}), do: :passed
:passed
iex> delete_user!(%User{id: 123}, %User{id: 123}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> with %User{} <- delete_user!(user, user), do: :passed
:passed
""" """
@spec delete_user!(User.t(), User.t()) :: User.t() @spec delete_user!(user_to_delete :: User.t(), User.t()) :: User.t()
def delete_user!(user, %User{role: :admin}), do: user |> Repo.delete!() def delete_user!(user, %User{role: :admin}), do: user |> Repo.delete!()
def delete_user!(%User{id: user_id} = user, %User{id: user_id}), do: user |> Repo.delete!() def delete_user!(%User{id: user_id} = user, %User{id: user_id}), do: user |> Repo.delete!()
@ -329,7 +364,7 @@ defmodule Lokal.Accounts do
@doc """ @doc """
Gets the user with the given signed token. Gets the user with the given signed token.
""" """
@spec get_user_by_session_token(String.t()) :: User.t() @spec get_user_by_session_token(token :: String.t()) :: User.t()
def get_user_by_session_token(token) do def get_user_by_session_token(token) do
{:ok, query} = UserToken.verify_session_token_query(token) {:ok, query} = UserToken.verify_session_token_query(token)
Repo.one(query) Repo.one(query)
@ -338,7 +373,7 @@ defmodule Lokal.Accounts do
@doc """ @doc """
Deletes the signed token with the given context. Deletes the signed token with the given context.
""" """
@spec delete_session_token(String.t()) :: :ok @spec delete_session_token(token :: String.t()) :: :ok
def delete_session_token(token) do def delete_session_token(token) do
Repo.delete_all(UserToken.token_and_context_query(token, "session")) Repo.delete_all(UserToken.token_and_context_query(token, "session"))
:ok :ok
@ -349,19 +384,45 @@ defmodule Lokal.Accounts do
""" """
@spec allow_registration?() :: boolean() @spec allow_registration?() :: boolean()
def allow_registration? do def allow_registration? do
Application.get_env(:lokal, LokalWeb.Endpoint)[:registration] == "public" or Application.get_env(:Lokal, LokalWeb.Endpoint)[:registration] == "public" or
list_users_by_role(:admin) |> Enum.empty?() list_users_by_role(:admin) |> Enum.empty?()
end end
@doc """ @doc """
Checks if user is an admin Checks if user is an admin
## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> is_admin?(user)
true
iex> is_admin?(%User{id: Ecto.UUID.generate()})
false
""" """
@spec is_admin?(User.t()) :: boolean() @spec is_admin?(User.t()) :: boolean()
def is_admin?(%User{id: user_id}) do def is_admin?(%User{id: user_id}) do
Repo.one(from u in User, where: u.id == ^user_id and u.role == :admin) Repo.exists?(from u in User, where: u.id == ^user_id, where: u.role == :admin)
|> is_nil()
end end
@doc """
Checks to see if user has the admin role
## Examples
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> is_already_admin?(user)
true
iex> is_already_admin?(%User{})
false
"""
@spec is_already_admin?(User.t() | nil) :: boolean()
def is_already_admin?(%User{role: :admin}), do: true
def is_already_admin?(_invalid_user), do: false
## Confirmation ## Confirmation
@doc """ @doc """
@ -369,10 +430,16 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> deliver_user_confirmation_instructions(user, &Routes.user_confirmation_url(conn, :confirm, &1)) iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %{to: ..., body: ...}} iex> with %Oban.Job{
...> args: %{email: :welcome, user_id: ^user_id, attrs: %{url: "example url"}}
...> } <- deliver_user_confirmation_instructions(user, fn _token -> "example url" end),
...> do: :passed
:passed
iex> deliver_user_confirmation_instructions(confirmed_user, &Routes.user_confirmation_url(conn, :confirm, &1)) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> user = user |> User.confirm_changeset() |> Repo.update!()
iex> deliver_user_confirmation_instructions(user, fn _token -> "example url" end)
{:error, :already_confirmed} {:error, :already_confirmed}
""" """
@ -394,14 +461,14 @@ defmodule Lokal.Accounts do
If the token matches, the user account is marked as confirmed If the token matches, the user account is marked as confirmed
and the token is deleted. and the token is deleted.
""" """
@spec confirm_user(String.t()) :: {:ok, User.t()} | atom() @spec confirm_user(token :: String.t()) :: {:ok, User.t()} | :error
def confirm_user(token) do def confirm_user(token) do
with {:ok, query} <- UserToken.verify_email_token_query(token, "confirm"), with {:ok, query} <- UserToken.verify_email_token_query(token, "confirm"),
%User{} = user <- Repo.one(query), %User{} = user <- Repo.one(query),
{:ok, %{user: user}} <- Repo.transaction(confirm_user_multi(user)) do {:ok, %{user: user}} <- Repo.transaction(confirm_user_multi(user)) do
{:ok, user} {:ok, user}
else else
_ -> :error _error_tuple -> :error
end end
end end
@ -419,8 +486,12 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> deliver_user_reset_password_instructions(user, &Routes.user_reset_password_url(conn, :edit, &1)) iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %{to: ..., body: ...}} iex> with %Oban.Job{args: %{
...> email: :reset_password, user_id: ^user_id, attrs: %{url: "example url"}}
...> } <- deliver_user_reset_password_instructions(user, fn _token -> "example url" end),
...> do: :passed
:passed
""" """
@spec deliver_user_reset_password_instructions(User.t(), function()) :: Job.t() @spec deliver_user_reset_password_instructions(User.t(), function()) :: Job.t()
@ -436,20 +507,23 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> get_user_by_reset_password_token("validtoken") iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
%User{} iex> {encoded_token, user_token} = UserToken.build_email_token(user, "reset_password")
iex> Repo.insert!(user_token)
iex> with %User{} <- get_user_by_reset_password_token(encoded_token), do: :passed
:passed
iex> get_user_by_reset_password_token("invalidtoken") iex> get_user_by_reset_password_token("invalidtoken")
nil nil
""" """
@spec get_user_by_reset_password_token(String.t()) :: User.t() | nil @spec get_user_by_reset_password_token(token :: String.t()) :: User.t() | nil
def get_user_by_reset_password_token(token) do def get_user_by_reset_password_token(token) do
with {:ok, query} <- UserToken.verify_email_token_query(token, "reset_password"), with {:ok, query} <- UserToken.verify_email_token_query(token, "reset_password"),
%User{} = user <- Repo.one(query) do %User{} = user <- Repo.one(query) do
user user
else else
_ -> nil _error_tuple -> nil
end end
end end
@ -458,14 +532,24 @@ defmodule Lokal.Accounts do
## Examples ## Examples
iex> reset_user_password(user, %{password: "new long password", password_confirmation: "new long password"}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:ok, %User{}} iex> with {:ok, %User{}} <-
...> reset_user_password(user, %{
...> password: "new password",
...> password_confirmation: "new password"
...> }),
...> do: :passed
:passed
iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"}) iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
{:error, %Changeset{}} iex> with {:error, %Changeset{}} <-
...> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"}),
...> do: :passed
:passed
""" """
@spec reset_user_password(User.t(), map()) :: {:ok, User.t()} | {:error, Changeset.t(User.t())} @spec reset_user_password(User.t(), attrs :: map()) ::
{:ok, User.t()} | {:error, User.changeset()}
def reset_user_password(user, attrs) do def reset_user_password(user, attrs) do
Multi.new() Multi.new()
|> Multi.update(:user, User.password_changeset(user, attrs)) |> Multi.update(:user, User.password_changeset(user, attrs))
@ -473,7 +557,7 @@ defmodule Lokal.Accounts do
|> Repo.transaction() |> Repo.transaction()
|> case do |> case do
{:ok, %{user: user}} -> {:ok, user} {:ok, %{user: user}} -> {:ok, user}
{:error, :user, changeset, _} -> {:error, changeset} {:error, :user, changeset, _changes_so_far} -> {:error, changeset}
end end
end end
end end

View File

@ -9,6 +9,14 @@ defmodule Lokal.Accounts.User do
alias Ecto.{Changeset, UUID} alias Ecto.{Changeset, UUID}
alias Lokal.{Accounts.User, Invites.Invite} alias Lokal.{Accounts.User, Invites.Invite}
@derive {Jason.Encoder,
only: [
:id,
:email,
:confirmed_at,
:role,
:locale
]}
@derive {Inspect, except: [:password]} @derive {Inspect, except: [:password]}
@primary_key {:id, :binary_id, autogenerate: true} @primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id @foreign_key_type :binary_id
@ -31,14 +39,16 @@ defmodule Lokal.Accounts.User do
password: String.t(), password: String.t(),
hashed_password: String.t(), hashed_password: String.t(),
confirmed_at: NaiveDateTime.t(), confirmed_at: NaiveDateTime.t(),
role: atom(), role: role(),
invites: [Invite.t()],
locale: String.t() | nil, locale: String.t() | nil,
invites: [Invite.t()],
inserted_at: NaiveDateTime.t(), inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t() updated_at: NaiveDateTime.t()
} }
@type new_user :: %User{} @type new_user :: %User{}
@type id :: UUID.t() @type id :: UUID.t()
@type changeset :: Changeset.t(t() | new_user())
@type role :: :admin | :user
@doc """ @doc """
A user changeset for registration. A user changeset for registration.
@ -57,26 +67,24 @@ defmodule Lokal.Accounts.User do
validations on a LiveView form), this option can be set to `false`. validations on a LiveView form), this option can be set to `false`.
Defaults to `true`. Defaults to `true`.
""" """
@spec registration_changeset(t() | new_user(), attrs :: map()) :: Changeset.t(t() | new_user()) @spec registration_changeset(attrs :: map()) :: changeset()
@spec registration_changeset(t() | new_user(), attrs :: map(), opts :: keyword()) :: @spec registration_changeset(attrs :: map(), opts :: keyword()) :: changeset()
Changeset.t(t() | new_user()) def registration_changeset(attrs, opts \\ []) do
def registration_changeset(user, attrs, opts \\ []) do %User{}
user |> cast(attrs, [:email, :password, :locale])
|> cast(attrs, [:email, :password, :role, :locale])
|> validate_email() |> validate_email()
|> validate_password(opts) |> validate_password(opts)
end end
@doc """ @doc """
A user changeset for role. A user changeset for role.
""" """
@spec role_changeset(t(), role :: atom()) :: Changeset.t(t()) @spec role_changeset(t() | new_user() | changeset(), role()) :: changeset()
def role_changeset(user, role) do def role_changeset(user, role) do
user |> cast(%{"role" => role}, [:role]) user |> change(role: role)
end end
@spec validate_email(Changeset.t(t() | new_user())) :: Changeset.t(t() | new_user()) @spec validate_email(changeset()) :: changeset()
defp validate_email(changeset) do defp validate_email(changeset) do
changeset changeset
|> validate_required([:email]) |> validate_required([:email])
@ -88,8 +96,8 @@ defmodule Lokal.Accounts.User do
|> unique_constraint(:email) |> unique_constraint(:email)
end end
@spec validate_password(Changeset.t(t() | new_user()), opts :: keyword()) :: @spec validate_password(changeset(), opts :: keyword()) ::
Changeset.t(t() | new_user()) changeset()
defp validate_password(changeset, opts) do defp validate_password(changeset, opts) do
changeset changeset
|> validate_required([:password]) |> validate_required([:password])
@ -100,8 +108,7 @@ defmodule Lokal.Accounts.User do
|> maybe_hash_password(opts) |> maybe_hash_password(opts)
end end
@spec maybe_hash_password(Changeset.t(t() | new_user()), opts :: keyword()) :: @spec maybe_hash_password(changeset(), opts :: keyword()) :: changeset()
Changeset.t(t() | new_user())
defp maybe_hash_password(changeset, opts) do defp maybe_hash_password(changeset, opts) do
hash_password? = Keyword.get(opts, :hash_password, true) hash_password? = Keyword.get(opts, :hash_password, true)
password = get_change(changeset, :password) password = get_change(changeset, :password)
@ -120,7 +127,7 @@ defmodule Lokal.Accounts.User do
It requires the email to change otherwise an error is added. It requires the email to change otherwise an error is added.
""" """
@spec email_changeset(t(), attrs :: map()) :: Changeset.t(t()) @spec email_changeset(t(), attrs :: map()) :: changeset()
def email_changeset(user, attrs) do def email_changeset(user, attrs) do
user user
|> cast(attrs, [:email]) |> cast(attrs, [:email])
@ -143,8 +150,8 @@ defmodule Lokal.Accounts.User do
validations on a LiveView form), this option can be set to `false`. validations on a LiveView form), this option can be set to `false`.
Defaults to `true`. Defaults to `true`.
""" """
@spec password_changeset(t(), attrs :: map()) :: Changeset.t(t()) @spec password_changeset(t(), attrs :: map()) :: changeset()
@spec password_changeset(t(), attrs :: map(), opts :: keyword()) :: Changeset.t(t()) @spec password_changeset(t(), attrs :: map(), opts :: keyword()) :: changeset()
def password_changeset(user, attrs, opts \\ []) do def password_changeset(user, attrs, opts \\ []) do
user user
|> cast(attrs, [:password]) |> cast(attrs, [:password])
@ -155,7 +162,7 @@ defmodule Lokal.Accounts.User do
@doc """ @doc """
Confirms the account by setting `confirmed_at`. Confirms the account by setting `confirmed_at`.
""" """
@spec confirm_changeset(t() | Changeset.t(t())) :: Changeset.t(t()) @spec confirm_changeset(t() | changeset()) :: changeset()
def confirm_changeset(user_or_changeset) do def confirm_changeset(user_or_changeset) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second) now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
user_or_changeset |> change(confirmed_at: now) user_or_changeset |> change(confirmed_at: now)
@ -173,7 +180,7 @@ defmodule Lokal.Accounts.User do
Bcrypt.verify_pass(password, hashed_password) Bcrypt.verify_pass(password, hashed_password)
end end
def valid_password?(_, _) do def valid_password?(_invalid_user, _invalid_password) do
Bcrypt.no_user_verify() Bcrypt.no_user_verify()
false false
end end
@ -181,7 +188,7 @@ defmodule Lokal.Accounts.User do
@doc """ @doc """
Validates the current password otherwise adds an error to the changeset. Validates the current password otherwise adds an error to the changeset.
""" """
@spec validate_current_password(Changeset.t(t()), String.t()) :: Changeset.t(t()) @spec validate_current_password(changeset(), String.t()) :: changeset()
def validate_current_password(changeset, password) do def validate_current_password(changeset, password) do
if valid_password?(changeset.data, password), if valid_password?(changeset.data, password),
do: changeset, do: changeset,
@ -191,7 +198,7 @@ defmodule Lokal.Accounts.User do
@doc """ @doc """
A changeset for changing the user's locale A changeset for changing the user's locale
""" """
@spec locale_changeset(t() | Changeset.t(t()), locale :: String.t() | nil) :: Changeset.t(t()) @spec locale_changeset(t() | changeset(), locale :: String.t() | nil) :: changeset()
def locale_changeset(user_or_changeset, locale) do def locale_changeset(user_or_changeset, locale) do
user_or_changeset user_or_changeset
|> cast(%{"locale" => locale}, [:locale]) |> cast(%{"locale" => locale}, [:locale])

View File

@ -2,7 +2,6 @@ defmodule LokalWeb.UserRegistrationController do
use LokalWeb, :controller use LokalWeb, :controller
import LokalWeb.Gettext import LokalWeb.Gettext
alias Lokal.{Accounts, Invites} alias Lokal.{Accounts, Invites}
alias Lokal.Accounts.User
alias LokalWeb.{Endpoint, HomeLive} alias LokalWeb.{Endpoint, HomeLive}
def new(conn, %{"invite" => invite_token}) do def new(conn, %{"invite" => invite_token}) do
@ -30,7 +29,7 @@ defmodule LokalWeb.UserRegistrationController do
# renders new user registration page # renders new user registration page
defp render_new(conn, invite \\ nil) do defp render_new(conn, invite \\ nil) do
render(conn, "new.html", render(conn, "new.html",
changeset: Accounts.change_user_registration(%User{}), changeset: Accounts.change_user_registration(),
invite: invite, invite: invite,
page_title: gettext("Register") page_title: gettext("Register")
) )

View File

@ -63,7 +63,7 @@ msgstr ""
msgid "Reconnecting..." msgid "Reconnecting..."
msgstr "" msgstr ""
#: lib/lokal_web/controllers/user_registration_controller.ex:35 #: lib/lokal_web/controllers/user_registration_controller.ex:34
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Register" msgid "Register"
msgstr "" msgstr ""

View File

@ -63,7 +63,7 @@ msgstr ""
msgid "Reconnecting..." msgid "Reconnecting..."
msgstr "" msgstr ""
#: lib/lokal_web/controllers/user_registration_controller.ex:35 #: lib/lokal_web/controllers/user_registration_controller.ex:34
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Register" msgid "Register"
msgstr "" msgstr ""

View File

@ -140,14 +140,14 @@ msgstr ""
msgid "Reset password link is invalid or it has expired." msgid "Reset password link is invalid or it has expired."
msgstr "" msgstr ""
#: lib/lokal_web/controllers/user_registration_controller.ex:25 #: lib/lokal_web/controllers/user_registration_controller.ex:24
#: lib/lokal_web/controllers/user_registration_controller.ex:56 #: lib/lokal_web/controllers/user_registration_controller.ex:55
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Sorry, public registration is disabled" msgid "Sorry, public registration is disabled"
msgstr "" msgstr ""
#: lib/lokal_web/controllers/user_registration_controller.ex:15 #: lib/lokal_web/controllers/user_registration_controller.ex:14
#: lib/lokal_web/controllers/user_registration_controller.ex:46 #: lib/lokal_web/controllers/user_registration_controller.ex:45
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Sorry, this invite was not found or expired" msgid "Sorry, this invite was not found or expired"
msgstr "" msgstr ""
@ -178,22 +178,22 @@ msgstr ""
msgid "You must confirm your account and log in to access this page." msgid "You must confirm your account and log in to access this page."
msgstr "" msgstr ""
#: lib/lokal/accounts/user.ex:130 #: lib/lokal/accounts/user.ex:137
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "did not change" msgid "did not change"
msgstr "" msgstr ""
#: lib/lokal/accounts/user.ex:151 #: lib/lokal/accounts/user.ex:158
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "does not match password" msgid "does not match password"
msgstr "" msgstr ""
#: lib/lokal/accounts/user.ex:188 #: lib/lokal/accounts/user.ex:195
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "is not valid" msgid "is not valid"
msgstr "" msgstr ""
#: lib/lokal/accounts/user.ex:84 #: lib/lokal/accounts/user.ex:92
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "must have the @ sign and no spaces" msgid "must have the @ sign and no spaces"
msgstr "" msgstr ""

View File

@ -60,7 +60,7 @@ msgstr ""
msgid "Password updated successfully." msgid "Password updated successfully."
msgstr "" msgstr ""
#: lib/lokal_web/controllers/user_registration_controller.ex:74 #: lib/lokal_web/controllers/user_registration_controller.ex:73
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Please check your email to verify your account" msgid "Please check your email to verify your account"
msgstr "" msgstr ""

View File

@ -137,14 +137,14 @@ msgstr ""
msgid "Reset password link is invalid or it has expired." msgid "Reset password link is invalid or it has expired."
msgstr "" msgstr ""
#: lib/lokal_web/controllers/user_registration_controller.ex:25 #: lib/lokal_web/controllers/user_registration_controller.ex:24
#: lib/lokal_web/controllers/user_registration_controller.ex:56 #: lib/lokal_web/controllers/user_registration_controller.ex:55
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Sorry, public registration is disabled" msgid "Sorry, public registration is disabled"
msgstr "" msgstr ""
#: lib/lokal_web/controllers/user_registration_controller.ex:15 #: lib/lokal_web/controllers/user_registration_controller.ex:14
#: lib/lokal_web/controllers/user_registration_controller.ex:46 #: lib/lokal_web/controllers/user_registration_controller.ex:45
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Sorry, this invite was not found or expired" msgid "Sorry, this invite was not found or expired"
msgstr "" msgstr ""
@ -175,22 +175,22 @@ msgstr ""
msgid "You must confirm your account and log in to access this page." msgid "You must confirm your account and log in to access this page."
msgstr "" msgstr ""
#: lib/lokal/accounts/user.ex:130 #: lib/lokal/accounts/user.ex:137
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "did not change" msgid "did not change"
msgstr "" msgstr ""
#: lib/lokal/accounts/user.ex:151 #: lib/lokal/accounts/user.ex:158
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "does not match password" msgid "does not match password"
msgstr "" msgstr ""
#: lib/lokal/accounts/user.ex:188 #: lib/lokal/accounts/user.ex:195
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "is not valid" msgid "is not valid"
msgstr "" msgstr ""
#: lib/lokal/accounts/user.ex:84 #: lib/lokal/accounts/user.ex:92
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "must have the @ sign and no spaces" msgid "must have the @ sign and no spaces"
msgstr "" msgstr ""

View File

@ -60,7 +60,7 @@ msgstr ""
msgid "Password updated successfully." msgid "Password updated successfully."
msgstr "" msgstr ""
#: lib/lokal_web/controllers/user_registration_controller.ex:74 #: lib/lokal_web/controllers/user_registration_controller.ex:73
#, elixir-autogen, elixir-format #, elixir-autogen, elixir-format
msgid "Please check your email to verify your account" msgid "Please check your email to verify your account"
msgstr "" msgstr ""

View File

@ -10,6 +10,8 @@ defmodule Lokal.AccountsTest do
@moduletag :accounts_test @moduletag :accounts_test
doctest Accounts, import: true
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")
@ -104,7 +106,7 @@ defmodule Lokal.AccountsTest do
describe "change_user_registration/2" do describe "change_user_registration/2" do
test "returns a changeset" do test "returns a changeset" do
assert %Changeset{} = changeset = Accounts.change_user_registration(%User{}) assert %Changeset{} = changeset = Accounts.change_user_registration()
assert changeset.required == [:password, :email] assert changeset.required == [:password, :email]
end end
@ -112,8 +114,7 @@ defmodule Lokal.AccountsTest do
email = unique_user_email() email = unique_user_email()
password = valid_user_password() password = valid_user_password()
changeset = changeset = Accounts.change_user_registration(%{"email" => email, "password" => password})
Accounts.change_user_registration(%User{}, %{"email" => email, "password" => password})
assert changeset.valid? assert changeset.valid?
assert get_change(changeset, :email) == email assert get_change(changeset, :email) == email

View File

@ -3,7 +3,7 @@ defmodule Lokal.Fixtures do
This module defines test helpers for creating entities This module defines test helpers for creating entities
""" """
alias Lokal.{Accounts, Accounts.User, Email} alias Lokal.{Accounts, Accounts.User, Email, Repo}
def unique_user_email, do: "user#{System.unique_integer()}@example.com" def unique_user_email, do: "user#{System.unique_integer()}@example.com"
def valid_user_password, do: "hello world!" def valid_user_password, do: "hello world!"
@ -26,11 +26,12 @@ defmodule Lokal.Fixtures do
attrs attrs
|> Enum.into(%{ |> Enum.into(%{
"email" => unique_user_email(), "email" => unique_user_email(),
"password" => valid_user_password(), "password" => valid_user_password()
"role" => "admin"
}) })
|> Accounts.register_user() |> Accounts.register_user()
|> unwrap_ok_tuple() |> unwrap_ok_tuple()
|> User.role_changeset(:admin)
|> Repo.update!()
end end
def extract_user_token(fun) do def extract_user_token(fun) do