Each user can make playlists: a name (1 to 200 characters), an optional description and an ordered list of tracks, where a track may appear more than once (each place has its own item id). The API makes, lists, renames and deletes playlists, and adds tracks (at the end or at a position), lists them, moves one and removes one; positions count from 0 without gaps. A playlist is its owner's alone: somebody else's answers playlist_not_found (404), as one that does not exist. A track whose files were deleted or moved stays in the playlist and is listed with available false. Deleting a user deletes their playlists. Limits: 1,000 playlists per user, 10,000 tracks per playlist, 500 tracks per request. Changes of one playlist are serialised by locking its row, so two requests at once keep the positions unique on PostgreSQL too. Migration 0019, OpenAPI spec, contract test, README and the web API types follow. The web app comes separately. Closes #89
315 lines
11 KiB
Go
315 lines
11 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/uptrace/bun"
|
|
"go.uber.org/zap"
|
|
|
|
"codeberg.org/squidd/sonda/internal/about"
|
|
"codeberg.org/squidd/sonda/internal/auth"
|
|
"codeberg.org/squidd/sonda/internal/catalog"
|
|
"codeberg.org/squidd/sonda/internal/discover"
|
|
"codeberg.org/squidd/sonda/internal/discovery"
|
|
"codeberg.org/squidd/sonda/internal/feedback"
|
|
"codeberg.org/squidd/sonda/internal/images"
|
|
"codeberg.org/squidd/sonda/internal/jobs"
|
|
"codeberg.org/squidd/sonda/internal/library"
|
|
"codeberg.org/squidd/sonda/internal/listening"
|
|
"codeberg.org/squidd/sonda/internal/playlists"
|
|
"codeberg.org/squidd/sonda/internal/providers"
|
|
"codeberg.org/squidd/sonda/internal/storage"
|
|
"codeberg.org/squidd/sonda/internal/streaming"
|
|
"codeberg.org/squidd/sonda/internal/taste"
|
|
"codeberg.org/squidd/sonda/internal/usersettings"
|
|
"codeberg.org/squidd/sonda/migrations"
|
|
)
|
|
|
|
const secret = "0123456789abcdef0123456789abcdef"
|
|
|
|
func init() { gin.SetMode(gin.TestMode) }
|
|
|
|
// newTestAPI wires real services on a temporary SQLite database.
|
|
func newTestAPI(t *testing.T) http.Handler {
|
|
t.Helper()
|
|
return newTestAPIWithScan(t, &fakeScan{})
|
|
}
|
|
|
|
func newTestAPIWithScan(t *testing.T, scan scanService) http.Handler {
|
|
t.Helper()
|
|
return newTestAPIWith(t, func(d *Deps) { d.Scan = scan })
|
|
}
|
|
|
|
// newTestAPIWith wires real services and lets a test replace parts of Deps.
|
|
func newTestAPIWith(t *testing.T, mutate func(*Deps)) http.Handler {
|
|
t.Helper()
|
|
h, _ := newTestEnv(t, mutate)
|
|
return h
|
|
}
|
|
|
|
// newTestEnv is newTestAPIWith that also returns the database, for tests that
|
|
// need to look at or seed it.
|
|
func newTestEnv(t *testing.T, mutate func(*Deps)) (http.Handler, *bun.DB) {
|
|
t.Helper()
|
|
return newTestEnvDB(t, func(d *Deps, _ *bun.DB) { mutate(d) })
|
|
}
|
|
|
|
// newTestEnvDB lets the mutate function see the database as well, for tests
|
|
// that build services on top of it.
|
|
func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun.DB) {
|
|
t.Helper()
|
|
db, err := storage.Open(storage.DriverSQLite, filepath.Join(t.TempDir(), "api.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = db.Close() })
|
|
fsys, err := migrations.For(storage.DriverSQLite)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := storage.Migrate(context.Background(), db, fsys); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := db.DB.Exec("INSERT INTO tracks (title) VALUES ('song')"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
tokens := auth.NewTokenIssuer(secret, time.Hour)
|
|
prov := newProviders(db)
|
|
deps := Deps{
|
|
Logger: zap.NewNop(),
|
|
Auth: auth.NewService(storage.NewUserRepo(db), storage.NewRefreshRepo(db), tokens, 24*time.Hour),
|
|
Tokens: tokens,
|
|
Listening: listening.NewService(storage.NewListeningRepo(db), 20*time.Second),
|
|
Scan: &fakeScan{},
|
|
Libraries: library.NewLibraries(storage.NewLibraryRepo(db)),
|
|
Catalog: catalog.NewService(storage.NewCatalogRepo(db)),
|
|
Providers: prov,
|
|
Lidarr: &fakeLidarr{},
|
|
LidarrArtists: &fakeSender{},
|
|
Discovery: discovery.NewService(storage.NewDiscoveryRepo(db), &fakeLastfm{}, prov, zap.NewNop()),
|
|
Tags: discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{}, prov, zap.NewNop()),
|
|
Taste: taste.NewService(storage.NewTasteRepo(db), taste.Options{MinListen: 20 * time.Second, HalfLife: 90 * 24 * time.Hour}),
|
|
Settings: usersettings.NewService(storage.NewUserSettingsRepo(db)),
|
|
Streams: streaming.NewService(noFiles{}),
|
|
}
|
|
feedbackSvc := feedback.NewService(storage.NewFeedbackRepo(db))
|
|
deps.Feedback = feedbackSvc
|
|
deps.Playlists = playlists.NewService(storage.NewPlaylistRepo(db))
|
|
imageRepo := storage.NewImageRepo(db)
|
|
imageSvc := images.NewService(filepath.Join(t.TempDir(), "data"), imageRepo, imageRepo)
|
|
deps.Images, deps.ArtistImages = imageSvc, imageSvc
|
|
deps.CoversJob = jobs.Runner{Ctx: context.Background(), Job: images.NewCoversJob(imageSvc, imageRepo)}
|
|
deps.ArtistImagesJob = jobs.Runner{Ctx: context.Background(), Job: images.NewArtistImagesJob(imageSvc, imageRepo)}
|
|
mutate(&deps, db)
|
|
if deps.About == nil {
|
|
deps.About = about.NewService(storage.NewDiscoveryRepo(db), noWikipedia{}, prov, []string{"en"}, zap.NewNop())
|
|
}
|
|
if deps.DescriptionsJob == nil {
|
|
deps.DescriptionsJob = jobs.Runner{Ctx: context.Background(), Job: about.NewJob(about.NewService(storage.NewDiscoveryRepo(db), noWikipedia{}, prov, []string{"en"}, zap.NewNop()), storage.NewDiscoveryRepo(db))}
|
|
}
|
|
// Built from the final services, so a test that swaps one gets a Discover that uses it.
|
|
if deps.Discover == nil {
|
|
deps.Discover = discover.NewService(deps.Taste.(discover.Taster), deps.Discovery.(discover.Similar),
|
|
discovery.NewTagService(storage.NewDiscoveryRepo(db), map[string]discovery.TagSource{}, prov, zap.NewNop()),
|
|
storage.NewTasteRepo(db), usersettings.NewService(storage.NewUserSettingsRepo(db)), feedbackSvc, discover.DefaultWeights())
|
|
}
|
|
return NewRouter(deps), db
|
|
}
|
|
|
|
// noWikipedia is a Wikipedia that knows nothing, for tests that do not care about it.
|
|
type noWikipedia struct{}
|
|
|
|
func (noWikipedia) WikidataID(context.Context, string) (string, error) { return "", nil }
|
|
func (noWikipedia) Pages(context.Context, string, []string) (string, map[string]string, error) {
|
|
return "", nil, about.ErrSourceNotFound
|
|
}
|
|
func (noWikipedia) Summary(context.Context, string, string) (about.Summary, error) {
|
|
return about.Summary{}, about.ErrSourceNotFound
|
|
}
|
|
|
|
// newProviders builds the provider service the way main does, with the real way
|
|
// of removing provider data.
|
|
func newProviders(db *bun.DB) *providers.Service {
|
|
svc := providers.NewService(storage.NewProviderRepo(db))
|
|
svc.SetPurger(func(ctx context.Context, name string) (providers.Purged, error) {
|
|
return storage.NewDiscoveryRepo(db).Purge(ctx, name)
|
|
})
|
|
return svc
|
|
}
|
|
|
|
// noFiles is a streaming source with nothing to play.
|
|
type noFiles struct{}
|
|
|
|
func (noFiles) PlayableFile(context.Context, int64) (streaming.File, error) {
|
|
return streaming.File{}, streaming.ErrNotFound
|
|
}
|
|
|
|
type resp struct {
|
|
Code int
|
|
Body map[string]any
|
|
}
|
|
|
|
func do(t *testing.T, h http.Handler, method, path, token string, body any) resp {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
if body != nil {
|
|
if s, ok := body.(string); ok {
|
|
buf.WriteString(s)
|
|
} else if err := json.NewEncoder(&buf).Encode(body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
req := httptest.NewRequest(method, path, &buf)
|
|
req.Header.Set("Content-Type", "application/json")
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
|
|
out := resp{Code: rec.Code, Body: map[string]any{}}
|
|
_ = json.Unmarshal(rec.Body.Bytes(), &out.Body)
|
|
return out
|
|
}
|
|
|
|
func errCode(r resp) string {
|
|
e, _ := r.Body["error"].(map[string]any)
|
|
c, _ := e["code"].(string)
|
|
return c
|
|
}
|
|
|
|
// registerAndLogin creates the first user and returns a valid token.
|
|
func registerAndLogin(t *testing.T, h http.Handler) string {
|
|
t.Helper()
|
|
creds := map[string]string{"email": "alice@example.org", "password": "longenough"}
|
|
if r := do(t, h, "POST", "/api/v1/auth/register", "", creds); r.Code != http.StatusCreated {
|
|
t.Fatalf("register: %d %v", r.Code, r.Body)
|
|
}
|
|
r := do(t, h, "POST", "/api/v1/auth/login", "", creds)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("login: %d %v", r.Code, r.Body)
|
|
}
|
|
tok, _ := r.Body["access_token"].(string)
|
|
if tok == "" {
|
|
t.Fatal("no access_token in login response")
|
|
}
|
|
return tok
|
|
}
|
|
|
|
func TestHealth(t *testing.T) {
|
|
r := do(t, newTestAPI(t), "GET", "/api/v1/health", "", nil)
|
|
if r.Code != http.StatusOK || r.Body["status"] != "ok" {
|
|
t.Fatalf("health: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestRegisterThenClosed(t *testing.T) {
|
|
h := newTestAPI(t)
|
|
registerAndLogin(t, h)
|
|
|
|
r := do(t, h, "POST", "/api/v1/auth/register", "", map[string]string{"email": "bob@example.org", "password": "longenough"})
|
|
if r.Code != http.StatusForbidden || errCode(r) != "registration_closed" {
|
|
t.Fatalf("second register: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestRegisterInvalid(t *testing.T) {
|
|
h := newTestAPI(t)
|
|
for name, body := range map[string]any{
|
|
"short password": map[string]string{"email": "alice@example.org", "password": "x"},
|
|
"bad email": map[string]string{"email": "a", "password": "longenough"},
|
|
"malformed json": "{not json",
|
|
} {
|
|
r := do(t, h, "POST", "/api/v1/auth/register", "", body)
|
|
if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
|
|
t.Errorf("%s: %d %v", name, r.Code, r.Body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLoginWrongPassword(t *testing.T) {
|
|
h := newTestAPI(t)
|
|
registerAndLogin(t, h)
|
|
r := do(t, h, "POST", "/api/v1/auth/login", "", map[string]string{"email": "alice@example.org", "password": "nopenopenope"})
|
|
if r.Code != http.StatusUnauthorized || errCode(r) != "invalid_credentials" {
|
|
t.Fatalf("login: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestListenRequiresAuth(t *testing.T) {
|
|
h := newTestAPI(t)
|
|
body := map[string]any{"track_id": 1, "event_type": "play", "idempotency_key": "k"}
|
|
|
|
if r := do(t, h, "POST", "/api/v1/listens", "", body); r.Code != http.StatusUnauthorized {
|
|
t.Errorf("no token: %d", r.Code)
|
|
}
|
|
if r := do(t, h, "POST", "/api/v1/listens", "garbage", body); r.Code != http.StatusUnauthorized {
|
|
t.Errorf("bad token: %d", r.Code)
|
|
}
|
|
|
|
// A token signed with another secret is rejected.
|
|
other, _, _ := auth.NewTokenIssuer("ffffffffffffffffffffffffffffffff", time.Hour).Issue(auth.User{ID: 1, Email: "alice"})
|
|
if r := do(t, h, "POST", "/api/v1/listens", other, body); r.Code != http.StatusUnauthorized {
|
|
t.Errorf("foreign token: %d", r.Code)
|
|
}
|
|
}
|
|
|
|
func TestListenFlow(t *testing.T) {
|
|
h := newTestAPI(t)
|
|
tok := registerAndLogin(t, h)
|
|
body := map[string]any{
|
|
"track_id": 1, "event_type": "complete", "duration_played_ms": 1000,
|
|
"idempotency_key": "abc", "occurred_at": "2026-01-02T03:04:05Z",
|
|
}
|
|
|
|
r := do(t, h, "POST", "/api/v1/listens", tok, body)
|
|
if r.Code != http.StatusCreated || r.Body["status"] != "recorded" {
|
|
t.Fatalf("first: %d %v", r.Code, r.Body)
|
|
}
|
|
|
|
// Same idempotency key: accepted, not duplicated.
|
|
r = do(t, h, "POST", "/api/v1/listens", tok, body)
|
|
if r.Code != http.StatusOK || r.Body["status"] != "duplicate" {
|
|
t.Fatalf("retry: %d %v", r.Code, r.Body)
|
|
}
|
|
|
|
body["idempotency_key"] = "def"
|
|
body["track_id"] = 999
|
|
r = do(t, h, "POST", "/api/v1/listens", tok, body)
|
|
if r.Code != http.StatusNotFound || errCode(r) != "track_not_found" {
|
|
t.Fatalf("unknown track: %d %v", r.Code, r.Body)
|
|
}
|
|
|
|
body["track_id"] = 1
|
|
body["event_type"] = "dance"
|
|
r = do(t, h, "POST", "/api/v1/listens", tok, body)
|
|
if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
|
|
t.Fatalf("bad type: %d %v", r.Code, r.Body)
|
|
}
|
|
|
|
body["event_type"] = "play"
|
|
body["occurred_at"] = "yesterday"
|
|
r = do(t, h, "POST", "/api/v1/listens", tok, body)
|
|
if r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
|
|
t.Fatalf("bad time: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestBodyTooLarge(t *testing.T) {
|
|
h := newTestAPI(t)
|
|
huge := `{"email":"` + strings.Repeat("a", 200_000) + `","password":"longenough"}`
|
|
r := do(t, h, "POST", "/api/v1/auth/register", "", huge)
|
|
if r.Code != http.StatusBadRequest && r.Code != http.StatusRequestEntityTooLarge {
|
|
t.Fatalf("huge body: %d", r.Code)
|
|
}
|
|
}
|