| 1 | import { describe, it, expect, afterAll, beforeEach } from 'vitest'
|
| 2 | import request from 'supertest'
|
| 3 | import { createApp } from '@app/createApp'
|
| 4 | import { buildPrisma, cleanupTestDb, createTestUser } from '../test-helper'
|
| 5 | import { recordedJobs, resetQueueMocks } from '../mocks/queue'
|
| 6 |
|
| 7 | const app = createApp()
|
| 8 | const db = buildPrisma()
|
| 9 |
|
| 10 | afterAll(() => db.$disconnect())
|
| 11 | beforeEach(async () => {
|
| 12 | await cleanupTestDb(db)
|
| 13 |
|
| 14 | resetQueueMocks()
|
| 15 | })
|
| 16 |
|
| 17 | describe('POST /auth/forgot-password', () => {
|
| 18 | it('stores a reset token and enqueues the email for a known user', async () => {
|
| 19 | const user = await createTestUser(db, { email: 'forgot@example.com' })
|
| 20 |
|
| 21 | const res = await request(app)
|
| 22 | .post('/auth/forgot-password')
|
| 23 | .send({ email: 'forgot@example.com' })
|
| 24 |
|
| 25 | expect(res.status).toBe(200)
|
| 26 | expect(res.body.success).toBe(true)
|
| 27 | expect(res.body.token).toBeUndefined()
|
| 28 |
|
| 29 | const dbUser = await db.user.findUnique({ where: { id: user.id } })
|
| 30 |
|
| 31 | expect(dbUser!.passwordResetToken).toBeTruthy()
|
| 32 | expect(dbUser!.passwordResetExpires).not.toBeNull()
|
| 33 |
|
| 34 | expect(recordedJobs).toContainEqual(
|
| 35 | expect.objectContaining({
|
| 36 | queue: 'mail',
|
| 37 | name: 'mail:password-reset',
|
| 38 | data: { userId: user.id, token: dbUser!.passwordResetToken },
|
| 39 | })
|
| 40 | )
|
| 41 | })
|
| 42 |
|
| 43 | it('answers 200 without a token for an unknown email (no enumeration)', async () => {
|
| 44 | const res = await request(app)
|
| 45 | .post('/auth/forgot-password')
|
| 46 | .send({ email: 'ghost@example.com' })
|
| 47 |
|
| 48 | expect(res.status).toBe(200)
|
| 49 | expect(res.body.success).toBe(true)
|
| 50 | expect(res.body.token).toBeUndefined()
|
| 51 | expect(recordedJobs).toHaveLength(0)
|
| 52 | })
|
| 53 |
|
| 54 | it('returns 400 for an invalid email', async () => {
|
| 55 | const res = await request(app)
|
| 56 | .post('/auth/forgot-password')
|
| 57 | .send({ email: 'not-an-email' })
|
| 58 |
|
| 59 | expect(res.status).toBe(400)
|
| 60 | expect(res.body.error).toBeTruthy()
|
| 61 | expect(res.body.fields).toBeTruthy()
|
| 62 | })
|
| 63 | })
|