mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-10 19:37:08 +02:00
* fix(playlist): block track edits on synced playlists across all APIs A synced playlist's tracks come from its source file, so any track edit made through the UI or an API was silently reverted on the next scan. Track mutations funnel through two service guards, checkTracksEditable (incremental edits) and Create (wholesale replace, used by Subsonic createPlaylist and Jellyfin's replace path), which each duplicated the smart-playlist check. Both now consult a shared model.Playlist.TracksEditable() predicate, so the native, Subsonic, and Jellyfin paths are all locked: track edits return ErrNotAuthorized (403, or Subsonic error 50) instead of being accepted and lost. Metadata-only edits (name, comment, public, the sync flag itself) still go through checkWritable and are unaffected. In the UI, a synced playlist's track list becomes read-only, mirroring how smart playlists already behave. * fix(playlist): return 409 Conflict for non-editable playlist track edits The previous commit rejected track edits on smart and synced playlists with ErrNotAuthorized (403). That conflates two different things: a 403 says the caller lacks permission, but a synced or smart playlist's tracks are immutable for everyone, including the owner and admins. It is a property of the resource, not the caller. Introduce ErrPlaylistNotEditable and return it from both track-edit guards. The Native and Jellyfin APIs now map it to 409 Conflict; Subsonic maps it to error 50, the closest code it has (it has no read-only concept). The Native track handlers previously mapped this rejection inconsistently (400 on add, 500 on remove, 403 on reorder) through a new shared writePlaylistError helper. Genuine authorization failures (non-owner, non-admin) still return ErrNotAuthorized. * fix(playlist): surface synced read-only state in picker, Jellyfin, and OpenSubsonic Follow-up to the track-edit lock: the read-only state was enforced but not advertised consistently, so clients still offered edits that the server rejects. - UI: the Add to Playlist picker filtered targets by isWritable only, offering synced playlists that then 409 on add. It now filters with canChangeTracks. - Jellyfin: addToPlaylist/removeFromPlaylist hard-coded every error to 404, so a locked playlist reported "not found" instead of 409. They now return 409 for ErrPlaylistNotEditable while keeping the deliberate anti-probing 404 for every other error (a non-owner never reaches ErrPlaylistNotEditable, so 409 leaks nothing). - OpenSubsonic: buildOSPlaylist marked only smart playlists readonly; owned synced playlists advertised readonly=false. Readonly now also covers !TracksEditable(), matching the existing smart-playlist treatment. * fix(jellyfin): report CanEdit from playlist editability in permission probes getPlaylistUsers and getPlaylistUser returned CanEdit: true unconditionally, so Finamp (which probes this before showing edit controls) offered track editing on synced/smart playlists whose add/remove requests now return 409. Both handlers now fetch the playlist and set CanEdit from TracksEditable(), keeping the deliberate non-owner looseness (CanEdit stays true for a normal playlist a non-owner views) and mapping any lookup error to 404 like the sibling probes. * fix(playlist): check ownership before editability when replacing tracks Create checked TracksEditable() before ownership, so a non-owner replacing another user's public smart/synced playlist (Jellyfin updatePlaylist with a non-empty Ids list) received a 409 read-only conflict instead of a 403 authorization failure. The incremental guards check ownership first via checkWritable; Create now matches that order. Subsonic is unaffected (both errors map to code 50). Owners of their own smart/synced playlists still get the read-only conflict. * fix(jellyfin): return 403 for locked playlists, matching Jellyfin Jellyfin itself refuses edits on its file-backed playlists with Forbid() (403): PlaylistsController gates every mutation on OwnerUserId == caller or a share with CanEdit, and playlists imported from .m3u files satisfy neither. Its CanEdit is an ACL field, not a read-only marker, and Jellyfin core has no server-managed playlist type at all. Our Jellyfin routes exist to imitate that API, so ErrPlaylistNotEditable now maps to 403 there instead of 409. The native API keeps 409 (a resource-state conflict is the accurate REST answer where we define the contract) and Subsonic keeps error 50, its closest code. * chore(playlist): trim comments added by this branch Several comments ran to three or four lines and carried rationale that belongs in the commit history rather than the code: what Jellyfin does with its own file-backed playlists, and restatements of the expressions directly below them. Each block is now one or two lines covering only the non-obvious why.
498 lines
16 KiB
JavaScript
498 lines
16 KiB
JavaScript
import * as React from 'react'
|
||
import { TestContext } from 'ra-test'
|
||
import { DataProviderContext } from 'react-admin'
|
||
import {
|
||
cleanup,
|
||
fireEvent,
|
||
render,
|
||
screen,
|
||
waitFor,
|
||
} from '@testing-library/react'
|
||
import { SelectPlaylistInput } from './SelectPlaylistInput'
|
||
import { describe, beforeAll, afterEach, it, expect, vi } from 'vitest'
|
||
|
||
const mockPlaylists = [
|
||
{ id: 'playlist-1', name: 'Rock Classics', ownerId: 'admin' },
|
||
{ id: 'playlist-2', name: 'Jazz Collection', ownerId: 'admin' },
|
||
{ id: 'playlist-3', name: 'Electronic Beats', ownerId: 'admin' },
|
||
{ id: 'playlist-4', name: 'Chill Vibes', ownerId: 'user2' }, // Not writable by admin
|
||
{ id: 'playlist-5', name: 'Synced List', ownerId: 'admin', sync: true },
|
||
]
|
||
|
||
const mockIndexedData = {
|
||
'playlist-1': { id: 'playlist-1', name: 'Rock Classics', ownerId: 'admin' },
|
||
'playlist-2': { id: 'playlist-2', name: 'Jazz Collection', ownerId: 'admin' },
|
||
'playlist-3': {
|
||
id: 'playlist-3',
|
||
name: 'Electronic Beats',
|
||
ownerId: 'admin',
|
||
},
|
||
'playlist-4': { id: 'playlist-4', name: 'Chill Vibes', ownerId: 'user2' },
|
||
'playlist-5': {
|
||
id: 'playlist-5',
|
||
name: 'Synced List',
|
||
ownerId: 'admin',
|
||
sync: true,
|
||
},
|
||
}
|
||
|
||
const createTestComponent = (
|
||
mockDataProvider = null,
|
||
onChangeMock = vi.fn(),
|
||
playlists = mockPlaylists,
|
||
indexedData = mockIndexedData,
|
||
) => {
|
||
const dataProvider = mockDataProvider || {
|
||
getList: vi.fn().mockResolvedValue({
|
||
data: playlists,
|
||
total: playlists.length,
|
||
}),
|
||
}
|
||
|
||
return render(
|
||
<DataProviderContext.Provider value={dataProvider}>
|
||
<TestContext
|
||
initialState={{
|
||
admin: {
|
||
ui: { optimistic: false },
|
||
resources: {
|
||
playlist: {
|
||
data: indexedData,
|
||
list: {
|
||
cachedRequests: {
|
||
'{"pagination":{"page":1,"perPage":-1},"sort":{"field":"name","order":"ASC"},"filter":{"smart":false}}':
|
||
{
|
||
ids: Object.keys(indexedData),
|
||
total: Object.keys(indexedData).length,
|
||
},
|
||
},
|
||
},
|
||
},
|
||
},
|
||
},
|
||
}}
|
||
>
|
||
<SelectPlaylistInput onChange={onChangeMock} />
|
||
</TestContext>
|
||
</DataProviderContext.Provider>,
|
||
)
|
||
}
|
||
|
||
describe('SelectPlaylistInput', () => {
|
||
beforeAll(() => localStorage.setItem('userId', 'admin'))
|
||
afterEach(cleanup)
|
||
|
||
describe('Basic Functionality', () => {
|
||
it('should render search field and playlist list', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument()
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument()
|
||
expect(screen.getByText('Jazz Collection')).toBeInTheDocument()
|
||
expect(screen.getByText('Electronic Beats')).toBeInTheDocument()
|
||
})
|
||
|
||
// Should not show playlists not owned by admin (not writable)
|
||
expect(screen.queryByText('Chill Vibes')).not.toBeInTheDocument()
|
||
// Should not show synced playlists (their tracks are not editable)
|
||
expect(screen.queryByText('Synced List')).not.toBeInTheDocument()
|
||
})
|
||
|
||
it('should filter playlists based on search input', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: 'rock' } })
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument()
|
||
expect(screen.queryByText('Jazz Collection')).not.toBeInTheDocument()
|
||
expect(screen.queryByText('Electronic Beats')).not.toBeInTheDocument()
|
||
})
|
||
})
|
||
|
||
it('should handle case-insensitive search', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Jazz Collection')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: 'JAZZ' } })
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Jazz Collection')).toBeInTheDocument()
|
||
expect(screen.queryByText('Rock Classics')).not.toBeInTheDocument()
|
||
})
|
||
})
|
||
})
|
||
|
||
describe('Playlist Selection', () => {
|
||
it('should select and deselect playlists by clicking', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument()
|
||
})
|
||
|
||
// Select first playlist
|
||
const rockPlaylist = screen.getByText('Rock Classics')
|
||
fireEvent.click(rockPlaylist)
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([
|
||
{ id: 'playlist-1', name: 'Rock Classics', ownerId: 'admin' },
|
||
])
|
||
})
|
||
|
||
// Select second playlist
|
||
const jazzPlaylist = screen.getByText('Jazz Collection')
|
||
fireEvent.click(jazzPlaylist)
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([
|
||
{ id: 'playlist-1', name: 'Rock Classics', ownerId: 'admin' },
|
||
{ id: 'playlist-2', name: 'Jazz Collection', ownerId: 'admin' },
|
||
])
|
||
})
|
||
|
||
// Deselect first playlist
|
||
fireEvent.click(rockPlaylist)
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([
|
||
{ id: 'playlist-2', name: 'Jazz Collection', ownerId: 'admin' },
|
||
])
|
||
})
|
||
})
|
||
|
||
it('should show selected playlists as chips', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument()
|
||
})
|
||
|
||
// Select a playlist
|
||
const rockPlaylist = screen.getByText('Rock Classics')
|
||
fireEvent.click(rockPlaylist)
|
||
|
||
await waitFor(() => {
|
||
// Should show the selected playlist as a chip
|
||
const chips = screen.getAllByText('Rock Classics')
|
||
expect(chips.length).toBeGreaterThan(1) // One in list, one in chip
|
||
})
|
||
})
|
||
|
||
it('should remove selected playlists via chip remove button', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument()
|
||
})
|
||
|
||
// Select a playlist
|
||
const rockPlaylist = screen.getByText('Rock Classics')
|
||
fireEvent.click(rockPlaylist)
|
||
|
||
await waitFor(() => {
|
||
// Should show selected playlist as chip
|
||
const chips = screen.getAllByText('Rock Classics')
|
||
expect(chips.length).toBeGreaterThan(1)
|
||
})
|
||
|
||
// Find and click the remove button (translation key)
|
||
const removeButton = screen.getByText('×')
|
||
fireEvent.click(removeButton)
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([])
|
||
// Should only have one instance (in the list) after removal
|
||
const remainingChips = screen.getAllByText('Rock Classics')
|
||
expect(remainingChips.length).toBe(1)
|
||
})
|
||
})
|
||
})
|
||
|
||
describe('Create New Playlist', () => {
|
||
it('should create new playlist by pressing Enter', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: 'My New Playlist' } })
|
||
fireEvent.keyDown(searchInput, { key: 'Enter' })
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([{ name: 'My New Playlist' }])
|
||
})
|
||
|
||
// Input should be cleared after creating
|
||
expect(searchInput.value).toBe('')
|
||
})
|
||
|
||
it('should create new playlist by clicking add button', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: 'Another Playlist' } })
|
||
|
||
// Find the add button by the translation key title
|
||
const addButton = screen.getByTitle(
|
||
'resources.playlist.actions.addNewPlaylist',
|
||
)
|
||
fireEvent.click(addButton)
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([
|
||
{ name: 'Another Playlist' },
|
||
])
|
||
})
|
||
})
|
||
|
||
it('should not show create option for existing playlist names', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: 'Rock Classics' } })
|
||
|
||
await waitFor(() => {
|
||
expect(
|
||
screen.queryByText('resources.playlist.actions.addNewPlaylist'),
|
||
).not.toBeInTheDocument()
|
||
})
|
||
})
|
||
|
||
it('should not create playlist with empty name', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: ' ' } }) // Only spaces
|
||
fireEvent.keyDown(searchInput, { key: 'Enter' })
|
||
|
||
// Should not call onChange
|
||
expect(onChangeMock).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('should show create options in appropriate contexts', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
|
||
// When typing a new name, should show create options
|
||
fireEvent.change(searchInput, { target: { value: 'My New Playlist' } })
|
||
|
||
await waitFor(() => {
|
||
// Should show the add button in the search field
|
||
expect(
|
||
screen.getByTitle('resources.playlist.actions.addNewPlaylist'),
|
||
).toBeInTheDocument()
|
||
// Should also show hint in empty message when no matches
|
||
expect(
|
||
screen.getByText('resources.playlist.actions.pressEnterToCreate'),
|
||
).toBeInTheDocument()
|
||
})
|
||
})
|
||
})
|
||
|
||
describe('Mixed Operations', () => {
|
||
it('should handle selecting existing playlists and creating new ones', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument()
|
||
})
|
||
|
||
// Select existing playlist
|
||
const rockPlaylist = screen.getByText('Rock Classics')
|
||
fireEvent.click(rockPlaylist)
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([
|
||
{ id: 'playlist-1', name: 'Rock Classics', ownerId: 'admin' },
|
||
])
|
||
})
|
||
|
||
// Create new playlist
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: 'New Mix' } })
|
||
fireEvent.keyDown(searchInput, { key: 'Enter' })
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([
|
||
{ id: 'playlist-1', name: 'Rock Classics', ownerId: 'admin' },
|
||
{ name: 'New Mix' },
|
||
])
|
||
})
|
||
})
|
||
|
||
it('should maintain selections when searching', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument()
|
||
})
|
||
|
||
// Select a playlist
|
||
const rockPlaylist = screen.getByText('Rock Classics')
|
||
fireEvent.click(rockPlaylist)
|
||
|
||
// Filter the list
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: 'jazz' } })
|
||
|
||
await waitFor(() => {
|
||
// Should still show selected playlists section
|
||
// Rock Classics should still be visible as a selected chip even though filtered out
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument() // In selected chips
|
||
expect(screen.getByText('Jazz Collection')).toBeInTheDocument()
|
||
})
|
||
})
|
||
})
|
||
|
||
describe('Empty States', () => {
|
||
it('should show empty message when no playlists exist', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock, [], {})
|
||
|
||
await waitFor(() => {
|
||
expect(
|
||
screen.getByText('resources.playlist.message.noPlaylists'),
|
||
).toBeInTheDocument()
|
||
})
|
||
})
|
||
|
||
it('should show "no results" message when search returns no matches', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, {
|
||
target: { value: 'NonExistentPlaylist' },
|
||
})
|
||
|
||
await waitFor(() => {
|
||
expect(
|
||
screen.getByText('resources.playlist.message.noPlaylistsFound'),
|
||
).toBeInTheDocument()
|
||
expect(
|
||
screen.getByText('resources.playlist.actions.pressEnterToCreate'),
|
||
).toBeInTheDocument()
|
||
})
|
||
})
|
||
})
|
||
|
||
describe('Keyboard Navigation', () => {
|
||
it('should not create playlist on Enter if input is empty', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.keyDown(searchInput, { key: 'Enter' })
|
||
|
||
expect(onChangeMock).not.toHaveBeenCalled()
|
||
})
|
||
|
||
it('should handle other keys without side effects', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByRole('textbox')).toBeInTheDocument()
|
||
})
|
||
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: 'test' } })
|
||
fireEvent.keyDown(searchInput, { key: 'ArrowDown' })
|
||
fireEvent.keyDown(searchInput, { key: 'Tab' })
|
||
fireEvent.keyDown(searchInput, { key: 'Escape' })
|
||
|
||
// Should not create playlist or trigger onChange
|
||
expect(onChangeMock).not.toHaveBeenCalled()
|
||
expect(searchInput.value).toBe('test')
|
||
})
|
||
})
|
||
|
||
describe('Integration Scenarios', () => {
|
||
it('should handle complex workflow: search, select, create, remove', async () => {
|
||
const onChangeMock = vi.fn()
|
||
createTestComponent(null, onChangeMock)
|
||
|
||
await waitFor(() => {
|
||
expect(screen.getByText('Rock Classics')).toBeInTheDocument()
|
||
})
|
||
|
||
// Search and select existing playlist
|
||
const searchInput = screen.getByRole('textbox')
|
||
fireEvent.change(searchInput, { target: { value: 'rock' } })
|
||
|
||
const rockPlaylist = screen.getByText('Rock Classics')
|
||
fireEvent.click(rockPlaylist)
|
||
|
||
// Clear search and create new playlist
|
||
fireEvent.change(searchInput, { target: { value: 'My Custom Mix' } })
|
||
fireEvent.keyDown(searchInput, { key: 'Enter' })
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([
|
||
{ id: 'playlist-1', name: 'Rock Classics', ownerId: 'admin' },
|
||
{ name: 'My Custom Mix' },
|
||
])
|
||
})
|
||
|
||
// Remove the first selected playlist via chip
|
||
const removeButtons = screen.getAllByText('×')
|
||
fireEvent.click(removeButtons[0])
|
||
|
||
await waitFor(() => {
|
||
expect(onChangeMock).toHaveBeenCalledWith([{ name: 'My Custom Mix' }])
|
||
})
|
||
})
|
||
})
|
||
})
|