quintodrome/server/e2e/subsonic_media_annotation_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

272 lines
7.5 KiB
Go
Raw Normal View History

test(subsonic): add comprehensive e2e test suite for Subsonic API (#5003) * test(e2e): add comprehensive tests for Subsonic API endpoints Signed-off-by: Deluan <deluan@navidrome.org> * fix(e2e): improve database handling and snapshot restoration in tests Signed-off-by: Deluan <deluan@navidrome.org> * test(e2e): add tests for album sharing and user isolation scenarios Signed-off-by: Deluan <deluan@navidrome.org> * test(e2e): add tests for multi-library support and user access control Signed-off-by: Deluan <deluan@navidrome.org> * test(e2e): tests are fast, no need to skip on -short Signed-off-by: Deluan <deluan@navidrome.org> * address gemini comments Signed-off-by: Deluan <deluan@navidrome.org> * fix(tests): prevent MockDataStore from caching repos with stale context When RealDS is set, MockDataStore previously cached repository instances on first access, binding them to the initial caller's context. This meant repos created with an admin context would skip library filtering for all subsequent non-admin calls, silently masking access control bugs. Changed MockDataStore to delegate to RealDS on every call without caching, so each caller gets a fresh repo with the correct context. Removed the pre-warm calls in e2e setupTestDB that were working around the old caching behavior. * test(e2e): route subsonic tests through full HTTP middleware stack Replace direct router method calls with full HTTP round-trips via router.ServeHTTP(w, r) across all 15 e2e test files. Tests now exercise the complete chi middleware chain including postFormToQueryParams, checkRequiredParameters, authenticate, UpdateLastAccessMiddleware, getPlayer, and sendResponse/sendError serialization. New helpers (doReq, doReqWithUser, doRawReq, buildReq, parseJSONResponse) use plaintext password auth and JSON response format. Old helpers that injected context directly (newReq, newReqWithUser, newRawReq) are removed. Sharing tests now set conf.Server.EnableSharing before router creation to ensure sharing routes are registered. --------- Signed-off-by: Deluan <deluan@navidrome.org>
2026-02-09 04:24:37 -09:00
package e2e
import (
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/subsonic/responses"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Media Annotation Endpoints", Ordered, func() {
BeforeAll(func() {
setupTestDB()
})
Describe("Star/Unstar", Ordered, func() {
var songID, albumID, artistID string
BeforeAll(func() {
// Look up a song from the scanned data
songs, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{Max: 1, Sort: "title"})
Expect(err).ToNot(HaveOccurred())
Expect(songs).ToNot(BeEmpty())
songID = songs[0].ID
// Look up an album
albums, err := ds.Album(ctx).GetAll(model.QueryOptions{Max: 1, Sort: "name"})
Expect(err).ToNot(HaveOccurred())
Expect(albums).ToNot(BeEmpty())
albumID = albums[0].ID
// Look up an artist
artists, err := ds.Artist(ctx).GetAll(model.QueryOptions{Max: 1, Sort: "name"})
Expect(err).ToNot(HaveOccurred())
Expect(artists).ToNot(BeEmpty())
artistID = artists[0].ID
})
It("stars a song by id", func() {
resp := doReq("star", "id", songID)
Expect(resp.Status).To(Equal(responses.StatusOK))
})
It("starred song appears in getStarred response", func() {
resp := doReq("getStarred")
Expect(resp.Status).To(Equal(responses.StatusOK))
Expect(resp.Starred).ToNot(BeNil())
Expect(resp.Starred.Song).To(HaveLen(1))
Expect(resp.Starred.Song[0].Id).To(Equal(songID))
})
It("unstars a previously starred song", func() {
resp := doReq("unstar", "id", songID)
Expect(resp.Status).To(Equal(responses.StatusOK))
// Verify song no longer appears in starred
resp = doReq("getStarred")
Expect(resp.Starred.Song).To(BeEmpty())
})
It("stars an album by albumId", func() {
resp := doReq("star", "albumId", albumID)
Expect(resp.Status).To(Equal(responses.StatusOK))
// Verify album appears in starred
resp = doReq("getStarred")
Expect(resp.Starred.Album).To(HaveLen(1))
Expect(resp.Starred.Album[0].Id).To(Equal(albumID))
})
It("stars an artist by artistId", func() {
resp := doReq("star", "artistId", artistID)
Expect(resp.Status).To(Equal(responses.StatusOK))
// Verify artist appears in starred
resp = doReq("getStarred")
Expect(resp.Starred.Artist).To(HaveLen(1))
Expect(resp.Starred.Artist[0].Id).To(Equal(artistID))
})
It("returns error when no id provided", func() {
resp := doReq("star")
Expect(resp.Status).To(Equal(responses.StatusFailed))
Expect(resp.Error).ToNot(BeNil())
})
})
Describe("SetRating", Ordered, func() {
var songID, albumID string
BeforeAll(func() {
songs, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{Max: 1, Sort: "title"})
Expect(err).ToNot(HaveOccurred())
Expect(songs).ToNot(BeEmpty())
songID = songs[0].ID
albums, err := ds.Album(ctx).GetAll(model.QueryOptions{Max: 1, Sort: "name"})
Expect(err).ToNot(HaveOccurred())
Expect(albums).ToNot(BeEmpty())
albumID = albums[0].ID
})
It("sets rating on a song", func() {
resp := doReq("setRating", "id", songID, "rating", "4")
Expect(resp.Status).To(Equal(responses.StatusOK))
})
It("rated song has correct userRating in getSong", func() {
resp := doReq("getSong", "id", songID)
Expect(resp.Status).To(Equal(responses.StatusOK))
Expect(resp.Song).ToNot(BeNil())
Expect(resp.Song.UserRating).To(Equal(int32(4)))
})
It("sets rating on an album", func() {
resp := doReq("setRating", "id", albumID, "rating", "3")
Expect(resp.Status).To(Equal(responses.StatusOK))
})
It("returns error for missing parameters", func() {
// Missing both id and rating
resp := doReq("setRating")
Expect(resp.Status).To(Equal(responses.StatusFailed))
// Missing rating
resp = doReq("setRating", "id", songID)
Expect(resp.Status).To(Equal(responses.StatusFailed))
})
})
Describe("Scrobble", func() {
It("submits a scrobble for a song", func() {
songs, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{Max: 1, Sort: "title"})
Expect(err).ToNot(HaveOccurred())
Expect(songs).ToNot(BeEmpty())
resp := doReq("scrobble", "id", songs[0].ID, "submission", "true")
Expect(resp.Status).To(Equal(responses.StatusOK))
})
It("returns error when id is missing", func() {
resp := doReq("scrobble")
Expect(resp.Status).To(Equal(responses.StatusFailed))
Expect(resp.Error).ToNot(BeNil())
})
})
feat(subsonic): implement playbackReport OpenSubsonic extension (#5442) * feat(req): add Float64Or helper for parsing float query params * feat(scrobbler): extend NowPlayingInfo with state/position/rate fields * feat(scrobbler): implement ReportPlayback with state machine and auto-scrobble * feat(responses): add state/positionMs/playbackRate to NowPlayingEntry * feat(subsonic): add reportPlayback endpoint handler * feat(subsonic): include state/positionMs/playbackRate in getNowPlaying response * feat(subsonic): register playbackReport OpenSubsonic extension * test(e2e): add reportPlayback endpoint e2e tests * refactor(scrobbler): simplify ReportPlayback — extract helpers, remove duplication - Add state constants and exported ValidStates map - Extract remainingTTL() helper (was duplicated 3x) - Merge playing/paused switch cases into single branch - Use Get instead of GetWithParticipants for non-stopped states - Guard NowPlayingCount broadcast with count-change detection - Use cache entry for NowPlaying dispatch instead of extra DB query - Remove redundant Position field from NowPlayingInfo * refactor(scrobbler): skip DB query in playing/paused when playMap has entry * fix(play_tracker): handle errors when adding/updating NowPlayingInfo in cache Signed-off-by: Deluan <deluan@navidrome.org> * refactor(play_tracker): replace sort with slices.SortFunc for NowPlayingInfo Signed-off-by: Deluan <deluan@navidrome.org> * fix(play_tracker): check all ReportPlayback errors in tests Replace _ = with explicit error assertions to avoid masking failures in intermediate calls. Signed-off-by: Deluan <deluan@navidrome.org> * test(e2e): use real PlayTracker and assert getNowPlaying after reportPlayback Replace noopPlayTracker with a real PlayTracker backed by the E2E database. E2E tests now verify the full round-trip: reportPlayback creates/updates/removes entries visible via getNowPlaying, including state, positionMs, and playbackRate fields. Export NewPlayTracker constructor for use outside the scrobbler package. * fix(play_tracker): account for playback rate in TTL and detect track switches The remainingTTL function now divides remaining time by the playback rate, so cache entries expire correctly at non-1x speeds (e.g., 2x playback halves the TTL). Zero/negative rates default to 1.0. The playing/paused case now checks if the cached MediaFile ID matches the reported mediaId, falling back to a DB fetch when the client switches tracks without sending stopped/starting. Adds parameterized tests for remainingTTL covering rate variations and edge cases. * fix(subsonic): validate positionMs and playbackRate in reportPlayback Reject negative positionMs values and invalid playbackRate values (NaN, Inf, zero, negative) at the API boundary before they reach TTL and position estimation math. Returns clear error messages for each case. * feat(play_tracker): add ClientId and ClientName to ReportPlayback parameters Signed-off-by: Deluan <deluan@navidrome.org> * refactor(play_tracker): replace NowPlaying method with ReportPlayback calls Signed-off-by: Deluan <deluan@navidrome.org> * refactor(play_tracker_test): remove redundant TTL behavior tests and clean up mockPluginLoader Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
2026-04-30 19:04:05 -08:00
Describe("ReportPlayback", Ordered, func() {
var songID string
BeforeAll(func() {
songs, err := ds.MediaFile(ctx).GetAll(model.QueryOptions{Max: 1, Sort: "title"})
Expect(err).ToNot(HaveOccurred())
Expect(songs).ToNot(BeEmpty())
songID = songs[0].ID
})
It("returns error when required params are missing", func() {
resp := doReq("reportPlayback")
Expect(resp.Status).To(Equal(responses.StatusFailed))
})
It("returns error for invalid state", func() {
resp := doReq("reportPlayback",
"mediaId", songID,
"mediaType", "song",
"positionMs", "0",
"state", "invalid",
)
Expect(resp.Status).To(Equal(responses.StatusFailed))
})
It("starting report creates a getNowPlaying entry", func() {
resp := doReq("reportPlayback",
"mediaId", songID,
"mediaType", "song",
"positionMs", "0",
"state", "starting",
)
Expect(resp.Status).To(Equal(responses.StatusOK))
np := doReq("getNowPlaying")
Expect(np.Status).To(Equal(responses.StatusOK))
Expect(np.NowPlaying.Entry).To(HaveLen(1))
Expect(np.NowPlaying.Entry[0].Id).To(Equal(songID))
Expect(np.NowPlaying.Entry[0].State).To(Equal("starting"))
})
It("playing report updates getNowPlaying state and position", func() {
resp := doReq("reportPlayback",
"mediaId", songID,
"mediaType", "song",
"positionMs", "30000",
"state", "playing",
)
Expect(resp.Status).To(Equal(responses.StatusOK))
np := doReq("getNowPlaying")
Expect(np.NowPlaying.Entry).To(HaveLen(1))
Expect(np.NowPlaying.Entry[0].State).To(Equal("playing"))
Expect(np.NowPlaying.Entry[0].PositionMs).To(BeNumerically(">=", int64(30000)))
})
It("paused report freezes position in getNowPlaying", func() {
resp := doReq("reportPlayback",
"mediaId", songID,
"mediaType", "song",
"positionMs", "30000",
"state", "paused",
)
Expect(resp.Status).To(Equal(responses.StatusOK))
np := doReq("getNowPlaying")
Expect(np.NowPlaying.Entry).To(HaveLen(1))
Expect(np.NowPlaying.Entry[0].State).To(Equal("paused"))
Expect(np.NowPlaying.Entry[0].PositionMs).To(Equal(int64(30000)))
})
It("stopped report removes entry from getNowPlaying", func() {
resp := doReq("reportPlayback",
"mediaId", songID,
"mediaType", "song",
"positionMs", "90000",
"state", "stopped",
)
Expect(resp.Status).To(Equal(responses.StatusOK))
np := doReq("getNowPlaying")
Expect(np.NowPlaying.Entry).To(BeEmpty())
})
It("accepts mediaType=podcast without error", func() {
resp := doReq("reportPlayback",
"mediaId", songID,
"mediaType", "podcast",
"positionMs", "0",
"state", "starting",
)
Expect(resp.Status).To(Equal(responses.StatusOK))
})
It("accepts optional playbackRate and ignoreScrobble", func() {
resp := doReq("reportPlayback",
"mediaId", songID,
"mediaType", "song",
"positionMs", "5000",
"state", "playing",
"playbackRate", "1.5",
"ignoreScrobble", "true",
)
Expect(resp.Status).To(Equal(responses.StatusOK))
np := doReq("getNowPlaying")
Expect(np.NowPlaying.Entry).To(HaveLen(1))
Expect(np.NowPlaying.Entry[0].PlaybackRate).To(Equal(1.5))
})
})
test(subsonic): add comprehensive e2e test suite for Subsonic API (#5003) * test(e2e): add comprehensive tests for Subsonic API endpoints Signed-off-by: Deluan <deluan@navidrome.org> * fix(e2e): improve database handling and snapshot restoration in tests Signed-off-by: Deluan <deluan@navidrome.org> * test(e2e): add tests for album sharing and user isolation scenarios Signed-off-by: Deluan <deluan@navidrome.org> * test(e2e): add tests for multi-library support and user access control Signed-off-by: Deluan <deluan@navidrome.org> * test(e2e): tests are fast, no need to skip on -short Signed-off-by: Deluan <deluan@navidrome.org> * address gemini comments Signed-off-by: Deluan <deluan@navidrome.org> * fix(tests): prevent MockDataStore from caching repos with stale context When RealDS is set, MockDataStore previously cached repository instances on first access, binding them to the initial caller's context. This meant repos created with an admin context would skip library filtering for all subsequent non-admin calls, silently masking access control bugs. Changed MockDataStore to delegate to RealDS on every call without caching, so each caller gets a fresh repo with the correct context. Removed the pre-warm calls in e2e setupTestDB that were working around the old caching behavior. * test(e2e): route subsonic tests through full HTTP middleware stack Replace direct router method calls with full HTTP round-trips via router.ServeHTTP(w, r) across all 15 e2e test files. Tests now exercise the complete chi middleware chain including postFormToQueryParams, checkRequiredParameters, authenticate, UpdateLastAccessMiddleware, getPlayer, and sendResponse/sendError serialization. New helpers (doReq, doReqWithUser, doRawReq, buildReq, parseJSONResponse) use plaintext password auth and JSON response format. Old helpers that injected context directly (newReq, newReqWithUser, newRawReq) are removed. Sharing tests now set conf.Server.EnableSharing before router creation to ensure sharing routes are registered. --------- Signed-off-by: Deluan <deluan@navidrome.org>
2026-02-09 04:24:37 -09:00
})