|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { INestApplication } from '@nestjs/common'; |
| 3 | +import * as request from 'supertest'; |
| 4 | +import { TypeOrmModule } from '@nestjs/typeorm'; |
| 5 | +import { UsersModule } from '../users.module'; |
| 6 | +import { User } from '../entities/user.entity'; |
| 7 | + |
| 8 | +describe('UsersController (e2e)', () => { |
| 9 | + let app: INestApplication; |
| 10 | + |
| 11 | + beforeAll(async () => { |
| 12 | + const moduleFixture: TestingModule = await Test.createTestingModule({ |
| 13 | + imports: [ |
| 14 | + TypeOrmModule.forRoot({ |
| 15 | + type: 'sqlite', |
| 16 | + database: ':memory:', |
| 17 | + dropSchema: true, |
| 18 | + entities: [User], |
| 19 | + synchronize: true, |
| 20 | + }), |
| 21 | + UsersModule, |
| 22 | + ], |
| 23 | + }).compile(); |
| 24 | + |
| 25 | + app = moduleFixture.createNestApplication(); |
| 26 | + await app.init(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterAll(async () => { |
| 30 | + await app.close(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should create a user', async () => { |
| 34 | + const res = await request(app.getHttpServer()) |
| 35 | + .post('/users') |
| 36 | + .send({ |
| 37 | + fullName: 'Test User', |
| 38 | + email: 'test@example.com', |
| 39 | + password: 'password123', |
| 40 | + role: 'admin', |
| 41 | + }); |
| 42 | + expect(res.status).toBe(201); |
| 43 | + expect(res.body).toHaveProperty('id'); |
| 44 | + expect(res.body.email).toBe('test@example.com'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should get all users', async () => { |
| 48 | + const res = await request(app.getHttpServer()).get('/users'); |
| 49 | + expect(res.status).toBe(200); |
| 50 | + expect(Array.isArray(res.body)).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should get a user by id', async () => { |
| 54 | + const createRes = await request(app.getHttpServer()) |
| 55 | + .post('/users') |
| 56 | + .send({ |
| 57 | + fullName: 'Another User', |
| 58 | + email: 'another@example.com', |
| 59 | + password: 'password123', |
| 60 | + role: 'user', |
| 61 | + }); |
| 62 | + const userId = createRes.body.id; |
| 63 | + const res = await request(app.getHttpServer()).get(`/users/${userId}`); |
| 64 | + expect(res.status).toBe(200); |
| 65 | + expect(res.body.id).toBe(userId); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should update a user', async () => { |
| 69 | + const createRes = await request(app.getHttpServer()) |
| 70 | + .post('/users') |
| 71 | + .send({ |
| 72 | + fullName: 'Update User', |
| 73 | + email: 'update@example.com', |
| 74 | + password: 'password123', |
| 75 | + role: 'user', |
| 76 | + }); |
| 77 | + const userId = createRes.body.id; |
| 78 | + const res = await request(app.getHttpServer()) |
| 79 | + .patch(`/users/${userId}`) |
| 80 | + .send({ fullName: 'Updated Name' }); |
| 81 | + expect(res.status).toBe(200); |
| 82 | + expect(res.body.fullName).toBe('Updated Name'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should delete a user', async () => { |
| 86 | + const createRes = await request(app.getHttpServer()) |
| 87 | + .post('/users') |
| 88 | + .send({ |
| 89 | + fullName: 'Delete User', |
| 90 | + email: 'delete@example.com', |
| 91 | + password: 'password123', |
| 92 | + role: 'user', |
| 93 | + }); |
| 94 | + const userId = createRes.body.id; |
| 95 | + const res = await request(app.getHttpServer()).delete(`/users/${userId}`); |
| 96 | + expect(res.status).toBe(200); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should not create user with duplicate email', async () => { |
| 100 | + await request(app.getHttpServer()) |
| 101 | + .post('/users') |
| 102 | + .send({ |
| 103 | + fullName: 'Dup User', |
| 104 | + email: 'dup@example.com', |
| 105 | + password: 'password123', |
| 106 | + role: 'user', |
| 107 | + }); |
| 108 | + const res = await request(app.getHttpServer()) |
| 109 | + .post('/users') |
| 110 | + .send({ |
| 111 | + fullName: 'Dup User2', |
| 112 | + email: 'dup@example.com', |
| 113 | + password: 'password123', |
| 114 | + role: 'user', |
| 115 | + }); |
| 116 | + expect(res.status).toBeGreaterThanOrEqual(400); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should filter users by role', async () => { |
| 120 | + await request(app.getHttpServer()) |
| 121 | + .post('/users') |
| 122 | + .send({ |
| 123 | + fullName: 'Admin User', |
| 124 | + email: 'admin@example.com', |
| 125 | + password: 'password123', |
| 126 | + role: 'admin', |
| 127 | + }); |
| 128 | + const res = await request(app.getHttpServer()) |
| 129 | + .get('/users?role=admin'); |
| 130 | + expect(res.status).toBe(200); |
| 131 | + expect(res.body.some((u: any) => u.role === 'admin')).toBe(true); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should paginate users', async () => { |
| 135 | + for (let i = 0; i < 15; i++) { |
| 136 | + await request(app.getHttpServer()) |
| 137 | + .post('/users') |
| 138 | + .send({ |
| 139 | + fullName: `User${i}`, |
| 140 | + email: `user${i}@example.com`, |
| 141 | + password: 'password123', |
| 142 | + role: 'user', |
| 143 | + }); |
| 144 | + } |
| 145 | + const res = await request(app.getHttpServer()) |
| 146 | + .get('/users?page=2&limit=10'); |
| 147 | + expect(res.status).toBe(200); |
| 148 | + expect(res.body.length).toBeLessThanOrEqual(10); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should not expose passwordHash in user response', async () => { |
| 152 | + const res = await request(app.getHttpServer()) |
| 153 | + .post('/users') |
| 154 | + .send({ |
| 155 | + fullName: 'Hidden Password', |
| 156 | + email: 'hidden@example.com', |
| 157 | + password: 'password123', |
| 158 | + role: 'user', |
| 159 | + }); |
| 160 | + expect(res.body.passwordHash).toBeUndefined(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should return 404 for non-existent user', async () => { |
| 164 | + const res = await request(app.getHttpServer()).get('/users/non-existent-id'); |
| 165 | + expect(res.status).toBe(404); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should validate required fields on create', async () => { |
| 169 | + const res = await request(app.getHttpServer()) |
| 170 | + .post('/users') |
| 171 | + .send({ email: 'invalid@example.com' }); |
| 172 | + expect(res.status).toBeGreaterThanOrEqual(400); |
| 173 | + }); |
| 174 | +}); |
0 commit comments