prop/test/support/fixtures/accounts_fixtures.ex
Graham McIntire 316fb2fbc7
Fix low-severity bugs and re-enable Credo checks
- Bug #12: Lower rate limit to 15/min
- Bug #13: Wrap model loading in Task.start
- Bug #14: Fix classify_time_period guard gap at -3.0
- Bug #15: Not applicable (Elixir has no ?? operator)
- Bug #16: Remove fallback Repo.get for station preload
- Bug #17: Extract cache_key() in ContactMapController
- Bug #18: Add has_many :contacts and :beacons to User schema
- A&D #4: Move serve_markdown_if_requested after secure headers
- Config #1: Move signing_salt to runtime.exs env var
- Config #2: Use --check-unused instead of --unused
- Config #3: Re-enable UnsafeToAtom Credo check
- Config #5: Re-enable LeakyEnvironment Credo check
- Add @spec annotations to fix re-enabled Specs violations
- Replace String.to_atom with to_existing_atom where guarded
2026-05-29 17:29:22 -05:00

95 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