Each user can make playlists: a name (1 to 200 characters), an optional description and an ordered list of tracks, where a track may appear more than once (each place has its own item id). The API makes, lists, renames and deletes playlists, and adds tracks (at the end or at a position), lists them, moves one and removes one; positions count from 0 without gaps. A playlist is its owner's alone: somebody else's answers playlist_not_found (404), as one that does not exist. A track whose files were deleted or moved stays in the playlist and is listed with available false. Deleting a user deletes their playlists. Limits: 1,000 playlists per user, 10,000 tracks per playlist, 500 tracks per request. Changes of one playlist are serialised by locking its row, so two requests at once keep the positions unique on PostgreSQL too. Migration 0019, OpenAPI spec, contract test, README and the web API types follow. The web app comes separately. Closes #89
261 lines
12 KiB
Go
261 lines
12 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
// plAPI is a server with an admin (alice), a second user (bob) and four tracks.
|
|
type plAPI struct {
|
|
h http.Handler
|
|
alice string
|
|
bob string
|
|
tracks []int64
|
|
}
|
|
|
|
func newPlAPI(t *testing.T) *plAPI {
|
|
t.Helper()
|
|
h, database := newTestEnvDB(t, func(*Deps, *bun.DB) {})
|
|
alice := registerAndLogin(t, h)
|
|
_, bob := addUser(t, h, alice, "bob", false)
|
|
for _, title := range []string{"one", "two", "three"} {
|
|
if _, err := database.Exec("INSERT INTO tracks (title) VALUES (?)", title); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var tracks []int64
|
|
rows, err := database.Query("SELECT id FROM tracks ORDER BY id")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var id int64
|
|
if err := rows.Scan(&id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tracks = append(tracks, id)
|
|
}
|
|
return &plAPI{h: h, alice: alice, bob: bob, tracks: tracks}
|
|
}
|
|
|
|
func (p *plAPI) create(t *testing.T, token, name string) int64 {
|
|
t.Helper()
|
|
r := do(t, p.h, "POST", "/api/v1/playlists", token, map[string]any{"name": name})
|
|
if r.Code != http.StatusCreated {
|
|
t.Fatalf("create %q: %d %v", name, r.Code, r.Body)
|
|
}
|
|
return int64(r.Body["id"].(float64))
|
|
}
|
|
|
|
func (p *plAPI) add(t *testing.T, token string, pl int64, ids ...int64) resp {
|
|
t.Helper()
|
|
return do(t, p.h, "POST", "/api/v1/playlists/"+id(pl)+"/tracks", token, map[string]any{"track_ids": ids})
|
|
}
|
|
|
|
func (p *plAPI) titles(t *testing.T, token string, pl int64) []string {
|
|
t.Helper()
|
|
r := do(t, p.h, "GET", "/api/v1/playlists/"+id(pl)+"/tracks", token, nil)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("items: %d %v", r.Code, r.Body)
|
|
}
|
|
var out []string
|
|
for _, it := range r.Body["items"].([]any) {
|
|
out = append(out, it.(map[string]any)["track"].(map[string]any)["title"].(string))
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestPlaylistRoutesNeedASignedInUser(t *testing.T) {
|
|
p := newPlAPI(t)
|
|
for _, rt := range []struct{ method, path string }{
|
|
{"GET", "/api/v1/playlists"}, {"POST", "/api/v1/playlists"},
|
|
{"GET", "/api/v1/playlists/1"}, {"PATCH", "/api/v1/playlists/1"}, {"DELETE", "/api/v1/playlists/1"},
|
|
{"GET", "/api/v1/playlists/1/tracks"}, {"POST", "/api/v1/playlists/1/tracks"},
|
|
{"DELETE", "/api/v1/playlists/1/tracks/1"}, {"PATCH", "/api/v1/playlists/1/tracks/1"},
|
|
} {
|
|
if r := do(t, p.h, rt.method, rt.path, "", nil); r.Code != http.StatusUnauthorized {
|
|
t.Errorf("%s %s without a token: %d", rt.method, rt.path, r.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCreateListGetUpdateAndDeleteAPlaylist(t *testing.T) {
|
|
p := newPlAPI(t)
|
|
r := do(t, p.h, "POST", "/api/v1/playlists", p.alice, map[string]any{"name": " Road trip ", "description": "for the car"})
|
|
if r.Code != http.StatusCreated || r.Body["name"] != "Road trip" || r.Body["description"] != "for the car" ||
|
|
r.Body["track_count"] != float64(0) || r.Body["duration_ms"] != float64(0) || r.Body["created_at"] == nil || r.Body["updated_at"] == nil {
|
|
t.Fatalf("create: %d %v", r.Code, r.Body)
|
|
}
|
|
pl := int64(r.Body["id"].(float64))
|
|
second := p.create(t, p.alice, "Second")
|
|
|
|
list := do(t, p.h, "GET", "/api/v1/playlists", p.alice, nil)
|
|
items, _ := list.Body["items"].([]any)
|
|
if list.Code != http.StatusOK || list.Body["total"] != float64(2) || len(items) != 2 || items[0].(map[string]any)["id"] != float64(second) {
|
|
t.Fatalf("list: %d %v", list.Code, list.Body)
|
|
}
|
|
if got := do(t, p.h, "GET", "/api/v1/playlists?limit=1&offset=1", p.alice, nil); len(got.Body["items"].([]any)) != 1 || got.Body["limit"] != float64(1) || got.Body["offset"] != float64(1) {
|
|
t.Errorf("paging: %v", got.Body)
|
|
}
|
|
if got := do(t, p.h, "GET", "/api/v1/playlists/"+id(pl), p.alice, nil); got.Code != http.StatusOK || got.Body["name"] != "Road trip" {
|
|
t.Errorf("get: %d %v", got.Code, got.Body)
|
|
}
|
|
|
|
upd := do(t, p.h, "PATCH", "/api/v1/playlists/"+id(pl), p.alice, map[string]any{"name": "Renamed"})
|
|
if upd.Code != http.StatusOK || upd.Body["name"] != "Renamed" || upd.Body["description"] != "for the car" {
|
|
t.Errorf("rename: %d %v", upd.Code, upd.Body)
|
|
}
|
|
upd = do(t, p.h, "PATCH", "/api/v1/playlists/"+id(pl), p.alice, map[string]any{"description": ""})
|
|
if upd.Code != http.StatusOK || upd.Body["description"] != "" || upd.Body["name"] != "Renamed" {
|
|
t.Errorf("clear the description: %d %v", upd.Code, upd.Body)
|
|
}
|
|
|
|
if r := do(t, p.h, "DELETE", "/api/v1/playlists/"+id(pl), p.alice, nil); r.Code != http.StatusNoContent {
|
|
t.Errorf("delete: %d %v", r.Code, r.Body)
|
|
}
|
|
if r := do(t, p.h, "GET", "/api/v1/playlists/"+id(pl), p.alice, nil); r.Code != http.StatusNotFound || errCode(r) != "playlist_not_found" {
|
|
t.Errorf("get after delete: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestPlaylistInputIsChecked(t *testing.T) {
|
|
p := newPlAPI(t)
|
|
pl := p.create(t, p.alice, "P")
|
|
long := strings.Repeat("a", 201)
|
|
for name, tt := range map[string]struct {
|
|
method, path string
|
|
body any
|
|
}{
|
|
"no name": {"POST", "/api/v1/playlists", map[string]any{}},
|
|
"a blank name": {"POST", "/api/v1/playlists", map[string]any{"name": " "}},
|
|
"a long name": {"POST", "/api/v1/playlists", map[string]any{"name": long}},
|
|
"not json": {"POST", "/api/v1/playlists", "{"},
|
|
"a name that is a number": {"POST", "/api/v1/playlists", `{"name": 5}`},
|
|
"nothing to change": {"PATCH", "/api/v1/playlists/" + id(pl), map[string]any{}},
|
|
"an empty new name": {"PATCH", "/api/v1/playlists/" + id(pl), map[string]any{"name": ""}},
|
|
"no track ids": {"POST", "/api/v1/playlists/" + id(pl) + "/tracks", map[string]any{}},
|
|
"an empty list": {"POST", "/api/v1/playlists/" + id(pl) + "/tracks", map[string]any{"track_ids": []int64{}}},
|
|
"a zero track id": {"POST", "/api/v1/playlists/" + id(pl) + "/tracks", map[string]any{"track_ids": []int64{0}}},
|
|
"a negative position": {"POST", "/api/v1/playlists/" + id(pl) + "/tracks", map[string]any{"track_ids": []int64{p.tracks[0]}, "position": -1}},
|
|
"a move with no place": {"PATCH", "/api/v1/playlists/" + id(pl) + "/tracks/1", map[string]any{}},
|
|
"a negative place": {"PATCH", "/api/v1/playlists/" + id(pl) + "/tracks/1", map[string]any{"position": -1}},
|
|
"a bad id": {"GET", "/api/v1/playlists/abc", nil},
|
|
"a zero id": {"GET", "/api/v1/playlists/0", nil},
|
|
"a bad limit": {"GET", "/api/v1/playlists?limit=x", nil},
|
|
"a negative offset": {"GET", "/api/v1/playlists/" + id(pl) + "/tracks?offset=-1", nil},
|
|
} {
|
|
if r := do(t, p.h, tt.method, tt.path, p.alice, tt.body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
|
|
t.Errorf("%s: %d %v, want 400 invalid_input", name, r.Code, r.Body)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAddTracksListThemMoveAndRemove(t *testing.T) {
|
|
p := newPlAPI(t)
|
|
pl := p.create(t, p.alice, "P")
|
|
a, b, c := p.tracks[1], p.tracks[2], p.tracks[3]
|
|
|
|
r := p.add(t, p.alice, pl, a, b, a)
|
|
if r.Code != http.StatusOK || r.Body["track_count"] != float64(3) {
|
|
t.Fatalf("add: %d %v", r.Code, r.Body)
|
|
}
|
|
if got := p.titles(t, p.alice, pl); strings.Join(got, ",") != "one,two,one" {
|
|
t.Fatalf("titles = %v", got)
|
|
}
|
|
// In the middle.
|
|
if r := do(t, p.h, "POST", "/api/v1/playlists/"+id(pl)+"/tracks", p.alice, map[string]any{"track_ids": []int64{c}, "position": 1}); r.Code != http.StatusOK {
|
|
t.Fatalf("add at 1: %d %v", r.Code, r.Body)
|
|
}
|
|
if got := p.titles(t, p.alice, pl); strings.Join(got, ",") != "one,three,two,one" {
|
|
t.Fatalf("titles = %v", got)
|
|
}
|
|
|
|
page := do(t, p.h, "GET", "/api/v1/playlists/"+id(pl)+"/tracks?limit=2&offset=1", p.alice, nil)
|
|
items := page.Body["items"].([]any)
|
|
first := items[0].(map[string]any)
|
|
if page.Code != http.StatusOK || len(items) != 2 || page.Body["total"] != float64(4) || first["position"] != float64(1) ||
|
|
first["id"] == nil || first["available"] != false || first["added_at"] == nil || first["track"].(map[string]any)["title"] != "three" {
|
|
t.Fatalf("page: %d %v", page.Code, page.Body)
|
|
}
|
|
|
|
// Move the item at 3 to the front, then remove the one now at 1.
|
|
all := do(t, p.h, "GET", "/api/v1/playlists/"+id(pl)+"/tracks", p.alice, nil).Body["items"].([]any)
|
|
itemID := func(i int) string { return id(int64(all[i].(map[string]any)["id"].(float64))) }
|
|
if r := do(t, p.h, "PATCH", "/api/v1/playlists/"+id(pl)+"/tracks/"+itemID(3), p.alice, map[string]any{"position": 0}); r.Code != http.StatusNoContent {
|
|
t.Fatalf("move: %d %v", r.Code, r.Body)
|
|
}
|
|
if got := p.titles(t, p.alice, pl); strings.Join(got, ",") != "one,one,three,two" {
|
|
t.Fatalf("after the move: %v", got)
|
|
}
|
|
if r := do(t, p.h, "DELETE", "/api/v1/playlists/"+id(pl)+"/tracks/"+itemID(1), p.alice, nil); r.Code != http.StatusNoContent {
|
|
t.Fatalf("remove: %d %v", r.Code, r.Body)
|
|
}
|
|
// itemID(1) was "three" before the move: it is the one that goes.
|
|
if got := p.titles(t, p.alice, pl); strings.Join(got, ",") != "one,one,two" {
|
|
t.Fatalf("after the removal: %v", got)
|
|
}
|
|
if got := do(t, p.h, "GET", "/api/v1/playlists/"+id(pl), p.alice, nil); got.Body["track_count"] != float64(3) {
|
|
t.Errorf("track count = %v", got.Body["track_count"])
|
|
}
|
|
}
|
|
|
|
func TestPlaylistErrorsHaveTheirOwnCodes(t *testing.T) {
|
|
p := newPlAPI(t)
|
|
pl := p.create(t, p.alice, "P")
|
|
if r := p.add(t, p.alice, pl, p.tracks[1]); r.Code != http.StatusOK {
|
|
t.Fatal(r.Body)
|
|
}
|
|
if r := p.add(t, p.alice, pl, p.tracks[1], 99999); r.Code != http.StatusUnprocessableEntity || errCode(r) != "track_not_found" {
|
|
t.Errorf("unknown track: %d %v", r.Code, r.Body)
|
|
}
|
|
if r := do(t, p.h, "POST", "/api/v1/playlists/"+id(pl)+"/tracks", p.alice, map[string]any{"track_ids": []int64{p.tracks[1]}, "position": 5}); r.Code != http.StatusBadRequest || errCode(r) != "invalid_position" {
|
|
t.Errorf("position beyond the end: %d %v", r.Code, r.Body)
|
|
}
|
|
if r := do(t, p.h, "PATCH", "/api/v1/playlists/"+id(pl)+"/tracks/99999", p.alice, map[string]any{"position": 0}); r.Code != http.StatusNotFound || errCode(r) != "item_not_found" {
|
|
t.Errorf("move an unknown item: %d %v", r.Code, r.Body)
|
|
}
|
|
if r := do(t, p.h, "DELETE", "/api/v1/playlists/"+id(pl)+"/tracks/99999", p.alice, nil); r.Code != http.StatusNotFound || errCode(r) != "item_not_found" {
|
|
t.Errorf("remove an unknown item: %d %v", r.Code, r.Body)
|
|
}
|
|
items := do(t, p.h, "GET", "/api/v1/playlists/"+id(pl)+"/tracks", p.alice, nil).Body["items"].([]any)
|
|
itemID := id(int64(items[0].(map[string]any)["id"].(float64)))
|
|
if r := do(t, p.h, "PATCH", "/api/v1/playlists/"+id(pl)+"/tracks/"+itemID, p.alice, map[string]any{"position": 1}); r.Code != http.StatusBadRequest || errCode(r) != "invalid_position" {
|
|
t.Errorf("move beyond the end: %d %v", r.Code, r.Body)
|
|
}
|
|
if r := do(t, p.h, "GET", "/api/v1/playlists/99999/tracks", p.alice, nil); r.Code != http.StatusNotFound || errCode(r) != "playlist_not_found" {
|
|
t.Errorf("items of an unknown playlist: %d %v", r.Code, r.Body)
|
|
}
|
|
}
|
|
|
|
func TestAPlaylistIsOnlyItsOwnersAndAnyoneElseGetsNotFound(t *testing.T) {
|
|
p := newPlAPI(t)
|
|
pl := p.create(t, p.alice, "Private")
|
|
if r := p.add(t, p.alice, pl, p.tracks[1]); r.Code != http.StatusOK {
|
|
t.Fatal(r.Body)
|
|
}
|
|
items := do(t, p.h, "GET", "/api/v1/playlists/"+id(pl)+"/tracks", p.alice, nil).Body["items"].([]any)
|
|
itemID := id(int64(items[0].(map[string]any)["id"].(float64)))
|
|
base := "/api/v1/playlists/" + id(pl)
|
|
for _, rt := range []struct {
|
|
method, path string
|
|
body any
|
|
}{
|
|
{"GET", base, nil}, {"PATCH", base, map[string]any{"name": "mine now"}}, {"DELETE", base, nil},
|
|
{"GET", base + "/tracks", nil}, {"POST", base + "/tracks", map[string]any{"track_ids": []int64{p.tracks[1]}}},
|
|
{"DELETE", base + "/tracks/" + itemID, nil}, {"PATCH", base + "/tracks/" + itemID, map[string]any{"position": 0}},
|
|
} {
|
|
if r := do(t, p.h, rt.method, rt.path, p.bob, rt.body); r.Code != http.StatusNotFound || errCode(r) != "playlist_not_found" {
|
|
t.Errorf("bob %s %s: %d %v, want 404 playlist_not_found", rt.method, rt.path, r.Code, r.Body)
|
|
}
|
|
}
|
|
if list := do(t, p.h, "GET", "/api/v1/playlists", p.bob, nil); list.Body["total"] != float64(0) {
|
|
t.Errorf("bob's list: %v", list.Body)
|
|
}
|
|
if got := do(t, p.h, "GET", base, p.alice, nil); got.Code != http.StatusOK || got.Body["name"] != "Private" || got.Body["track_count"] != float64(1) {
|
|
t.Errorf("alice's playlist was changed: %v", got.Body)
|
|
}
|
|
}
|