Nav hint from own grants and roles (never a control); RequireCatalog, RequireAdmin (no admin read before aal2), Accès non autorisé page, placeholder routes; TOTP factor list, enrolment with QR and one-time secret, removal, inline step-up for aal2 actions, step-up in password reset; auth kind aal2_required. App 601, e2e:ui 227.
136 lines
5.5 KiB
TypeScript
136 lines
5.5 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { cleanup, render, screen, within } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { MemoryRouter, Route, Routes } from 'react-router-dom'
|
|
import type { OwnCatalogGrant, PlatformRole } from '@faunosteo/supabase'
|
|
import { createFakeSupabase, fakeProfile, fakeUser, type FakeSupabase } from '../helpers/fakeSupabase'
|
|
|
|
const mocks = vi.hoisted(() => ({ fake: null as unknown }))
|
|
vi.mock('@/lib/supabase', () => ({
|
|
getSupabaseClient: () => (mocks.fake as FakeSupabase | null)?.client ?? null,
|
|
isSupabaseConfigured: () => mocks.fake !== null,
|
|
}))
|
|
|
|
import AppLayout from '@/components/shell/AppLayout'
|
|
import { navigationHint } from '@/auth/navigationHint'
|
|
import { useAuthStore } from '@/state/authStore'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { fr } from '@/i18n/fr'
|
|
|
|
const NOW = Date.parse('2026-11-02T12:00:00Z')
|
|
|
|
function grant(kind: OwnCatalogGrant['kind'], patch: Partial<OwnCatalogGrant> = {}): OwnCatalogGrant {
|
|
return { kind, expires_at: null, revoked_at: null, ...patch }
|
|
}
|
|
|
|
describe('navigationHint', () => {
|
|
it('shows nothing extra without a grant or the owner role', () => {
|
|
expect(navigationHint([], [], NOW)).toEqual({ catalog: false, admin: false })
|
|
expect(navigationHint([], ['admin', 'support'], NOW)).toEqual({ catalog: false, admin: false })
|
|
})
|
|
|
|
it('shows the catalogue to a catalogue reader only', () => {
|
|
expect(navigationHint([grant('catalog_reader')], [], NOW)).toEqual({ catalog: true, admin: false })
|
|
})
|
|
|
|
it('shows both entries to any capability holder', () => {
|
|
for (const kind of ['sheet_editor', 'sheet_reviewer', 'sheet_publisher'] as const) {
|
|
expect(navigationHint([grant(kind)], [], NOW)).toEqual({ catalog: true, admin: true })
|
|
}
|
|
})
|
|
|
|
it('shows the administration to the owner role, the catalogue only with a grant', () => {
|
|
expect(navigationHint([], ['owner'], NOW)).toEqual({ catalog: false, admin: true })
|
|
})
|
|
|
|
it('ignores revoked and expired grants, keeps one expiring later', () => {
|
|
const revoked = grant('sheet_editor', { revoked_at: '2026-10-01T00:00:00Z' })
|
|
const expired = grant('catalog_reader', { expires_at: '2026-11-01T00:00:00Z' })
|
|
expect(navigationHint([revoked, expired], [], NOW)).toEqual({ catalog: false, admin: false })
|
|
const later = grant('catalog_reader', { expires_at: '2026-12-01T00:00:00Z' })
|
|
expect(navigationHint([later], [], NOW).catalog).toBe(true)
|
|
})
|
|
})
|
|
|
|
function mount(grants: OwnCatalogGrant[], roles: PlatformRole[] = []) {
|
|
useAuthStore.setState({ catalogGrants: grants, roles })
|
|
return render(
|
|
<MemoryRouter initialEntries={['/app']}>
|
|
<Routes>
|
|
<Route path="/app" element={<AppLayout />}>
|
|
<Route index element={<p>dashboard page</p>} />
|
|
</Route>
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
)
|
|
}
|
|
|
|
function sidebar() {
|
|
return screen.getByRole('navigation', { name: fr['shell.nav_label'] })
|
|
}
|
|
|
|
beforeEach(() => {
|
|
useLocaleStore.setState({ locale: 'fr' })
|
|
mocks.fake = createFakeSupabase() satisfies FakeSupabase
|
|
useAuthStore.setState({
|
|
status: 'signed_in',
|
|
user: fakeUser(),
|
|
profile: fakeProfile(),
|
|
avatar: null,
|
|
avatarUrl: null,
|
|
})
|
|
})
|
|
afterEach(cleanup)
|
|
|
|
describe('SidebarNav entries', () => {
|
|
it('hides both V1-2 entries from an account without a right', () => {
|
|
mount([])
|
|
expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.anatomy'] })).toBeNull()
|
|
expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.admin'] })).toBeNull()
|
|
expect(within(sidebar()).queryByRole('separator')).toBeNull()
|
|
})
|
|
|
|
it('shows « Fiches anatomiques » to a catalogue reader, without the administration', () => {
|
|
mount([grant('catalog_reader')])
|
|
expect(within(sidebar()).getByRole('link', { name: fr['shell.nav.anatomy'] })).toHaveAttribute(
|
|
'href',
|
|
'/app/anatomy',
|
|
)
|
|
expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.admin'] })).toBeNull()
|
|
})
|
|
|
|
it('shows both to a capability holder, « Administration » set apart before « Mon compte »', () => {
|
|
mount([grant('sheet_reviewer')])
|
|
const links = within(sidebar()).getAllByRole('link').map((link) => link.textContent)
|
|
expect(links).toEqual([
|
|
fr['shell.nav.dashboard'],
|
|
fr['shell.nav.clients'],
|
|
fr['shell.nav.animals'],
|
|
fr['shell.nav.anatomy'],
|
|
fr['shell.nav.admin'],
|
|
fr['shell.nav.account'],
|
|
fr['shell.nav.local_tool'] + fr['shell.nav.local_tool_hint'],
|
|
])
|
|
expect(within(sidebar()).getByRole('link', { name: fr['shell.nav.admin'] })).toHaveAttribute(
|
|
'href',
|
|
'/app/admin',
|
|
)
|
|
expect(within(sidebar()).getAllByRole('separator')).toHaveLength(2)
|
|
})
|
|
|
|
it('shows « Administration » to the owner role without a grant', () => {
|
|
mount([], ['owner'])
|
|
expect(within(sidebar()).getByRole('link', { name: fr['shell.nav.admin'] })).toBeInTheDocument()
|
|
expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.anatomy'] })).toBeNull()
|
|
})
|
|
|
|
it('carries the same entries into the mobile drawer', async () => {
|
|
const user = userEvent.setup()
|
|
mount([grant('sheet_editor')])
|
|
await user.click(screen.getByRole('button', { name: fr['shell.open_menu'] }))
|
|
const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] })
|
|
expect(within(drawer).getByRole('link', { name: fr['shell.nav.anatomy'] })).toBeInTheDocument()
|
|
expect(within(drawer).getByRole('link', { name: fr['shell.nav.admin'] })).toBeInTheDocument()
|
|
})
|
|
})
|