Files
TradarrV2/frontend/tests/admin-users.test.tsx
Blomios 50bde816ed feat(accounts): add admin accounts and user preferences
Remove public registration, support administrator-managed accounts, and persist personal preferences.

Refs #2 #5

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-20 19:54:06 +01:00

93 lines
3.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { cleanup, render, screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { AdminUsers } from '../src/pages/admin/AdminUsers'
import { Login } from '../src/pages/Login'
const { listUsers, createUser } = vi.hoisted(() => ({
listUsers: vi.fn(),
createUser: vi.fn(),
}))
vi.mock('@/api/admin', () => ({
adminApi: {
listUsers,
createUser,
updateUser: vi.fn(),
deleteUser: vi.fn(),
},
}))
vi.mock('@/lib/auth', () => ({
useAuth: () => ({ user: { id: 'admin-1', email: 'admin@example.test', role: 'admin' }, token: null, login: vi.fn() }),
}))
describe('création admin de comptes', () => {
afterEach(cleanup)
beforeEach(() => {
listUsers.mockResolvedValue([])
createUser.mockResolvedValue({ id: 'new-1', email: 'reader@example.test', role: 'user' })
})
it('ouvre le formulaire et crée un compte avec email, rôle et mot de passe', async () => {
const user = userEvent.setup()
render(<AdminUsers />)
await screen.findByRole('heading', { name: 'Utilisateurs' })
await user.click(screen.getByRole('button', { name: 'Créer un compte' }))
const form = screen.getByRole('form', { name: /créer un compte/i })
await user.type(within(form).getByLabelText(/email/i), 'reader@example.test')
await user.selectOptions(within(form).getByLabelText(/rôle/i), 'user')
await user.type(within(form).getByLabelText(/mot de passe/i), 'safe-pass-123')
await user.click(within(form).getByRole('button', { name: /créer/i }))
await waitFor(() => expect(createUser).toHaveBeenCalledWith({
email: 'reader@example.test',
role: 'user',
password: 'safe-pass-123',
}))
})
it('conserve les saisies sauf le secret après conflit et permet une reprise au clavier', async () => {
createUser.mockRejectedValueOnce(new Error('email already in use'))
const user = userEvent.setup()
render(<AdminUsers />)
const trigger = await screen.findByRole('button', { name: 'Créer un compte' })
trigger.focus()
await user.keyboard('{Enter}')
const email = screen.getByLabelText(/email/i)
const role = screen.getByLabelText(/rôle/i)
const password = screen.getByLabelText(/mot de passe/i)
expect(email).toHaveFocus()
await user.type(email, 'reader@example.test')
await user.selectOptions(role, 'admin')
await user.type(password, 'safe-pass-123{Enter}')
expect(await screen.findByRole('alert')).toHaveTextContent('Cet email est déjà utilisé.')
expect(email).toHaveValue('reader@example.test')
expect(role).toHaveValue('admin')
expect(password).toHaveValue('')
await user.type(password, 'another-pass{Enter}')
expect(await screen.findByRole('status')).toHaveTextContent('Compte créé avec succès.')
expect(password).toHaveValue('')
expect(email).toHaveValue('reader@example.test')
})
it('affiche une erreur réseau sans conserver le secret', async () => {
createUser.mockRejectedValueOnce(new Error('network unavailable'))
const user = userEvent.setup()
render(<AdminUsers />)
await user.click(await screen.findByRole('button', { name: 'Créer un compte' }))
await user.type(screen.getByLabelText(/email/i), 'reader@example.test')
await user.type(screen.getByLabelText(/mot de passe/i), 'safe-pass-123{Enter}')
expect(await screen.findByRole('alert')).toHaveTextContent('Impossible de créer le compte. Réessayez.')
expect(screen.getByLabelText(/mot de passe/i)).toHaveValue('')
})
it('ne présente aucun lien public d’inscription sur la page de connexion', () => {
render(<Login />)
expect(screen.getByText('Pour obtenir un compte, contactez votre administrateur.')).toBeInTheDocument()
expect(screen.queryByRole('link', { name: /inscri|créer un compte/i })).not.toBeInTheDocument()
})
})