- Add jump_credo_checks ~> 0.4 with all 20 checks enabled - Fix all standard Credo issues: 139 @spec (113 done, 26 remain), 4 refactoring, 3 alias usage, 9 System.cmd env, 5 unsafe_to_atom, 2 max line length, 9 assert_receive timeout - Fix 170+ jump_credo_checks warnings: - 117 TopLevelAliasImportRequire: move nested alias/import to module top - 32 UseObanProWorker: switch to Oban.Pro.Worker - 4 DoctestIExExamples: add doctests / create test file - ~20 WeakAssertion: strengthen type-check assertions - Various ConditionalAssertion, AssertReceiveTimeout fixes - Exclude vendor/ from Credo analysis - Remaining: 175 warnings (mostly opinionated WeakAssertion, AvoidSocketAssignsInTest), 26 @spec annotations
96 lines
2.6 KiB
Elixir
96 lines
2.6 KiB
Elixir
defmodule Microwaveprop.AccountsFixtures do
|
|
@moduledoc """
|
|
This module defines test helpers for creating
|
|
entities via the `Microwaveprop.Accounts` context.
|
|
"""
|
|
|
|
import Ecto.Query
|
|
|
|
alias Microwaveprop.Accounts
|
|
alias Microwaveprop.Accounts.Scope
|
|
alias Microwaveprop.Accounts.User
|
|
alias Microwaveprop.Repo
|
|
|
|
@spec unique_user_email :: String.t()
|
|
def unique_user_email, do: "user#{System.unique_integer([:positive])}@example.com"
|
|
|
|
@spec unique_user_callsign :: String.t()
|
|
def unique_user_callsign, do: "W#{[:positive] |> System.unique_integer() |> rem(1_000_000)}X"
|
|
|
|
@spec valid_user_password :: String.t()
|
|
def valid_user_password, do: "hello world!"
|
|
|
|
@spec valid_user_attributes(map()) :: map()
|
|
def valid_user_attributes(attrs \\ %{}) do
|
|
password = valid_user_password()
|
|
|
|
Enum.into(attrs, %{
|
|
callsign: unique_user_callsign(),
|
|
name: "Test User",
|
|
email: unique_user_email(),
|
|
password: password,
|
|
password_confirmation: password
|
|
})
|
|
end
|
|
|
|
@spec unconfirmed_user_fixture(map()) :: User.t()
|
|
def unconfirmed_user_fixture(attrs \\ %{}) do
|
|
{:ok, user} =
|
|
attrs
|
|
|> valid_user_attributes()
|
|
|> Accounts.register_user()
|
|
|
|
user
|
|
end
|
|
|
|
@spec user_fixture(map()) :: User.t()
|
|
def user_fixture(attrs \\ %{}) do
|
|
user = unconfirmed_user_fixture(attrs)
|
|
|
|
{:ok, user} =
|
|
user
|
|
|> User.confirm_changeset()
|
|
|> Repo.update()
|
|
|
|
user
|
|
end
|
|
|
|
@spec user_scope_fixture() :: Scope.t()
|
|
def user_scope_fixture do
|
|
user = user_fixture()
|
|
user_scope_fixture(user)
|
|
end
|
|
|
|
@spec user_scope_fixture(User.t()) :: Scope.t()
|
|
def user_scope_fixture(user) do
|
|
Scope.for_user(user)
|
|
end
|
|
|
|
@spec extract_user_token(fun :: function()) :: String.t()
|
|
def extract_user_token(fun) do
|
|
{:ok, captured_email} = fun.(&"[TOKEN]#{&1}[TOKEN]")
|
|
[_, token | _] = String.split(captured_email.text_body, "[TOKEN]")
|
|
token
|
|
end
|
|
|
|
@spec override_token_authenticated_at(String.t(), DateTime.t()) :: {non_neg_integer(), nil | [term()]}
|
|
def override_token_authenticated_at(token, authenticated_at) when is_binary(token) do
|
|
Repo.update_all(
|
|
from(t in Accounts.UserToken,
|
|
where: t.token == ^token
|
|
),
|
|
set: [authenticated_at: authenticated_at]
|
|
)
|
|
end
|
|
|
|
@spec offset_user_token(String.t(), integer(), :second | :minute | :hour | :day) ::
|
|
{non_neg_integer(), nil | [term()]}
|
|
def offset_user_token(token, amount_to_add, unit) do
|
|
dt = DateTime.add(DateTime.utc_now(:second), amount_to_add, unit)
|
|
|
|
Repo.update_all(
|
|
from(ut in Accounts.UserToken, where: ut.token == ^token),
|
|
set: [inserted_at: dt, authenticated_at: dt]
|
|
)
|
|
end
|
|
end
|