release / go (push) Successful in 2m3s
release / web (push) Successful in 1m29s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 3m29s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Publish the release image to GHCR (dispatch only) (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / android (push) Canceled after 3m28s
release / Build signed APK (push) Canceled after 3m3s
The access tests checked hand-built routers that applied RequireAdmin themselves, so none could catch a production route mounted outside its group. One test now walks every route Mount registers: each non-public route answers 401 without a session, and each admin route 403 to a listener. The per-endpoint malformed-id (400) and unknown-id (404) tests become one table over the real routes. Profile updates, playlist kind filters and empty searches each fold into one table; writeErr's envelope tests move to apierror.Write, which they were testing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
113 lines
3.4 KiB
Go
113 lines
3.4 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestHandleSearch_ThreeFacets(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
artist := seedArtist(t, pool, "Copper Lanterns")
|
|
album := seedAlbum(t, pool, artist.ID, "Copper Lanterns Greatest", 1982)
|
|
seedTrack(t, pool, album.ID, artist.ID, "Copper Lanterns Jam", 1, 60_000)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/search?q=copper", nil)
|
|
w := httptest.NewRecorder()
|
|
newLibraryRouter(h).ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d body = %s", w.Code, w.Body.String())
|
|
}
|
|
var got SearchResponse
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if got.Artists.Total != 1 || len(got.Artists.Items) != 1 {
|
|
t.Errorf("artists = %+v", got.Artists)
|
|
}
|
|
if got.Albums.Total != 1 || len(got.Albums.Items) != 1 {
|
|
t.Errorf("albums = %+v", got.Albums)
|
|
}
|
|
if got.Tracks.Total != 1 || len(got.Tracks.Items) != 1 {
|
|
t.Errorf("tracks = %+v", got.Tracks)
|
|
}
|
|
if got.Artists.Items[0].Name != "Copper Lanterns" {
|
|
t.Errorf("artist name = %q", got.Artists.Items[0].Name)
|
|
}
|
|
if got.Albums.Items[0].ArtistName != "Copper Lanterns" {
|
|
t.Errorf("album.artist_name = %q", got.Albums.Items[0].ArtistName)
|
|
}
|
|
if got.Tracks.Items[0].AlbumTitle != "Copper Lanterns Greatest" {
|
|
t.Errorf("track.album_title = %q", got.Tracks.Items[0].AlbumTitle)
|
|
}
|
|
}
|
|
|
|
func TestHandleSearch_NoMatches(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
seedArtist(t, pool, "Copper Lanterns")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/search?q=nothinghere", nil)
|
|
w := httptest.NewRecorder()
|
|
newLibraryRouter(h).ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", w.Code)
|
|
}
|
|
var got SearchResponse
|
|
_ = json.Unmarshal(w.Body.Bytes(), &got)
|
|
if got.Artists.Total != 0 || got.Albums.Total != 0 || got.Tracks.Total != 0 {
|
|
t.Errorf("totals = %+v", got)
|
|
}
|
|
// All three item slices must JSON-encode as [] not null.
|
|
if got.Artists.Items == nil || got.Albums.Items == nil || got.Tracks.Items == nil {
|
|
t.Errorf("nil item slice: %+v", got)
|
|
}
|
|
}
|
|
|
|
// A search with no query, or only spaces, is a 400.
|
|
func TestHandleSearch_AnEmptyQueryIs400(t *testing.T) {
|
|
h, _ := testHandlers(t)
|
|
for _, path := range []string{"/api/search", "/api/search?q=%20%20"} {
|
|
w := httptest.NewRecorder()
|
|
newLibraryRouter(h).ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Errorf("%s: status = %d, want 400", path, w.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHandleSearch_PagingAppliedPerFacet(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
// Create three matching artists so paging shows.
|
|
a1 := seedArtist(t, pool, "Match One")
|
|
a2 := seedArtist(t, pool, "Match Two")
|
|
a3 := seedArtist(t, pool, "Match Three")
|
|
_ = a1
|
|
_ = a2
|
|
_ = a3
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/search?q=Match&limit=2&offset=1", nil)
|
|
w := httptest.NewRecorder()
|
|
newLibraryRouter(h).ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status = %d", w.Code)
|
|
}
|
|
var got SearchResponse
|
|
_ = json.Unmarshal(w.Body.Bytes(), &got)
|
|
if got.Artists.Total != 3 {
|
|
t.Errorf("total = %d, want 3", got.Artists.Total)
|
|
}
|
|
if got.Artists.Limit != 2 || got.Artists.Offset != 1 {
|
|
t.Errorf("page = limit=%d offset=%d", got.Artists.Limit, got.Artists.Offset)
|
|
}
|
|
if len(got.Artists.Items) != 2 {
|
|
t.Errorf("items len = %d, want 2", len(got.Artists.Items))
|
|
}
|
|
}
|