test(api): merge repeated cases into tables, drop skips dbtest already makes (#8295)
release / web (push) Successful in 1m34s
release / go (push) Successful in 2m0s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m24s
release / android (push) Successful in 4m46s
release / Build signed APK (push) Successful in 5m12s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped

- The like/unlike story runs as one sequence that checks the rows after each step.
- The invite-only sign-up cases are one test, which now also checks that a
  refused sign-up creates no account.
- Demoting an admin, and stopping at the last one, is one test.
- A wrong password and an unknown user are one test, which now also checks
  that the two refusals are identical.
- The cover-path resolver tests repeated the cover handler's tests branch
  for branch; the handler tests stay.
- The Range-from-start stream test only re-checked http.ServeContent.
- The APK's nil-user 401 is covered by the mounted-route walk.
- 68 copies of the MINSTREL_TEST_DATABASE_URL skip go: every one of those
  tests reaches dbtest.Pool, which already skips.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 10:35:48 -05:00
co-authored by Claude Opus 5.5
parent 81cc29595b
commit 3fd3c1f7c6
23 changed files with 145 additions and 580 deletions
+53 -97
View File
@@ -6,7 +6,6 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"sync"
"testing"
@@ -34,9 +33,6 @@ func resetInvites(t *testing.T, h *handlers) {
}
func TestRegister_FirstUserBecomesAdmin(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, _ := testHandlers(t)
resetUsers(t, h)
@@ -69,91 +65,7 @@ func TestRegister_FirstUserBecomesAdmin(t *testing.T) {
}
}
func TestRegister_InviteOnlyMode_RequiresToken(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
// Seed an admin user so we're past the empty-users state.
seedUser(t, pool, "existingadmin", "pw", true)
body := `{"username":"newuser","password":"abcd1234"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleRegister(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400 (invite_required)", rec.Code)
}
}
func TestRegister_InviteOnlyMode_ValidTokenSucceeds(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
resetInvites(t, h)
admin := seedUser(t, pool, "admin1", "pw", true)
// Insert a fresh invite directly.
token := "test-invite-abc123"
if _, err := pool.Exec(context.Background(),
`INSERT INTO user_invites (token, invited_by, expires_at)
VALUES ($1, $2, now() + interval '24 hours')`,
token, admin.ID,
); err != nil {
t.Fatalf("seed invite: %v", err)
}
body := `{"username":"invitee1","password":"abcd1234","invite_token":"` + token + `"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleRegister(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
// Invite redeemed?
var redeemedAt *string
if err := pool.QueryRow(context.Background(),
`SELECT redeemed_at::text FROM user_invites WHERE token = $1`, token,
).Scan(&redeemedAt); err != nil {
t.Fatalf("verify redeem: %v", err)
}
if redeemedAt == nil {
t.Errorf("invite not redeemed after successful registration")
}
// New user is_admin = false?
var isAdmin bool
if err := pool.QueryRow(context.Background(),
`SELECT is_admin FROM users WHERE username = 'invitee1'`).Scan(&isAdmin); err != nil {
t.Fatalf("verify user: %v", err)
}
if isAdmin {
t.Errorf("new invitee marked as admin; only first user should be admin")
}
}
func TestRegister_InviteOnlyMode_InvalidTokenRejects(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
seedUser(t, pool, "adm", "pw", true)
body := `{"username":"baduser","password":"abcd1234","invite_token":"nonexistent-token"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleRegister(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("status = %d, want 400 (invite_invalid)", rec.Code)
}
}
func TestRegister_DuplicateUsername_409(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
resetUsers(t, h)
resetInvites(t, h)
@@ -207,9 +119,6 @@ func TestRegister_FirstAdminRace(t *testing.T) {
// usernames are different, so users.username uniqueness doesn't
// arbitrate. Both will see "no users yet" and both insert as
// admin. Test asserts the at-least-one-admin invariant.
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, _ := testHandlers(t)
resetUsers(t, h)
@@ -243,9 +152,6 @@ func TestRegister_FirstAdminRace(t *testing.T) {
// With the gate on (as Mount sets it), the first account needs the setup
// token from the log; a missing or wrong one is refused and creates nothing.
func TestRegister_FirstUserNeedsSetupToken(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, _ := testHandlers(t)
resetUsers(t, h)
token, err := auth.NewSetupToken()
@@ -307,9 +213,6 @@ func TestSetupToken_MatchesOnlyItself(t *testing.T) {
// Sign-ups racing on one invite make exactly one account: the others are
// told the invite is used, and nothing of theirs is left behind.
func TestRegister_OneInviteMakesOneAccountUnderARace(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
resetInvites(t, h)
admin := seedUser(t, pool, "raceadmin", "pw", true)
@@ -360,3 +263,56 @@ func TestRegister_OneInviteMakesOneAccountUnderARace(t *testing.T) {
t.Fatalf("%d accounts exist after the race, want 1", made)
}
}
// Once an account exists, an invite is the only way in: a sign-up without
// one, or with a token that was never issued, is refused and creates no
// account. A valid invite makes an ordinary (non-admin) account and is
// marked redeemed.
func TestRegister_AfterTheFirstAccountAnInviteIsTheOnlyWayIn(t *testing.T) {
h, pool := testHandlers(t)
resetInvites(t, h)
admin := seedUser(t, pool, "admin1", "pw", true)
const token = "test-invite-abc123"
if _, err := pool.Exec(context.Background(),
`INSERT INTO user_invites (token, invited_by, expires_at)
VALUES ($1, $2, now() + interval '24 hours')`, token, admin.ID); err != nil {
t.Fatalf("seed invite: %v", err)
}
for _, tc := range []struct {
username, inviteToken string
status int
}{
{"noinvite", "", http.StatusBadRequest},
{"badinvite", "nonexistent-token", http.StatusBadRequest},
{"invitee", token, http.StatusOK},
} {
body := `{"username":"` + tc.username + `","password":"abcd1234","invite_token":"` + tc.inviteToken + `"}`
rec := httptest.NewRecorder()
h.handleRegister(rec, httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(body))))
if rec.Code != tc.status {
t.Errorf("%s: status %d, want %d; body=%s", tc.username, rec.Code, tc.status, rec.Body.String())
}
var accounts, admins int
if err := pool.QueryRow(context.Background(),
`SELECT count(*), count(*) FILTER (WHERE is_admin) FROM users WHERE username = $1`,
tc.username).Scan(&accounts, &admins); err != nil {
t.Fatalf("count accounts: %v", err)
}
wantAccounts := 0
if tc.status == http.StatusOK {
wantAccounts = 1
}
if accounts != wantAccounts || admins != 0 {
t.Errorf("%s: %d accounts (%d admin), want %d (0 admin)", tc.username, accounts, admins, wantAccounts)
}
}
var redeemed bool
if err := pool.QueryRow(context.Background(),
`SELECT redeemed_at IS NOT NULL FROM user_invites WHERE token = $1`, token).Scan(&redeemed); err != nil {
t.Fatalf("read invite: %v", err)
}
if !redeemed {
t.Errorf("the invite was not marked redeemed")
}
}