package httpapi import ( "bytes" "context" "encoding/json" "image" "image/color" "image/jpeg" "net/http" "net/http/httptest" "net/url" "os" "path/filepath" "strings" "sync/atomic" "testing" "github.com/uptrace/bun" "git.kmalloc.org/squid/sonda/internal/images" "git.kmalloc.org/squid/sonda/internal/library" "git.kmalloc.org/squid/sonda/internal/storage" "git.kmalloc.org/squid/sonda/internal/streaming" ) type fakeArchive struct { found images.Found err error calls int32 } func (f *fakeArchive) AlbumImage(context.Context, int64) (images.Found, error) { atomic.AddInt32(&f.calls, 1) return f.found, f.err } type coversEnv struct { svc *images.Service archive *fakeArchive h http.Handler db *bun.DB token string root string data string withArt int64 // an album with a cover.jpg in its folder without int64 // an album with no picture at all } func testJPEG(t *testing.T, w, h int, c color.Color) []byte { t.Helper() img := image.NewRGBA(image.Rect(0, 0, w, h)) for y := 0; y < h; y++ { for x := 0; x < w; x++ { img.Set(x, y, c) } } var b bytes.Buffer if err := jpeg.Encode(&b, img, nil); err != nil { t.Fatal(err) } return b.Bytes() } func newCoversEnv(t *testing.T) *coversEnv { t.Helper() e := &coversEnv{root: t.TempDir(), data: filepath.Join(t.TempDir(), "data"), archive: &fakeArchive{err: images.ErrNoImage}} for _, dir := range []string{"With Art", "Without"} { if err := os.MkdirAll(filepath.Join(e.root, dir), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(e.root, dir, "01.mp3"), []byte("not audio"), 0o644); err != nil { t.Fatal(err) } } if err := os.WriteFile(filepath.Join(e.root, "With Art", "cover.jpg"), testJPEG(t, 1500, 1200, color.RGBA{200, 30, 30, 255}), 0o644); err != nil { t.Fatal(err) } e.h, e.db = newTestEnvDB(t, func(d *Deps, db *bun.DB) { repo := storage.NewImageRepo(db) svc := images.NewService(e.data, repo, repo) svc.SetAlbumRemote(e.archive) e.svc = svc d.Images = svc d.Streams = streaming.NewService(storage.NewLibraryRepo(db)) }) e.token = registerAndLogin(t, e.h) lib := storage.NewLibraryRepo(e.db) l, err := lib.CreateLibrary(t.Context(), "music", e.root) if err != nil { t.Fatal(err) } for _, name := range []string{"With Art", "Without"} { f := library.ParsedFile{ FileInfo: library.FileInfo{RelPath: name + "/01.mp3", Size: 9, MTimeUnix: 1}, Metadata: library.Metadata{ Tags: library.Tags{Title: name + " song", Album: name, AlbumArtist: "Band", Artists: []string{"Band"}}, Properties: library.Properties{Format: "mpeg", DurationMs: 1000}, }, } if _, err := lib.SaveFile(t.Context(), l.ID, f, 0); err != nil { t.Fatal(err) } } e.withArt, e.without = e.album(t, "With Art"), e.album(t, "Without") return e } func (e *coversEnv) album(t *testing.T, title string) int64 { t.Helper() var id int64 if err := e.db.QueryRow("SELECT id FROM albums WHERE title = ?", title).Scan(&id); err != nil { t.Fatal(err) } return id } func (e *coversEnv) get(method, path, token string, header ...string) *httptest.ResponseRecorder { req := httptest.NewRequest(method, "/api/v1"+path, nil) if token != "" { req.Header.Set("Authorization", "Bearer "+token) } for i := 0; i+1 < len(header); i += 2 { req.Header.Set(header[i], header[i+1]) } rec := httptest.NewRecorder() e.h.ServeHTTP(rec, req) return rec } func TestCoverNeedsASignedInUserAndAcceptsTheTokenInTheQuery(t *testing.T) { e := newCoversEnv(t) path := "/albums/" + id(e.withArt) + "/cover" if rec := e.get("GET", path, ""); rec.Code != http.StatusUnauthorized { t.Errorf("no token: %d", rec.Code) } // An element cannot send a header. if rec := e.get("GET", path+"?access_token="+e.token, ""); rec.Code != http.StatusOK { t.Errorf("token in the query: %d %s", rec.Code, rec.Body.String()) } if rec := e.get("GET", path+"?access_token=wrong", ""); rec.Code != http.StatusUnauthorized { t.Errorf("a bad token in the query: %d", rec.Code) } } func TestCoverIsAJPEGInTheSizeAsked(t *testing.T) { e := newCoversEnv(t) path := "/albums/" + id(e.withArt) + "/cover" for query, width := range map[string]int{"": 1000, "?size=large": 1000, "?size=small": 300} { rec := e.get("GET", path+query, e.token) if rec.Code != http.StatusOK { t.Fatalf("%q: %d %s", query, rec.Code, rec.Body.String()) } if ct := rec.Header().Get("Content-Type"); ct != "image/jpeg" { t.Errorf("%q: content type %q", query, ct) } cfg, format, err := image.DecodeConfig(bytes.NewReader(rec.Body.Bytes())) if err != nil || format != "jpeg" || cfg.Width != width || cfg.Height != width*1200/1500 { t.Errorf("%q: %v %s %v, want a jpeg %d wide, same shape as the original", query, cfg, format, err, width) } if rec.Header().Get("X-Content-Type-Options") != "nosniff" { t.Errorf("%q: no nosniff", query) } if cc := rec.Header().Get("Cache-Control"); cc != "private, max-age=3600" { t.Errorf("%q: cache-control %q, want it private", query, cc) } } } func TestCoverIsCachedWithAnETag(t *testing.T) { e := newCoversEnv(t) path := "/albums/" + id(e.withArt) + "/cover" first := e.get("GET", path, e.token) etag := first.Header().Get("ETag") if etag == "" || etag[0] != '"' || first.Header().Get("Last-Modified") == "" { t.Fatalf("etag %q, last-modified %q", etag, first.Header().Get("Last-Modified")) } if rec := e.get("GET", path, e.token, "If-None-Match", etag); rec.Code != http.StatusNotModified || rec.Body.Len() != 0 { t.Errorf("If-None-Match: %d with %d bytes", rec.Code, rec.Body.Len()) } if small := e.get("GET", path+"?size=small", e.token); small.Header().Get("ETag") == etag { t.Error("the small and the large picture have the same etag") } if rec := e.get("HEAD", path, e.token); rec.Code != http.StatusOK || rec.Body.Len() != 0 || rec.Header().Get("Content-Length") == "" { t.Errorf("HEAD: %d, %d bytes, length %q", rec.Code, rec.Body.Len(), rec.Header().Get("Content-Length")) } if rec := e.get("GET", path, e.token, "Range", "bytes=0-9"); rec.Code != http.StatusPartialContent || rec.Body.Len() != 10 { t.Errorf("Range: %d, %d bytes", rec.Code, rec.Body.Len()) } } func TestCoverErrors(t *testing.T) { e := newCoversEnv(t) for name, tt := range map[string]struct { path string code int err string }{ "no picture": {"/albums/" + id(e.without) + "/cover", 404, "cover_not_found"}, "unknown album": {"/albums/99999/cover", 404, "album_not_found"}, "bad id": {"/albums/abc/cover", 400, "invalid_input"}, "zero id": {"/albums/0/cover", 400, "invalid_input"}, "bad size": {"/albums/" + id(e.withArt) + "/cover?size=huge", 400, "invalid_input"}, "size twice": {"/albums/" + id(e.withArt) + "/cover?size=small&size=large", 400, "invalid_input"}, "size in capital": {"/albums/" + id(e.withArt) + "/cover?size=Small", 400, "invalid_input"}, } { rec := e.get("GET", tt.path, e.token) var body struct { Error struct{ Code string } `json:"error"` } _ = json.Unmarshal(rec.Body.Bytes(), &body) if rec.Code != tt.code || body.Error.Code != tt.err { t.Errorf("%s: %d %s, want %d %s", name, rec.Code, rec.Body.String(), tt.code, tt.err) } } } // A picture is looked for again when a file of the album changes. func TestCoverIsBuiltAgainAfterTheFilesChange(t *testing.T) { e := newCoversEnv(t) path := "/albums/" + id(e.without) + "/cover" if rec := e.get("GET", path, e.token); rec.Code != http.StatusNotFound { t.Fatalf("before: %d", rec.Code) } if err := os.WriteFile(filepath.Join(e.root, "Without", "folder.jpg"), testJPEG(t, 500, 500, color.White), 0o644); err != nil { t.Fatal(err) } if rec := e.get("GET", path, e.token); rec.Code != http.StatusNotFound { t.Fatalf("the absence is remembered for a day: %d", rec.Code) } // What the scanner does when a file of the album changes. lib := storage.NewLibraryRepo(e.db) var libID, fileID int64 if err := e.db.QueryRow("SELECT library_id, id FROM audio_files WHERE rel_path = 'Without/01.mp3'").Scan(&libID, &fileID); err != nil { t.Fatal(err) } f := library.ParsedFile{ FileInfo: library.FileInfo{RelPath: "Without/01.mp3", Size: 9, MTimeUnix: 2}, Metadata: library.Metadata{ Tags: library.Tags{Title: "Without song", Album: "Without", AlbumArtist: "Band", Artists: []string{"Band"}}, Properties: library.Properties{Format: "mpeg", DurationMs: 1000}, }, } if _, err := lib.SaveFile(t.Context(), libID, f, fileID); err != nil { t.Fatal(err) } if rec := e.get("GET", path, e.token); rec.Code != http.StatusOK { t.Errorf("after the file changed: %d %s", rec.Code, rec.Body.String()) } } func TestAnAlbumWithNoPictureGetsTheCoverOfTheArchive(t *testing.T) { e := newCoversEnv(t) e.archive.found, e.archive.err = images.Found{Data: testJPEG(t, 1200, 1200, color.RGBA{10, 100, 10, 255})}, nil path := "/albums/" + id(e.without) + "/cover" rec := e.get("GET", path, e.token) if rec.Code != http.StatusOK || rec.Header().Get("Content-Type") != "image/jpeg" { t.Fatalf("%d %s", rec.Code, rec.Body.String()) } cfg, _, err := image.DecodeConfig(bytes.NewReader(rec.Body.Bytes())) if err != nil || cfg.Width != 1000 { t.Errorf("cover = %v %v, want 1000 wide", cfg, err) } // Stored: asked again, the archive is not. e.get("GET", path+"?size=small", e.token) if n := atomic.LoadInt32(&e.archive.calls); n != 1 { t.Errorf("the archive was asked %d times, want 1", n) } // The files of the user come first: the album that has a picture never asks the archive. e.get("GET", "/albums/"+id(e.withArt)+"/cover", e.token) if n := atomic.LoadInt32(&e.archive.calls); n != 1 { t.Errorf("the archive was asked about an album that has a picture in its files (%d calls)", n) } } func TestAnOutageOfTheArchiveIsABadGatewayNotAnInternalError(t *testing.T) { e := newCoversEnv(t) e.archive.err = images.ErrSourceUnavailable rec := e.get("GET", "/albums/"+id(e.without)+"/cover", e.token) var body struct { Error struct{ Code string } `json:"error"` } _ = json.Unmarshal(rec.Body.Bytes(), &body) if rec.Code != http.StatusBadGateway || body.Error.Code != "provider_unavailable" { t.Errorf("%d %s, want 502 provider_unavailable", rec.Code, rec.Body.String()) } } func TestACoverOfFilesThatCannotBeReadIsServiceUnavailableNotNoCover(t *testing.T) { e := newCoversEnv(t) if err := os.RemoveAll(e.root); err != nil { // the disk went away t.Fatal(err) } rec := e.get("GET", "/albums/"+id(e.without)+"/cover", e.token) var body struct { Error struct{ Code string } `json:"error"` } _ = json.Unmarshal(rec.Body.Bytes(), &body) if rec.Code != http.StatusServiceUnavailable || body.Error.Code != "files_unreadable" { t.Errorf("%d %s, want 503 files_unreadable", rec.Code, rec.Body.String()) } if n := atomic.LoadInt32(&e.archive.calls); n != 0 { t.Errorf("the archive was asked %d times about files that could not be read", n) } } func TestAnAppGetsCoversAndSongsOverRest(t *testing.T) { e := newCoversEnv(t) secret := do(t, e.h, "POST", "/api/v1/me/devices", e.token, map[string]any{"name": "Phone"}).Body["secret"].(string) get := func(path string, q url.Values) *httptest.ResponseRecorder { q.Set("apiKey", secret) rec := httptest.NewRecorder() e.h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+path+"?"+q.Encode(), nil)) return rec } // The cover, small and large, is the one the main API makes. for size, max := range map[string]int{"150": 300, "1200": 1000} { rec := get("getCoverArt", url.Values{"id": {"al-" + itoa(e.withArt)}, "size": {size}}) if rec.Code != 200 || rec.Header().Get("Content-Type") != "image/jpeg" { t.Fatalf("size %s: %d %s %s", size, rec.Code, rec.Header().Get("Content-Type"), rec.Body.String()) } img, err := jpeg.Decode(rec.Body) if err != nil || img.Bounds().Dx() > max || img.Bounds().Dx() < 100 { t.Fatalf("size %s: %v %v", size, err, img.Bounds()) } } if rec := get("getCoverArt", url.Values{"id": {"al-" + itoa(e.without)}, "f": {"json"}}); errorFrom(t, rec) != 70 { t.Fatalf("an album with no picture: %s", rec.Body.String()) } // The album, found by search, has a song that plays: the file as it is, with Range. res := get("search3", url.Values{"query": {"With Art"}, "f": {"json"}}) var found struct { R struct { S struct { Song []struct{ ID, Suffix, CoverArt string } `json:"song"` } `json:"searchResult3"` } `json:"subsonic-response"` } if err := json.Unmarshal(res.Body.Bytes(), &found); err != nil || len(found.R.S.Song) != 1 { t.Fatalf("search: %v %s", err, res.Body.String()) } song := found.R.S.Song[0] if song.Suffix != "mp3" || song.CoverArt != "al-"+itoa(e.withArt) { t.Fatalf("song %+v", song) } if rec := get("stream", url.Values{"id": {song.ID}}); rec.Code != 200 || rec.Body.String() != "not audio" || rec.Header().Get("Content-Type") != "audio/mpeg" { t.Fatalf("stream: %d %q %q", rec.Code, rec.Body.String(), rec.Header().Get("Content-Type")) } } func errorFrom(t *testing.T, rec *httptest.ResponseRecorder) int { t.Helper() var out struct { R map[string]any `json:"subsonic-response"` } if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { t.Fatalf("%v: %s", err, rec.Body.String()) } return errorCode(out.R) } func TestPlaylistsAndListensOfAnAppAreThoseOfThePerson(t *testing.T) { e := newCoversEnv(t) secret := do(t, e.h, "POST", "/api/v1/me/devices", e.token, map[string]any{"name": "Phone"}).Body["secret"].(string) var song int64 if err := e.db.QueryRow("SELECT id FROM tracks WHERE title = 'With Art song'").Scan(&song); err != nil { t.Fatal(err) } rest := func(path string, q url.Values) map[string]any { q.Set("apiKey", secret) q.Set("f", "json") rec := httptest.NewRecorder() e.h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+path+"?"+q.Encode(), nil)) var out struct { R map[string]any `json:"subsonic-response"` } if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || out.R["status"] != "ok" { t.Fatalf("%s: %v %s", path, err, rec.Body.String()) } return out.R } // A playlist made in the app is in the web app, and the other way round. made := rest("createPlaylist", url.Values{"name": {"From the phone"}, "songId": {"tr-" + itoa(song)}})["playlist"].(map[string]any) if made["songCount"].(float64) != 1 { t.Fatalf("made %v", made) } listed := do(t, e.h, "GET", "/api/v1/playlists", e.token, nil) items, _ := listed.Body["items"].([]any) if len(items) != 1 || items[0].(map[string]any)["name"] != "From the phone" { t.Fatalf("the web app sees %v", listed.Body) } web := do(t, e.h, "POST", "/api/v1/playlists", e.token, map[string]any{"name": "From the web"}) if web.Code != 201 { t.Fatalf("%d %v", web.Code, web.Body) } own := 0 // the stations of the radio are in the list too: only the playlists of the person are counted for _, p := range rest("getPlaylists", url.Values{})["playlists"].(map[string]any)["playlist"].([]any) { if strings.HasPrefix(p.(map[string]any)["id"].(string), "pl-") { own++ } } if n := own; n != 2 { t.Fatalf("the app sees %d playlists", n) } rest("deletePlaylist", url.Values{"id": {made["id"].(string)}}) if n := len(do(t, e.h, "GET", "/api/v1/playlists", e.token, nil).Body["items"].([]any)); n != 1 { t.Fatalf("after delete the web app sees %d", n) } // A listen reported by the app is in the history of the person, once. for i := 0; i < 2; i++ { rest("scrobble", url.Values{"id": {"tr-" + itoa(song)}, "time": {"1700000000000"}}) } hist := do(t, e.h, "GET", "/api/v1/listens", e.token, nil) if got := hist.Body["total"]; got != float64(1) { t.Fatalf("history %v", hist.Body) } } func TestAStarInAnAppIsAFavouriteInTheWebApp(t *testing.T) { e := newCoversEnv(t) secret := do(t, e.h, "POST", "/api/v1/me/devices", e.token, map[string]any{"name": "Phone"}).Body["secret"].(string) var song int64 _ = e.db.QueryRow("SELECT id FROM tracks WHERE title = 'With Art song'").Scan(&song) rest := func(path string, q url.Values) map[string]any { q.Set("apiKey", secret) q.Set("f", "json") rec := httptest.NewRecorder() e.h.ServeHTTP(rec, httptest.NewRequest("GET", "/rest/"+path+"?"+q.Encode(), nil)) var out struct { R map[string]any `json:"subsonic-response"` } if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil || out.R["status"] != "ok" { t.Fatalf("%s: %v %s", path, err, rec.Body.String()) } return out.R } rest("star", url.Values{"id": {"tr-" + itoa(song)}, "albumId": {"al-" + itoa(e.withArt)}}) ids := do(t, e.h, "GET", "/api/v1/me/favorites/ids", e.token, nil).Body if len(ids["tracks"].([]any)) != 1 || len(ids["albums"].([]any)) != 1 { t.Fatalf("the web app sees %v", ids) } if n := len(rest("getAlbumList2", url.Values{"type": {"starred"}})["albumList2"].(map[string]any)["album"].([]any)); n != 1 { t.Fatalf("starred albums %d", n) } // And the other way round: a favourite made in the web app is starred in the app. do(t, e.h, "DELETE", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil) do(t, e.h, "PUT", "/api/v1/me/favorites/tracks/"+itoa(song), e.token, nil) st := rest("getStarred2", url.Values{})["starred2"].(map[string]any) if len(st["song"].([]any)) != 1 || st["song"].([]any)[0].(map[string]any)["starred"] == nil { t.Fatalf("starred %v", st) } rest("unstar", url.Values{"id": {"tr-" + itoa(song)}}) if got := do(t, e.h, "GET", "/api/v1/me/favorites/ids", e.token, nil).Body["tracks"].([]any); len(got) != 0 { t.Fatalf("after unstar %v", got) } }