2022-11-23 09:09:41 +13:00
|
|
|
defmodule DataCase do
|
2022-10-25 11:07:07 +13:00
|
|
|
@moduledoc """
|
|
|
|
This module defines the setup for tests requiring
|
|
|
|
access to the application's data layer.
|
|
|
|
|
|
|
|
You may define functions here to be used as helpers in
|
|
|
|
your tests.
|
|
|
|
|
|
|
|
Finally, if the test case interacts with the database,
|
|
|
|
we enable the SQL sandbox, so changes done to the database
|
|
|
|
are reverted at the end of every test. If you are using
|
|
|
|
PostgreSQL, you can even run database tests asynchronously
|
2022-11-23 09:09:41 +13:00
|
|
|
by setting `use DataCase, async: true`, although
|
2022-10-25 11:07:07 +13:00
|
|
|
this option is not recommended for other databases.
|
|
|
|
"""
|
|
|
|
|
|
|
|
use ExUnit.CaseTemplate
|
|
|
|
alias Ecto.Adapters.SQL.Sandbox
|
|
|
|
|
|
|
|
using do
|
|
|
|
quote do
|
|
|
|
alias Example.Repo
|
|
|
|
|
|
|
|
import Ecto
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
2022-11-23 09:09:41 +13:00
|
|
|
import DataCase
|
2022-10-25 11:07:07 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup tags do
|
2022-11-23 09:09:41 +13:00
|
|
|
DataCase.setup_sandbox(tags)
|
2022-10-25 11:07:07 +13:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Sets up the sandbox based on the test tags.
|
|
|
|
"""
|
2022-11-04 11:24:33 +13:00
|
|
|
@spec setup_sandbox(any) :: :ok
|
2022-10-25 11:07:07 +13:00
|
|
|
def setup_sandbox(tags) do
|
|
|
|
pid = Sandbox.start_owner!(Example.Repo, shared: not tags[:async])
|
|
|
|
on_exit(fn -> Sandbox.stop_owner(pid) end)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
A helper that transforms changeset errors into a map of messages.
|
|
|
|
|
|
|
|
assert {:error, changeset} = Accounts.create_user(%{password: "short"})
|
|
|
|
assert "password is too short" in errors_on(changeset).password
|
|
|
|
assert %{password: ["password is too short"]} = errors_on(changeset)
|
|
|
|
|
|
|
|
"""
|
2022-11-04 11:24:33 +13:00
|
|
|
@spec errors_on(Ecto.Changeset.t()) :: %{atom => [any]}
|
2022-10-25 11:07:07 +13:00
|
|
|
def errors_on(changeset) do
|
|
|
|
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
|
|
|
|
Regex.replace(~r"%{(\w+)}", message, fn _, key ->
|
|
|
|
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-11-04 11:24:33 +13:00
|
|
|
@doc "Generate a random username using Faker"
|
|
|
|
@spec username :: String.t()
|
2022-10-25 11:07:07 +13:00
|
|
|
def username, do: Faker.Internet.user_name()
|
2022-11-04 11:24:33 +13:00
|
|
|
|
|
|
|
@doc "Generate a random password using Faker"
|
|
|
|
@spec password :: String.t()
|
2022-10-25 11:07:07 +13:00
|
|
|
def password, do: Faker.Lorem.words(4) |> Enum.join(" ")
|
|
|
|
|
2022-11-04 11:24:33 +13:00
|
|
|
@doc "User factory"
|
2022-11-23 09:09:41 +13:00
|
|
|
@spec build_user(keyword) :: Example.User.t() | no_return
|
2022-10-31 16:43:00 +13:00
|
|
|
def build_user(attrs \\ []) do
|
2022-10-25 11:07:07 +13:00
|
|
|
password = password()
|
|
|
|
|
|
|
|
attrs =
|
|
|
|
attrs
|
2022-10-31 16:43:00 +13:00
|
|
|
|> Map.new()
|
2022-10-25 11:07:07 +13:00
|
|
|
|> Map.put_new(:username, username())
|
|
|
|
|> Map.put_new(:password, password)
|
|
|
|
|> Map.put_new(:password_confirmation, password)
|
|
|
|
|
2022-11-23 09:09:41 +13:00
|
|
|
user =
|
|
|
|
Example.User
|
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.for_create(:register_with_password, attrs)
|
|
|
|
|> Example.create!()
|
|
|
|
|
|
|
|
attrs
|
|
|
|
|> Enum.reduce(user, fn {field, value}, user ->
|
|
|
|
Ash.Resource.put_metadata(user, field, value)
|
|
|
|
end)
|
2022-10-25 11:07:07 +13:00
|
|
|
end
|
2023-01-11 15:12:53 +13:00
|
|
|
|
|
|
|
@doc "User with token required factory"
|
|
|
|
@spec build_user_with_token_required(keyword) :: Example.UserWithTokenRequired.t() | no_return
|
|
|
|
def build_user_with_token_required(attrs \\ []) do
|
|
|
|
password = password()
|
|
|
|
|
|
|
|
attrs =
|
|
|
|
attrs
|
|
|
|
|> Map.new()
|
|
|
|
|> Map.put_new(:email, Faker.Internet.email())
|
|
|
|
|> Map.put_new(:password, password)
|
|
|
|
|> Map.put_new(:password_confirmation, password)
|
|
|
|
|
|
|
|
user =
|
|
|
|
Example.UserWithTokenRequired
|
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.for_create(:register_with_password, attrs)
|
|
|
|
|> Example.create!()
|
|
|
|
|
|
|
|
attrs
|
|
|
|
|> Enum.reduce(user, fn {field, value}, user ->
|
|
|
|
Ash.Resource.put_metadata(user, field, value)
|
|
|
|
end)
|
|
|
|
end
|
2022-10-25 11:07:07 +13:00
|
|
|
end
|