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.
109 lines
4.3 KiB
TypeScript
109 lines
4.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { cleanup, render, screen } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { MemoryRouter, Route, Routes } from 'react-router-dom'
|
|
import {
|
|
createFakeSupabase,
|
|
fakeFactor,
|
|
fakeUser,
|
|
setMfa,
|
|
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 ResetPasswordRoute from '@/app/routes/auth/ResetPasswordRoute'
|
|
import { useAuthStore } from '@/state/authStore'
|
|
import { useLocaleStore } from '@/state/localeStore'
|
|
import { fr } from '@/i18n/fr'
|
|
|
|
let fake: FakeSupabase
|
|
|
|
beforeEach(() => {
|
|
useLocaleStore.setState({ locale: 'fr' })
|
|
fake = createFakeSupabase()
|
|
mocks.fake = fake
|
|
fake.auth.updateUser.mockResolvedValue({ data: {}, error: null })
|
|
useAuthStore.setState({ status: 'signed_in', user: fakeUser(), recovery: true })
|
|
})
|
|
afterEach(cleanup)
|
|
|
|
function mount() {
|
|
return render(
|
|
<MemoryRouter initialEntries={['/reset-password']}>
|
|
<Routes>
|
|
<Route path="/reset-password" element={<ResetPasswordRoute />} />
|
|
<Route path="/app" element={<p>dashboard page</p>} />
|
|
</Routes>
|
|
</MemoryRouter>,
|
|
)
|
|
}
|
|
|
|
async function submitPassword(user: ReturnType<typeof userEvent.setup>) {
|
|
await user.type(screen.getByLabelText(fr['auth.password']), 'CorrectHorse12')
|
|
await user.type(screen.getByLabelText(fr['auth.password_confirm']), 'CorrectHorse12')
|
|
await user.click(screen.getByRole('button', { name: fr['auth.reset.submit'] }))
|
|
}
|
|
|
|
describe('ResetPasswordRoute', () => {
|
|
it('saves the new password at once for an account without a factor', async () => {
|
|
const user = userEvent.setup()
|
|
mount()
|
|
await submitPassword(user)
|
|
expect(await screen.findByText('dashboard page')).toBeInTheDocument()
|
|
expect(fake.auth.updateUser).toHaveBeenCalledWith({ password: 'CorrectHorse12' })
|
|
expect(fake.auth.mfa.challengeAndVerify).not.toHaveBeenCalled()
|
|
expect(useAuthStore.getState().recovery).toBe(false)
|
|
})
|
|
|
|
it('with a verified factor, asks for a code before sending the password', async () => {
|
|
const user = userEvent.setup()
|
|
setMfa(fake, [fakeFactor({ id: 'f-1' })], 'aal1')
|
|
mount()
|
|
await submitPassword(user)
|
|
expect(await screen.findByText(fr['mfa.step_up.required'])).toBeInTheDocument()
|
|
expect(fake.auth.updateUser).not.toHaveBeenCalled()
|
|
await user.type(screen.getByLabelText(fr['mfa.code.label']), '123456')
|
|
await user.click(screen.getByRole('button', { name: fr['mfa.code.verify'] }))
|
|
expect(fake.auth.mfa.challengeAndVerify).toHaveBeenCalledWith({ factorId: 'f-1', code: '123456' })
|
|
expect(await screen.findByText('dashboard page')).toBeInTheDocument()
|
|
expect(fake.auth.updateUser).toHaveBeenCalledWith({ password: 'CorrectHorse12' })
|
|
})
|
|
|
|
it('steps up when GoTrue refuses for insufficient_aal, then sends it again', async () => {
|
|
const user = userEvent.setup()
|
|
// The level read says nothing (no factor in the session yet), GoTrue knows better.
|
|
fake.auth.mfa.listFactors.mockResolvedValue({
|
|
data: { all: [fakeFactor({ id: 'f-1' })], totp: [] },
|
|
error: null,
|
|
})
|
|
fake.auth.updateUser.mockResolvedValueOnce({
|
|
data: null,
|
|
error: { name: 'AuthApiError', code: 'insufficient_aal', status: 401, message: 'raw' },
|
|
})
|
|
mount()
|
|
await submitPassword(user)
|
|
expect(await screen.findByText(fr['mfa.step_up.required'])).toBeInTheDocument()
|
|
await user.type(screen.getByLabelText(fr['mfa.code.label']), '123456')
|
|
await user.click(screen.getByRole('button', { name: fr['mfa.code.verify'] }))
|
|
expect(await screen.findByText('dashboard page')).toBeInTheDocument()
|
|
expect(fake.auth.updateUser).toHaveBeenCalledTimes(2)
|
|
})
|
|
|
|
it('phrases insufficient_aal when the account has no factor to step up with', async () => {
|
|
const user = userEvent.setup()
|
|
fake.auth.updateUser.mockResolvedValueOnce({
|
|
data: null,
|
|
error: { name: 'AuthApiError', code: 'insufficient_aal', status: 401, message: 'raw' },
|
|
})
|
|
mount()
|
|
await submitPassword(user)
|
|
expect(await screen.findByText(fr['auth.failure.aal2_required'])).toBeInTheDocument()
|
|
expect(screen.queryByText('raw')).toBeNull()
|
|
})
|
|
})
|