Files
minstrel/internal/api/admin_covers_test.go
T
bvandeusenandClaude Opus 5.5 56285881aa
release / go (push) Successful in 1m58s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 22s
release / integration (push) Successful in 4m24s
release / android (push) Successful in 4m33s
release / Build signed APK (push) Successful in 5m4s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Publish the release image to GHCR (dispatch only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
fix(coverart): album art goes to Minstrel's data folder, never beside the music (#8286)
Another program files the library now. The cover enricher wrote a
cover.jpg into each album folder it found art for, from the files' tags or
from a provider, and an admin's cover retry deleted that file again, even
if the other program had since put its own cover at that path.

The enricher now keeps every cover it writes under
<data>/album-art/<album>/cover.jpg, and a retry removes a cover only from
there. A cover.jpg an older build left beside the music stays, and is read
as the library's own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 23:24:55 -05:00

134 lines
4.5 KiB
Go

package api
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/go-chi/chi/v5"
"github.com/Fabled-Sword/minstrel/internal/auth"
"github.com/Fabled-Sword/minstrel/internal/coverart"
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
)
// newAdminCoversRouter builds a test chi router with the two cover admin
// endpoints. RequireAdmin middleware is applied; tests inject the user into
// context manually (RequireUser is bypassed).
func newAdminCoversRouter(h *handlers) chi.Router {
r := chi.NewRouter()
r.Route("/api/admin", func(admin chi.Router) {
admin.Use(auth.RequireAdmin())
admin.Post("/albums/{id}/cover/refetch", h.handleAdminAlbumRefetchCover)
admin.Post("/covers/refetch-missing", h.handleAdminBulkRefetchCovers)
})
return r
}
// doAdminCoversReq fires an HTTP request against the admin covers router
// with the given user in context.
func doAdminCoversReq(t *testing.T, h *handlers, method, path string, user dbq.User) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(method, path, nil)
req = withUser(req, user)
w := httptest.NewRecorder()
newAdminCoversRouter(h).ServeHTTP(w, req)
return w
}
// testHandlersWithCovers returns a handlers instance with a stub coverart
// Enricher wired in. No external providers are registered, so the
// sidecar-only path is exercised and cover_art_source lands as 'none' for
// albums without real track files.
func testHandlersWithCovers(t *testing.T) (*handlers, *coverart.Enricher) {
t.Helper()
h, pool := testHandlers(t)
settings, err := coverart.NewSettingsService(context.Background(), pool, slog.Default())
if err != nil {
t.Fatalf("NewSettingsService: %v", err)
}
enricher := coverart.NewEnricher(pool, slog.Default(), settings, t.TempDir())
h.coverart = enricher
return h, enricher
}
func TestAdminAlbumRefetchCover_NonAdminReturns403(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, _ := testHandlersWithCovers(t)
user := seedUser(t, h.pool, "alice", "pw", false)
artist := seedArtist(t, h.pool, "TestArtist")
album := seedAlbum(t, h.pool, artist.ID, "TestAlbum", 0)
w := doAdminCoversReq(t, h, http.MethodPost,
"/api/admin/albums/"+uuidToString(album.ID)+"/cover/refetch", user)
if w.Code != http.StatusForbidden {
t.Errorf("status = %d, want 403; body=%s", w.Code, w.Body.String())
}
}
func TestAdminAlbumRefetchCover_AdminOK(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, _ := testHandlersWithCovers(t)
admin := seedUser(t, h.pool, "rootuser", "pw", true)
artist := seedArtist(t, h.pool, "TestArtist")
album := seedAlbum(t, h.pool, artist.ID, "TestAlbum", 0)
w := doAdminCoversReq(t, h, http.MethodPost,
"/api/admin/albums/"+uuidToString(album.ID)+"/cover/refetch", admin)
if w.Code != http.StatusOK {
t.Errorf("status = %d, want 200; body=%s", w.Code, w.Body.String())
}
var resp adminCoverRefetchResp
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
// Album has no MBID and no sidecar, so source should be 'none'.
if resp.CoverArtSource == nil || *resp.CoverArtSource != "none" {
t.Errorf("source = %v, want 'none'", resp.CoverArtSource)
}
}
func TestAdminBulkRefetchCovers_AdminStartsRefetch(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, _ := testHandlersWithCovers(t)
admin := seedUser(t, h.pool, "rootuser2", "pw", true)
artist := seedArtist(t, h.pool, "TestArtist")
_ = seedAlbum(t, h.pool, artist.ID, "Album1", 0)
_ = seedAlbum(t, h.pool, artist.ID, "Album2", 0)
w := doAdminCoversReq(t, h, http.MethodPost, "/api/admin/covers/refetch-missing", admin)
if w.Code != http.StatusOK {
t.Errorf("status = %d, want 200; body=%s", w.Code, w.Body.String())
}
var resp adminBulkRefetchResp
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if !resp.Started {
t.Errorf("started = false, want true (bulk refetch should acknowledge)")
}
}
func TestAdminBulkRefetchCovers_NonAdminReturns403(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, _ := testHandlersWithCovers(t)
user := seedUser(t, h.pool, "alicebulk", "pw", false)
w := doAdminCoversReq(t, h, http.MethodPost, "/api/admin/covers/refetch-missing", user)
if w.Code != http.StatusForbidden {
t.Errorf("status = %d, want 403; body=%s", w.Code, w.Body.String())
}
}