sonda/internal/api/http/favorites_test.go
tgiachi 74f683d26e feat: favourites of songs, albums and artists, in the API and in apps
A person can mark songs, albums and artists as favourites. They are theirs
alone: never shown to a friend, never sent anywhere. Only what can be played is
listed.

- migration 0035 favorites, internal/favorites, storage.FavoritesRepo
- PUT/DELETE /me/favorites/{kind}/{id}, GET /me/favorites/{kind} and
  GET /me/favorites/ids, in the OpenAPI spec
- catalog.Filter IDs now also reads artists by id
- OpenSubsonic: star, unstar, getStarred2, the starred date on songs, albums and
  artists, and getAlbumList2 of type starred
2026-11-16 10:51:07 +01:00

99 lines
4 KiB
Go

package httpapi
import (
"net/http"
"testing"
)
func TestFavouritesNeedASignedInUser(t *testing.T) {
h := newTestAPI(t)
for _, c := range [][2]string{{"GET", "/api/v1/me/favorites/ids"}, {"GET", "/api/v1/me/favorites/tracks"}, {"PUT", "/api/v1/me/favorites/tracks/1"}, {"DELETE", "/api/v1/me/favorites/tracks/1"}} {
if r := do(t, h, c[0], c[1], "", map[string]any{}); r.Code != http.StatusUnauthorized {
t.Errorf("%s %s: %d", c[0], c[1], r.Code)
}
}
}
func TestSongsAlbumsAndArtistsAreMarkedListedAndUnmarked(t *testing.T) {
e := newCoversEnv(t)
var song, artist int64
if err := e.db.QueryRow("SELECT id FROM tracks WHERE title = 'With Art song'").Scan(&song); err != nil {
t.Fatal(err)
}
if err := e.db.QueryRow("SELECT id FROM artists WHERE name = 'Band'").Scan(&artist); err != nil {
t.Fatal(err)
}
put := func(kind string, id int64) resp {
return do(t, e.h, "PUT", "/api/v1/me/favorites/"+kind+"/"+itoa(id), e.token, nil)
}
for kind, id := range map[string]int64{"tracks": song, "albums": e.withArt, "artists": artist} {
if r := put(kind, id); r.Code != http.StatusNoContent {
t.Fatalf("PUT %s: %d %v", kind, r.Code, r.Body)
}
if r := put(kind, id); r.Code != http.StatusNoContent {
t.Fatalf("PUT %s again: %d", kind, r.Code)
}
}
ids := do(t, e.h, "GET", "/api/v1/me/favorites/ids", e.token, nil)
sameKeys(t, "ids", ids.Body, "artists", "albums", "tracks")
if got := ids.Body["tracks"].([]any); len(got) != 1 || int64(got[0].(float64)) != song {
t.Fatalf("ids %v", ids.Body)
}
tracks := do(t, e.h, "GET", "/api/v1/me/favorites/tracks", e.token, nil)
items := tracks.Body["items"].([]any)
if len(items) != 1 || items[0].(map[string]any)["title"] != "With Art song" || tracks.Body["total"] != float64(1) {
t.Fatalf("tracks %v", tracks.Body)
}
if a := do(t, e.h, "GET", "/api/v1/me/favorites/albums", e.token, nil).Body["items"].([]any); len(a) != 1 || a[0].(map[string]any)["title"] != "With Art" {
t.Fatalf("albums %v", a)
}
if a := do(t, e.h, "GET", "/api/v1/me/favorites/artists", e.token, nil).Body["items"].([]any); len(a) != 1 || a[0].(map[string]any)["name"] != "Band" {
t.Fatalf("artists %v", a)
}
if r := do(t, e.h, "DELETE", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil); r.Code != http.StatusNoContent {
t.Fatalf("DELETE: %d", r.Code)
}
if r := do(t, e.h, "DELETE", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil); r.Code != http.StatusNoContent {
t.Fatalf("DELETE twice: %d", r.Code)
}
if got := do(t, e.h, "GET", "/api/v1/me/favorites/tracks", e.token, nil).Body["items"].([]any); len(got) != 0 {
t.Fatalf("after delete %v", got)
}
}
func TestWhatIsNotThereCannotBeMarked(t *testing.T) {
e := newCoversEnv(t)
for path, code := range map[string]string{
"/api/v1/me/favorites/tracks/9999": "track_not_found",
"/api/v1/me/favorites/albums/9999": "album_not_found",
"/api/v1/me/favorites/artists/9999": "artist_not_found",
"/api/v1/me/favorites/songs/1": "not_found",
"/api/v1/me/favorites/tracks/0": "invalid_input",
"/api/v1/me/favorites/tracks/x": "invalid_input",
} {
r := do(t, e.h, "PUT", path, e.token, nil)
if r.Code < 400 || errCode(r) != code {
t.Errorf("PUT %s: %d %v, want %s", path, r.Code, r.Body, code)
}
}
if r := do(t, e.h, "GET", "/api/v1/me/favorites/songs", e.token, nil); r.Code != http.StatusNotFound || errCode(r) != "not_found" {
t.Errorf("an unknown kind: %d %v", r.Code, r.Body)
}
if r := do(t, e.h, "GET", "/api/v1/me/favorites/tracks?limit=-1", e.token, nil); r.Code != http.StatusBadRequest {
t.Errorf("a negative limit: %d", r.Code)
}
}
func TestFavouritesAreOfEachPersonAlone(t *testing.T) {
e := newCoversEnv(t)
var song int64
_ = e.db.QueryRow("SELECT id FROM tracks WHERE title = 'With Art song'").Scan(&song)
do(t, e.h, "PUT", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil)
_, bob := addUser(t, e.h, e.token, "bob", false)
if got := do(t, e.h, "GET", "/api/v1/me/favorites/ids", bob, nil).Body["tracks"].([]any); len(got) != 0 {
t.Fatalf("bob sees %v", got)
}
}