fix(app): unreadable permissions show a retry, never the form (V1-2.5 L1)

Owner checkpoint 2026-11-02: creation page checks loading, server refusal, failed read, then the editor rule; Administration home shows the same notice. reloadAccess re-reads roles and grants without wiping held state; refreshAccount (no caller) removed. Zone panel live region always mounted. Unit and e2e for reviewer-only and editor.
This commit is contained in:
bchanot committed 2026-11-02 21:21:59 +01:00
1 parent 14ff890761
commit 4111efa1d1
15 files changed
+856 -56

No files matched your search

+112 -9
View File
@@ -148,8 +148,9 @@ function mount(
path: string,
grants: OwnCatalogGrant[],
roles: PlatformRole[] = [],
status: 'loading' | 'ready' | 'failed' = 'ready',
) {
useAuthStore.setState({ catalogGrants: grants, catalogGrantsStatus: 'ready', roles })
useAuthStore.setState({ catalogGrants: grants, catalogGrantsStatus: status, roles })
return render(
<MemoryRouter initialEntries={[path]}>
<Routes>
@@ -171,6 +172,7 @@ function mount(
beforeAll(async () => {
setCoreRuntime(await testCoreRuntime())
})
const realReloadAccess = useAuthStore.getState().reloadAccess
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
fake = createFakeSupabase()
@@ -180,7 +182,39 @@ beforeEach(() => {
fake.rpcResults.list_catalog_species = { data: SPECIES }
mocks.fake = fake
})
afterEach(() => cleanup())
afterEach(() => {
cleanup()
useAuthStore.setState({ reloadAccess: realReloadAccess })
})
describe('Administration page: own grants states', () => {
it('loading: the waiting status, not « aucune »', () => {
mount('/app/admin', [], [], 'loading')
expect(screen.getByText(fr['shell.loading_session'])).toHaveAttribute('role', 'status')
expect(screen.queryByText(/aucune/)).toBeNull()
})
it('failed: the unavailable notice as a section, Retry, focus kept on the wrapper', async () => {
const user = userEvent.setup()
const reloadAccess = vi.fn(async () =>
useAuthStore.setState({ catalogGrants: [grant('sheet_editor')], catalogGrantsStatus: 'ready' }),
)
mount('/app/admin', [], [], 'failed')
useAuthStore.setState({ reloadAccess })
expect(screen.getAllByRole('heading', { level: 1 })).toHaveLength(1)
expect(
screen.getByRole('heading', { level: 2, name: fr['permissions_unavailable.title'] }),
).toBeInTheDocument()
expect(screen.queryByText(/aucune/)).toBeNull()
await user.click(screen.getByRole('button', { name: 'Réessayer' }))
expect(reloadAccess).toHaveBeenCalledTimes(1)
await waitFor(() =>
expect(screen.getByText(/Vos habilitations : éditeur de fiches/)).toBeInTheDocument(),
)
const wrapper = screen.getByText(/Vos habilitations/).parentElement
expect(wrapper).toHaveFocus()
})
})
describe('Administration page (board 10)', () => {
it('editor and publisher: permissions line, sheets with badges, the working copy action disabled with its reason', async () => {
@@ -655,13 +689,82 @@ 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()
describe.each([
['a reviewer-only account', [grant('sheet_reviewer')], false],
['an editor account', [grant('sheet_editor')], true],
] as const)('permission states for %s', (_, grants, canCreate) => {
const denied = () =>
screen.queryByRole('heading', { name: fr['access_denied.title'] })
const unavailable = () =>
screen.queryByRole('heading', {
name: fr['permissions_unavailable.title'],
})
it('ready: the form for the editor, « Accès non autorisé » otherwise', async () => {
mount('/app/admin/sheets/new', [...grants])
if (canCreate) {
expect(await screen.findByLabelText('Espèce')).toBeInTheDocument()
expect(denied()).toBeNull()
} else {
expect(await screen.findByRole('heading', { name: fr['access_denied.title'] })).toBeInTheDocument()
expect(screen.queryByLabelText('Espèce')).toBeNull()
}
expect(unavailable()).toBeNull()
})
it('failed: the unavailable state, no form, no refusal deduced', async () => {
mount('/app/admin/sheets/new', [...grants], [], 'failed')
expect(await screen.findByRole('heading', { name: fr['permissions_unavailable.title'] })).toBeInTheDocument()
expect(screen.getByText(fr['permissions_unavailable.body'])).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Réessayer' })).toBeInTheDocument()
expect(document.querySelector('input, select')).toBeNull()
expect(denied()).toBeNull()
})
it('loading: waits, decides nothing', async () => {
mount('/app/admin/sheets/new', [...grants], [], 'loading')
expect(await screen.findByRole('status')).toHaveTextContent(fr['shell.loading_session'])
expect(denied()).toBeNull()
expect(unavailable()).toBeNull()
expect(document.querySelector('input, select')).toBeNull()
})
it('Retry lands on what the new answer gives', async () => {
const user = userEvent.setup()
const reloadAccess = vi.fn(async () =>
useAuthStore.setState({ catalogGrants: [...grants], catalogGrantsStatus: 'ready' }),
)
mount('/app/admin/sheets/new', [], [], 'failed')
useAuthStore.setState({ reloadAccess })
await user.click(await screen.findByRole('button', { name: 'Réessayer' }))
expect(reloadAccess).toHaveBeenCalledTimes(1)
if (canCreate) expect(await screen.findByLabelText('Espèce')).toBeInTheDocument()
else expect(await screen.findByRole('heading', { name: fr['access_denied.title'] })).toBeInTheDocument()
})
})
it('Retry also reads a failed species list again', async () => {
const user = userEvent.setup()
let calls = 0
fake.rpcResults.list_catalog_species = () =>
++calls === 1 ? { error: new TypeError('fetch failed') } : { data: SPECIES }
mount('/app/admin/sheets/new', [], [], 'failed')
useAuthStore.setState({
reloadAccess: vi.fn(async () =>
useAuthStore.setState({ catalogGrants: [grant('sheet_editor')], catalogGrantsStatus: 'ready' }),
),
})
await user.click(await screen.findByRole('button', { name: 'Réessayer' }))
const species = await screen.findByLabelText('Espèce')
await waitFor(() => expect(within(species).getAllByRole('option')).toHaveLength(3))
expect(calls).toBe(2)
})
it('a server rights refusal wins over a failed grants read', async () => {
fake.rpcResults.list_catalog_species = refusal('rights.capability')
mount('/app/admin/sheets/new', [], [], 'failed')
expect(await screen.findByRole('heading', { name: fr['access_denied.title'] })).toBeInTheDocument()
expect(screen.queryByRole('heading', { name: fr['permissions_unavailable.title'] })).toBeNull()
})
it('proposes a slug from the French name', () => {
@@ -117,6 +117,91 @@ describe('authStore', () => {
expect(useAuthStore.getState().catalogGrantsStatus).toBe('loading')
})
describe('reloadAccess', () => {
type Answer = { data: unknown; error: unknown; status?: number }
const FAILED: Answer = { data: null, error: { code: '', message: 'FetchError' }, status: 0 }
const GRANT = { user_id: 'user-1', kind: 'sheet_editor', expires_at: null, revoked_at: null }
/** Every chain on `table` ends in `answer()`, as a dropped request or a late reply would. */
function answerTable(table: string, answer: () => Answer | Promise<Answer>) {
const from = fake.client.from.bind(fake.client)
const chain: unknown = new Proxy(() => undefined, {
get: (_, key) =>
key === 'then'
? (resolve: (v: unknown) => void, reject: (e: unknown) => void) =>
Promise.resolve(answer()).then(resolve, reject)
: () => chain,
})
;(fake.client as unknown as { from: (t: string) => unknown }).from = (t: string) =>
t === table ? chain : from(t as never)
}
async function signedIn() {
fake.tables.profiles = [fakeProfile(), fakeProfile({ id: 'user-2' })]
stop = useAuthStore.getState().start()
fake.emit('SIGNED_IN', fakeSession())
await flush()
await flush()
}
it('goes loading then ready, replacing the roles and the grants', async () => {
await signedIn()
expect(useAuthStore.getState().roles).toEqual(['admin'])
fake.tables.profile_roles = [{ user_id: 'user-1', role: 'owner', revoked_at: null }]
fake.tables.anatomical_catalog_grants = [GRANT]
const reload = useAuthStore.getState().reloadAccess()
expect(useAuthStore.getState().catalogGrantsStatus).toBe('loading')
await reload
const s = useAuthStore.getState()
expect(s.catalogGrantsStatus).toBe('ready')
expect(s.roles).toEqual(['owner'])
expect(s.catalogGrants).toEqual([GRANT])
})
it.each(['anatomical_catalog_grants', 'profile_roles'])(
'ends failed with no grants and keeps the roles, profile and workspace when the %s read rejects',
async (table) => {
fake.tables.workspaces = [{ id: 'ws-1', personal_user_id: 'user-1' }]
fake.tables.anatomical_catalog_grants = [GRANT]
await signedIn()
const before = useAuthStore.getState()
expect(before.catalogGrants).toEqual([GRANT])
answerTable(table, () => FAILED)
await expect(useAuthStore.getState().reloadAccess()).resolves.toBeUndefined()
const s = useAuthStore.getState()
expect(s.catalogGrantsStatus).toBe('failed')
expect(s.catalogGrants).toEqual([])
expect(s.roles).toEqual(['admin'])
expect(s.profile).toEqual(before.profile)
expect(s.workspace).toEqual({ status: 'ready', id: 'ws-1' })
},
)
it('ends failed and resolves without a user', async () => {
useAuthStore.setState({ catalogGrants: [], catalogGrantsStatus: 'ready' })
await expect(useAuthStore.getState().reloadAccess()).resolves.toBeUndefined()
expect(useAuthStore.getState().catalogGrantsStatus).toBe('failed')
})
it('drops a late answer once another account signed in', async () => {
await signedIn()
const releases: Array<(a: Answer) => void> = []
answerTable(
'anatomical_catalog_grants',
() => new Promise<Answer>((resolve) => releases.push(resolve)),
)
const reload = useAuthStore.getState().reloadAccess()
await flush()
fake.emit('SIGNED_IN', fakeSession(fakeUser({ id: 'user-2' })))
releases.forEach((release) => release({ data: [GRANT], error: null }))
await reload
await flush()
expect(useAuthStore.getState().catalogGrants).toEqual([])
expect(useAuthStore.getState().roles).not.toEqual(['owner'])
expect(useAuthStore.getState().user?.id).toBe('user-2')
})
})
it('updateProfile writes the row and keeps the store in sync', async () => {
stop = useAuthStore.getState().start()
fake.emit('SIGNED_IN', fakeSession())