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>
This commit is contained in:
92
frontend/tests/admin-users.test.tsx
Normal file
92
frontend/tests/admin-users.test.tsx
Normal file
@ -0,0 +1,92 @@
|
||||
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()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user