| 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 | expectPublicUser,
|
| 10 | } from '../test-helper'
|
| 11 |
|
| 12 | const app = createApp()
|
| 13 | const db = buildPrisma()
|
| 14 |
|
| 15 | afterAll(() => db.$disconnect())
|
| 16 | beforeEach(() => cleanupTestDb(db))
|
| 17 |
|
| 18 | describe('GET /auth/verify', () => {
|
| 19 | it('returns the token payload and a safe slice of the user', async () => {
|
| 20 | const user = await createTestUser(db, {
|
| 21 | email: 'verify@example.com',
|
| 22 | firstName: 'Pedro',
|
| 23 | })
|
| 24 |
|
| 25 | const token = generateTestToken(user.id, user.email)
|
| 26 |
|
| 27 | const res = await request(app)
|
| 28 | .get('/auth/verify')
|
| 29 | .set('Authorization', `Bearer ${token}`)
|
| 30 |
|
| 31 | expect(res.status).toBe(200)
|
| 32 | expect(res.body.valid).toBe(true)
|
| 33 | expect(res.body.userId).toBe(user.id)
|
| 34 | expect(res.body.email).toBe('verify@example.com')
|
| 35 | expect(res.body.user.firstName).toBe('Pedro')
|
| 36 | expectPublicUser(res.body.user)
|
| 37 | })
|
| 38 |
|
| 39 | it('returns 401 without an authorization header', async () => {
|
| 40 | const res = await request(app).get('/auth/verify')
|
| 41 | expect(res.status).toBe(401)
|
| 42 | })
|
| 43 |
|
| 44 | it('returns 401 for a malformed token', async () => {
|
| 45 | const res = await request(app)
|
| 46 | .get('/auth/verify')
|
| 47 | .set('Authorization', 'Bearer not.a.jwt')
|
| 48 |
|
| 49 | expect(res.status).toBe(401)
|
| 50 | })
|
| 51 |
|
| 52 | it('returns 404 when the token is valid but the user is gone', async () => {
|
| 53 | const token = generateTestToken(999999, 'ghost@example.com')
|
| 54 |
|
| 55 | const res = await request(app)
|
| 56 | .get('/auth/verify')
|
| 57 | .set('Authorization', `Bearer ${token}`)
|
| 58 |
|
| 59 | expect(res.status).toBe(404)
|
| 60 | })
|
| 61 | })
|