package httpapi import ( "bytes" "context" "encoding/json" "fmt" "mime/multipart" "net/http" "net/http/httptest" "strings" "testing" "github.com/uptrace/bun" "git.kmalloc.org/squid/sonda/internal/library" "git.kmalloc.org/squid/sonda/internal/storage" ) const importPath = "/api/v1/me/import/spotify" // upload posts the files as a multipart form, each as `files`. func upload(t *testing.T, h http.Handler, token string, files ...string) resp { t.Helper() var body bytes.Buffer w := multipart.NewWriter(&body) for i, content := range files { part, err := w.CreateFormFile("files", fmt.Sprintf("file%d.json", i)) if err != nil { t.Fatal(err) } _, _ = part.Write([]byte(content)) } _ = w.Close() req := httptest.NewRequest("POST", importPath, &body) req.Header.Set("Content-Type", w.FormDataContentType()) 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 seedSong(t *testing.T, db *bun.DB, rel, title, album, artist string) { t.Helper() repo := storage.NewLibraryRepo(db) lib, err := repo.CreateLibrary(context.Background(), "music-"+rel, "/lib/"+rel) if err != nil { t.Fatal(err) } f := library.ParsedFile{ FileInfo: library.FileInfo{RelPath: rel, Size: 100, MTimeUnix: 1}, Metadata: library.Metadata{ Tags: library.Tags{Title: title, Album: album, AlbumArtist: artist, Artists: []string{artist}}, Properties: library.Properties{Format: "flac", DurationMs: 260_000, SampleRate: 44100, BitRate: 900}, }, } if _, err := repo.SaveFile(context.Background(), lib.ID, f, 0); err != nil { t.Fatal(err) } } const spotifyHistory = `[ {"ts":"2023-05-01T10:00:00Z","ms_played":260000,"ip_addr":"203.0.113.9","master_metadata_track_name":"Karma Police", "master_metadata_album_artist_name":"Radiohead","master_metadata_album_album_name":"OK Computer","reason_end":"trackdone"}, {"ts":"2023-05-01T10:10:00Z","ms_played":100000,"master_metadata_track_name":"Creep","master_metadata_album_artist_name":"Radiohead"} ]` func TestImportingAHistoryRecordsTheListensAndTheReportSaysWhatHappened(t *testing.T) { h, db := newTestEnv(t, func(*Deps) {}) seedSong(t, db, "a.flac", "Karma Police", "OK Computer", "Radiohead") tok := registerAndLogin(t, h) r := upload(t, h, tok, spotifyHistory) if r.Code != http.StatusOK { t.Fatalf("import: %d %v", r.Code, r.Body) } if r.Body["id"] != 1.0 || r.Body["created_at"] == nil { t.Errorf("id and time = %v %v, want the import kept as 1", r.Body["id"], r.Body["created_at"]) } hist, _ := r.Body["history"].(map[string]any) sameKeys(t, "history", hist, "entries", "matched", "imported", "already_there", "not_found", "ignored", "kept_outside") if hist["entries"] != 2.0 || hist["matched"] != 1.0 || hist["imported"] != 1.0 || hist["not_found"] != 1.0 || hist["kept_outside"] != 1.0 { t.Errorf("history = %v", hist) } missing, _ := r.Body["missing"].(map[string]any) sameKeys(t, "missing", missing, "total", "artists") artists, _ := missing["artists"].([]any) if len(artists) != 1 || artists[0].(map[string]any)["name"] != "Radiohead" || artists[0].(map[string]any)["count"] != 1.0 { t.Errorf("missing = %v", missing) } if pl, ok := r.Body["playlists"].([]any); !ok || len(pl) != 0 { t.Errorf("playlists = %#v, want an empty list", r.Body["playlists"]) } // The listen is in the history, with the time of the play, and counts. list := do(t, h, "GET", "/api/v1/listens", tok, nil) items, _ := list.Body["items"].([]any) if len(items) != 1 { t.Fatalf("history has %d events: %v", len(items), list.Body) } if ev := items[0].(map[string]any); ev["counted"] != true || ev["occurred_at"] != "2023-05-01T10:00:00Z" { t.Errorf("event = %v", ev) } // The same file again adds nothing. r = upload(t, h, tok, spotifyHistory) hist, _ = r.Body["history"].(map[string]any) if r.Code != http.StatusOK || hist["imported"] != 0.0 || hist["already_there"] != 1.0 { t.Errorf("second import: %d %v", r.Code, hist) } } func TestImportingPlaylistsMakesPlaylistsOfTheSignedInPerson(t *testing.T) { h, db := newTestEnv(t, func(*Deps) {}) seedSong(t, db, "a.flac", "Karma Police", "OK Computer", "Radiohead") tok := registerAndLogin(t, h) r := upload(t, h, tok, `{"playlists":[{"name":"Road","items":[ {"track":{"trackName":"Karma Police","artistName":"Radiohead","albumName":"OK Computer"}}, {"track":{"trackName":"Creep","artistName":"Radiohead","albumName":"Pablo Honey"}}]}]}`) if r.Code != http.StatusOK { t.Fatalf("import: %d %v", r.Code, r.Body) } pl := r.Body["playlists"].([]any)[0].(map[string]any) sameKeys(t, "playlist", pl, "name", "tracks", "matched", "status") if pl["name"] != "Road" || pl["tracks"] != 2.0 || pl["matched"] != 1.0 || pl["status"] != "created" { t.Errorf("playlist = %v", pl) } list := do(t, h, "GET", "/api/v1/playlists", tok, nil) items, _ := list.Body["items"].([]any) if len(items) != 1 || items[0].(map[string]any)["name"] != "Road" || items[0].(map[string]any)["track_count"] != 1.0 { t.Errorf("playlists = %v", list.Body) } } func TestTheImportNeedsASignedInPerson(t *testing.T) { h := newTestAPI(t) if r := upload(t, h, "", spotifyHistory); r.Code != http.StatusUnauthorized { t.Errorf("without a token: %d, want 401", r.Code) } } func TestImportRefusesWhatIsNotAnExport(t *testing.T) { h := newTestAPI(t) tok := registerAndLogin(t, h) if r := do(t, h, "POST", importPath, tok, "{}"); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { t.Errorf("a JSON body: %d %v, want 400 invalid_input", r.Code, r.Body) } if r := upload(t, h, tok); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { t.Errorf("no files: %d %v, want 400 invalid_input", r.Code, r.Body) } if r := upload(t, h, tok, `{"hello":"world"}`, "words"); r.Code != http.StatusBadRequest || errCode(r) != "no_spotify_data" { t.Errorf("not Spotify's: %d %v, want 400 no_spotify_data", r.Code, r.Body) } if r := upload(t, h, tok, spotifyHistory[:len(spotifyHistory)-30]); r.Code != http.StatusBadRequest || errCode(r) != "unreadable_file" { t.Errorf("a cut file: %d %v, want 400 unreadable_file", r.Code, r.Body) } } func TestAnUploadBeyondTheLimitIsRefused(t *testing.T) { old := importMaxBytes importMaxBytes = 2000 defer func() { importMaxBytes = old }() h := newTestAPI(t) tok := registerAndLogin(t, h) big := "[" + strings.Repeat(`{"ts":"2023-05-01T10:00:00Z"},`, 200) + `{"ts":"2023-05-01T10:00:00Z"}]` if r := upload(t, h, tok, big); r.Code != http.StatusRequestEntityTooLarge || errCode(r) != "too_large" { t.Errorf("%d %v, want 413 too_large", r.Code, r.Body) } } func TestAnUploadLargerThanOtherRequestsIsAccepted(t *testing.T) { h, db := newTestEnv(t, func(*Deps) {}) seedSong(t, db, "a.flac", "Karma Police", "OK Computer", "Radiohead") tok := registerAndLogin(t, h) // A history of more than the 64 KB that any other request may be. var plays []string for i := 0; i < 1500; i++ { plays = append(plays, fmt.Sprintf(`{"ts":"2023-05-%02dT%02d:%02d:00Z","ms_played":260000,"master_metadata_track_name":"Karma Police","master_metadata_album_artist_name":"Radiohead"}`, 1+i/1440, i/60%24, i%60)) } data := "[" + strings.Join(plays, ",") + "]" if len(data) < 2*maxBodyBytes { t.Fatalf("test file is %d bytes, too small to prove anything", len(data)) } r := upload(t, h, tok, data) hist, _ := r.Body["history"].(map[string]any) if r.Code != http.StatusOK || hist["imported"] != 1500.0 { t.Errorf("%d %v", r.Code, r.Body) } } func TestPastImportsAreListedNewestFirstAndAreOnlyMine(t *testing.T) { h, db := newTestEnv(t, func(*Deps) {}) seedSong(t, db, "a.flac", "Karma Police", "OK Computer", "Radiohead") tok := registerAndLogin(t, h) upload(t, h, tok, spotifyHistory) upload(t, h, tok, spotifyHistory) r := do(t, h, "GET", importPath, tok, nil) if r.Code != http.StatusOK || r.Body["total"] != 2.0 || r.Body["limit"] != 20.0 || r.Body["offset"] != 0.0 { t.Fatalf("list: %d %v", r.Code, r.Body) } items, _ := r.Body["items"].([]any) if len(items) != 2 { t.Fatalf("%d imports, want 2", len(items)) } newest, oldest := items[0].(map[string]any), items[1].(map[string]any) sameKeys(t, "import", newest, "id", "created_at", "history", "playlists", "missing") if newest["id"] != 2.0 || oldest["id"] != 1.0 { t.Errorf("order = %v, %v, want 2 then 1", newest["id"], oldest["id"]) } if h, _ := oldest["history"].(map[string]any); h["imported"] != 1.0 { t.Errorf("the first import = %v, want one listen recorded", h) } if h, _ := newest["history"].(map[string]any); h["already_there"] != 1.0 || h["imported"] != 0.0 { t.Errorf("the second import = %v, want the play already there", h) } if m, _ := newest["missing"].(map[string]any); m["total"] != 1.0 { t.Errorf("missing = %v, want the one artist", m) } if page := do(t, h, "GET", importPath+"?limit=1&offset=1", tok, nil); page.Code != http.StatusOK || len(page.Body["items"].([]any)) != 1 || page.Body["items"].([]any)[0].(map[string]any)["id"] != 1.0 { t.Errorf("second page: %d %v", page.Code, page.Body) } if r := do(t, h, "GET", importPath+"?offset=-3", tok, nil); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" { t.Errorf("a negative offset: %d %v", r.Code, r.Body) } // Somebody else sees none of it. _, other := addUser(t, h, tok, "other", false) if r := do(t, h, "GET", importPath, other, nil); r.Code != http.StatusOK || r.Body["total"] != 0.0 || len(r.Body["items"].([]any)) != 0 { t.Errorf("another person's list: %d %v, want empty", r.Code, r.Body) } } func TestAnImportThatFindsNothingIsNotListed(t *testing.T) { h := newTestAPI(t) tok := registerAndLogin(t, h) upload(t, h, tok, `{"hello":"world"}`) if r := do(t, h, "GET", importPath, tok, nil); r.Body["total"] != 0.0 { t.Errorf("list = %v, want nothing", r.Body) } if r := do(t, h, "GET", importPath, "", nil); r.Code != http.StatusUnauthorized { t.Errorf("without a token: %d, want 401", r.Code) } } func TestThePlayOfAnArtistNotInTheLibraryKeepsOnlyTheArtistAndTheTime(t *testing.T) { h, db := newTestEnv(t, func(*Deps) {}) seedSong(t, db, "a.flac", "Karma Police", "OK Computer", "Radiohead") tok := registerAndLogin(t, h) if r := upload(t, h, tok, spotifyHistory); r.Code != http.StatusOK { t.Fatalf("import: %d %v", r.Code, r.Body) } var artist string var at int64 if err := db.QueryRow("SELECT artist, played_at FROM outside_plays").Scan(&artist, &at); err != nil { t.Fatal(err) } if artist != "Radiohead" || at != 1682935800 { // Creep, 2023-05-01T10:10:00Z, 100 s played t.Errorf("kept %q at %d, want Radiohead at 1682935800", artist, at) } var columns int if err := db.QueryRow("SELECT COUNT(*) FROM pragma_table_info('outside_plays')").Scan(&columns); err == nil && columns != 5 { t.Errorf("outside_plays has %d columns, want the 5 that hold no title, album or address", columns) } }