sonda/internal/storage/storage_test.go
tgiachi dbe9338af3 feat(auth): add refresh tokens, admin role and user management
- feat(auth): short-lived access tokens plus rotating refresh tokens stored as
  SHA-256 hashes; replaying a spent token revokes every session of that user
- feat(auth): is_admin flag; the first registered user is the admin and the
  last admin can be neither demoted nor deleted
- feat(api): POST /auth/refresh, /auth/logout, /auth/password and GET /auth/me
- feat(api): admin-only GET/POST /users, PATCH /users/{id}, PUT
  /users/{id}/password and DELETE /users/{id}; the admin check reads the role
  from the database on every call
- feat(api): starting a library scan is now admin-only
- feat(storage): users and refresh token repositories for SQLite and
  PostgreSQL (migration 0004; the oldest existing account becomes admin)
- feat(config): auth.access_ttl_minutes and auth.refresh_ttl_hours replace
  auth.token_ttl_hours
- test: rotation, reuse detection, concurrent rotation with a single winner,
  admin rules, role changes taking effect immediately
2026-11-11 12:38:42 +01:00

269 lines
7.3 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"} {
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 != 4 {
t.Errorf("applied migrations = %d, want 4", 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 TestAudioFilePathIsUniquePerRoot(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)
}
insert := func(root, rel string) error {
_, err := db.Exec(
`INSERT INTO audio_files (track_id, root, rel_path, format, size_bytes, mtime_unix)
VALUES (1, ?, ?, 'mp3', 1, 1)`, root, rel)
return err
}
if err := insert("/music/a", "x/y.mp3"); err != nil {
t.Fatal(err)
}
// Same relative path under another root is a different file.
if err := insert("/music/b", "x/y.mp3"); err != nil {
t.Fatalf("same rel_path in another root should be allowed: %v", err)
}
// Same root and same relative path is a duplicate.
if err := insert("/music/a", "x/y.mp3"); err == nil {
t.Fatal("expected unique violation on (root, rel_path)")
}
})
}
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)
}
}