release / web (push) Successful in 1m35s
release / go (push) Successful in 1m58s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m0s
release / android (push) Successful in 4m26s
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 / Build signed APK (push) Canceled after 5m8s
- coverart: the four providers' identity and default tests, Last.fm's five not-found-without-asking tests, the four "which artists are asked again" database tests (now one reset over four artists), and the thumbnail-only and fanart-only tests. - subsonic: getUser's six tests and decodePassword's three, one table each. Dropped TestTokenMD5Derivation: it checked crypto/md5 against a constant and called no Minstrel code. - server: the four web-app-versus-API path tests start one server, not four. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
191 lines
6.7 KiB
Go
191 lines
6.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/auth"
|
|
"github.com/Fabled-Sword/minstrel/internal/config"
|
|
"github.com/Fabled-Sword/minstrel/internal/db/dbq"
|
|
"github.com/Fabled-Sword/minstrel/internal/dbtest"
|
|
"github.com/Fabled-Sword/minstrel/internal/library"
|
|
"github.com/Fabled-Sword/minstrel/internal/subsonic"
|
|
)
|
|
|
|
func TestHealthz(t *testing.T) {
|
|
s := New(slog.New(slog.NewTextHandler(io.Discard, nil)), nil, nil, subsonic.Config{}, config.EventsConfig{}, config.RecommendationConfig{}, "", config.BrandingConfig{}, nil, nil, nil, library.RunScanConfig{})
|
|
ts := httptest.NewServer(s.Router())
|
|
defer ts.Close()
|
|
|
|
resp, err := http.Get(ts.URL + "/healthz")
|
|
if err != nil {
|
|
t.Fatalf("GET /healthz: %v", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Errorf("status = %d, want 200", resp.StatusCode)
|
|
}
|
|
var body map[string]string
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if body["status"] != "ok" {
|
|
t.Errorf("body = %v, want status=ok", body)
|
|
}
|
|
}
|
|
|
|
func TestHealthz_IncludesMinClientVersion(t *testing.T) {
|
|
t.Parallel()
|
|
s := &Server{}
|
|
r := chi.NewRouter()
|
|
r.Get("/healthz", s.handleHealthz)
|
|
ts := httptest.NewServer(r)
|
|
defer ts.Close()
|
|
|
|
resp, err := http.Get(ts.URL + "/healthz")
|
|
if err != nil {
|
|
t.Fatalf("GET /healthz: %v", err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
var body map[string]string
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if body["status"] != "ok" {
|
|
t.Errorf("status: got %q want \"ok\"", body["status"])
|
|
}
|
|
if body["min_client_version"] == "" {
|
|
t.Error("min_client_version is empty; clients can't enforce pairing")
|
|
}
|
|
}
|
|
|
|
// The web app is served at / and at any deep link the client router owns,
|
|
// but an unknown /api or /rest path is a 404 that is never HTML, so an API
|
|
// client never parses the web app as an answer.
|
|
func TestRouter_TheWebAppAndTheAPIsKeepTheirOwnPaths(t *testing.T) {
|
|
s := New(slog.New(slog.NewTextHandler(io.Discard, nil)), nil, nil, subsonic.Config{}, config.EventsConfig{}, config.RecommendationConfig{}, "", config.BrandingConfig{}, nil, nil, nil, library.RunScanConfig{})
|
|
ts := httptest.NewServer(s.Router())
|
|
defer ts.Close()
|
|
for _, tc := range []struct {
|
|
path string
|
|
wantCode int
|
|
wantHTML bool
|
|
}{
|
|
{"/", http.StatusOK, true},
|
|
{"/artists/00000000-0000-0000-0000-000000000001", http.StatusOK, true},
|
|
{"/api/does-not-exist", http.StatusNotFound, false},
|
|
{"/rest/does-not-exist", http.StatusNotFound, false},
|
|
} {
|
|
resp, err := http.Get(ts.URL + tc.path)
|
|
if err != nil {
|
|
t.Fatalf("GET %s: %v", tc.path, err)
|
|
}
|
|
_ = resp.Body.Close()
|
|
isHTML := strings.Contains(resp.Header.Get("Content-Type"), "text/html")
|
|
if resp.StatusCode != tc.wantCode || isHTML != tc.wantHTML {
|
|
t.Errorf("GET %s: status %d, HTML %v; want %d, HTML %v", tc.path, resp.StatusCode, isHTML, tc.wantCode, tc.wantHTML)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestRouter_AdminSubtreeNotShadowed guards the trap of a second
|
|
// r.Route("/api/admin", ...) in server.go: it creates a chi subtree at the
|
|
// same prefix as the nested admin Route inside api.Mount. chi.Walk shows both
|
|
// subtrees as registered, but runtime routing dispatch only matches one, so
|
|
// every admin endpoint registered by api.Mount (summary, quarantine, etc.)
|
|
// would silently 404 for authenticated callers.
|
|
//
|
|
// chi.Walk enumeration would NOT have caught this (both branches are in
|
|
// the tree). The only reliable check is to make a real authenticated
|
|
// request and confirm it reaches a handler — i.e. doesn't fall through
|
|
// to the JSON NotFound (404) that signals shadowing.
|
|
//
|
|
// The existing tests in this file pass nil for pool, which skips api.Mount
|
|
// entirely (gated behind `if s.Pool != nil`), so the conflict didn't manifest.
|
|
//
|
|
// Gated on MINSTREL_TEST_DATABASE_URL like other integration-leaning tests.
|
|
func TestRouter_AdminSubtreeNotShadowed(t *testing.T) {
|
|
pool := dbtest.Open(t)
|
|
|
|
// Seed a test admin user + session so the request actually reaches the
|
|
// inner route lookup (auth-middleware short-circuits don't catch shadowing).
|
|
ctx := context.Background()
|
|
q := dbq.New(pool)
|
|
_, _ = pool.Exec(ctx, "DELETE FROM sessions WHERE user_agent = 'shadowing-test'")
|
|
_, _ = pool.Exec(ctx, "DELETE FROM users WHERE username = 'test-shadowing-admin'")
|
|
user, err := q.CreateUser(ctx, dbq.CreateUserParams{
|
|
Username: "test-shadowing-admin",
|
|
PasswordHash: "x",
|
|
ApiTokenHash: "test-shadowing-token",
|
|
IsAdmin: true,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("CreateUser: %v", err)
|
|
}
|
|
t.Cleanup(func() { _, _ = pool.Exec(ctx, "DELETE FROM users WHERE id = $1", user.ID) })
|
|
token := "shadow-test-" + time.Now().Format("20060102150405")
|
|
tokenHash := auth.HashSessionToken(token)
|
|
_, err = pool.Exec(ctx,
|
|
"INSERT INTO sessions (user_id, token_hash, user_agent) VALUES ($1, $2, 'shadowing-test')",
|
|
user.ID, tokenHash[:],
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("insert session: %v", err)
|
|
}
|
|
|
|
s := New(slog.New(slog.NewTextHandler(io.Discard, nil)), pool,
|
|
stubScanner{}, subsonic.Config{}, config.EventsConfig{}, config.RecommendationConfig{}, "", config.BrandingConfig{}, nil, nil, nil, library.RunScanConfig{})
|
|
ts := httptest.NewServer(s.Router())
|
|
defer ts.Close()
|
|
|
|
// Each path is registered by a different package: admin/summary from
|
|
// api.Mount and admin/scan from server.Router. If chi runtime dispatch
|
|
// only routes one /api/admin subtree, one of these returns 404.
|
|
cases := []struct {
|
|
method string
|
|
path string
|
|
owner string
|
|
}{
|
|
{http.MethodGet, "/api/admin/summary", "api.Mount"},
|
|
// /api/admin/scan would actually trigger a real library scan via
|
|
// stubScanner; we don't include it. The admin/summary path is the
|
|
// canonical regression check.
|
|
}
|
|
for _, tc := range cases {
|
|
req, err := http.NewRequest(tc.method, ts.URL+tc.path, nil)
|
|
if err != nil {
|
|
t.Fatalf("build %s %s: %v", tc.method, tc.path, err)
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("%s %s: %v", tc.method, tc.path, err)
|
|
}
|
|
_ = resp.Body.Close()
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
t.Errorf("%s %s (owner=%s) returned 404 with valid admin auth — route is shadowed",
|
|
tc.method, tc.path, tc.owner)
|
|
}
|
|
}
|
|
}
|
|
|
|
// stubScanner is a no-op ScanTrigger used only to make Server.Router()
|
|
// register /api/admin/scan. Its Scan method must never be called by the
|
|
// route-presence assertions in this file.
|
|
type stubScanner struct{}
|
|
|
|
func (stubScanner) Scan(_ context.Context, _ func(library.Stats)) (library.Stats, error) {
|
|
panic("stubScanner.Scan called from server_test")
|
|
}
|