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
+29 -74
View File
@@ -6,13 +6,13 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/go-chi/chi/v5"
"golang.org/x/crypto/bcrypt"
"github.com/Fabled-Sword/minstrel/internal/auth"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
)
func newAdminUsersRouter(h *handlers) chi.Router {
@@ -30,9 +30,6 @@ func newAdminUsersRouter(h *handlers) chi.Router {
}
func TestAdminUsers_List(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "listadmin", "pw", true)
@@ -56,9 +53,6 @@ func TestAdminUsers_List(t *testing.T) {
}
func TestAdminUsers_Promote(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "promadmin", "pw", true)
target := seedUser(t, pool, "promtarget", "pw", false)
@@ -83,56 +77,7 @@ func TestAdminUsers_Promote(t *testing.T) {
}
}
func TestAdminUsers_Demote_LastAdmin_Refused(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
// Reset to ensure exactly one admin exists.
if _, err := pool.Exec(context.Background(), "DELETE FROM users"); err != nil {
t.Fatalf("reset: %v", err)
}
soleAdmin := seedUser(t, pool, "soleadmin", "pw", true)
body := `{"is_admin": false}`
req := httptest.NewRequest(http.MethodPut, "/api/admin/users/"+uuidToString(soleAdmin.ID)+"/admin",
bytes.NewReader([]byte(body)))
req = withUser(req, soleAdmin)
rec := httptest.NewRecorder()
newAdminUsersRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusConflict {
t.Errorf("status = %d, want 409 (last_admin guard)", rec.Code)
}
}
func TestAdminUsers_Demote_OtherAdmin_Allowed(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
if _, err := pool.Exec(context.Background(), "DELETE FROM users"); err != nil {
t.Fatalf("reset: %v", err)
}
caller := seedUser(t, pool, "caller", "pw", true)
target := seedUser(t, pool, "targetadmin", "pw", true)
body := `{"is_admin": false}`
req := httptest.NewRequest(http.MethodPut, "/api/admin/users/"+uuidToString(target.ID)+"/admin",
bytes.NewReader([]byte(body)))
req = withUser(req, caller)
rec := httptest.NewRecorder()
newAdminUsersRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Errorf("status = %d, want 200 (two admins exist; demoting one is fine); body=%s", rec.Code, rec.Body.String())
}
}
func TestAdminCreateUser_Happy(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "creator", "pw", true)
@@ -155,9 +100,6 @@ func TestAdminCreateUser_Happy(t *testing.T) {
}
func TestAdminCreateUser_DuplicateUsername_409(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "duper", "pw", true)
seedUser(t, pool, "existing", "pw", false)
@@ -177,9 +119,6 @@ func TestAdminCreateUser_DuplicateUsername_409(t *testing.T) {
}
func TestAdminDeleteUser_Happy(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "delcaller", "pw", true)
target := seedUser(t, pool, "deltarget", "pw", false)
@@ -203,9 +142,6 @@ func TestAdminDeleteUser_Happy(t *testing.T) {
}
func TestAdminDeleteUser_LastAdmin_Refused(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
if _, err := pool.Exec(context.Background(), "DELETE FROM users"); err != nil {
t.Fatalf("reset: %v", err)
@@ -223,9 +159,6 @@ func TestAdminDeleteUser_LastAdmin_Refused(t *testing.T) {
}
func TestAdminResetPassword_Happy(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "rcaller", "pw", true)
target := seedUser(t, pool, "rtarget", "oldpw1234", false)
@@ -252,9 +185,6 @@ func TestAdminResetPassword_Happy(t *testing.T) {
}
func TestAdminResetPassword_TooShort_400(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "rsadm", "pw", true)
target := seedUser(t, pool, "rstgt", "pw", false)
@@ -272,9 +202,6 @@ func TestAdminResetPassword_TooShort_400(t *testing.T) {
}
func TestAdminResetPassword_SignsOutSubsonicApps(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "rsubadm", "pw", true)
target := seedUser(t, pool, "rsubtgt", "oldpw1234", false)
@@ -290,3 +217,31 @@ func TestAdminResetPassword_SignsOutSubsonicApps(t *testing.T) {
}
assertSubsonicSignedOut(t, h, target, before)
}
// An admin can take admin away from another admin, but never from the last
// one: the server always keeps someone who can manage it.
func TestAdminUsers_DemotingStopsAtTheLastAdmin(t *testing.T) {
h, pool := testHandlers(t)
if _, err := pool.Exec(context.Background(), "DELETE FROM users"); err != nil {
t.Fatalf("reset: %v", err)
}
caller := seedUser(t, pool, "caller", "pw", true)
other := seedUser(t, pool, "otheradmin", "pw", true)
for _, step := range []struct {
target dbq.User
status int
}{
{other, http.StatusOK}, // two admins: one can go
{caller, http.StatusConflict}, // the last admin stays
} {
req := withUser(httptest.NewRequest(http.MethodPut,
"/api/admin/users/"+uuidToString(step.target.ID)+"/admin",
bytes.NewReader([]byte(`{"is_admin": false}`))), caller)
rec := httptest.NewRecorder()
newAdminUsersRouter(h).ServeHTTP(rec, req)
if rec.Code != step.status {
t.Errorf("demote %s: status %d, want %d; body=%s", step.target.Username, rec.Code, step.status, rec.Body.String())
}
}
}