Files
minstrel/internal/api/admin_scan_test.go
T
bvandeusen 3d0e213081 feat(server/m7-381): admin scan-status + manual trigger endpoints
Adds GET /api/admin/scan/status and POST /api/admin/scan/run under the
existing RequireAdmin middleware block; plumbs *library.Scanner and
library.RunScanConfig through api.Mount, server.New, and main.go so the
manual trigger reuses the same RunScan orchestrator as startup scans.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-04 20:13:31 -04:00

103 lines
2.9 KiB
Go

package api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/go-chi/chi/v5"
"git.fabledsword.com/bvandeusen/minstrel/internal/auth"
)
func newAdminScanRouter(h *handlers) chi.Router {
r := chi.NewRouter()
r.Route("/api/admin", func(admin chi.Router) {
admin.Use(auth.RequireAdmin())
admin.Get("/scan/status", h.handleGetScanStatus)
admin.Post("/scan/run", h.handleTriggerScan)
})
return r
}
func TestAdminScanStatus_NoRowReturnsEmpty(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "rootscan", "pw", true)
req := httptest.NewRequest(http.MethodGet, "/api/admin/scan/status", nil)
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminScanRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
var resp scanStatusResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.ID != "" || resp.InFlight {
t.Errorf("expected empty payload; got %+v", resp)
}
}
func TestAdminScanStatus_RowReturnsValues(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "rootscan2", "pw", true)
if _, err := pool.Exec(context.Background(), `
INSERT INTO scan_runs (started_at, finished_at, library)
VALUES (now() - interval '1 minute', now(), '{"scanned":10,"added":2}'::jsonb)
`); err != nil {
t.Fatalf("seed scan_runs: %v", err)
}
req := httptest.NewRequest(http.MethodGet, "/api/admin/scan/status", nil)
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminScanRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d", rec.Code)
}
var resp scanStatusResp
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.InFlight {
t.Error("expected InFlight=false (finished_at set)")
}
if resp.FinishedAt == nil {
t.Error("expected FinishedAt to be non-nil")
}
if len(resp.Library) == 0 {
t.Error("expected Library jsonb to be non-empty")
}
}
func TestAdminTriggerScan_InFlightReturns409(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
admin := seedUser(t, pool, "rootscan3", "pw", true)
if _, err := pool.Exec(context.Background(), `
INSERT INTO scan_runs (started_at) VALUES (now())
`); err != nil {
t.Fatalf("seed in-flight scan: %v", err)
}
req := httptest.NewRequest(http.MethodPost, "/api/admin/scan/run", nil)
req = withUser(req, admin)
rec := httptest.NewRecorder()
newAdminScanRouter(h).ServeHTTP(rec, req)
if rec.Code != http.StatusConflict {
t.Fatalf("status = %d, want 409; body=%s", rec.Code, rec.Body.String())
}
}