| 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('activates an account whose code begins with zero', async () => {
|
| 50 | const user = await createTestUser(db, { email: 'leading-zero@example.com' })
|
| 51 | await seedActivationCode(user.id, '012345')
|
| 52 |
|
| 53 | const res = await request(app)
|
| 54 | .post('/auth/activate')
|
| 55 | .send({ code: '012345' })
|
| 56 |
|
| 57 | expect(res.status).toBe(200)
|
| 58 | expect(res.body.user.active).toBe(true)
|
| 59 |
|
| 60 | const dbUser = await db.user.findUnique({ where: { id: user.id } })
|
| 61 | expect(dbUser!.active).toBe(true)
|
| 62 | expect(dbUser!.activationCode).toBeNull()
|
| 63 | })
|
| 64 |
|
| 65 | it('returns 422 for a well-formed but unknown code', async () => {
|
| 66 | await createTestUser(db, { email: 'activate@example.com' })
|
| 67 |
|
| 68 | const res = await request(app)
|
| 69 | .post('/auth/activate')
|
| 70 | .send({ code: '999999' })
|
| 71 |
|
| 72 | expect(res.status).toBe(422)
|
| 73 | expect(res.body.error).toBeTruthy()
|
| 74 | })
|
| 75 |
|
| 76 | it.each([
|
| 77 | ['too short', '12345'],
|
| 78 | ['too long', '1234567'],
|
| 79 | ['non-digit', '12a456'],
|
| 80 | ['a number', 123456],
|
| 81 | ])('rejects a code that is %s as malformed', async (_shape, code) => {
|
| 82 | await createTestUser(db, { email: 'activate@example.com' })
|
| 83 |
|
| 84 | const res = await request(app)
|
| 85 | .post('/auth/activate')
|
| 86 | .send({ code })
|
| 87 |
|
| 88 | expect(res.status).toBe(400)
|
| 89 | expect(res.body.fields.code).toBeTruthy()
|
| 90 | })
|
| 91 | })
|
| 92 |
|
| 93 | describe('POST /auth/resend_code', () => {
|
| 94 | it('issues a fresh activation code for the authenticated user', async () => {
|
| 95 | const user = await createTestUser(db, { email: 'resend@example.com' })
|
| 96 | const token = generateTestToken(user.id, user.email)
|
| 97 |
|
| 98 | const res = await request(app)
|
| 99 | .post('/auth/resend_code')
|
| 100 | .set('Authorization', `Bearer ${token}`)
|
| 101 |
|
| 102 | expect(res.status).toBe(200)
|
| 103 |
|
| 104 | const dbUser = await db.user.findUnique({ where: { id: user.id } })
|
| 105 | expect(dbUser!.activationCode).toMatch(/^\d{6}$/)
|
| 106 | expect(dbUser!.activationCodeExpiresAt).not.toBeNull()
|
| 107 | })
|
| 108 |
|
| 109 | it('returns 401 without a token', async () => {
|
| 110 | const res = await request(app).post('/auth/resend_code')
|
| 111 | expect(res.status).toBe(401)
|
| 112 | })
|
| 113 | })
|