|
| 1 | +// test/api/user.test.ts |
| 2 | +import Fastify, { FastifyInstance } from 'fastify'; |
| 3 | +import sensible from '@fastify/sensible'; |
| 4 | +import rootRoutes from '../../../src/routes'; |
| 5 | +import errorHandler from '../../../src/plugins/errorHandler'; |
| 6 | + |
| 7 | +describe('User API Routes', () => { |
| 8 | + let app: FastifyInstance; |
| 9 | + |
| 10 | + beforeAll(async () => { |
| 11 | + app = Fastify(); |
| 12 | + await app.register(sensible); |
| 13 | + |
| 14 | + // Register routes with API prefix and error handler |
| 15 | + await app.register( |
| 16 | + async (fastify) => { |
| 17 | + await fastify.register(errorHandler); |
| 18 | + await fastify.register(rootRoutes); |
| 19 | + }, |
| 20 | + { prefix: '/api' }, |
| 21 | + ); |
| 22 | + |
| 23 | + await app.ready(); |
| 24 | + }); |
| 25 | + |
| 26 | + afterAll(async () => { |
| 27 | + await app.close(); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('POST /api/user', () => { |
| 31 | + const validUser = { |
| 32 | + name: 'john_doe', |
| 33 | + |
| 34 | + password: 'password123', |
| 35 | + }; |
| 36 | + |
| 37 | + // TODO: Add test for valid user creation |
| 38 | + |
| 39 | + test('should reject user with conflicting name', async () => { |
| 40 | + // Try to create another user with the same name |
| 41 | + const response = await app.inject({ |
| 42 | + method: 'POST', |
| 43 | + url: '/api/user', |
| 44 | + payload: validUser, |
| 45 | + }); |
| 46 | + |
| 47 | + expect(response.statusCode).toBe(409); |
| 48 | + expect(response.json()).toMatchObject({ |
| 49 | + error: 'User with this name already exists', |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + test('should reject invalid name format', async () => { |
| 54 | + const response = await app.inject({ |
| 55 | + method: 'POST', |
| 56 | + url: '/api/user', |
| 57 | + payload: { |
| 58 | + ...validUser, |
| 59 | + name: 'invalid@name', |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(response.statusCode).toBe(400); |
| 64 | + expect(response.json()).toMatchObject({ |
| 65 | + error: 'Validation Error', |
| 66 | + details: expect.arrayContaining([ |
| 67 | + expect.objectContaining({ |
| 68 | + message: 'name must contain only letters, numbers, and underscores', |
| 69 | + }), |
| 70 | + ]), |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + test('should reject missing required fields', async () => { |
| 75 | + const response = await app.inject({ |
| 76 | + method: 'POST', |
| 77 | + url: '/api/user', |
| 78 | + payload: {}, |
| 79 | + }); |
| 80 | + |
| 81 | + expect(response.statusCode).toBe(400); |
| 82 | + expect(response.json()).toMatchObject({ |
| 83 | + error: 'Validation Error', |
| 84 | + details: expect.arrayContaining([ |
| 85 | + expect.objectContaining({ |
| 86 | + message: 'name is required', |
| 87 | + }), |
| 88 | + ]), |
| 89 | + }); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('GET /api/user', () => { |
| 94 | + test('should return list of user names', async () => { |
| 95 | + const response = await app.inject({ |
| 96 | + method: 'GET', |
| 97 | + url: '/api/user', |
| 98 | + }); |
| 99 | + |
| 100 | + expect(response.statusCode).toBe(200); |
| 101 | + expect(Array.isArray(response.json())).toBe(true); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments