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:
@@ -3,21 +3,14 @@ package api
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/Fabled-Sword/minstrel/internal/auth"
|
||||
"github.com/Fabled-Sword/minstrel/internal/backup"
|
||||
"github.com/Fabled-Sword/minstrel/internal/config"
|
||||
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
||||
"github.com/Fabled-Sword/minstrel/internal/eventbus"
|
||||
"github.com/Fabled-Sword/minstrel/internal/playevents"
|
||||
)
|
||||
|
||||
// newLibraryRouter builds a test-only chi router with the library handlers
|
||||
@@ -87,28 +80,6 @@ func TestHandleGetTrack_HappyPath(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetTrack_BadUUID(t *testing.T) {
|
||||
h, _ := testHandlers(t)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/tracks/not-a-uuid", nil)
|
||||
w := httptest.NewRecorder()
|
||||
newLibraryRouter(h).ServeHTTP(w, req)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetTrack_NotFound(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
req := httptest.NewRequest(http.MethodGet,
|
||||
"/api/tracks/00000000-0000-0000-0000-000000000001", nil)
|
||||
w := httptest.NewRecorder()
|
||||
newLibraryRouter(h).ServeHTTP(w, req)
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want 404", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetAlbum_HappyPath(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
@@ -206,28 +177,6 @@ func TestHandleGetAlbum_NoTracks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetAlbum_BadUUID(t *testing.T) {
|
||||
h, _ := testHandlers(t)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/albums/not-a-uuid", nil)
|
||||
w := httptest.NewRecorder()
|
||||
newLibraryRouter(h).ServeHTTP(w, req)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetAlbum_NotFound(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
req := httptest.NewRequest(http.MethodGet,
|
||||
"/api/albums/00000000-0000-0000-0000-000000000001", nil)
|
||||
w := httptest.NewRecorder()
|
||||
newLibraryRouter(h).ServeHTTP(w, req)
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want 404", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetArtist_HappyPath(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
@@ -297,28 +246,6 @@ func TestHandleGetArtist_NoAlbums(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetArtist_BadUUID(t *testing.T) {
|
||||
h, _ := testHandlers(t)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/artists/not-a-uuid", nil)
|
||||
w := httptest.NewRecorder()
|
||||
newLibraryRouter(h).ServeHTTP(w, req)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleGetArtist_NotFound(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
req := httptest.NewRequest(http.MethodGet,
|
||||
"/api/artists/00000000-0000-0000-0000-000000000001", nil)
|
||||
w := httptest.NewRecorder()
|
||||
newLibraryRouter(h).ServeHTTP(w, req)
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want 404", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleListArtists_DefaultAlpha(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
@@ -420,35 +347,6 @@ func TestHandleListArtists_Pagination(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleListArtists_LimitClamped(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
seedArtist(t, pool, "One")
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/artists?limit=9999", nil)
|
||||
w := httptest.NewRecorder()
|
||||
newLibraryRouter(h).ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", w.Code)
|
||||
}
|
||||
var got Page[ArtistRef]
|
||||
_ = json.Unmarshal(w.Body.Bytes(), &got)
|
||||
if got.Limit != 200 {
|
||||
t.Errorf("limit = %d, want 200 (max clamp)", got.Limit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleListArtists_LimitNonNumeric(t *testing.T) {
|
||||
h, _ := testHandlers(t)
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/artists?limit=abc", nil)
|
||||
w := httptest.NewRecorder()
|
||||
newLibraryRouter(h).ServeHTTP(w, req)
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("status = %d, want 400", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleListArtists_AlbumCount(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
truncateLibrary(t, pool)
|
||||
@@ -488,51 +386,6 @@ func TestHandleListArtists_EmptyDB(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestRoutesRegisteredInMount verifies the production Mount() wires every
|
||||
// library route. We hit each path through the real Mount (no RequireUser
|
||||
// stripped — we expect 401 from the middleware, which is fine — that
|
||||
// proves the route reached the authenticated group).
|
||||
func TestRoutesRegisteredInMount(t *testing.T) {
|
||||
h, _ := testHandlers(t)
|
||||
r := chi.NewRouter()
|
||||
w := playevents.NewWriter(h.pool, slog.New(slog.NewTextHandler(io.Discard, nil)),
|
||||
30*time.Minute, 0.5, 30000)
|
||||
Mount(r, h.pool, h.logger, w, config.RecommendationConfig{RadioSize: 50, RadioSizeMax: 200, RecentlyPlayedHours: 1}, h.recSettings, h.quarantine, h.tracks, h.playlists, h.coverart, h.coverSettings, h.tagSettings, h.scanner, h.scanCfg, h.dataDir, nil, eventbus.New(), nil, nil, h.netSettings, nil, nil, nil, nil, backup.NewService(h.pool, h.logger, "test"), nil)
|
||||
|
||||
paths := []string{
|
||||
"/api/artists",
|
||||
"/api/artists/00000000-0000-0000-0000-000000000001",
|
||||
"/api/albums/00000000-0000-0000-0000-000000000001",
|
||||
"/api/albums/00000000-0000-0000-0000-000000000001/cover",
|
||||
"/api/tracks/00000000-0000-0000-0000-000000000001",
|
||||
"/api/tracks/00000000-0000-0000-0000-000000000001/stream",
|
||||
"/api/search?q=x",
|
||||
// Browse indexes.
|
||||
"/api/library/genres",
|
||||
"/api/library/years",
|
||||
// Admin surfaces for the missing-file lifecycle. Both must
|
||||
// 401 at the middleware rather than 404 — they live inside the
|
||||
// admin group, so reaching the auth check is what proves they are
|
||||
// wired.
|
||||
"/api/admin/library/missing",
|
||||
"/api/admin/library/fingerprints",
|
||||
"/api/admin/library/fingerprint-settings",
|
||||
"/api/admin/library/duplicates",
|
||||
}
|
||||
for _, p := range paths {
|
||||
req := httptest.NewRequest(http.MethodGet, p, nil)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
// Unauthenticated → 401. What we must NOT see is 404 (route missing).
|
||||
if w.Code == http.StatusNotFound {
|
||||
t.Errorf("path %s returned 404 — route not registered", p)
|
||||
}
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("path %s status = %d, want 401 (unauth)", p, w.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetArtistTracks_HappyPath(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
user := seedUser(t, pool, "alice", "pw", false)
|
||||
@@ -614,21 +467,3 @@ func TestGetArtistTracks_HonorsQuarantine(t *testing.T) {
|
||||
t.Errorf("len = %d, want 0 (quarantined)", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetArtistTracks_NotFound(t *testing.T) {
|
||||
h, pool := testHandlers(t)
|
||||
user := seedUser(t, pool, "alice", "pw", false)
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Get("/api/artists/{id}/tracks", h.handleGetArtistTracks)
|
||||
// Random UUID — no such artist exists.
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/artists/00000000-0000-0000-0000-000000000000/tracks", nil)
|
||||
req = withUser(req, user)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("status = %d, want 404", w.Code)
|
||||
}
|
||||
_ = pool
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user