| 1 | import { describe, it, expect, afterAll, beforeEach } from 'vitest'
|
| 2 | import request from 'supertest'
|
| 3 | import { createApp } from '@app/createApp'
|
| 4 | import { buildPrisma, cleanupTestDb, createTestUser } 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 /auth/login', () => {
|
| 13 | it('returns a token for valid credentials', async () => {
|
| 14 | await createTestUser(db, { email: 'login@example.com', password: 'senha123' })
|
| 15 |
|
| 16 | const res = await request(app)
|
| 17 | .post('/auth/login')
|
| 18 | .send({ email: 'login@example.com', password: 'senha123' })
|
| 19 |
|
| 20 | expect(res.status).toBe(200)
|
| 21 | expect(res.body.token).toBeTruthy()
|
| 22 | expect(res.body.user.email).toBe('login@example.com')
|
| 23 | })
|
| 24 |
|
| 25 | it('returns 401 for wrong password', async () => {
|
| 26 | await createTestUser(db, { email: 'login@example.com', password: 'senha123' })
|
| 27 |
|
| 28 | const res = await request(app)
|
| 29 | .post('/auth/login')
|
| 30 | .send({ email: 'login@example.com', password: 'errada' })
|
| 31 |
|
| 32 | expect(res.status).toBe(401)
|
| 33 | })
|
| 34 |
|
| 35 | it('returns 401 for unknown email', async () => {
|
| 36 | const res = await request(app)
|
| 37 | .post('/auth/login')
|
| 38 | .send({ email: 'naoexiste@example.com', password: 'qualquer' })
|
| 39 |
|
| 40 | expect(res.status).toBe(401)
|
| 41 | })
|
| 42 | })
|