b64d8ad334
The native API endpoints GET /playlist/{id}/tracks and
GET /playlist/{id}/tracks/{id} were panicking with a nil pointer
dereference (resulting in a 500) when the playlist did not exist.
This happened because Tracks() returns nil for missing playlists,
and the nil repository was passed directly to the rest handler.
Extracted a shared playlistTracksHandler that checks for nil and
returns 404 early. Added tests covering both the error and happy paths.
39 lines
730 B
Go
39 lines
730 B
Go
package tests
|
|
|
|
import (
|
|
"github.com/deluan/rest"
|
|
"github.com/navidrome/navidrome/model"
|
|
)
|
|
|
|
type MockPlaylistRepo struct {
|
|
model.PlaylistRepository
|
|
|
|
Entity *model.Playlist
|
|
Error error
|
|
TracksReturn model.PlaylistTrackRepository
|
|
}
|
|
|
|
func (m *MockPlaylistRepo) Get(_ string) (*model.Playlist, error) {
|
|
if m.Error != nil {
|
|
return nil, m.Error
|
|
}
|
|
if m.Entity == nil {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
return m.Entity, nil
|
|
}
|
|
|
|
func (m *MockPlaylistRepo) Tracks(_ string, _ bool) model.PlaylistTrackRepository {
|
|
return m.TracksReturn
|
|
}
|
|
|
|
func (m *MockPlaylistRepo) Count(_ ...rest.QueryOptions) (int64, error) {
|
|
if m.Error != nil {
|
|
return 0, m.Error
|
|
}
|
|
if m.Entity == nil {
|
|
return 0, nil
|
|
}
|
|
return 1, nil
|
|
}
|