sonda/internal/storage/storage_test.go
tgiachi 89826fcd07 feat(federation): shared libraries and the signed read API for friends
A friend's server can now read what an admin chose to share.

- A library has a shared flag, off by default (migration 0024), set with
  PATCH /libraries/{id}. Changing it never waits for a scan.
- Requests between friends are signed with each server's Ed25519 key: the
  method, the path and query from /api/v1/peer on, a date, a nonce and the
  hash of the empty body. A date more than two minutes off, a nonce used
  before, an unknown or removed key and a bad signature all get the same
  401. A friend still waiting for approval gets 403, and while federation is
  off the routes are missing.
- Routes under /api/v1/peer: info, artists, albums, tracks, covers and
  streaming with Range. The catalogue, the counts and the format come from
  the shared libraries only, and the file served is the one in a shared
  library. No path, library id, listen, taste, user or provider data is part
  of an answer.
- A friend never makes this server build a cover, read an audio file or ask
  a provider: only covers already built from the audio files are served.
- Per-friend limits, set by the admin: parallel streams (default 3) and KiB
  a second (default 4096, 0 for none), shared by all the streams of a friend.
- A log of the last requests friends made (route, status, bytes; never the
  query), kept for 7 days, in GET /federation/requests.
- POST /federation/friends/{id}/check lets the side that pasted an invite
  find out that the other admin approved; a signed call from a friend this
  server is waiting for makes it active too.
- Admin: a Shared with friends switch on each library, which asks first, and
  limits, recent requests and a Check button in the Federation section, in
  English and Italian.
- OpenAPI paths and schemas, contract test, two real servers checked end to
  end with tagged audio files.
2026-11-13 15:04:07 +01:00

281 lines
7.9 KiB
Go

package storage
import (
"context"
"fmt"
"io/fs"
"net/url"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"testing/fstest"
"github.com/uptrace/bun"
"codeberg.org/squidd/sonda/migrations"
)
// postgresDSNEnv points the tests at a throwaway PostgreSQL server, e.g.
// postgres://postgres:sonda@127.0.0.1:55432/sonda?sslmode=disable
// Without it only the SQLite tests run, so the default suite needs no network.
const postgresDSNEnv = "SONDA_TEST_POSTGRES_DSN"
var schemaCounter atomic.Int64
// openBackend opens a fresh, empty database for driver. Postgres tests get
// their own schema, dropped when the test ends.
func openBackend(t *testing.T, driver string) *bun.DB {
t.Helper()
switch driver {
case DriverSQLite:
db, err := Open(DriverSQLite, filepath.Join(t.TempDir(), "test.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = db.Close() })
return db
case DriverPostgres:
admin, err := Open(DriverPostgres, os.Getenv(postgresDSNEnv))
if err != nil {
t.Fatal(err)
}
schema := fmt.Sprintf("t_%d_%d", os.Getpid(), schemaCounter.Add(1))
if _, err := admin.Exec("CREATE SCHEMA " + schema); err != nil {
t.Fatal(err)
}
u, err := url.Parse(os.Getenv(postgresDSNEnv))
if err != nil {
t.Fatal(err)
}
q := u.Query()
q.Set("search_path", schema)
u.RawQuery = q.Encode()
db, err := Open(DriverPostgres, u.String())
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = db.Close()
_, _ = admin.Exec("DROP SCHEMA " + schema + " CASCADE")
_ = admin.Close()
})
return db
}
t.Fatalf("unknown driver %q", driver)
return nil
}
// forEachBackend runs fn once per available backend with a migrated database.
func forEachBackend(t *testing.T, fn func(t *testing.T, db *bun.DB, driver string)) {
t.Helper()
drivers := []string{DriverSQLite}
if os.Getenv(postgresDSNEnv) != "" {
drivers = append(drivers, DriverPostgres)
}
for _, driver := range drivers {
t.Run(driver, func(t *testing.T) {
db := openBackend(t, driver)
fsys, err := migrations.For(driver)
if err != nil {
t.Fatal(err)
}
if err := Migrate(context.Background(), db, fsys); err != nil {
t.Fatal(err)
}
fn(t, db, driver)
})
}
}
func tableExists(t *testing.T, db *bun.DB, driver, table string) bool {
t.Helper()
var n int
var err error
if driver == DriverPostgres {
err = db.QueryRow(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = ?",
table).Scan(&n)
} else {
err = db.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?", table).Scan(&n)
}
if err != nil {
t.Fatal(err)
}
return n == 1
}
func TestOpenRejectsUnknownDriver(t *testing.T) {
if _, err := Open("mysql", "x"); err == nil {
t.Fatal("expected error for unknown driver")
}
}
func TestSQLiteOpenSetsPragmas(t *testing.T) {
db := openBackend(t, DriverSQLite)
var fk int
if err := db.QueryRow("PRAGMA foreign_keys").Scan(&fk); err != nil {
t.Fatal(err)
}
if fk != 1 {
t.Errorf("foreign_keys = %d, want 1", fk)
}
var mode string
if err := db.QueryRow("PRAGMA journal_mode").Scan(&mode); err != nil {
t.Fatal(err)
}
if mode != "wal" {
t.Errorf("journal_mode = %q, want wal", mode)
}
var busy int
if err := db.QueryRow("PRAGMA busy_timeout").Scan(&busy); err != nil {
t.Fatal(err)
}
if busy <= 0 {
t.Errorf("busy_timeout = %d, want > 0", busy)
}
}
func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, driver string) {
fsys, _ := migrations.For(driver)
if err := Migrate(context.Background(), db, fsys); err != nil {
t.Fatalf("second Migrate: %v", err)
}
for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries", "provider_settings", "artist_similar", "provider_fetches", "artist_tags", "track_genres", "user_settings", "artist_feedback", "images", "artist_descriptions", "federation_settings", "federation_invites", "federation_friends", "federation_requests"} {
if !tableExists(t, db, driver, table) {
t.Errorf("table %s missing", table)
}
}
var applied int
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil {
t.Fatal(err)
}
if applied != 24 {
t.Errorf("applied migrations = %d, want 24", applied)
}
})
}
func TestSchemaEnforcesForeignKeys(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
if _, err := db.Exec("INSERT INTO tracks (title, album_id) VALUES ('x', 999)"); err == nil {
t.Fatal("expected foreign key violation")
}
})
}
func TestSchemaAllowsUnknownMBIDsButRejectsDuplicates(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
// Several artists without an MBID must be allowed.
for _, name := range []string{"A", "B"} {
if _, err := db.Exec("INSERT INTO artists (name) VALUES (?)", name); err != nil {
t.Fatalf("insert %s: %v", name, err)
}
}
// The same MBID twice must not.
if _, err := db.Exec("INSERT INTO artists (name, mbid) VALUES ('C', 'm1')"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("INSERT INTO artists (name, mbid) VALUES ('D', 'm1')"); err == nil {
t.Fatal("expected unique violation on mbid")
}
})
}
func TestAudioFilePathIsUniquePerLibrary(t *testing.T) {
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
if _, err := db.Exec("INSERT INTO tracks (title) VALUES ('t')"); err != nil {
t.Fatal(err)
}
var a, b int64
for name, dst := range map[string]*int64{"a": &a, "b": &b} {
err := db.QueryRow("INSERT INTO libraries (name, path, created_at) VALUES (?, ?, 1) RETURNING id",
name, "/music/"+name).Scan(dst)
if err != nil {
t.Fatal(err)
}
}
insert := func(lib int64, rel string) error {
_, err := db.Exec(
`INSERT INTO audio_files (track_id, library_id, rel_path, format, size_bytes, mtime_unix)
VALUES (1, ?, ?, 'mp3', 1, 1)`, lib, rel)
return err
}
if err := insert(a, "x/y.mp3"); err != nil {
t.Fatal(err)
}
// Same relative path in another library is a different file.
if err := insert(b, "x/y.mp3"); err != nil {
t.Fatalf("same rel_path in another library should be allowed: %v", err)
}
// Same library and same relative path is a duplicate.
if err := insert(a, "x/y.mp3"); err == nil {
t.Fatal("expected unique violation on (library_id, rel_path)")
}
// A file must belong to a real library.
if err := insert(999, "z.mp3"); err == nil {
t.Fatal("expected foreign key violation for an unknown library")
}
})
}
func TestMigrateRollsBackFailedMigration(t *testing.T) {
for _, driver := range availableDrivers() {
t.Run(driver, func(t *testing.T) {
db := openBackend(t, driver)
fsys := fstest.MapFS{
"0001_ok.sql": {Data: []byte("CREATE TABLE a (id INTEGER);")},
"0002_bad.sql": {Data: []byte("CREATE TABLE b (id INTEGER); THIS IS NOT SQL;")},
}
if err := Migrate(context.Background(), db, fsys); err == nil {
t.Fatal("expected error from bad migration")
}
if tableExists(t, db, driver, "b") {
t.Error("table b exists: failed migration was not rolled back")
}
var n int
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&n); err != nil {
t.Fatal(err)
}
if n != 1 {
t.Errorf("applied = %d, want 1 (only 0001)", n)
}
})
}
}
func availableDrivers() []string {
drivers := []string{DriverSQLite}
if os.Getenv(postgresDSNEnv) != "" {
drivers = append(drivers, DriverPostgres)
}
return drivers
}
func TestMigrationFoldersMatch(t *testing.T) {
names := func(driver string) string {
fsys, err := migrations.For(driver)
if err != nil {
t.Fatal(err)
}
entries, err := fs.ReadDir(fsys, ".")
if err != nil {
t.Fatal(err)
}
var out []string
for _, e := range entries {
out = append(out, e.Name())
}
return strings.Join(out, ",")
}
if a, b := names(DriverSQLite), names(DriverPostgres); a != b {
t.Errorf("migration files differ: sqlite=%s postgres=%s", a, b)
}
}