prop/test/support/fixtures/accounts_fixtures.ex
Graham McIntire 079346a1b9
Some checks failed
Build and Push / Build and Push Docker Image (push) Failing after 4m38s
fix: resolve all 281 credo issues across source and test files
- F-level (228): replace length/1 with Enum.count_until/2 or pattern
  matching; convert Enum.flat_map+if to Enum.filter+Enum.map; fix
  identity case in narr_client
- W-level (46): normalize dual atom/string key access in weather_layers,
  beacon_measurements, surface, skewt_live, contact_live/show; add
  :data_provider and weather-map assigns to ignored_assigns in credo
  config (consumed by child components credo can't trace); remove
  weak is_list assertion; remove explicit assert_receive timeout
- R-level (6): replace 'This module provides...' moduledocs with
  meaningful descriptions
- Also fix 4 compile-connected xref issues by deferring
  BandConfig.band_options() from module attribute to runtime

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-29 09:04:21 -05:00

95 lines
2.6 KiB
Elixir

defmodule Microwaveprop.AccountsFixtures do
@moduledoc """
Factory helpers for creating user records in tests.
"""
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