import { flushPromises } from '@vue/test-utils' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { resetAuth } from '@/composables/useAuth' import ForgotPasswordView from '@/views/ForgotPasswordView.vue' import LoginView from '@/views/LoginView.vue' import ResetPasswordView from '@/views/ResetPasswordView.vue' import { jsonResponse, makeRouter, mockFetch, mountWithPlugins } from './helpers' beforeEach(resetAuth) afterEach(() => { vi.unstubAllGlobals() history.replaceState(null, '') }) const NEW_PASSWORD = 'un-tout-nouveau-mot-de-passe' describe('ForgotPasswordView', () => { it('envoie la demande puis affiche « Vérifiez votre boîte mail »', async () => { const fetch = mockFetch(jsonResponse(202), jsonResponse(202)) const wrapper = mountWithPlugins(ForgotPasswordView) await wrapper.find('#email').setValue('medecin@exemple.be') await wrapper.find('form').trigger('submit') await flushPromises() expect(fetch.mock.calls[0]![0]).toBe('/api/v1/auth/password-reset/') expect(JSON.parse(fetch.mock.calls[0]![1]!.body as string)).toEqual({ email: 'medecin@exemple.be', }) expect(wrapper.find('h1').text()).toBe('Vérifiez votre boîte mail') expect(wrapper.text()).toContain('Si un compte existe pour medecin@exemple.be') await wrapper.find('button').trigger('click') await flushPromises() expect(fetch).toHaveBeenCalledTimes(2) expect(wrapper.find('button').attributes('disabled')).toBeDefined() }) it('signale la limitation de débit', async () => { mockFetch(jsonResponse(429, { detail: 'Throttled' })) const wrapper = mountWithPlugins(ForgotPasswordView) await wrapper.find('#email').setValue('medecin@exemple.be') await wrapper.find('form').trigger('submit') await flushPromises() expect(wrapper.find('[role="alert"]').text()).toContain('Trop de demandes') }) }) describe('ResetPasswordView', () => { async function mountWithToken(token: string | null, ...responses: Response[]) { const fetch = mockFetch(...responses) const router = makeRouter() await router.push(token ? `/reinitialisation/${token}` : '/reinitialisation') const push = vi.spyOn(router, 'push') const wrapper = mountWithPlugins(ResetPasswordView, { router }) await flushPromises() return { wrapper, router, fetch, push } } it('vérifie le lien, retire le jeton de l’URL et affiche le formulaire', async () => { const { wrapper, router, fetch } = await mountWithToken('Mw.abc-123', jsonResponse(204)) expect(JSON.parse(fetch.mock.calls[0]![1]!.body as string)).toEqual({ token: 'Mw.abc-123' }) expect(router.currentRoute.value.fullPath).toBe('/reinitialisation') expect(wrapper.find('h1').text()).toBe('Nouveau mot de passe') }) it('lien invalide ou expiré : propose un nouveau lien', async () => { const { wrapper } = await mountWithToken( 'vieux', jsonResponse(400, { code: 'invalid_token', detail: '' }), ) expect(wrapper.find('h1').text()).toBe('Lien invalide ou expiré') expect(wrapper.find('a.btn').attributes('href')).toBe('/mot-de-passe-oublie') }) it('sans jeton : lien invalide, sans appel à l’API', async () => { const { wrapper, fetch } = await mountWithToken(null) expect(wrapper.find('h1').text()).toBe('Lien invalide ou expiré') expect(fetch).not.toHaveBeenCalled() }) it('enregistre le mot de passe puis renvoie à la connexion avec un message', async () => { const { wrapper, fetch, push } = await mountWithToken( 'Mw.abc-123', jsonResponse(204), jsonResponse(204), ) push.mockResolvedValue(undefined) await wrapper.find('#password').setValue(NEW_PASSWORD) await wrapper.find('#confirm').setValue(NEW_PASSWORD) await wrapper.find('form').trigger('submit') await flushPromises() expect(fetch.mock.calls[1]![0]).toBe('/api/v1/auth/password-reset/confirm/') expect(JSON.parse(fetch.mock.calls[1]![1]!.body as string)).toEqual({ token: 'Mw.abc-123', new_password: NEW_PASSWORD, }) expect(push).toHaveBeenCalledWith({ name: 'login', state: { notice: 'password-reset' } }) }) it('bloque l’envoi si les mots de passe diffèrent ou sont trop courts', async () => { const { wrapper } = await mountWithToken('Mw.abc-123', jsonResponse(204)) const button = () => wrapper.find('button[type="submit"]') await wrapper.find('#password').setValue(NEW_PASSWORD) await wrapper.find('#confirm').setValue('autre-mot-de-passe-long') expect(button().attributes('disabled')).toBeDefined() expect(wrapper.find('#confirm').classes()).toContain('is-invalid') await wrapper.find('#password').setValue('court') await wrapper.find('#confirm').setValue('court') expect(button().attributes('disabled')).toBeDefined() }) it('affiche le refus du mot de passe par le serveur', async () => { const { wrapper } = await mountWithToken( 'Mw.abc-123', jsonResponse(204), jsonResponse(400, { new_password: ['Ce mot de passe est trop courant.'] }), ) await wrapper.find('#password').setValue(NEW_PASSWORD) await wrapper.find('#confirm').setValue(NEW_PASSWORD) await wrapper.find('form').trigger('submit') await flushPromises() expect(wrapper.find('#password').classes()).toContain('is-invalid') expect(wrapper.text()).toContain('Ce mot de passe est trop courant.') }) it('lien devenu invalide entre-temps : écran « Lien invalide »', async () => { const { wrapper } = await mountWithToken( 'Mw.abc-123', jsonResponse(204), jsonResponse(400, { code: 'invalid_token', detail: '' }), ) await wrapper.find('#password').setValue(NEW_PASSWORD) await wrapper.find('#confirm').setValue(NEW_PASSWORD) await wrapper.find('form').trigger('submit') await flushPromises() expect(wrapper.find('h1').text()).toBe('Lien invalide ou expiré') }) }) describe('LoginView', () => { it('propose le lien « Mot de passe oublié ? »', () => { const wrapper = mountWithPlugins(LoginView) const link = wrapper.findAll('a').find((a) => a.text() === 'Mot de passe oublié ?') expect(link?.attributes('href')).toBe('/mot-de-passe-oublie') }) it('affiche la confirmation après réinitialisation', () => { history.replaceState({ notice: 'password-reset' }, '') const wrapper = mountWithPlugins(LoginView) expect(wrapper.find('[role="status"]').text()).toContain('Votre mot de passe a été modifié') }) })