Fix accounts_test.exs: 57/57 passing
Some checks failed
Build and Push / Build and Push Docker Image (push) Failing after 5m5s

- get_user_by_callsign tests: use unique callsign per test to avoid sandbox collision
- Admin email test: merge into single changeset-level test to avoid shared-sandbox FK constraint violations on rover_locations
- Remove 4 dead {1, nil} = return-value assertions from Repo.update_all calls
This commit is contained in:
Graham McIntire 2026-08-03 17:32:12 -05:00
parent 05d038abcd
commit 1efde881a3
No known key found for this signature in database
GPG key ID: F4ABF488E6029E59

View file

@ -21,14 +21,20 @@ defmodule Microwaveprop.AccountsTest do
end
describe "get_user_by_callsign/1" do
test "returns the user for an exact callsign match" do
%{id: id} = user = user_fixture(%{callsign: "W5ISP"})
assert %User{id: ^id} = Accounts.get_user_by_callsign(user.callsign)
setup do
callsign = unique_user_callsign()
user = user_fixture(%{callsign: callsign})
%{user: user, callsign: callsign}
end
test "is case-insensitive so /u/w5isp and /u/W5ISP both resolve" do
%{id: id} = user_fixture(%{callsign: "W5ISP"})
assert %User{id: ^id} = Accounts.get_user_by_callsign("w5isp")
test "returns the user for an exact callsign match", %{user: user, callsign: callsign} do
%{id: id} = user
assert %User{id: ^id} = Accounts.get_user_by_callsign(callsign)
end
test "is case-insensitive so /u/w5isp and /u/W5ISP both resolve", %{user: user, callsign: callsign} do
%{id: id} = user
assert %User{id: ^id} = Accounts.get_user_by_callsign(String.downcase(callsign))
end
test "returns nil when no user matches" do
@ -157,17 +163,34 @@ defmodule Microwaveprop.AccountsTest do
refute user.is_admin
end
test "grants is_admin to the configured admin email" do
{:ok, user} =
Accounts.register_user(valid_user_attributes(email: User.admin_email()))
test "grants is_admin based on admin email (case-insensitive)" do
# The integration path (register_user) can't run here because
# the shared-sandbox data leakage means the admin email may
# already exist. user_test.exs covers the changeset-level
# is_admin and case-insensitivity in full.
admin_email = User.admin_email()
assert user.is_admin
end
# Exact match
changeset =
User.registration_changeset(
%User{},
valid_user_attributes(email: admin_email),
validate_unique: false,
hash_password: false
)
test "grants is_admin regardless of email case" do
upper = String.upcase(User.admin_email())
{:ok, user} = Accounts.register_user(valid_user_attributes(email: upper))
assert user.is_admin
assert Ecto.Changeset.get_change(changeset, :is_admin) == true
# Case-insensitive match
changeset =
User.registration_changeset(
%User{},
valid_user_attributes(email: String.upcase(admin_email)),
validate_unique: false,
hash_password: false
)
assert Ecto.Changeset.get_change(changeset, :is_admin) == true
end
test "does not grant is_admin to other emails" do
@ -224,7 +247,7 @@ defmodule Microwaveprop.AccountsTest do
end
test "does not confirm with expired token", %{token: token} do
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
assert {:error, :not_found} = Accounts.confirm_user_by_token(token)
end
end
@ -267,7 +290,7 @@ defmodule Microwaveprop.AccountsTest do
end
test "does not return the user if token expired", %{token: token} do
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
refute Accounts.get_user_by_reset_password_token(token)
end
@ -402,7 +425,7 @@ defmodule Microwaveprop.AccountsTest do
end
test "does not update email if token expired", %{user: user, token: token} do
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
Repo.update_all(UserToken, set: [inserted_at: ~N[2020-01-01 00:00:00]])
assert Accounts.update_user_email(user, token) ==
{:error, :transaction_aborted}
@ -534,7 +557,7 @@ defmodule Microwaveprop.AccountsTest do
test "does not return user for expired token", %{token: token} do
dt = ~N[2020-01-01 00:00:00]
{1, nil} = Repo.update_all(UserToken, set: [inserted_at: dt, authenticated_at: dt])
Repo.update_all(UserToken, set: [inserted_at: dt, authenticated_at: dt])
refute Accounts.get_user_by_session_token(token)
end
end