import { beforeEach, describe, expect, it, vi } from 'vitest'; import { screen, within } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { http, HttpResponse } from 'msw'; import { renderApp, mockAuthenticated } from '@/test/utils'; import { server } from '@/mocks/server'; import { API_BASE, makeAuthResponse, mockUser } from '@/features/auth/mocks/fixtures'; import { getMockUsers } from '@/features/users/mocks/handlers'; import { _resetSetupStatusCache } from '@/router'; beforeEach(() => { _resetSetupStatusCache(); }); const USERS_URL = `${API_BASE}/api/v1/Users`; describe('UsersPage', () => { it('renders the page title and invite button', async () => { mockAuthenticated(); renderApp('/users'); expect(await screen.findByTestId('users-page', {}, { timeout: 10000 })).toBeInTheDocument(); expect(screen.getByTestId('users-invite-button')).toBeInTheDocument(); }); it('renders the users table with all mock users', async () => { mockAuthenticated(); renderApp('/users'); const table = await screen.findByTestId('users-table'); expect(table).toBeInTheDocument(); for (const user of getMockUsers()) { expect(screen.getByTestId(`user-row-${user.id}`)).toBeInTheDocument(); } }); it('shows "Pending invite" badge for invitation-pending users', async () => { mockAuthenticated(); renderApp('/users'); await screen.findByTestId('users-table'); const pendingUser = getMockUsers().find((u) => u.invitationPending)!; const row = screen.getByTestId(`user-row-${pendingUser.id}`); expect(within(row).getByText('Pending invite')).toBeInTheDocument(); }); it('shows copy invite link action for pending user', async () => { mockAuthenticated(); renderApp('/users'); await screen.findByTestId('users-table'); const pendingUser = getMockUsers().find((u) => u.invitationPending)!; const actionsBtn = screen.getByTestId(`user-actions-${pendingUser.id}`); await userEvent.click(actionsBtn); expect(screen.getByTestId(`user-copy-link-${pendingUser.id}`)).toBeInTheDocument(); }); it('lets the Owner delete a pending invitation', async () => { mockAuthenticated(); renderApp('/users'); await screen.findByTestId('users-table'); const pendingUser = getMockUsers().find((u) => u.invitationPending)!; await userEvent.click(screen.getByTestId(`user-actions-${pendingUser.id}`)); expect(screen.getByTestId(`user-delete-${pendingUser.id}`)).toBeInTheDocument(); }); it('lets an Administrator withdraw a pending invitation for a User, but not delete accounts', async () => { server.use( http.post(`${API_BASE}/api/v1/auth/refresh`, () => HttpResponse.json(makeAuthResponse({ user: { ...mockUser, role: 'Administrator', name: 'Test Admin' } })), ), ); renderApp('/users'); await screen.findByTestId('users-table'); const pendingUser = getMockUsers().find((u) => u.invitationPending)!; await userEvent.click(screen.getByTestId(`user-actions-${pendingUser.id}`)); expect(screen.getByTestId(`user-delete-${pendingUser.id}`)).toBeInTheDocument(); await userEvent.keyboard('{Escape}'); const account = getMockUsers().find((u) => !u.invitationPending && u.role === 'Administrator')!; expect(screen.queryByTestId(`user-delete-${account.id}`)).not.toBeInTheDocument(); }); it('lists an expired invitation as expired, removable, without a copy-link action', async () => { mockAuthenticated(); server.use( http.get(USERS_URL, () => HttpResponse.json([ { id: '44444444-4444-4444-4444-444444444444', email: 'late@example.com', name: 'late@example.com', role: 'User', isActive: true, createdAt: '2026-01-01T00:00:00.000Z', invitationPending: true, invitationExpired: true, inviteLink: null, }, ]), ), ); renderApp('/users'); const row = await screen.findByTestId('user-row-44444444-4444-4444-4444-444444444444'); expect(within(row).getByText('Invitation expired')).toBeInTheDocument(); await userEvent.click(screen.getByTestId('user-actions-44444444-4444-4444-4444-444444444444')); expect(screen.getByTestId('user-delete-44444444-4444-4444-4444-444444444444')).toBeInTheDocument(); expect(screen.queryByTestId('user-copy-link-44444444-4444-4444-4444-444444444444')).not.toBeInTheDocument(); }); it('mirrors the API hierarchy for an Administrator: plain Users yes, Administrators and Owners no', async () => { server.use( http.post(`${API_BASE}/api/v1/auth/refresh`, () => HttpResponse.json(makeAuthResponse({ user: { ...mockUser, role: 'Administrator', name: 'Test Admin' } })), ), http.get(USERS_URL, () => HttpResponse.json([ { id: 'a1', email: 'plain@example.com', name: 'Plain', role: 'User', isActive: true, createdAt: '2026-01-01T00:00:00.000Z', invitationPending: false, inviteLink: null }, { id: 'a2', email: 'other-admin@example.com', name: 'Other Admin', role: 'Administrator', isActive: true, createdAt: '2026-01-01T00:00:00.000Z', invitationPending: false, inviteLink: null }, { id: 'a3', email: 'owner2@example.com', name: 'Owner Two', role: 'Owner', isActive: true, createdAt: '2026-01-01T00:00:00.000Z', invitationPending: false, inviteLink: null }, ]), ), ); renderApp('/users'); await screen.findByTestId('user-row-a1'); await userEvent.click(screen.getByTestId('user-actions-a1')); expect(screen.getByTestId('user-delete-a1')).toBeInTheDocument(); expect(screen.getByTestId('user-toggle-active-a1')).toBeInTheDocument(); await userEvent.keyboard('{Escape}'); expect(screen.queryByTestId('user-actions-a2')).not.toBeInTheDocument(); expect(screen.queryByTestId('user-actions-a3')).not.toBeInTheDocument(); }); it('asks before creating a new invitation link, and without mail offers only the link', async () => { mockAuthenticated(); const writeText = vi.fn().mockResolvedValue(undefined); Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true }); let body: Record | undefined; server.use( http.post(`${USERS_URL}/:id/regenerate-invite`, async ({ request }) => { body = (await request.json()) as Record; return HttpResponse.json({ inviteLink: '/invite/complete?token=fresh', mail: null }); }), ); renderApp('/users'); await screen.findByTestId('users-table'); const pending = getMockUsers().find((u) => u.invitationPending)!; await userEvent.click(screen.getByTestId(`user-actions-${pending.id}`)); await userEvent.click(screen.getByTestId(`user-regenerate-invite-${pending.id}`)); // Nothing has been created yet; the dialog has no mail choice on a site without mail. expect(await screen.findByTestId('regenerate-invite-dialog')).toBeInTheDocument(); expect(body).toBeUndefined(); expect(screen.queryByTestId('regenerate-copy-and-mail')).not.toBeInTheDocument(); await userEvent.click(screen.getByTestId('regenerate-copy-only')); await vi.waitFor(() => expect(writeText).toHaveBeenCalled()); expect(body).toEqual({ sendEmail: false }); expect(writeText.mock.calls[0][0]).toContain('/invite/complete?token=fresh'); }); it('cancelling the dialog creates nothing', async () => { mockAuthenticated(); let called = false; server.use( http.post(`${USERS_URL}/:id/regenerate-invite`, () => { called = true; return HttpResponse.json({ inviteLink: '/x' }); }), ); renderApp('/users'); await screen.findByTestId('users-table'); const pending = getMockUsers().find((u) => u.invitationPending)!; await userEvent.click(screen.getByTestId(`user-actions-${pending.id}`)); await userEvent.click(screen.getByTestId(`user-regenerate-invite-${pending.id}`)); await userEvent.click(await screen.findByTestId('regenerate-cancel')); expect(screen.queryByTestId('regenerate-invite-dialog')).not.toBeInTheDocument(); expect(called).toBe(false); }); it('shows an error message when the API fails', async () => { mockAuthenticated(); server.use(http.get(USERS_URL, () => HttpResponse.json({}, { status: 500 }))); renderApp('/users'); await screen.findByTestId('users-page'); expect(await screen.findByText(/something went wrong/i)).toBeInTheDocument(); }); it('opens the invite dialog when the invite button is clicked', async () => { mockAuthenticated(); renderApp('/users'); await screen.findByTestId('users-invite-button'); await userEvent.click(screen.getByTestId('users-invite-button')); expect(screen.getByTestId('invite-dialog-email')).toBeInTheDocument(); }); });