test(api): one walk over the mounted router checks access and id errors (#8295)
release / go (push) Successful in 2m3s
release / web (push) Successful in 1m29s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m29s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 3m28s
release / Build signed APK (push) Canceled after 3m3s

The access tests checked hand-built routers that applied RequireAdmin
themselves, so none could catch a production route mounted outside its
group. One test now walks every route Mount registers: each non-public
route answers 401 without a session, and each admin route 403 to a
listener. The per-endpoint malformed-id (400) and unknown-id (404) tests
become one table over the real routes. Profile updates, playlist kind
filters and empty searches each fold into one table; writeErr's envelope
tests move to apierror.Write, which they were testing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 10:29:59 -05:00
co-authored by Claude Opus 5.5
parent 877e1d2631
commit 81cc29595b
21 changed files with 329 additions and 967 deletions
-42
View File
@@ -2,7 +2,6 @@ package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
@@ -262,44 +261,3 @@ func TestListMine_OK(t *testing.T) {
}
}
}
// TestQuarantineEndpointsRequireAuth verifies the /api/quarantine endpoints
// return 401 when no user is in context (covers RequireUser failure mode
// uniformly across the three handlers).
func TestQuarantineEndpointsRequireAuth(t *testing.T) {
h, pool := testHandlers(t)
truncateLibrary(t, pool)
cases := []struct {
name string
method string
path string
body string
}{
{"flag", http.MethodPost, "/api/quarantine", `{"track_id":"00000000-0000-0000-0000-000000000001","reason":"bad_rip"}`},
{"unflag", http.MethodDelete, "/api/quarantine/00000000-0000-0000-0000-000000000001", ""},
{"mine", http.MethodGet, "/api/quarantine/mine", ""},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
var reqBody *bytes.Buffer
if tc.body != "" {
reqBody = bytes.NewBufferString(tc.body)
} else {
reqBody = bytes.NewBuffer(nil)
}
req := httptest.NewRequest(tc.method, tc.path, reqBody)
if tc.body != "" {
req.Header.Set("Content-Type", "application/json")
}
// No user injected into context.
req = req.WithContext(context.Background())
w := httptest.NewRecorder()
newQuarantineRouter(h).ServeHTTP(w, req)
if w.Code != http.StatusUnauthorized {
t.Errorf("%s: status = %d, want 401; body = %s", tc.name, w.Code, w.Body.String())
}
})
}
}