test: replace shared repository fakes with a real per-suite database (#6519)

This commit is contained in:
Deluan Quintão authored and GitHub committed 2026-11-22 15:41:43 -05:00
1 parent 71553644ed
commit f78929da2e
195 files changed
+7059 -10969

No files matched your search

+151 -83
View File
@@ -2,34 +2,103 @@ package nativeapi
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/tests"
"github.com/navidrome/navidrome/persistence"
"github.com/navidrome/navidrome/persistence/persistencetest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// queueFaults makes the play queue repository fail on demand, and records the columns of the last Store.
type queueFaults struct {
lastCols []string
storeErr error
retrieveErr error
withMediaFilesErr error
clearErr error
}
type faultyPlayQueue struct {
model.PlayQueueRepository
*queueFaults
}
func (q faultyPlayQueue) Store(ctx context.Context, pq *model.PlayQueue, cols ...string) error {
if q.storeErr != nil {
return q.storeErr
}
q.lastCols = cols
return q.PlayQueueRepository.Store(ctx, pq, cols...)
}
func (q faultyPlayQueue) RetrieveWithMediaFiles(ctx context.Context, userID string) (*model.PlayQueue, error) {
if q.retrieveErr != nil {
return nil, q.retrieveErr
}
pq, err := q.PlayQueueRepository.RetrieveWithMediaFiles(ctx, userID)
if err == nil {
err = q.withMediaFilesErr
}
return pq, err
}
func (q faultyPlayQueue) Clear(ctx context.Context, userID string) error {
if q.clearErr != nil {
return q.clearErr
}
return q.PlayQueueRepository.Clear(ctx, userID)
}
var _ = Describe("Queue Endpoints", func() {
var (
ds *tests.MockDataStore
repo *tests.MockPlayQueueRepo
user model.User
userRepo *tests.MockedUserRepo
ctx context.Context
realDS model.DataStore
ds model.DataStore
faults *queueFaults
user model.User
)
seed := func(pq *model.PlayQueue) {
GinkgoHelper()
pq.UserID = user.ID
Expect(realDS.PlayQueue().Store(ctx, pq, "current", "position", "items")).To(Succeed())
}
stored := func() *model.PlayQueue {
GinkgoHelper()
pq, err := realDS.PlayQueue().Retrieve(ctx, user.ID)
Expect(err).ToNot(HaveOccurred())
return pq
}
expectNoQueue := func() {
GinkgoHelper()
_, err := realDS.PlayQueue().Retrieve(ctx, user.ID)
Expect(err).To(MatchError(model.ErrNotFound))
}
BeforeEach(func() {
repo = &tests.MockPlayQueueRepo{}
ctx = GinkgoT().Context()
realDS = persistence.New(db.Db())
faults = &queueFaults{}
ds = persistencetest.Stub(realDS, persistencetest.Repos{PlayQueue: func(r model.PlayQueueRepository) model.PlayQueueRepository {
return faultyPlayQueue{r, faults}
}})
user = model.User{ID: "u1", UserName: "user"}
userRepo = tests.CreateMockUserRepo()
_ = userRepo.Put(GinkgoT().Context(), &user)
ds = &tests.MockDataStore{MockedPlayQueue: repo, MockedUser: userRepo, MockedProperty: &tests.MockedPropertyRepo{}}
putUsers(ctx, realDS, &user)
for _, id := range []string{"s1", "s2", "s3", "s4", "track1", "track2", "track3"} {
mf := &model.MediaFile{ID: id, LibraryID: model.DefaultLibraryID, Path: id + ".mp3", Title: id}
Expect(realDS.MediaFile().Put(ctx, mf)).To(Succeed())
}
})
DescribeTable("rejects a body over the size limit with 413 without saving",
@@ -41,7 +110,7 @@ var _ = Describe("Queue Endpoints", func() {
handler(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusRequestEntityTooLarge))
Expect(repo.Queue).To(BeNil())
expectNoQueue()
},
Entry("POST", http.MethodPost, saveQueue),
Entry("PUT", http.MethodPut, updateQueue),
@@ -56,7 +125,7 @@ var _ = Describe("Queue Endpoints", func() {
saveQueue(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Items).To(HaveLen(1))
Expect(stored().Items).To(HaveLen(1))
})
Describe("POST /queue", func() {
@@ -71,14 +140,15 @@ var _ = Describe("Queue Endpoints", func() {
saveQueue(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusNoContent))
Expect(repo.Queue).ToNot(BeNil())
Expect(repo.Queue.Current).To(Equal(1))
Expect(repo.Queue.Items).To(HaveLen(2))
Expect(repo.Queue.Items[1].ID).To(Equal("s2"))
Expect(repo.Queue.ChangedBy).To(Equal("TestClient"))
pq := stored()
Expect(pq.Current).To(Equal(1))
Expect(pq.Items).To(HaveLen(2))
Expect(pq.Items[1].ID).To(Equal("s2"))
Expect(pq.ChangedBy).To(Equal("TestClient"))
})
It("saves an empty queue", func() {
seed(&model.PlayQueue{Items: model.MediaFiles{{ID: "s1"}}})
payload := updateQueuePayload{Ids: new([]string{}), Current: new(0), Position: new(int64(0))}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPost, "/queue", bytes.NewReader(body))
@@ -87,8 +157,7 @@ var _ = Describe("Queue Endpoints", func() {
saveQueue(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusNoContent))
Expect(repo.Queue).ToNot(BeNil())
Expect(repo.Queue.Items).To(BeEmpty())
expectNoQueue()
})
It("returns bad request for invalid current index (negative)", func() {
@@ -125,7 +194,7 @@ var _ = Describe("Queue Endpoints", func() {
})
It("returns internal server error when store fails", func() {
repo.Err = true
faults.storeErr = errors.New("db unavailable")
payload := updateQueuePayload{Ids: new([]string{"s1"}), Current: new(0), Position: new(int64(10))}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPost, "/queue", bytes.NewReader(body))
@@ -139,17 +208,11 @@ var _ = Describe("Queue Endpoints", func() {
Describe("GET /queue", func() {
It("returns the queue", func() {
queue := &model.PlayQueue{
UserID: user.ID,
seed(&model.PlayQueue{
Current: 1,
Position: 55,
Items: model.MediaFiles{
{ID: "track1", Title: "Song 1"},
{ID: "track2", Title: "Song 2"},
{ID: "track3", Title: "Song 3"},
},
}
repo.Queue = queue
Items: model.MediaFiles{{ID: "track1"}, {ID: "track2"}, {ID: "track3"}},
})
req := httptest.NewRequest(http.MethodGet, "/queue", nil)
req = req.WithContext(request.WithUser(req.Context(), user))
w := httptest.NewRecorder()
@@ -163,6 +226,7 @@ var _ = Describe("Queue Endpoints", func() {
Expect(resp.Position).To(Equal(int64(55)))
Expect(resp.Items).To(HaveLen(3))
Expect(resp.Items[0].ID).To(Equal("track1"))
Expect(resp.Items[0].Title).To(Equal("track1"))
Expect(resp.Items[1].ID).To(Equal("track2"))
Expect(resp.Items[2].ID).To(Equal("track3"))
})
@@ -182,8 +246,8 @@ var _ = Describe("Queue Endpoints", func() {
})
It("returns the loaded tracks when some of them fail to load", func() {
repo.Queue = &model.PlayQueue{UserID: user.ID, Items: model.MediaFiles{{ID: "track1"}}}
repo.WithMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
seed(&model.PlayQueue{Items: model.MediaFiles{{ID: "track1"}}})
faults.withMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
req := httptest.NewRequest(http.MethodGet, "/queue", nil)
req = req.WithContext(request.WithUser(req.Context(), user))
w := httptest.NewRecorder()
@@ -196,7 +260,7 @@ var _ = Describe("Queue Endpoints", func() {
})
It("returns internal server error when retrieve fails", func() {
repo.Err = true
faults.retrieveErr = errors.New("db unavailable")
req := httptest.NewRequest(http.MethodGet, "/queue", nil)
req = req.WithContext(request.WithUser(req.Context(), user))
w := httptest.NewRecorder()
@@ -207,8 +271,9 @@ var _ = Describe("Queue Endpoints", func() {
})
Describe("PUT /queue", func() {
It("updates the queue fields", func() {
repo.Queue = &model.PlayQueue{UserID: user.ID, Items: model.MediaFiles{{ID: "s1"}, {ID: "s2"}, {ID: "s3"}}}
updateFieldsAsTestClient := func() {
GinkgoHelper()
seed(&model.PlayQueue{Items: model.MediaFiles{{ID: "s1"}, {ID: "s2"}, {ID: "s3"}}})
payload := updateQueuePayload{Current: new(2), Position: new(int64(20))}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPut, "/queue", bytes.NewReader(body))
@@ -219,14 +284,21 @@ var _ = Describe("Queue Endpoints", func() {
updateQueue(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusNoContent))
Expect(repo.Queue).ToNot(BeNil())
Expect(repo.Queue.Current).To(Equal(2))
Expect(repo.Queue.Position).To(Equal(int64(20)))
Expect(repo.Queue.ChangedBy).To(Equal("TestClient"))
}
It("updates the queue fields", func() {
updateFieldsAsTestClient()
Expect(stored().Current).To(Equal(2))
Expect(stored().Position).To(Equal(int64(20)))
})
PIt("records the client that updated the queue", func() {
updateFieldsAsTestClient()
Expect(stored().ChangedBy).To(Equal("TestClient"))
})
It("updates only ids", func() {
repo.Queue = &model.PlayQueue{UserID: user.ID, Current: 1}
seed(&model.PlayQueue{Current: 1})
payload := updateQueuePayload{Ids: new([]string{"s1", "s2"})}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPut, "/queue", bytes.NewReader(body))
@@ -235,16 +307,14 @@ var _ = Describe("Queue Endpoints", func() {
updateQueue(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Items).To(HaveLen(2))
Expect(repo.LastCols).To(ConsistOf("items"))
Expect(stored().Items).To(HaveLen(2))
Expect(faults.lastCols).To(ConsistOf("items"))
})
It("updates only ids when some tracks fail to load", func() {
repo.Queue = &model.PlayQueue{UserID: user.ID, Current: 1, Position: 45,
Items: model.MediaFiles{{ID: "s1"}, {ID: "s2"}, {ID: "s3"}}}
repo.QueueWithMediaFiles = &model.PlayQueue{UserID: user.ID, Current: 1, Position: 45,
Items: model.MediaFiles{{ID: "s1"}, {ID: "s2"}}}
repo.WithMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
seed(&model.PlayQueue{Current: 1, Position: 45,
Items: model.MediaFiles{{ID: "s1"}, {ID: "s2"}, {ID: "s3"}}})
faults.withMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
body, _ := json.Marshal(updateQueuePayload{Ids: new([]string{"s1", "s2", "s3", "s4"})})
req := httptest.NewRequest(http.MethodPut, "/queue", bytes.NewReader(body))
req = req.WithContext(request.WithUser(req.Context(), user))
@@ -252,17 +322,15 @@ var _ = Describe("Queue Endpoints", func() {
updateQueue(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Items).To(HaveLen(4))
Expect(repo.LastCols).To(ConsistOf("items"))
Expect(stored().Items).To(HaveLen(4))
Expect(faults.lastCols).To(ConsistOf("items"))
})
Context("when the stored queue has tracks the user can't see", func() {
storedItems := model.MediaFiles{{ID: "gone1"}, {ID: "s1"}, {ID: "gone2"}, {ID: "s2"}}
BeforeEach(func() {
repo.Queue = &model.PlayQueue{UserID: user.ID, Current: 2, Position: 45,
Items: model.MediaFiles{{ID: "gone1"}, {ID: "s1"}, {ID: "gone2"}, {ID: "s2"}}}
repo.QueueWithMediaFiles = &model.PlayQueue{UserID: user.ID, Current: 1, Position: 0,
Items: model.MediaFiles{{ID: "s1"}, {ID: "s2"}}}
ds.MediaFile().(*tests.MockMediaFileRepo).SetData(repo.QueueWithMediaFiles.Items)
seed(&model.PlayQueue{Current: 2, Position: 45, Items: storedItems})
})
put := func(payload updateQueuePayload) int {
@@ -276,8 +344,8 @@ var _ = Describe("Queue Endpoints", func() {
It("stores current as the index of the same track in the stored queue", func() {
Expect(put(updateQueuePayload{Current: new(1)})).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Current).To(Equal(3))
Expect(repo.LastCols).To(ConsistOf("current"))
Expect(stored().Current).To(Equal(3))
Expect(faults.lastCols).To(ConsistOf("current"))
})
It("rejects a current that is only valid for the stored queue", func() {
@@ -286,50 +354,49 @@ var _ = Describe("Queue Endpoints", func() {
It("moves current to the track the client saw when only position changes", func() {
Expect(put(updateQueuePayload{Position: new(int64(30))})).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Current).To(Equal(3))
Expect(repo.Queue.Position).To(Equal(int64(30)))
Expect(repo.LastCols).To(ConsistOf("current", "position"))
Expect(stored().Current).To(Equal(3))
Expect(stored().Position).To(Equal(int64(30)))
Expect(faults.lastCols).To(ConsistOf("current", "position"))
})
It("writes only position when the stored current track is visible", func() {
repo.Queue.Current = 1
repo.QueueWithMediaFiles.Current = 0
seed(&model.PlayQueue{Current: 1, Position: 45, Items: storedItems})
Expect(put(updateQueuePayload{Position: new(int64(30))})).To(Equal(http.StatusNoContent))
Expect(repo.LastCols).To(ConsistOf("position"))
Expect(stored().Current).To(Equal(1))
Expect(faults.lastCols).To(ConsistOf("position"))
})
It("refuses to update current when the tracks fail to load", func() {
repo.WithMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
faults.withMediaFilesErr = fmt.Errorf("%w: chunk failed", model.ErrIncompleteQueue)
Expect(put(updateQueuePayload{Current: new(0)})).To(Equal(http.StatusInternalServerError))
Expect(repo.LastCols).To(BeNil())
Expect(repo.Queue.Current).To(Equal(2))
Expect(faults.lastCols).To(BeNil())
Expect(stored().Current).To(Equal(2))
})
It("keeps the current track and position the client saw when only ids change", func() {
Expect(put(updateQueuePayload{Ids: new([]string{"s1", "s2", "s3"})})).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Current).To(Equal(1))
Expect(repo.Queue.Position).To(BeZero())
Expect(repo.LastCols).To(ConsistOf("items", "current", "position"))
Expect(stored().Current).To(Equal(1))
Expect(stored().Position).To(BeZero())
Expect(faults.lastCols).To(ConsistOf("items", "current", "position"))
})
It("writes only the current index when the current track is visible and only ids change", func() {
repo.Queue.Current = 1
repo.QueueWithMediaFiles.Current = 0
repo.QueueWithMediaFiles.Position = 45
seed(&model.PlayQueue{Current: 1, Position: 45, Items: storedItems})
Expect(put(updateQueuePayload{Ids: new([]string{"s1", "s2"})})).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Current).To(BeZero())
Expect(repo.LastCols).To(ConsistOf("items", "current"))
Expect(stored().Current).To(BeZero())
Expect(stored().Position).To(Equal(int64(45)))
Expect(faults.lastCols).To(ConsistOf("items", "current"))
})
It("uses the position from the request when ids and position change", func() {
Expect(put(updateQueuePayload{Ids: new([]string{"s1", "s2"}), Position: new(int64(10))})).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Current).To(Equal(1))
Expect(repo.Queue.Position).To(Equal(int64(10)))
Expect(stored().Current).To(Equal(1))
Expect(stored().Position).To(Equal(int64(10)))
})
})
It("updates ids and current", func() {
repo.Queue = &model.PlayQueue{UserID: user.ID}
seed(&model.PlayQueue{})
payload := updateQueuePayload{Ids: new([]string{"s1", "s2"}), Current: new(1)}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPut, "/queue", bytes.NewReader(body))
@@ -338,13 +405,13 @@ var _ = Describe("Queue Endpoints", func() {
updateQueue(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusNoContent))
Expect(repo.Queue.Items).To(HaveLen(2))
Expect(repo.Queue.Current).To(Equal(1))
Expect(repo.LastCols).To(ConsistOf("items", "current"))
Expect(stored().Items).To(HaveLen(2))
Expect(stored().Current).To(Equal(1))
Expect(faults.lastCols).To(ConsistOf("items", "current"))
})
It("returns bad request when new ids invalidate current", func() {
repo.Queue = &model.PlayQueue{UserID: user.ID, Current: 2}
seed(&model.PlayQueue{Current: 2})
payload := updateQueuePayload{Ids: new([]string{"s1", "s2"})}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPut, "/queue", bytes.NewReader(body))
@@ -356,7 +423,7 @@ var _ = Describe("Queue Endpoints", func() {
})
It("returns bad request when current out of bounds", func() {
repo.Queue = &model.PlayQueue{UserID: user.ID, Items: model.MediaFiles{{ID: "s1"}}}
seed(&model.PlayQueue{Items: model.MediaFiles{{ID: "s1"}}})
payload := updateQueuePayload{Current: new(3)}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPut, "/queue", bytes.NewReader(body))
@@ -377,7 +444,8 @@ var _ = Describe("Queue Endpoints", func() {
})
It("returns internal server error when store fails", func() {
repo.Err = true
seed(&model.PlayQueue{Items: model.MediaFiles{{ID: "s1"}}})
faults.storeErr = errors.New("db unavailable")
payload := updateQueuePayload{Position: new(int64(10))}
body, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPut, "/queue", bytes.NewReader(body))
@@ -391,18 +459,18 @@ var _ = Describe("Queue Endpoints", func() {
Describe("DELETE /queue", func() {
It("clears the queue", func() {
repo.Queue = &model.PlayQueue{UserID: user.ID, Items: model.MediaFiles{{ID: "s1"}}}
seed(&model.PlayQueue{Items: model.MediaFiles{{ID: "s1"}}})
req := httptest.NewRequest(http.MethodDelete, "/queue", nil)
req = req.WithContext(request.WithUser(req.Context(), user))
w := httptest.NewRecorder()
clearQueue(ds)(w, req)
Expect(w.Code).To(Equal(http.StatusNoContent))
Expect(repo.Queue).To(BeNil())
expectNoQueue()
})
It("returns internal server error when clear fails", func() {
repo.Err = true
faults.clearErr = errors.New("db unavailable")
req := httptest.NewRequest(http.MethodDelete, "/queue", nil)
req = req.WithContext(request.WithUser(req.Context(), user))
w := httptest.NewRecorder()