eletrotupi / tcc/ api/tests/users/create.test.tse95369371855208056f9243ac68d0ace4cac4cb3
1.4 KB | 47 lines Raw
1
import { describe, it, expect, afterAll, beforeEach } from 'vitest'
2
import request from 'supertest'
3
import { createApp } from '@app/createApp'
4
import { buildPrisma, cleanupTestDb } from '../test-helper'
5

6
const app = createApp()
7
const db = buildPrisma()
8

9
afterAll(() => db.$disconnect())
10
beforeEach(() => cleanupTestDb(db))
11

12
describe('POST /users', () => {
13
  it('creates a user and returns a JWT token', async () => {
14
    const res = await request(app)
15
      .post('/users')
16
      .send({ user: { email: 'new@example.com', firstName: 'Novo', password: 'senha123' } })
17

18
    expect(res.status).toBe(201)
19
    expect(res.body.token).toBeTruthy()
20

21
    const dbUser = await db.user.findUnique({ where: { email: 'new@example.com' } })
22
    expect(dbUser).not.toBeNull()
23
    expect(dbUser!.firstName).toBe('Novo')
24
  })
25

26
  it('returns 409 for duplicate email', async () => {
27
    await db.user.create({
28
      data: { email: 'dup@example.com', firstName: 'A', encryptedPassword: 'x' },
29
    })
30

31
    const res = await request(app)
32
      .post('/users')
33
      .send({ user: { email: 'dup@example.com', firstName: 'B', password: 'senha123' } })
34

35
    expect(res.status).toBe(409)
36
  })
37

38
  it('returns 400 for missing required fields', async () => {
39
    const res = await request(app)
40
      .post('/users')
41
      .send({ user: { email: 'x@example.com' } })
42

43
    expect(res.status).toBe(400)
44
    expect(res.body.error).toBeTruthy()
45
    expect(res.body.fields).toBeTruthy()
46
  })
47
})