Skip to content

A full-featured download manager.

4.3 KB
Raw Blame
import '@renderer/lib/i18n'
import { useOperatorSession } from '@renderer/lib/operator-auth'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { OperatorUnlockGate } from './operator-unlock-gate'

const { transportMock } = vi.hoisted(() => ({
  transportMock: { platform: 'web' as string },
}))
vi.mock('@renderer/lib/transport', () => ({ transport: transportMock }))
const fetchMock = vi.fn()
const reply = (authed: boolean) =>
  new Response(
    JSON.stringify({
      authed,
      mode: authed ? 'cookie' : 'unauthenticated',
      canLogout: authed,
    })
  )
const child = <div data-testid="app">APP</div>
beforeEach(() => {
  transportMock.platform = 'web'
  useOperatorSession.setState({
    state: 'checking',
    status: null,
    epoch: useOperatorSession.getState().epoch + 1,
  })
  vi.stubGlobal('fetch', fetchMock)
  fetchMock.mockReset()
})
afterEach(() => vi.unstubAllGlobals())
describe('OperatorUnlockGate', () => {
  it('bypasses HTTP authentication on desktop', () => {
    transportMock.platform = 'darwin'
    render(<OperatorUnlockGate>{child}</OperatorUnlockGate>)
    expect(screen.getByTestId('app')).toBeTruthy()
    expect(fetchMock).not.toHaveBeenCalled()
  })
  it('renders children for an authenticated cookie', async () => {
    fetchMock.mockResolvedValue(reply(true))
    render(<OperatorUnlockGate>{child}</OperatorUnlockGate>)
    expect(await screen.findByTestId('app')).toBeTruthy()
  })
  it('exchanges a token and checks the resulting session', async () => {
    fetchMock
      .mockResolvedValueOnce(reply(false))
      .mockResolvedValueOnce(new Response('{}'))
      .mockResolvedValueOnce(reply(true))
    render(<OperatorUnlockGate>{child}</OperatorUnlockGate>)
    fireEvent.change(await screen.findByPlaceholderText('Access password'), {
      target: { value: 'machine-token' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Sign in' }))
    expect(await screen.findByTestId('app')).toBeTruthy()
    expect(fetchMock.mock.calls[1][1].body).toBe(
      JSON.stringify({ token: 'machine-token' })
    )
  })
  it('keeps rejected credentials locked', async () => {
    fetchMock
      .mockResolvedValueOnce(reply(false))
      .mockResolvedValueOnce(new Response('{}', { status: 401 }))
    render(<OperatorUnlockGate>{child}</OperatorUnlockGate>)
    fireEvent.change(await screen.findByPlaceholderText('Access password'), {
      target: { value: 'bad' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Sign in' }))
    expect(await screen.findByText(/Incorrect password/)).toBeTruthy()
  })
  it('explains rate limiting instead of reporting a correct token as invalid', async () => {
    fetchMock
      .mockResolvedValueOnce(reply(false))
      .mockResolvedValueOnce(new Response('{}', { status: 429 }))
    render(<OperatorUnlockGate>{child}</OperatorUnlockGate>)
    fireEvent.change(await screen.findByPlaceholderText('Access password'), {
      target: { value: 'test' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Sign in' }))
    expect(await screen.findByText(/Too many attempts/)).toBeTruthy()
    expect(screen.queryByTestId('app')).toBeNull()
  })
  it('offers retry for an unreachable server without pretending the session was revoked', async () => {
    fetchMock
      .mockRejectedValueOnce(new TypeError('offline'))
      .mockResolvedValueOnce(reply(true))
    render(<OperatorUnlockGate>{child}</OperatorUnlockGate>)
    fireEvent.click(await screen.findByRole('button', { name: 'Retry' }))
    await waitFor(() => expect(screen.getByTestId('app')).toBeTruthy())
  })
})

it('shows a safe DSM failure code while keeping the app locked without requesting a password', async () => {
  fetchMock.mockResolvedValueOnce(
    new Response(
      JSON.stringify({
        authed: false,
        mode: 'synologyDsm',
        canLogout: false,
        nasAuthDiagnostic: {
          stage: 'session',
          code: 'commandFailed',
          failure: 'exit',
          exitCode: 5,
        },
      })
    )
  )
  render(<OperatorUnlockGate>{child}</OperatorUnlockGate>)
  expect(
    await screen.findByText('session / commandFailed / exit / 5')
  ).toBeTruthy()
  expect(screen.queryByTestId('app')).toBeNull()
  expect(screen.queryByPlaceholderText('Access password')).toBeNull()
})