Files
minstrel/internal/api/admin_cover_sources_test.go
T
bvandeusenandClaude Opus 5.5 e1f270362d
release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
chore(release): everything that ships points at GitHub and GHCR (#7897)
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every
  import and the Dockerfile's version ldflags.
- buildinfo.ProjectURL, the contact outside services see in Minstrel's
  User-Agent: https://github.com/Fabled-Sword/minstrel.
- README's compose example pulls ghcr.io/fabled-sword/minstrel:latest.
- docs/security.md sends problem reports to the GitHub issues.
- The demo-library stack builds Minstrel from the checkout, since :dev
  is not published to GHCR.
- hygiene.go's same-site example uses example.com names.

The workbench CI (.gitea/, ci/, export-ignored) still builds and
publishes :dev to the private registry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:28:16 -05:00

272 lines
9.2 KiB
Go

package api
import (
"bytes"
"context"
"encoding/json"
"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"
)
// apiTestAlbumProvider is a minimal AlbumCoverProvider for use in api
// package tests. Does not implement TestableProvider.
type apiTestAlbumProvider struct {
id string
display string
}
func (p *apiTestAlbumProvider) ID() string { return p.id }
func (p *apiTestAlbumProvider) DisplayName() string { return p.display }
func (p *apiTestAlbumProvider) RequiresAPIKey() bool { return false }
func (p *apiTestAlbumProvider) DefaultEnabled() bool { return true }
func (p *apiTestAlbumProvider) Configure(_ coverart.ProviderSettings) error { return nil }
func (p *apiTestAlbumProvider) FetchAlbumCover(_ context.Context, _ coverart.AlbumRef) ([]byte, error) {
return []byte("img"), nil
}
// apiTestTestableProvider is an AlbumCoverProvider that also implements
// TestableProvider (so the test connection button exercises the success path).
type apiTestTestableProvider struct {
apiTestAlbumProvider
}
func (p *apiTestTestableProvider) TestConnection(_ context.Context) error { return nil }
func newAdminCoverSourcesRouter(h *handlers) chi.Router {
r := chi.NewRouter()
r.Route("/api/admin", func(admin chi.Router) {
admin.Use(auth.RequireAdmin())
admin.Get("/cover-sources", h.handleListCoverSources)
admin.Patch("/cover-sources/{provider_id}", h.handleUpdateCoverSource)
admin.Post("/cover-sources/{provider_id}/test", h.handleTestCoverSource)
})
return r
}
// testHandlersWithCoverSettings extends testHandlers by installing a
// SettingsService backed by the test DB. Caller is responsible for
// registering whichever fake providers the test needs BEFORE calling
// this helper, via coverart.Register. Use coverart.ResetRegistryForTests
// with t.Cleanup to restore registry state after the test.
func testHandlersWithCoverSettings(t *testing.T) (*handlers, *coverart.SettingsService) {
t.Helper()
h, pool := testHandlers(t)
s, err := coverart.NewSettingsService(context.Background(), pool, h.logger)
if err != nil {
t.Fatalf("NewSettingsService: %v", err)
}
h.coverSettings = s
return h, s
}
func TestAdminListCoverSources_ReturnsRegisteredProviders(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
coverart.ResetRegistryForTests()
t.Cleanup(coverart.ResetRegistryForTests)
coverart.Register(&apiTestAlbumProvider{id: "test-album-prov", display: "Test Album Provider"})
h, _ := testHandlersWithCoverSettings(t)
admin := seedUser(t, h.pool, "cslist", "pw", true)
req := httptest.NewRequest(http.MethodGet, "/api/admin/cover-sources", nil)
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminCoverSourcesRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
var resp providerSourcesListResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if len(resp.Providers) != 1 {
t.Fatalf("providers len = %d, want 1", len(resp.Providers))
}
p := resp.Providers[0]
if p.ID != "test-album-prov" {
t.Errorf("ID = %q, want test-album-prov", p.ID)
}
if p.DisplayName != "Test Album Provider" {
t.Errorf("DisplayName = %q, want Test Album Provider", p.DisplayName)
}
if p.APIKeySet {
t.Error("api_key_set = true, want false (no key set)")
}
// apiTestAlbumProvider implements AlbumCoverProvider but not
// TestableProvider.
if p.Testable {
t.Error("testable = true, want false")
}
found := false
for _, s := range p.Supports {
if s == "album_cover" {
found = true
}
}
if !found {
t.Errorf("supports = %v, want to include album_cover", p.Supports)
}
}
func TestAdminUpdateCoverSource_FlippingEnabledBumpsVersion(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
coverart.ResetRegistryForTests()
t.Cleanup(coverart.ResetRegistryForTests)
coverart.Register(&apiTestAlbumProvider{id: "flip-prov", display: "Flip Provider"})
h, _ := testHandlersWithCoverSettings(t)
admin := seedUser(t, h.pool, "csflip", "pw", true)
disabled := false
body, _ := json.Marshal(updateProviderSourceReq{Enabled: &disabled})
req := httptest.NewRequest(http.MethodPatch, "/api/admin/cover-sources/flip-prov", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminCoverSourcesRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
var resp updateProviderSourceResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if !resp.VersionBumped {
t.Error("version_bumped = false, want true after flipping enabled")
}
}
func TestAdminUpdateCoverSource_ReplacingAKeyDoesNotBump(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
coverart.ResetRegistryForTests()
t.Cleanup(coverart.ResetRegistryForTests)
coverart.Register(&apiTestAlbumProvider{id: "key-prov", display: "Key Provider"})
h, _ := testHandlersWithCoverSettings(t)
admin := seedUser(t, h.pool, "cskey", "pw", true)
// The first key on an enabled source moves the version (it searched
// without one until now); replacing that key with another doesn't.
for i, step := range []struct {
key string
wantBumped bool
}{{"firstkey", true}, {"newkey", false}} {
body, _ := json.Marshal(updateProviderSourceReq{APIKey: &step.key})
req := httptest.NewRequest(http.MethodPatch, "/api/admin/cover-sources/key-prov", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminCoverSourcesRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("step %d: status = %d, want 200; body=%s", i, rec.Code, rec.Body.String())
}
var resp updateProviderSourceResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.VersionBumped != step.wantBumped {
t.Errorf("key %q: version_bumped = %v, want %v", step.key, resp.VersionBumped, step.wantBumped)
}
if !resp.APIKeySet {
t.Error("api_key_set = false, want true after setting key")
}
}
}
func TestAdminUpdateCoverSource_UnknownProvider404(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
coverart.ResetRegistryForTests()
t.Cleanup(coverart.ResetRegistryForTests)
// No providers registered — anything we patch will be not-found.
h, _ := testHandlersWithCoverSettings(t)
admin := seedUser(t, h.pool, "cs404", "pw", true)
body, _ := json.Marshal(updateProviderSourceReq{})
req := httptest.NewRequest(http.MethodPatch, "/api/admin/cover-sources/missing", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminCoverSourcesRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound {
t.Fatalf("status = %d, want 404; body=%s", rec.Code, rec.Body.String())
}
}
func TestAdminTestCoverSource_NonAdminReturns403(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
coverart.ResetRegistryForTests()
t.Cleanup(coverart.ResetRegistryForTests)
coverart.Register(&apiTestTestableProvider{apiTestAlbumProvider{id: "testable-prov", display: "Testable"}})
h, _ := testHandlersWithCoverSettings(t)
nonAdmin := seedUser(t, h.pool, "csnoadmin", "pw", false)
req := httptest.NewRequest(http.MethodPost, "/api/admin/cover-sources/testable-prov/test", nil)
req = withUser(req, nonAdmin)
rec := httptest.NewRecorder()
newAdminCoverSourcesRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusForbidden {
t.Fatalf("status = %d, want 403; body=%s", rec.Code, rec.Body.String())
}
}
func TestAdminTestCoverSource_NotTestableReturnsOkFalse(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
coverart.ResetRegistryForTests()
t.Cleanup(coverart.ResetRegistryForTests)
// apiTestAlbumProvider does NOT implement TestableProvider.
coverart.Register(&apiTestAlbumProvider{id: "nontestable-prov", display: "Non-Testable"})
h, _ := testHandlersWithCoverSettings(t)
admin := seedUser(t, h.pool, "csntest", "pw", true)
req := httptest.NewRequest(http.MethodPost, "/api/admin/cover-sources/nontestable-prov/test", nil)
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminCoverSourcesRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
var resp testProviderSourceResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.OK {
t.Error("ok = true, want false for non-testable provider")
}
if resp.Error == "" {
t.Error("error string empty, want message mentioning test connection")
}
}