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( } /> dashboard page

} />
, ) } async function submitPassword(user: ReturnType) { 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() }) })