prop/test/microwaveprop/accounts/user_api_token_test.exs
Graham McIntire 581955bd69
Some checks failed
Build and Push / Build and Push Docker Image (push) Has been cancelled
fix: prevent contacts_dedup_idx collisions in parallel async tests
Create shared ContactsFixtures module with globally-unique qso_timestamp
seconds to prevent unique-constraint violations when async test modules
run in parallel and insert contacts with identical dedup-key columns.
The qso_timestamp column is timestamp(0), so microsecond offsets were
truncated — use System.unique_integer monotonic seconds instead.

Also make count-asserting tests resilient to sandbox-leaked contacts
from prior tests by using >= assertions or status-based checks rather
than exact counts.

Includes automated DateTime.add → DateTime.shift migration from
mix format.
2026-08-04 17:05:16 -05:00

71 lines
2.3 KiB
Elixir

defmodule Microwaveprop.Accounts.UserApiTokenTest do
use Microwaveprop.DataCase, async: true
alias Microwaveprop.Accounts.UserApiToken
alias Microwaveprop.AccountsFixtures
describe "token_prefix/0" do
test "is a stable namespaced prefix" do
assert UserApiToken.token_prefix() == "mwp_"
end
end
describe "build/2" do
setup do
%{user: AccountsFixtures.user_fixture()}
end
test "returns plaintext + valid changeset on good attrs", %{user: user} do
assert {:ok, {plaintext, changeset}} =
UserApiToken.build(user, %{name: "MyLaptop"})
assert String.starts_with?(plaintext, "mwp_")
assert changeset.valid?
assert Ecto.Changeset.get_change(changeset, :user_id) == user.id
assert Ecto.Changeset.get_change(changeset, :token_hash) == UserApiToken.hash_token(plaintext)
end
test "name is required", %{user: user} do
assert {:error, changeset} = UserApiToken.build(user, %{})
assert %{name: ["can't be blank"]} = errors_on(changeset)
refute changeset.valid?
assert changeset.action == :insert
end
test "name length is bounded", %{user: user} do
assert {:error, changeset} =
UserApiToken.build(user, %{name: String.duplicate("x", 200)})
assert %{name: ["should be at most 100 character(s)"]} = errors_on(changeset)
end
test "expires_at must be in the future", %{user: user} do
past = DateTime.shift(DateTime.utc_now(), minute: -1)
assert {:error, changeset} =
UserApiToken.build(user, %{name: "n", expires_at: past})
assert %{expires_at: ["must be in the future"]} = errors_on(changeset)
end
test "expires_at in the future is accepted", %{user: user} do
future = DateTime.shift(DateTime.utc_now(), hour: 1)
assert {:ok, {_plaintext, changeset}} =
UserApiToken.build(user, %{name: "n", expires_at: future})
assert changeset.valid?
end
end
describe "hash_token/1" do
test "produces a 32-byte sha256 hash" do
assert byte_size(UserApiToken.hash_token("abc")) == 32
end
test "is deterministic" do
assert UserApiToken.hash_token("x") == UserApiToken.hash_token("x")
assert UserApiToken.hash_token("x") != UserApiToken.hash_token("y")
end
end
end