package httpapi import ( "bytes" "context" "encoding/json" "image" "image/color" "image/jpeg" "net/http" "net/http/httptest" "os" "path/filepath" "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" ) 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 }) 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) } }