2022-07-25 19:31:54 -04:00
|
|
|
defmodule Memex.Fixtures do
|
2022-02-25 21:53:15 -05:00
|
|
|
@moduledoc """
|
|
|
|
This module defines test helpers for creating entities
|
|
|
|
"""
|
|
|
|
|
2022-07-25 19:31:54 -04:00
|
|
|
alias Memex.{Accounts, Accounts.User, Email}
|
2022-02-25 21:53:15 -05:00
|
|
|
|
|
|
|
def unique_user_email, do: "user#{System.unique_integer()}@example.com"
|
|
|
|
def valid_user_password, do: "hello world!"
|
|
|
|
|
2022-02-25 22:17:30 -05:00
|
|
|
@spec user_fixture() :: User.t()
|
|
|
|
@spec user_fixture(attrs :: map()) :: User.t()
|
2022-02-25 21:53:15 -05:00
|
|
|
def user_fixture(attrs \\ %{}) do
|
|
|
|
attrs
|
|
|
|
|> Enum.into(%{
|
|
|
|
"email" => unique_user_email(),
|
|
|
|
"password" => valid_user_password()
|
|
|
|
})
|
|
|
|
|> Accounts.register_user()
|
|
|
|
|> unwrap_ok_tuple()
|
|
|
|
end
|
|
|
|
|
2022-02-25 22:17:30 -05:00
|
|
|
@spec admin_fixture() :: User.t()
|
|
|
|
@spec admin_fixture(attrs :: map()) :: User.t()
|
2022-02-25 21:53:15 -05:00
|
|
|
def admin_fixture(attrs \\ %{}) do
|
|
|
|
attrs
|
|
|
|
|> Enum.into(%{
|
|
|
|
"email" => unique_user_email(),
|
|
|
|
"password" => valid_user_password(),
|
|
|
|
"role" => "admin"
|
|
|
|
})
|
|
|
|
|> Accounts.register_user()
|
|
|
|
|> unwrap_ok_tuple()
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_user_token(fun) do
|
|
|
|
%{args: %{attrs: attrs, email: email_key, user_id: user_id}} = fun.(&"[TOKEN]#{&1}[TOKEN]")
|
|
|
|
|
|
|
|
# convert atoms to string keys
|
|
|
|
attrs = attrs |> Map.new(fn {atom_key, value} -> {atom_key |> Atom.to_string(), value} end)
|
|
|
|
|
|
|
|
email =
|
|
|
|
email_key
|
|
|
|
|> Atom.to_string()
|
|
|
|
|> Email.generate_email(Accounts.get_user!(user_id), attrs)
|
|
|
|
|
|
|
|
[_, html_token | _] = email.html_body |> String.split("[TOKEN]")
|
|
|
|
[_, text_token | _] = email.text_body |> String.split("[TOKEN]")
|
|
|
|
^text_token = html_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_user_attributes(attrs \\ %{}) do
|
|
|
|
Enum.into(attrs, %{
|
|
|
|
email: unique_user_email(),
|
|
|
|
password: valid_user_password()
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp unwrap_ok_tuple({:ok, value}), do: value
|
|
|
|
end
|