From f04bfdd040103ac8f3a6ecc07fff2fe2d53907f2 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 2 Nov 2026 18:33:57 +0100 Subject: [PATCH] fix(app): creation page waits for own grants; e2e never prints setup keys Delta verifier: two remaining setup-key assertions now booleans. Its observation: the editor rule read grants before they loaded; authStore.catalogGrantsStatus (loading, ready, failed): the page waits, and a failed read leaves the decision to the server. App 654, e2e:ui 227, admin shell spec 14 + 6 skipped. --- apps/app/e2e/shell/admin.spec.ts | 4 ++-- .../routes/app/admin/AdminSheetCreateRoute.tsx | 18 ++++++++++++++---- apps/app/src/components/shell/AccessDenied.tsx | 6 ++++-- apps/app/src/state/authStore.ts | 8 ++++++-- apps/app/tests/unit/routes.admin.test.tsx | 11 ++++++++++- apps/app/tests/unit/state.authStore.test.ts | 1 + 6 files changed, 37 insertions(+), 11 deletions(-) diff --git a/apps/app/e2e/shell/admin.spec.ts b/apps/app/e2e/shell/admin.spec.ts index b5472469..acd9a528 100644 --- a/apps/app/e2e/shell/admin.spec.ts +++ b/apps/app/e2e/shell/admin.spec.ts @@ -250,13 +250,13 @@ for (const [locale, dict] of LANGUAGES) { 'the enrolment step with the QR code', ) const first = (await box.locator('code').textContent())?.trim() ?? '' - expect(first).toMatch(/^[A-Z2-7]{16,}$/) + expect(/^[A-Z2-7]{16,}$/.test(first), 'base32 setup key shown').toBe(true) await enterCode(box, dict, first) await expect(page.getByText(dict['mfa.enrol.done'])).toBeVisible() expect(await sessionAal(page)).toBe('aal2') // A second factor at aal2: no step-up asked. const second = await enrolFromPage(page, dict, 'Clé de secours') - expect(second).not.toBe(first) + expect(second !== first, 'a new setup key').toBe(true) await expect(factorList(page, dict)).toHaveCount(2) await expect( factorList(page, dict).filter({ hasText: dict['mfa.factor.verified'] }), diff --git a/apps/app/src/app/routes/app/admin/AdminSheetCreateRoute.tsx b/apps/app/src/app/routes/app/admin/AdminSheetCreateRoute.tsx index db8e6a5d..93457f06 100644 --- a/apps/app/src/app/routes/app/admin/AdminSheetCreateRoute.tsx +++ b/apps/app/src/app/routes/app/admin/AdminSheetCreateRoute.tsx @@ -33,6 +33,7 @@ import { coreFailureMessage } from '@/core/failure' import { coreRuntime } from '@/core/runtime' import { emptySheetDocument } from '@/core/sheetDocumentOps' import { getSupabaseClient } from '@/lib/supabase' +import { useAuthStore } from '@/state/authStore' import { useLocaleStore } from '@/state/localeStore' import { useT } from '@/i18n/useT' @@ -95,6 +96,7 @@ export default function AdminSheetCreateRoute() { const navigate = useNavigate() const locale = useLocaleStore((s) => s.locale) const { editor } = useAdminPermissions() + const grantsStatus = useAuthStore((s) => s.catalogGrantsStatus) const species = useAdminQuery('species', listCatalogSpecies) const [values, setValues] = useState(EMPTY) const [slugEdited, setSlugEdited] = useState(false) @@ -110,11 +112,19 @@ export default function AdminSheetCreateRoute() { if (first) document.getElementById(fieldId(first))?.focus() }, [errorRound, errors]) - // A display rule like the « Nouvelle fiche » button: a reviewer or a - // publisher reaching the address is not offered the form. The server still - // refuses the creation to anyone without the editor capability. + // A display rule like the « Nouvelle fiche » button: once the caller's own + // grants are read, a reviewer or a publisher reaching the address is not + // offered the form. While they load nothing is decided; if the read failed + // the form shows and the server, which refuses creation to anyone without + // the editor capability, decides. + if (grantsStatus === 'loading') + return ( +

+ {t('shell.loading_session')} +

+ ) if ( - !editor || + (grantsStatus === 'ready' && !editor) || (species.state.status === 'error' && isRightsRefusal(species.state.failure)) ) diff --git a/apps/app/src/components/shell/AccessDenied.tsx b/apps/app/src/components/shell/AccessDenied.tsx index c8f6fd0f..50838544 100644 --- a/apps/app/src/components/shell/AccessDenied.tsx +++ b/apps/app/src/components/shell/AccessDenied.tsx @@ -6,8 +6,10 @@ import { useT } from '@/i18n/useT' /** * « Accès non autorisé » (contract §5.3, BDR-094): what a catalogue or * administration page shows when the server refuses (`rights.*`, and - * `not_found` under /app/anatomy), the same whether the sheet exists or not. - * Nothing from the address is echoed. + * `not_found` under /app/anatomy), the same whether the sheet exists or not; + * also the sheet creation page for an account whose own grants hold no editor + * capability (a display rule, the server still decides). Nothing from the + * address is echoed. */ export default function AccessDenied() { const t = useT() diff --git a/apps/app/src/state/authStore.ts b/apps/app/src/state/authStore.ts index e1ec1252..cbc21a43 100644 --- a/apps/app/src/state/authStore.ts +++ b/apps/app/src/state/authStore.ts @@ -68,6 +68,8 @@ export interface AuthState { * Empty until loaded and when the read fails. */ catalogGrants: OwnCatalogGrant[] + /** Whether `catalogGrants` is read yet: a display rule waits for `ready`, and a `failed` read leaves the decision to the server. */ + catalogGrantsStatus: 'loading' | 'ready' | 'failed' /** Subscribes to the client; returns the unsubscribe. Safe to call twice. */ start: () => () => void refreshAccount: () => Promise @@ -150,8 +152,8 @@ async function loadAccount(client: FaunosteoClient, user: User, set: Set, get: G loadWorkspace(client, user, profile, setOwn, get), syncAvatar(client, user, setOwn, get).catch(() => setOwn({ avatar: null })), listOwnCatalogGrants(client, user.id) - .then((catalogGrants) => setOwn({ catalogGrants })) - .catch(() => setOwn({ catalogGrants: [] })), + .then((catalogGrants) => setOwn({ catalogGrants, catalogGrantsStatus: 'ready' })) + .catch(() => setOwn({ catalogGrants: [], catalogGrantsStatus: 'failed' })), ]) } @@ -165,6 +167,7 @@ function accountReset(get: Get): Partial { avatarUrl: null, workspace: { status: 'idle' }, catalogGrants: [], + catalogGrantsStatus: 'loading', } } @@ -236,6 +239,7 @@ export const useAuthStore = create((set, get) => ({ avatarUrl: null, workspace: { status: 'idle' }, catalogGrants: [], + catalogGrantsStatus: 'loading', start: () => { const client = getSupabaseClient() diff --git a/apps/app/tests/unit/routes.admin.test.tsx b/apps/app/tests/unit/routes.admin.test.tsx index a2c86f16..59fa8479 100644 --- a/apps/app/tests/unit/routes.admin.test.tsx +++ b/apps/app/tests/unit/routes.admin.test.tsx @@ -149,7 +149,7 @@ function mount( grants: OwnCatalogGrant[], roles: PlatformRole[] = [], ) { - useAuthStore.setState({ catalogGrants: grants, roles }) + useAuthStore.setState({ catalogGrants: grants, catalogGrantsStatus: 'ready', roles }) return render( @@ -655,6 +655,15 @@ describe('Sheet creation (A1)', () => { expect(rpcArgs('create_official_sheet')).toEqual([]) }) + it('decides nothing while the own grants load, and leaves a failed read to the server', async () => { + mount('/app/admin/sheets/new', []) + useAuthStore.setState({ catalogGrantsStatus: 'loading' }) + expect(await screen.findByRole('status')).toHaveTextContent(fr['shell.loading_session']) + expect(screen.queryByRole('heading', { name: fr['access_denied.title'] })).toBeNull() + useAuthStore.setState({ catalogGrantsStatus: 'failed' }) + expect(await screen.findByLabelText('Espèce')).toBeInTheDocument() + }) + it('proposes a slug from the French name', () => { expect(proposeSlug('Chien : squelette et rachis ')).toBe( 'chien-squelette-et-rachis', diff --git a/apps/app/tests/unit/state.authStore.test.ts b/apps/app/tests/unit/state.authStore.test.ts index 5073fac0..455abfe1 100644 --- a/apps/app/tests/unit/state.authStore.test.ts +++ b/apps/app/tests/unit/state.authStore.test.ts @@ -29,6 +29,7 @@ const INITIAL = { avatarUrl: null, workspace: { status: 'idle' as const }, catalogGrants: [], + catalogGrantsStatus: 'loading' as const, } let fake: FakeSupabase