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
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:
+39
-151
@@ -6,6 +6,8 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -64,29 +66,6 @@ func seedSimpleTrack(t *testing.T, pool *pgxpool.Pool, title, artist string) dbq
|
||||
return seedTrack(t, pool, al.ID, a.ID, title, 1, 1000)
|
||||
}
|
||||
|
||||
// TestPlaylists_NoSession401 verifies the handler-owned defensive 401
|
||||
// path on each route. Real traffic hits RequireUser upstream, but the
|
||||
// handler also defends against routing misconfigurations.
|
||||
func TestPlaylists_NoSession401(t *testing.T) {
|
||||
h, _ := testHandlers(t)
|
||||
r := newPlaylistsRouter(h)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/playlists", nil)
|
||||
w := httptest.NewRecorder()
|
||||
// Note: NO user in context.
|
||||
r.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want 401; body = %s", w.Code, w.Body.String())
|
||||
}
|
||||
var env apierror.Body
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &env); err != nil {
|
||||
t.Fatalf("decode: %v; body = %s", err, w.Body.String())
|
||||
}
|
||||
if env.Error.Code != "unauthenticated" {
|
||||
t.Errorf("error.code = %q, want unauthenticated", env.Error.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPlaylists_CreateThenGet exercises the happy-path end-to-end:
|
||||
// POST creates a row, the response carries the id, GET round-trips
|
||||
// the same fields.
|
||||
@@ -190,20 +169,6 @@ func TestPlaylists_PatchOwnerOnly(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPlaylists_GetNotFound verifies a well-formed UUID with no row
|
||||
// surfaces as 404 not_found.
|
||||
func TestPlaylists_GetNotFound(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
user := seedUser(t, pool, "alice", "pw", false)
|
||||
|
||||
missing := "00000000-0000-0000-0000-000000000099"
|
||||
w := doPlaylistsReq(h, user, http.MethodGet, "/api/playlists/"+missing, nil)
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want 404; body = %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// TestPlaylists_ListSplitsOwnedAndPublic verifies the response carries
|
||||
// owned vs. public buckets correctly. Alice owns one private + one
|
||||
// public; Bob sees only Alice's public.
|
||||
@@ -397,19 +362,6 @@ func TestPlaylists_DeleteOwnerOnly(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestPlaylists_BadUUID400 verifies a malformed playlist id returns
|
||||
// bad_request rather than reaching the service layer.
|
||||
func TestPlaylists_BadUUID400(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
user := seedUser(t, pool, "alice", "pw", false)
|
||||
|
||||
w := doPlaylistsReq(h, user, http.MethodGet, "/api/playlists/not-a-uuid", nil)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPlaylists_AppendInvalidTrackUUID400 verifies a malformed track id
|
||||
// in the append body returns bad_request before reaching the DB.
|
||||
func TestPlaylists_AppendInvalidTrackUUID400(t *testing.T) {
|
||||
@@ -456,53 +408,14 @@ func TestPlaylists_CoverNoneReturns404(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestListPlaylists_KindFilterDefaultsToUser verifies that an absent ?kind=
|
||||
// parameter defaults to "user" and excludes system-kind playlists.
|
||||
func TestListPlaylists_KindFilterDefaultsToUser(t *testing.T) {
|
||||
// The playlist list filters by kind: the person's own playlists by default,
|
||||
// the system mixes with kind=system, both with kind=all, and any other kind
|
||||
// is a 400.
|
||||
func TestListPlaylists_FiltersByKind(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
u := seedUser(t, pool, "klistdefault", "pw", false)
|
||||
|
||||
// Create one user-kind playlist via the service.
|
||||
userPL, err := h.playlists.Create(context.Background(), u.ID, "My PL", "", false)
|
||||
if err != nil {
|
||||
t.Fatalf("create user playlist: %v", err)
|
||||
}
|
||||
// Insert a system-kind playlist directly via SQL.
|
||||
if _, err := pool.Exec(context.Background(), `
|
||||
INSERT INTO playlists (user_id, name, description, is_public, kind, system_variant)
|
||||
VALUES ($1, 'For You', '', false, 'system', 'for_you')
|
||||
`, u.ID); err != nil {
|
||||
t.Fatalf("seed system playlist: %v", err)
|
||||
}
|
||||
|
||||
w := doPlaylistsReq(h, u, "GET", "/api/playlists", nil)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status: got %d, want 200; body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var resp listPlaylistsResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if len(resp.Owned) != 1 {
|
||||
t.Fatalf("default should return 1 user-kind playlist; got %d", len(resp.Owned))
|
||||
}
|
||||
if resp.Owned[0].Kind != "user" {
|
||||
t.Errorf("expected kind=user; got %q", resp.Owned[0].Kind)
|
||||
}
|
||||
if resp.Owned[0].ID != uuidToString(userPL.ID) {
|
||||
t.Errorf("returned wrong playlist id")
|
||||
}
|
||||
}
|
||||
|
||||
// TestListPlaylists_KindSystem verifies ?kind=system returns only
|
||||
// system-generated playlists and excludes user-created ones.
|
||||
func TestListPlaylists_KindSystem(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
u := seedUser(t, pool, "klistsys", "pw", false)
|
||||
|
||||
if _, err := h.playlists.Create(context.Background(), u.ID, "User PL", "", false); err != nil {
|
||||
u := seedUser(t, pool, "klist", "pw", false)
|
||||
if _, err := h.playlists.Create(context.Background(), u.ID, "My PL", "", false); err != nil {
|
||||
t.Fatalf("create user playlist: %v", err)
|
||||
}
|
||||
if _, err := pool.Exec(context.Background(), `
|
||||
@@ -512,62 +425,37 @@ func TestListPlaylists_KindSystem(t *testing.T) {
|
||||
t.Fatalf("seed system playlist: %v", err)
|
||||
}
|
||||
|
||||
w := doPlaylistsReq(h, u, "GET", "/api/playlists?kind=system", nil)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status: got %d, want 200; body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var resp listPlaylistsResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if len(resp.Owned) != 1 {
|
||||
t.Fatalf("kind=system should return 1 system playlist; got %d", len(resp.Owned))
|
||||
}
|
||||
if resp.Owned[0].Kind != "system" {
|
||||
t.Errorf("expected kind=system; got %q", resp.Owned[0].Kind)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListPlaylists_KindAll verifies ?kind=all returns all playlists
|
||||
// regardless of kind.
|
||||
func TestListPlaylists_KindAll(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
u := seedUser(t, pool, "klistall", "pw", false)
|
||||
|
||||
if _, err := h.playlists.Create(context.Background(), u.ID, "User PL", "", false); err != nil {
|
||||
t.Fatalf("create user playlist: %v", err)
|
||||
}
|
||||
if _, err := pool.Exec(context.Background(), `
|
||||
INSERT INTO playlists (user_id, name, description, is_public, kind, system_variant)
|
||||
VALUES ($1, 'For You', '', false, 'system', 'for_you')
|
||||
`, u.ID); err != nil {
|
||||
t.Fatalf("seed system playlist: %v", err)
|
||||
}
|
||||
|
||||
w := doPlaylistsReq(h, u, "GET", "/api/playlists?kind=all", nil)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status: got %d, want 200; body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
var resp listPlaylistsResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("decode: %v", err)
|
||||
}
|
||||
if len(resp.Owned) != 2 {
|
||||
t.Fatalf("kind=all should return both playlists; got %d", len(resp.Owned))
|
||||
}
|
||||
}
|
||||
|
||||
// TestListPlaylists_BadKindReturns400 verifies an unrecognised kind value
|
||||
// is rejected with 400 bad_kind.
|
||||
func TestListPlaylists_BadKindReturns400(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
u := seedUser(t, pool, "klistbad", "pw", false)
|
||||
_ = pool
|
||||
|
||||
w := doPlaylistsReq(h, u, "GET", "/api/playlists?kind=garbage", nil)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400; got %d, body=%s", w.Code, w.Body.String())
|
||||
for _, tc := range []struct {
|
||||
query string
|
||||
kinds []string // the kinds listed, in any order; nil means a 400
|
||||
}{
|
||||
{"", []string{"user"}},
|
||||
{"?kind=system", []string{"system"}},
|
||||
{"?kind=all", []string{"system", "user"}},
|
||||
{"?kind=garbage", nil},
|
||||
} {
|
||||
w := doPlaylistsReq(h, u, "GET", "/api/playlists"+tc.query, nil)
|
||||
if tc.kinds == nil {
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("%q: status %d, want 400", tc.query, w.Code)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("%q: status %d, want 200; body=%s", tc.query, w.Code, w.Body.String())
|
||||
}
|
||||
var resp listPlaylistsResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("%q: decode: %v", tc.query, err)
|
||||
}
|
||||
var got []string
|
||||
for _, p := range resp.Owned {
|
||||
got = append(got, p.Kind)
|
||||
}
|
||||
sort.Strings(got)
|
||||
if !reflect.DeepEqual(got, tc.kinds) {
|
||||
t.Errorf("%q listed kinds %v, want %v", tc.query, got, tc.kinds)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user