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
+33
View File
@@ -1,8 +1,11 @@
package apierror
import (
"encoding/json"
"errors"
"fmt"
"net/http/httptest"
"reflect"
"testing"
)
@@ -171,3 +174,33 @@ func TestSentinels_Identity(t *testing.T) {
t.Fatalf("recovered error pointer = %p, want %p", apiErr, ErrNotFound)
}
}
// Write sends the status and a JSON body holding exactly the code and the
// message. A plain error goes out as a 500 whose message hides its text.
func TestWrite_SendsTheEnvelopeAndHidesAPlainErrorsText(t *testing.T) {
for _, tc := range []struct {
err error
status int
code, message string
}{
{fmt.Errorf("ctx: %w", NotFound("track")), 404, "track_not_found", "track not found"},
{errors.New("boom: secret detail"), 500, "server_error", "internal server error"},
} {
rec := httptest.NewRecorder()
Write(rec, tc.err)
if rec.Code != tc.status {
t.Errorf("%v: status = %d, want %d", tc.err, rec.Code, tc.status)
}
if ct := rec.Header().Get("Content-Type"); ct != "application/json" {
t.Errorf("%v: Content-Type = %q, want application/json", tc.err, ct)
}
var raw map[string]map[string]string
if err := json.Unmarshal(rec.Body.Bytes(), &raw); err != nil {
t.Fatalf("%v: body is not the envelope: %v (%s)", tc.err, err, rec.Body.String())
}
want := map[string]map[string]string{"error": {"code": tc.code, "message": tc.message}}
if !reflect.DeepEqual(raw, want) {
t.Errorf("%v: body = %v, want %v", tc.err, raw, want)
}
}
}