937a1930ad
Integration tests previously TRUNCATEd the users table at setup, which wiped the operator's admin login every time the suite ran against a Postgres instance shared with their dev environment. This commit: - Adds internal/dbtest/reset.go exposing ResetDB(t, pool) and TestUserPrefix. ResetDB truncates every data table EXCEPT users, then deletes only users whose username starts with TestUserPrefix. - Migrates 9 test files (subsonic/scrobble, subsonic/star, recommendation/candidates, scrobble/queue, similarity/worker, playevents/writer, playsessions/service, api/auth, api/me) to call ResetDB instead of issuing TRUNCATE-with-users. - Renames hardcoded test usernames to use TestUserPrefix so ResetDB cleans them between runs. seedUser in api/auth_test now prefixes internally; existing call sites pass bare names. - Leaves auth/bootstrap_test.go alone — it legitimately tests first-time admin bootstrap and must wipe users. Verified locally: full integration suite with -p 1 runs cleanly under the existing MINSTREL_TEST_DATABASE_URL setup, and the admin row in the shared dev DB survives the run. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
118 lines
3.3 KiB
Go
118 lines
3.3 KiB
Go
package subsonic
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
|
|
"git.fabledsword.com/bvandeusen/minstrel/internal/playevents"
|
|
)
|
|
|
|
func testScrobblePool(t *testing.T) (*pgxpool.Pool, dbq.User, dbq.Track) {
|
|
t.Helper()
|
|
if testing.Short() {
|
|
t.Skip("skipping in -short mode")
|
|
}
|
|
dsn := os.Getenv("MINSTREL_TEST_DATABASE_URL")
|
|
if dsn == "" {
|
|
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
|
|
}
|
|
if err := db.Migrate(dsn, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
pool, err := pgxpool.New(context.Background(), dsn)
|
|
if err != nil {
|
|
t.Fatalf("pool: %v", err)
|
|
}
|
|
t.Cleanup(pool.Close)
|
|
dbtest.ResetDB(t, pool)
|
|
q := dbq.New(pool)
|
|
u, err := q.CreateUser(context.Background(), dbq.CreateUserParams{
|
|
Username: dbtest.TestUserPrefix + "tester", PasswordHash: "x", ApiToken: "x", IsAdmin: false,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("user: %v", err)
|
|
}
|
|
a, _ := q.UpsertArtist(context.Background(), dbq.UpsertArtistParams{Name: "X", SortName: "X"})
|
|
al, _ := q.UpsertAlbum(context.Background(), dbq.UpsertAlbumParams{Title: "X", SortTitle: "X", ArtistID: a.ID})
|
|
tr, _ := q.UpsertTrack(context.Background(), dbq.UpsertTrackParams{
|
|
Title: "Y", AlbumID: al.ID, ArtistID: a.ID, FilePath: "/tmp/scrob.flac", DurationMs: 100_000,
|
|
})
|
|
return pool, u, tr
|
|
}
|
|
|
|
func newScrobbleHandlers(pool *pgxpool.Pool) *mediaHandlers {
|
|
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
w := playevents.NewWriter(pool, logger, 30*time.Minute, 0.5, 30000)
|
|
return newMediaHandlers(pool, w, logger)
|
|
}
|
|
|
|
func TestHandleScrobble_SubmissionFalseInsertsOpenPlayEvent(t *testing.T) {
|
|
pool, user, track := testScrobblePool(t)
|
|
m := newScrobbleHandlers(pool)
|
|
|
|
q := url.Values{}
|
|
q.Set("id", uuidToID(track.ID))
|
|
q.Set("submission", "false")
|
|
req := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q.Encode(), nil)
|
|
ctx := context.WithValue(req.Context(), userCtxKey, user)
|
|
w := httptest.NewRecorder()
|
|
m.handleScrobble(w, req.WithContext(ctx))
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", w.Code)
|
|
}
|
|
rows, _ := pool.Query(context.Background(),
|
|
"SELECT id FROM play_events WHERE user_id=$1 AND ended_at IS NULL", user.ID)
|
|
defer rows.Close()
|
|
count := 0
|
|
for rows.Next() {
|
|
count++
|
|
}
|
|
if count != 1 {
|
|
t.Errorf("open play_events count = %d, want 1", count)
|
|
}
|
|
}
|
|
|
|
func TestHandleScrobble_SubmissionTrueWritesCompletedPlay(t *testing.T) {
|
|
pool, user, track := testScrobblePool(t)
|
|
m := newScrobbleHandlers(pool)
|
|
|
|
q := url.Values{}
|
|
q.Set("id", uuidToID(track.ID))
|
|
q.Set("submission", "true")
|
|
req := httptest.NewRequest(http.MethodGet, "/rest/scrobble?"+q.Encode(), nil)
|
|
ctx := context.WithValue(req.Context(), userCtxKey, user)
|
|
w := httptest.NewRecorder()
|
|
m.handleScrobble(w, req.WithContext(ctx))
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", w.Code)
|
|
}
|
|
rows, _ := pool.Query(context.Background(),
|
|
"SELECT ended_at IS NOT NULL FROM play_events WHERE user_id=$1", user.ID)
|
|
defer rows.Close()
|
|
closed := 0
|
|
for rows.Next() {
|
|
var c bool
|
|
_ = rows.Scan(&c)
|
|
if c {
|
|
closed++
|
|
}
|
|
}
|
|
if closed != 1 {
|
|
t.Errorf("closed play_events = %d, want 1", closed)
|
|
}
|
|
}
|