mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-27 20:39:53 +01:00
* fix: return 404 instead of 500 for missing native API resources The deluan/rest controller only maps rest.ErrNotFound to 404, comparing with ==. Most repositories return model.ErrNotFound, which had the same message but was a different value, so requesting a missing playlist, album, artist, song, radio, player, transcoding or library returned 500. This also applied to other users' private playlists. Make model.ErrNotFound the same value as rest.ErrNotFound. This fixes every REST route at once, with no per-route wrapping. errors.Is checks against either error keep working, and nothing wraps model.ErrNotFound before it reaches the controller. Fixes #6130 * fix(radio): return not found when deleting a missing radio station radioRepository.Delete used the shared delete helper, which never reports a missing row because SQL DELETE on zero rows is not an error. Deleting an unknown id silently succeeded: DELETE /api/radio/{id} returned 200, and the Subsonic deleteInternetRadioStation endpoint returned ok. Delete now checks the affected row count and returns model.ErrNotFound when nothing was deleted. The native API returns 404, and deleteInternetRadioStation returns error 70 (data not found). This matches Subsonic 6.1.6, gonic (both verified live) and Ampache (verified in source). Airsonic-Advanced does not implement this endpoint. The shared delete helper is unchanged, as several callers rely on deletes of absent rows succeeding. * fix(ui): return 404 for missing files that are not missing or do not exist missingRepository.Read filtered media files by bare "id" and "missing" columns. The media file query joins the library table, so SQLite rejected the query as ambiguous and GET /api/missing/{id} returned 500. Read now loads the file with MediaFileRepository.Get, which qualifies the column, and returns not found when the file does not exist or is not marked missing. * fix: report missing rows on single-item deletes and adopt deluan/rest errors.Is Bump github.com/deluan/rest to the version whose controller matches errors with errors.Is and errors.As. model.ErrNotFound stays the same value as rest.ErrNotFound, so the many hand-written conversions from model.ErrNotFound to rest.ErrNotFound in repositories, core services and test mocks did nothing. Remove them, along with the duplicate rest.ErrNotFound check in the Subsonic error mapper. Mappings from model.ErrNotAuthorized stay, as those are different errors. User and transcoding deletes had the same silent success as radio: the shared delete helper never reports a missing row, so their not-found checks never fired and DELETE /api/user/{id} and /api/transcoding/{id} returned 200 for unknown ids. Add deleteByID, which returns model.ErrNotFound when no row matched, and use it for radio, user and transcoding. Also drop the dead sql.ErrNoRows branch from delete, since a DELETE never returns it. The Subsonic deleteUser endpoint is not implemented (501), so this does not change the Subsonic API. Plugin deletes keep the silent helper: there is no REST route for them, and the plugin manager only deletes rows it just read. * chore: drop ErrNotFound comment and its identity test The alias to rest.ErrNotFound is self-explanatory, and the identity test only restated the declaration. * refactor: alias model.ErrNotAuthorized to rest.ErrPermissionDenied Like ErrNotFound, make model.ErrNotAuthorized the same value as the rest library's error, so REST endpoints map it to 403 directly. This removes the ErrNotAuthorized to rest.ErrPermissionDenied mappings in the library and playlist REST adapters and the duplicate check in the Subsonic error mapper. Handlers that check model.ErrNotAuthorized now also recognize rest.ErrPermissionDenied returned by repositories, so writePlaylistError, the image upload handlers and the public share handler return 403 for it instead of their fallback status. The error message changes from "not authorized" to "permission denied".
310 lines
6.8 KiB
Go
310 lines
6.8 KiB
Go
package tests
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"slices"
|
|
"strconv"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/deluan/rest"
|
|
"github.com/navidrome/navidrome/model"
|
|
)
|
|
|
|
type MockLibraryRepo struct {
|
|
model.LibraryRepository
|
|
Data map[int]model.Library
|
|
Err error
|
|
PutFn func(*model.Library) error // Allow custom Put behavior for testing
|
|
PutCols []string
|
|
}
|
|
|
|
func (m *MockLibraryRepo) SetData(data model.Libraries) {
|
|
m.Data = make(map[int]model.Library)
|
|
for _, d := range data {
|
|
m.Data[d.ID] = d
|
|
}
|
|
}
|
|
|
|
func (m *MockLibraryRepo) GetAll(...model.QueryOptions) (model.Libraries, error) {
|
|
if m.Err != nil {
|
|
return nil, m.Err
|
|
}
|
|
var libraries model.Libraries
|
|
for _, lib := range m.Data {
|
|
libraries = append(libraries, lib)
|
|
}
|
|
// Sort by ID for predictable order
|
|
slices.SortFunc(libraries, func(a, b model.Library) int {
|
|
return a.ID - b.ID
|
|
})
|
|
return libraries, nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) CountAll(qo ...model.QueryOptions) (int64, error) {
|
|
if m.Err != nil {
|
|
return 0, m.Err
|
|
}
|
|
|
|
// If no query options, return total count
|
|
if len(qo) == 0 || qo[0].Filters == nil {
|
|
return int64(len(m.Data)), nil
|
|
}
|
|
|
|
// Handle squirrel.Eq filter for ID validation
|
|
if eq, ok := qo[0].Filters.(squirrel.Eq); ok {
|
|
if idFilter, exists := eq["id"]; exists {
|
|
if ids, isSlice := idFilter.([]int); isSlice {
|
|
count := 0
|
|
for _, id := range ids {
|
|
if _, exists := m.Data[id]; exists {
|
|
count++
|
|
}
|
|
}
|
|
return int64(count), nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// Default to total count for other filters
|
|
return int64(len(m.Data)), nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) Get(id int) (*model.Library, error) {
|
|
if m.Err != nil {
|
|
return nil, m.Err
|
|
}
|
|
if lib, ok := m.Data[id]; ok {
|
|
return &lib, nil
|
|
}
|
|
return nil, model.ErrNotFound
|
|
}
|
|
|
|
func (m *MockLibraryRepo) GetPath(id int) (string, error) {
|
|
if m.Err != nil {
|
|
return "", m.Err
|
|
}
|
|
if lib, ok := m.Data[id]; ok {
|
|
return lib.Path, nil
|
|
}
|
|
return "", model.ErrNotFound
|
|
}
|
|
|
|
func (m *MockLibraryRepo) Put(library *model.Library, colsToUpdate ...string) error {
|
|
m.PutCols = colsToUpdate
|
|
if m.PutFn != nil {
|
|
return m.PutFn(library)
|
|
}
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
if m.Data == nil {
|
|
m.Data = make(map[int]model.Library)
|
|
}
|
|
m.Data[library.ID] = *library
|
|
return nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) Delete(id int) error {
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
if _, ok := m.Data[id]; !ok {
|
|
return model.ErrNotFound
|
|
}
|
|
delete(m.Data, id)
|
|
return nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) StoreMusicFolder() error {
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) AddArtist(id int, artistID string) error {
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) ScanBegin(id int, fullScan bool) error {
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) ScanEnd(id int) error {
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) ScanInProgress() (bool, error) {
|
|
if m.Err != nil {
|
|
return false, m.Err
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) RefreshStats(id int) error {
|
|
return nil
|
|
}
|
|
|
|
// User-library association methods - mock implementations
|
|
|
|
func (m *MockLibraryRepo) GetUsersWithLibraryAccess(libraryID int) (model.Users, error) {
|
|
if m.Err != nil {
|
|
return nil, m.Err
|
|
}
|
|
// Mock: return empty users for now
|
|
return model.Users{}, nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) Count(options ...rest.QueryOptions) (int64, error) {
|
|
return m.CountAll()
|
|
}
|
|
|
|
func (m *MockLibraryRepo) Read(id string) (any, error) {
|
|
idInt, _ := strconv.Atoi(id)
|
|
return m.Get(idInt)
|
|
}
|
|
|
|
func (m *MockLibraryRepo) ReadAll(options ...rest.QueryOptions) (any, error) {
|
|
return m.GetAll()
|
|
}
|
|
|
|
func (m *MockLibraryRepo) EntityName() string {
|
|
return "library"
|
|
}
|
|
|
|
func (m *MockLibraryRepo) NewInstance() any {
|
|
return &model.Library{}
|
|
}
|
|
|
|
// REST Repository methods (string-based IDs)
|
|
|
|
func (m *MockLibraryRepo) Save(entity any) (string, error) {
|
|
lib := entity.(*model.Library)
|
|
if m.Err != nil {
|
|
return "", m.Err
|
|
}
|
|
|
|
// Validate required fields
|
|
if lib.Name == "" {
|
|
return "", &rest.ValidationError{Errors: map[string]string{"name": "library name is required"}}
|
|
}
|
|
if lib.Path == "" {
|
|
return "", &rest.ValidationError{Errors: map[string]string{"path": "library path is required"}}
|
|
}
|
|
|
|
// Generate ID if not set
|
|
if lib.ID == 0 {
|
|
lib.ID = len(m.Data) + 1
|
|
}
|
|
if m.Data == nil {
|
|
m.Data = make(map[int]model.Library)
|
|
}
|
|
m.Data[lib.ID] = *lib
|
|
return strconv.Itoa(lib.ID), nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) Update(id string, entity any, cols ...string) error {
|
|
lib := entity.(*model.Library)
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
|
|
// Validate required fields
|
|
if lib.Name == "" {
|
|
return &rest.ValidationError{Errors: map[string]string{"name": "library name is required"}}
|
|
}
|
|
if lib.Path == "" {
|
|
return &rest.ValidationError{Errors: map[string]string{"path": "library path is required"}}
|
|
}
|
|
|
|
idInt, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
return errors.New("invalid ID format")
|
|
}
|
|
if _, exists := m.Data[idInt]; !exists {
|
|
return rest.ErrNotFound
|
|
}
|
|
lib.ID = idInt
|
|
m.Data[idInt] = *lib
|
|
return nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) DeleteByStringID(id string) error {
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
idInt, err := strconv.Atoi(id)
|
|
if err != nil {
|
|
return errors.New("invalid ID format")
|
|
}
|
|
if _, exists := m.Data[idInt]; !exists {
|
|
return rest.ErrNotFound
|
|
}
|
|
delete(m.Data, idInt)
|
|
return nil
|
|
}
|
|
|
|
// Service-level methods for core.Library interface
|
|
|
|
func (m *MockLibraryRepo) GetUserLibraries(ctx context.Context, userID string) (model.Libraries, error) {
|
|
if m.Err != nil {
|
|
return nil, m.Err
|
|
}
|
|
if userID == "non-existent" {
|
|
return nil, model.ErrNotFound
|
|
}
|
|
// Convert map to slice for return
|
|
var libraries model.Libraries
|
|
for _, lib := range m.Data {
|
|
libraries = append(libraries, lib)
|
|
}
|
|
// Sort by ID for predictable order
|
|
slices.SortFunc(libraries, func(a, b model.Library) int {
|
|
return a.ID - b.ID
|
|
})
|
|
return libraries, nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) SetUserLibraries(ctx context.Context, userID string, libraryIDs []int) error {
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
if userID == "non-existent" {
|
|
return model.ErrNotFound
|
|
}
|
|
if userID == "admin-1" {
|
|
return fmt.Errorf("%w: cannot manually assign libraries to admin users", model.ErrValidation)
|
|
}
|
|
if len(libraryIDs) == 0 {
|
|
return fmt.Errorf("%w: at least one library must be assigned to non-admin users", model.ErrValidation)
|
|
}
|
|
// Validate all library IDs exist
|
|
for _, id := range libraryIDs {
|
|
if _, exists := m.Data[id]; !exists {
|
|
return fmt.Errorf("%w: library ID %d does not exist", model.ErrValidation, id)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *MockLibraryRepo) ValidateLibraryAccess(ctx context.Context, userID string, libraryID int) error {
|
|
if m.Err != nil {
|
|
return m.Err
|
|
}
|
|
// For testing purposes, allow access to all libraries
|
|
return nil
|
|
}
|
|
|
|
var _ model.LibraryRepository = (*MockLibraryRepo)(nil)
|
|
var _ model.ResourceRepository = (*MockLibraryRepo)(nil)
|