|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import request from 'supertest'; |
| 3 | +import app from '../../../app'; |
| 4 | + |
| 5 | +describe('Auth API', () => { |
| 6 | + describe('POST /api/auth/register', () => { |
| 7 | + it('registers a new user', async () => { |
| 8 | + const res = await request(app).post('/api/auth/register').send({ |
| 9 | + email: 'new@test.com', |
| 10 | + password: 'password123', |
| 11 | + name: 'New User', |
| 12 | + }); |
| 13 | + expect(res.status).toBe(201); |
| 14 | + expect(res.body.data.user.email).toBe('new@test.com'); |
| 15 | + expect(res.body.data.user).not.toHaveProperty('passwordHash'); |
| 16 | + expect(res.body.data.accessToken).toBeTruthy(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('rejects duplicate email', async () => { |
| 20 | + await request(app).post('/api/auth/register').send({ |
| 21 | + email: 'dupe@test.com', |
| 22 | + password: 'password123', |
| 23 | + name: 'First', |
| 24 | + }); |
| 25 | + const res = await request(app).post('/api/auth/register').send({ |
| 26 | + email: 'dupe@test.com', |
| 27 | + password: 'password123', |
| 28 | + name: 'Second', |
| 29 | + }); |
| 30 | + expect(res.status).toBe(409); |
| 31 | + }); |
| 32 | + |
| 33 | + it('validates required fields', async () => { |
| 34 | + const res = await request(app).post('/api/auth/register').send({ email: 'bad' }); |
| 35 | + expect(res.status).toBe(400); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + describe('POST /api/auth/login', () => { |
| 40 | + it('logs in with correct credentials', async () => { |
| 41 | + const res = await request(app).post('/api/auth/login').send({ |
| 42 | + email: 'customer@test.com', |
| 43 | + password: 'password123', |
| 44 | + }); |
| 45 | + expect(res.status).toBe(200); |
| 46 | + expect(res.body.data.accessToken).toBeTruthy(); |
| 47 | + expect(res.headers['set-cookie']).toBeDefined(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('rejects wrong password', async () => { |
| 51 | + const res = await request(app).post('/api/auth/login').send({ |
| 52 | + email: 'customer@test.com', |
| 53 | + password: 'wrongpassword', |
| 54 | + }); |
| 55 | + expect(res.status).toBe(401); |
| 56 | + }); |
| 57 | + |
| 58 | + it('rejects unknown email', async () => { |
| 59 | + const res = await request(app).post('/api/auth/login').send({ |
| 60 | + email: 'nobody@test.com', |
| 61 | + password: 'password123', |
| 62 | + }); |
| 63 | + expect(res.status).toBe(401); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('GET /api/auth/me', () => { |
| 68 | + it('returns current user with valid token', async () => { |
| 69 | + const loginRes = await request(app).post('/api/auth/login').send({ |
| 70 | + email: 'customer@test.com', |
| 71 | + password: 'password123', |
| 72 | + }); |
| 73 | + const token = loginRes.body.data.accessToken; |
| 74 | + |
| 75 | + const res = await request(app) |
| 76 | + .get('/api/auth/me') |
| 77 | + .set('Authorization', `Bearer ${token}`); |
| 78 | + expect(res.status).toBe(200); |
| 79 | + expect(res.body.data.user.email).toBe('customer@test.com'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('rejects unauthenticated request', async () => { |
| 83 | + const res = await request(app).get('/api/auth/me'); |
| 84 | + expect(res.status).toBe(401); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('POST /api/auth/logout', () => { |
| 89 | + it('clears the refresh token cookie', async () => { |
| 90 | + const loginRes = await request(app).post('/api/auth/login').send({ |
| 91 | + email: 'customer@test.com', |
| 92 | + password: 'password123', |
| 93 | + }); |
| 94 | + const cookies = loginRes.headers['set-cookie']; |
| 95 | + |
| 96 | + const res = await request(app) |
| 97 | + .post('/api/auth/logout') |
| 98 | + .set('Cookie', cookies); |
| 99 | + expect(res.status).toBe(200); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments