add accounts doctests

This commit is contained in:
shibao 2023-01-29 14:32:24 -05:00
parent 725df05521
commit 2cf705c46f
2 changed files with 143 additions and 64 deletions

View File

@ -16,8 +16,9 @@ defmodule Cannery.Accounts do
## Examples
iex> get_user_by_email("foo@example.com")
%User{}
iex> register_user(%{email: "foo@example.com", password: "valid_password"})
iex> with %User{} <- get_user_by_email("foo@example.com"), do: :passed
:passed
iex> get_user_by_email("unknown@example.com")
nil
@ -31,8 +32,9 @@ defmodule Cannery.Accounts do
## Examples
iex> get_user_by_email_and_password("foo@example.com", "correct_password")
%User{}
iex> register_user(%{email: "foo@example.com", password: "valid_password"})
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")
nil
@ -53,10 +55,11 @@ defmodule Cannery.Accounts do
## Examples
iex> get_user!(123)
%User{}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> get_user!(user.id)
user
iex> get_user!(456)
> get_user!()
** (Ecto.NoResultsError)
"""
@ -68,13 +71,15 @@ defmodule Cannery.Accounts do
## Examples
iex> list_users_by_role(%User{id: 123, role: :admin})
[admin: [%User{}], user: [%User{}, %User{}]]
iex> {:ok, user1} = register_user(%{email: "foo1@example.com", password: "valid_password"})
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
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
@doc """
@ -82,13 +87,13 @@ defmodule Cannery.Accounts do
## Examples
iex> list_users_by_role(%User{id: 123, role: :admin})
[%User{}]
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> with [^user] <- list_users_by_role(:admin), do: :passed
:passed
"""
@spec list_users_by_role(User.role()) :: [User.t()]
def list_users_by_role(role) do
role = role |> to_string()
@spec list_users_by_role(:admin) :: [User.t()]
def list_users_by_role(:admin = role) do
Repo.all(from u in User, where: u.role == ^role, order_by: u.email)
end
@ -99,11 +104,13 @@ defmodule Cannery.Accounts do
## Examples
iex> register_user(%{field: value})
{:ok, %User{}}
iex> with {:ok, %User{email: "foo@example.com"}} <-
...> register_user(%{email: "foo@example.com", password: "valid_password"}),
...> do: :passed
:passed
iex> register_user(%{field: bad_value})
{:error, %Changeset{}}
iex> with {:error, %Changeset{}} <- register_user(%{email: "foo@example"}), do: :passed
:passed
"""
@spec register_user(attrs :: map()) :: {:ok, User.t()} | {:error, User.changeset()}
@ -128,8 +135,11 @@ defmodule Cannery.Accounts do
## Examples
iex> change_user_registration(user)
%Changeset{data: %User{}}
iex> with %Changeset{} <- change_user_registration(), do: :passed
:passed
iex> with %Changeset{} <- change_user_registration(%{password: "hi"}), do: :passed
:passed
"""
@spec change_user_registration() :: User.changeset()
@ -144,8 +154,8 @@ defmodule Cannery.Accounts do
## Examples
iex> change_user_email(user)
%Changeset{data: %User{}}
iex> with %Changeset{} <- change_user_email(%User{email: "foo@example.com"}), do: :passed
:passed
"""
@spec change_user_email(User.t()) :: User.changeset()
@ -157,8 +167,8 @@ defmodule Cannery.Accounts do
## Examples
iex> change_user_role(user)
%Changeset{data: %User{}}
iex> with %Changeset{} <- change_user_role(%User{}, :user), do: :passed
:passed
"""
@spec change_user_role(User.t(), User.role()) :: User.changeset()
@ -170,11 +180,17 @@ defmodule Cannery.Accounts do
## Examples
iex> apply_user_email(user, "valid password", %{email: ...})
{:ok, %User{}}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
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: ...})
{:error, %Changeset{}}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> with {:error, %Changeset{}} <-
...> apply_user_email(user, "invalid password", %{email: "new_email@account"}),
...> do: :passed
:passed
"""
@spec apply_user_email(User.t(), email :: String.t(), attrs :: map()) ::
@ -219,8 +235,12 @@ defmodule Cannery.Accounts do
## Examples
iex> deliver_update_email_instructions(user, current_email, &Routes.user_update_email_url(conn, :edit, &1))
{:ok, %{to: ..., body: ...}}
iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
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(), current_email :: String.t(), function) ::
@ -237,8 +257,8 @@ defmodule Cannery.Accounts do
## Examples
iex> change_user_password(user)
%Changeset{data: %User{}}
iex> with %Changeset{} <- change_user_password(%User{}), do: :passed
:passed
"""
@spec change_user_password(User.t(), attrs :: map()) :: User.changeset()
@ -250,11 +270,20 @@ defmodule Cannery.Accounts do
## Examples
iex> update_user_password(user, "valid password", %{password: ...})
{:ok, %User{}}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
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: ...})
{:error, %Changeset{}}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> with {:error, %Changeset{}} <-
...> update_user_password(user, "invalid password", %{password: "123"}),
...> do: :passed
:passed
"""
@spec update_user_password(User.t(), String.t(), attrs :: map()) ::
@ -276,12 +305,12 @@ defmodule Cannery.Accounts do
end
@doc """
Returns an `%Changeset{}` for changing the user locale.
Returns an `Ecto.Changeset.t()` for changing the user locale.
## Examples
iex> change_user_locale(user)
%Changeset{data: %User{}}
iex> with %Changeset{} <- change_user_locale(%User{}), do: :passed
:passed
"""
@spec change_user_locale(User.t()) :: User.changeset()
@ -292,11 +321,9 @@ defmodule Cannery.Accounts do
## Examples
iex> update_user_locale(user, "valid locale")
{:ok, %User{}}
iex> update_user_password(user, "invalid locale")
{:error, %Changeset{}}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> with {:ok, %User{}} <- update_user_locale(user, "en_US"), do: :passed
:passed
"""
@spec update_user_locale(User.t(), locale :: String.t()) ::
@ -309,11 +336,13 @@ defmodule Cannery.Accounts do
## Examples
iex> delete_user!(user_to_delete, %User{id: 123, role: :admin})
%User{}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> with %User{} <- delete_user!(user, %User{id: 123, role: :admin}), do: :passed
:passed
iex> delete_user!(%User{id: 123}, %User{id: 123})
%User{}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> with %User{} <- delete_user!(user, user), do: :passed
:passed
"""
@spec delete_user!(user_to_delete :: User.t(), User.t()) :: User.t()
@ -355,19 +384,45 @@ defmodule Cannery.Accounts do
"""
@spec allow_registration?() :: boolean()
def allow_registration? do
Application.get_env(:cannery, CanneryWeb.Endpoint)[:registration] == "public" or
Application.get_env(:Cannery, CanneryWeb.Endpoint)[:registration] == "public" or
list_users_by_role(:admin) |> Enum.empty?()
end
@doc """
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()
def is_admin?(%User{id: user_id}) do
Repo.one(from u in User, where: u.id == ^user_id and u.role == :admin)
|> is_nil()
Repo.exists?(from u in User, where: u.id == ^user_id, where: u.role == :admin)
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
@doc """
@ -375,10 +430,16 @@ defmodule Cannery.Accounts do
## Examples
iex> deliver_user_confirmation_instructions(user, &Routes.user_confirmation_url(conn, :confirm, &1))
{:ok, %{to: ..., body: ...}}
iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
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}
"""
@ -425,8 +486,12 @@ defmodule Cannery.Accounts do
## Examples
iex> deliver_user_reset_password_instructions(user, &Routes.user_reset_password_url(conn, :edit, &1))
{:ok, %{to: ..., body: ...}}
iex> {:ok, %{id: user_id} = user} = register_user(%{email: "foo@example.com", password: "valid_password"})
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()
@ -442,8 +507,11 @@ defmodule Cannery.Accounts do
## Examples
iex> get_user_by_reset_password_token("validtoken")
%User{}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
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")
nil
@ -464,11 +532,20 @@ defmodule Cannery.Accounts do
## Examples
iex> reset_user_password(user, %{password: "new long password", password_confirmation: "new long password"})
{:ok, %User{}}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
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"})
{:error, %Changeset{}}
iex> {:ok, user} = register_user(%{email: "foo@example.com", password: "valid_password"})
iex> with {:error, %Changeset{}} <-
...> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"}),
...> do: :passed
:passed
"""
@spec reset_user_password(User.t(), attrs :: map()) ::

View File

@ -10,6 +10,8 @@ defmodule Cannery.AccountsTest do
@moduletag :accounts_test
doctest Accounts, import: true
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")