| 1 | import { describe, it, expect, afterAll, beforeEach } from 'vitest'
|
| 2 | import request from 'supertest'
|
| 3 | import { createApp } from '@app/createApp'
|
| 4 | import {
|
| 5 | buildPrisma,
|
| 6 | cleanupTestDb,
|
| 7 | createTestUser,
|
| 8 | generateTestToken,
|
| 9 | } from '../test-helper'
|
| 10 |
|
| 11 | const app = createApp()
|
| 12 | const db = buildPrisma()
|
| 13 |
|
| 14 | afterAll(() => db.$disconnect())
|
| 15 | beforeEach(() => cleanupTestDb(db))
|
| 16 |
|
| 17 | async function seedActivationCode(
|
| 18 | userId: number,
|
| 19 | code = '123456',
|
| 20 | expiresInMs = 5 * 60 * 1000
|
| 21 | ) {
|
| 22 | return db.user.update({
|
| 23 | where: { id: userId },
|
| 24 | data: {
|
| 25 | activationCode: code,
|
| 26 | activationCodeExpiresAt: new Date(Date.now() + expiresInMs),
|
| 27 | },
|
| 28 | })
|
| 29 | }
|
| 30 |
|
| 31 | describe('POST /auth/activate', () => {
|
| 32 | it('activates the user and clears the code', async () => {
|
| 33 | const user = await createTestUser(db, { email: 'activate@example.com' })
|
| 34 | await seedActivationCode(user.id)
|
| 35 |
|
| 36 | const res = await request(app)
|
| 37 | .post('/auth/activate')
|
| 38 | .send({ code: 123456 })
|
| 39 |
|
| 40 | expect(res.status).toBe(200)
|
| 41 | expect(res.body.user.active).toBe(true)
|
| 42 | expect(res.body.user.activationCode).toBeNull()
|
| 43 |
|
| 44 | const dbUser = await db.user.findUnique({ where: { id: user.id } })
|
| 45 | expect(dbUser!.active).toBe(true)
|
| 46 | expect(dbUser!.activationCode).toBeNull()
|
| 47 | })
|
| 48 |
|
| 49 | it('returns 422 for an unknown code', async () => {
|
| 50 | await createTestUser(db, { email: 'activate@example.com' })
|
| 51 |
|
| 52 | const res = await request(app)
|
| 53 | .post('/auth/activate')
|
| 54 | .send({ code: 999999 })
|
| 55 |
|
| 56 | expect(res.status).toBe(422)
|
| 57 | expect(res.body.error).toBeTruthy()
|
| 58 | })
|
| 59 |
|
| 60 | it('returns 400 when the code is not a number', async () => {
|
| 61 | const res = await request(app)
|
| 62 | .post('/auth/activate')
|
| 63 | .send({ code: '123456' })
|
| 64 |
|
| 65 | expect(res.status).toBe(400)
|
| 66 | expect(res.body.fields).toBeTruthy()
|
| 67 | })
|
| 68 | })
|
| 69 |
|
| 70 | describe('POST /auth/resend_code', () => {
|
| 71 | it('issues a fresh activation code for the authenticated user', async () => {
|
| 72 | const user = await createTestUser(db, { email: 'resend@example.com' })
|
| 73 | const token = generateTestToken(user.id, user.email)
|
| 74 |
|
| 75 | const res = await request(app)
|
| 76 | .post('/auth/resend_code')
|
| 77 | .set('Authorization', `Bearer ${token}`)
|
| 78 |
|
| 79 | expect(res.status).toBe(200)
|
| 80 |
|
| 81 | const dbUser = await db.user.findUnique({ where: { id: user.id } })
|
| 82 | expect(dbUser!.activationCode).toMatch(/^\d{6}$/)
|
| 83 | expect(dbUser!.activationCodeExpiresAt).not.toBeNull()
|
| 84 | })
|
| 85 |
|
| 86 | it('returns 401 without a token', async () => {
|
| 87 | const res = await request(app).post('/auth/resend_code')
|
| 88 | expect(res.status).toBe(401)
|
| 89 | })
|
| 90 | })
|