Files
minstrel/internal/api/playlists_system_test.go
T
bvandeusen 53a02322fb fix: A2 (relax art-source CHECK) + D + E integration residual
A2 — migration 0030: the albums/artists art `*_source` CHECK was a
fixed provider-ID allowlist fighting the extensible coverart.Register
registry (per-provider migration churn at 0016/0018/0020; rejected
test stub providers → ~11 enricher tests failed). Relax to "NULL or
non-empty"; the registry is the source of truth. Same brittleness
class as the #433 discovery-mix CHECK.

D — lidarr Approve resolves metadata/quality profiles (JSON arrays)
before the add; the test stubs returned {"id":1} for every path, so
ListMetadataProfiles failed to unmarshal → 500/Approve errors. Make
the lidarrrequests + admin_requests stubs path-aware (arrays for
/metadataprofile, /qualityprofile).

E:
- auth: requireUser (prelude.go) emitted code "auth_required"; the
  canonical code is "unauthenticated" (operator decision). Change the
  code; drop the now-dead "auth_required" web error-copy key
  ("unauthenticated" already has copy).
- playlists_system_test wrapped the real auth.RequireUser middleware
  but only injected withUser() context → 401. Like every other api
  handler test, drop the middleware and rely on requireUser().
- admin_users dup-username test seeded "test-existing" (seedUser
  prefixes) but POSTed "existing" → no collision; POST the prefixed
  name.
- me_timezone test decoded a top-level {"code"} but the envelope is
  {"error":{"code"}}; decode the nested shape.
- audit test asserted compact JSON; Postgres jsonb::text is spaced.
- put-lidarr-config tests predated the missing_defaults gate (correct
  handler behavior); supply the required defaults in the bodies.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-17 20:22:30 -04:00

114 lines
3.6 KiB
Go

package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/go-chi/chi/v5"
)
// Generic registry-driven system-playlist endpoints (#411 R2).
// Replaces the former per-kind refresh handler tests.
func newSystemPlaylistRouter(h *handlers) chi.Router {
r := chi.NewRouter()
r.Route("/api", func(api chi.Router) {
// No real auth.RequireUser middleware: like every other api
// handler test, auth is supplied via withUser() context and the
// handlers self-guard with requireUser() (prelude.go). The real
// middleware needs a live session and would 401 the injected
// test user.
api.Post("/playlists/system/{kind}/refresh", h.handleSystemPlaylistRefresh)
api.Get("/playlists/system/{kind}/shuffle", h.handleSystemPlaylistShuffle)
})
return r
}
func TestSystemRefresh_ForYou_Returns200(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
user := seedUser(t, pool, "sysforyou", "pw", false)
req := httptest.NewRequest(http.MethodPost, "/api/playlists/system/for_you/refresh", nil)
req = withUser(req, user)
rec := httptest.NewRecorder()
newSystemPlaylistRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
var resp systemRefreshResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.TrackIDs == nil {
t.Errorf("track_ids = nil, want [] (empty array, not null)")
}
if int(resp.TrackCount) != len(resp.TrackIDs) {
t.Errorf("track_count=%d, len(track_ids)=%d — mismatch", resp.TrackCount, len(resp.TrackIDs))
}
}
func TestSystemRefresh_Discover_Returns200(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
user := seedUser(t, pool, "sysdiscover", "pw", false)
req := httptest.NewRequest(http.MethodPost, "/api/playlists/system/discover/refresh", nil)
req = withUser(req, user)
rec := httptest.NewRecorder()
newSystemPlaylistRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
var resp systemRefreshResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.TrackCount < 0 {
t.Errorf("track_count = %d, want >= 0", resp.TrackCount)
}
}
// A non-singleton (songs_like_artist) or unknown kind isn't
// addressable by the generic endpoints → 404, not a build.
func TestSystemRefresh_NonSingletonKind_Returns404(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
user := seedUser(t, pool, "sysbadkind", "pw", false)
for _, kind := range []string{"songs_like_artist", "bogus"} {
req := httptest.NewRequest(http.MethodPost, "/api/playlists/system/"+kind+"/refresh", nil)
req = withUser(req, user)
rec := httptest.NewRecorder()
newSystemPlaylistRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Errorf("kind=%s: status = %d, want 404", kind, rec.Code)
}
}
}
func TestSystemRefresh_NoAuth_Returns401(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, _ := testHandlers(t)
req := httptest.NewRequest(http.MethodPost, "/api/playlists/system/for_you/refresh", nil)
rec := httptest.NewRecorder()
newSystemPlaylistRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusUnauthorized {
t.Errorf("status = %d, want 401", rec.Code)
}
}