Files
navidrome/ui/src/dialogs/QuickConnectDialog.test.jsx
T
Deluan Quintão b76ae14286 feat(jellyfin): add Quick Connect sign-in (#6174)
* feat(jellyfin): add Quick Connect sign-in

Jellyfin clients can now sign in without a password: the client shows a
6-digit code, a signed-in user approves it, and the client redeems a secret
for its access token.

- core/quickconnect: in-memory store shared by both routers through wire.
  Codes expire after 10 minutes; a secret redeems only once (Jellyfin
  allows repeats for 10 minutes); at most 1000 pending requests.
- Jellyfin API: Initiate, Connect, Authorize and AuthenticateWithQuickConnect.
  Admins may approve for another user via UserId, like Swiftfin's admin page.
  Initiate and redeem share the login rate limiter; Connect does not, since
  Finamp and Streamyfin poll it every second.
- Web UI: a Quick Connect item in the user menu looks up the code and shows
  the app and device before approving, so a user can't be tricked into
  approving an unknown device blindly.
- Jellyfin.QuickConnect option, on by default like Jellyfin. It only matters
  when the Jellyfin API is enabled.

* refactor(jellyfin): tidy Quick Connect naming and route guards

Group the Quick Connect routes under one requireQuickConnect guard, make the
request's device a named field so req.Device.ID can't be mistaken for a
request id, and rename the web API response type to quickConnectDevice.

* refactor(jellyfin): remove duplicated Jellyfin date formatting function

* test(jellyfin): set play count and starred in the song fixture literal

* refactor(jellyfin): inline the Quick Connect redeem body and use the shared date helper

* fix(jellyfin): bound the client fields Quick Connect keeps in memory

Initiate is unauthenticated and keeps the Client, Device, DeviceId and Version
header fields for up to ten minutes. With no header size limit, each pending
request could hold about 1 MB, and even a short field kept the whole header
alive because the parsed values are substrings of it. Reject fields over 512
bytes and copy the stored values.

Also answer 500 instead of 401 when the redeem user lookup fails for a reason
other than the user being gone.

* fix(jellyfin): rate-limit Quick Connect code approval

Any signed-in user could try codes without limit on the Jellyfin Authorize
endpoint and the web UI lookup/authorize endpoints, and so could approve
another person's pending device for their own account. Apply the same per-IP
limiter as the login (AuthRequestLimit/AuthWindowLength) to both surfaces.
2026-09-19 14:57:01 -04:00

104 lines
3.4 KiB
React

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 { describe, afterEach, it, expect, vi } from 'vitest'
import { QuickConnectDialog } from './QuickConnectDialog'
const finamp = { appName: 'Finamp', appVersion: '1.0.0', deviceName: 'Pixel 7' }
const renderDialog = (dataProvider, onClose = vi.fn()) => {
render(
<DataProviderContext.Provider value={dataProvider}>
<TestContext initialState={{ admin: { ui: { optimistic: false } } }}>
<QuickConnectDialog open={true} onClose={onClose} />
</TestContext>
</DataProviderContext.Provider>,
)
return onClose
}
const enterCode = (code) => {
fireEvent.change(screen.getByRole('textbox'), { target: { value: code } })
fireEvent.click(screen.getByText('menu.quickConnect.continue'))
}
describe('QuickConnectDialog', () => {
afterEach(cleanup)
it('shows the device before approving the code', async () => {
const dataProvider = {
lookupQuickConnect: vi.fn().mockResolvedValue({ data: finamp }),
authorizeQuickConnect: vi.fn().mockResolvedValue({ data: finamp }),
}
const onClose = renderDialog(dataProvider)
enterCode('123 456')
await screen.findByText('menu.quickConnect.approve')
expect(dataProvider.lookupQuickConnect.mock.calls[0][0]).toBe('123 456')
expect(dataProvider.authorizeQuickConnect).not.toHaveBeenCalled()
fireEvent.click(screen.getByText('menu.quickConnect.approve'))
await waitFor(() => expect(onClose).toHaveBeenCalled())
expect(dataProvider.authorizeQuickConnect.mock.calls[0][0]).toBe('123 456')
})
it('goes back to the code input', async () => {
const dataProvider = {
lookupQuickConnect: vi.fn().mockResolvedValue({ data: finamp }),
authorizeQuickConnect: vi.fn(),
}
renderDialog(dataProvider)
enterCode('123456')
await screen.findByText('menu.quickConnect.approve')
fireEvent.click(screen.getByText('ra.action.back'))
expect(screen.getByRole('textbox')).toHaveValue('123456')
expect(dataProvider.authorizeQuickConnect).not.toHaveBeenCalled()
})
it('stays on the code input when the code is unknown', async () => {
const dataProvider = {
lookupQuickConnect: vi.fn().mockRejectedValue({ status: 404 }),
authorizeQuickConnect: vi.fn(),
}
const onClose = renderDialog(dataProvider)
enterCode('000000')
await waitFor(() =>
expect(dataProvider.lookupQuickConnect).toHaveBeenCalled(),
)
expect(screen.getByRole('textbox')).toBeInTheDocument()
expect(screen.queryByText('menu.quickConnect.approve')).toBeNull()
expect(onClose).not.toHaveBeenCalled()
})
it('returns to the code input when approval fails', async () => {
const dataProvider = {
lookupQuickConnect: vi.fn().mockResolvedValue({ data: finamp }),
authorizeQuickConnect: vi.fn().mockRejectedValue({ status: 409 }),
}
const onClose = renderDialog(dataProvider)
enterCode('123456')
fireEvent.click(await screen.findByText('menu.quickConnect.approve'))
await screen.findByRole('textbox')
expect(onClose).not.toHaveBeenCalled()
})
it('disables Continue until a code is typed', () => {
renderDialog({ lookupQuickConnect: vi.fn() })
expect(
screen.getByText('menu.quickConnect.continue').closest('button'),
).toBeDisabled()
})
})