eletrotupi / tcc/ api/tests/exports/create.test.tsc901c35af9c84b6a29e5185545ced1129f33b8f4
1.3 KB | 46 lines Raw
1
import { describe, it, expect, afterAll, beforeEach } from 'vitest'
2
import request from 'supertest'
3
import { createApp } from '@app/createApp'
4
import { ExportJobName } from '@app/lib/queue/types'
5
import { buildPrisma, cleanupTestDb, createTestUser, generateTestToken } from '../test-helper'
6
import { recordedJobs, resetQueueMocks } from '../mocks/queue'
7

8
const app = createApp()
9
const db = buildPrisma()
10

11
afterAll(() => db.$disconnect())
12
beforeEach(async () => {
13
  await cleanupTestDb(db)
14

15
  resetQueueMocks()
16
})
17

18
describe('POST /exports', () => {
19
  it('returns 401 without a token and enqueues nothing', async () => {
20
    const res = await request(app).post('/exports')
21

22
    expect(res.status).toBe(401)
23
    expect(recordedJobs).toHaveLength(0)
24
  })
25

26
  it('enqueues exactly one export job for the authenticated user', async () => {
27
    const user = await createTestUser(db)
28
    const token = generateTestToken(user.id, user.email)
29

30
    const res = await request(app)
31
      .post('/exports')
32
      .set('Authorization', `Bearer ${token}`)
33

34
    expect(res.status).toBe(202)
35
    expect(res.body.message).toBeTruthy()
36

37
    expect(recordedJobs).toHaveLength(1)
38
    expect(recordedJobs[0]).toEqual(
39
      expect.objectContaining({
40
        queue: 'exports',
41
        name: ExportJobName.Data,
42
        data: { userId: user.id },
43
      })
44
    )
45
  })
46
})