|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { INestApplication, ValidationPipe } from '@nestjs/common'; |
| 3 | +import { ConfigModule } from '@nestjs/config'; |
| 4 | +import { JwtModule, JwtService } from '@nestjs/jwt'; |
| 5 | +import { PassportModule } from '@nestjs/passport'; |
| 6 | +import * as request from 'supertest'; |
| 7 | + |
| 8 | +import { AuthController } from './auth.controller'; |
| 9 | +import { AuthService } from './auth.service'; |
| 10 | +import { JwtStrategy } from './strategies/jwt.strategy'; |
| 11 | +import { UsersService } from '../users/users.service'; |
| 12 | + |
| 13 | +const TEST_SECRET = 'e2e-test-secret'; |
| 14 | + |
| 15 | +const mockUser = { |
| 16 | + id: 'user-1', |
| 17 | + email: 'test@example.com', |
| 18 | + username: 'testuser', |
| 19 | + role: 'USER', |
| 20 | +}; |
| 21 | + |
| 22 | +describe('Auth Module (e2e)', () => { |
| 23 | + let app: INestApplication; |
| 24 | + let jwtService: JwtService; |
| 25 | + |
| 26 | + const mockUsersService = { |
| 27 | + findOne: jest.fn(), |
| 28 | + findAll: jest.fn(), |
| 29 | + }; |
| 30 | + |
| 31 | + beforeAll(async () => { |
| 32 | + const module: TestingModule = await Test.createTestingModule({ |
| 33 | + imports: [ |
| 34 | + ConfigModule.forRoot({ |
| 35 | + isGlobal: true, |
| 36 | + load: [() => ({ JWT_SECRET: TEST_SECRET })], |
| 37 | + }), |
| 38 | + PassportModule.register({ defaultStrategy: 'jwt' }), |
| 39 | + JwtModule.register({ |
| 40 | + secret: TEST_SECRET, |
| 41 | + signOptions: { expiresIn: '1h' }, |
| 42 | + }), |
| 43 | + ], |
| 44 | + controllers: [AuthController], |
| 45 | + providers: [ |
| 46 | + AuthService, |
| 47 | + JwtStrategy, |
| 48 | + { provide: UsersService, useValue: mockUsersService }, |
| 49 | + ], |
| 50 | + }).compile(); |
| 51 | + |
| 52 | + app = module.createNestApplication(); |
| 53 | + app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true })); |
| 54 | + await app.init(); |
| 55 | + |
| 56 | + jwtService = module.get(JwtService); |
| 57 | + }); |
| 58 | + |
| 59 | + afterAll(() => app.close()); |
| 60 | + |
| 61 | + afterEach(() => jest.clearAllMocks()); |
| 62 | + |
| 63 | + function getAccessToken(userId: string) { |
| 64 | + return jwtService.sign({ sub: userId, email: mockUser.email }); |
| 65 | + } |
| 66 | + |
| 67 | + describe('GET /v1/auth/users', () => { |
| 68 | + it('returns 401 without a bearer token', async () => { |
| 69 | + const { status } = await request(app.getHttpServer()).get('/v1/auth/users'); |
| 70 | + expect(status).toBe(401); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns paginated users with valid token', async () => { |
| 74 | + mockUsersService.findOne.mockResolvedValue(mockUser); |
| 75 | + mockUsersService.findAll.mockResolvedValue({ |
| 76 | + data: [mockUser], |
| 77 | + total: 1, |
| 78 | + }); |
| 79 | + |
| 80 | + const token = getAccessToken(mockUser.id); |
| 81 | + |
| 82 | + const { status, body } = await request(app.getHttpServer()) |
| 83 | + .get('/v1/auth/users?limit=10&page=1') |
| 84 | + .set('Authorization', `Bearer ${token}`); |
| 85 | + |
| 86 | + expect(status).toBe(200); |
| 87 | + expect(body).toHaveProperty('data'); |
| 88 | + expect(body).toHaveProperty('limit'); |
| 89 | + expect(body).toHaveProperty('hasMore'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns 401 with an invalid token', async () => { |
| 93 | + const { status } = await request(app.getHttpServer()) |
| 94 | + .get('/v1/auth/users') |
| 95 | + .set('Authorization', 'Bearer invalid.token.here'); |
| 96 | + |
| 97 | + expect(status).toBe(401); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('GET /v1/auth/profile', () => { |
| 102 | + it('returns 401 without a bearer token', async () => { |
| 103 | + const { status } = await request(app.getHttpServer()).get( |
| 104 | + '/v1/auth/profile', |
| 105 | + ); |
| 106 | + expect(status).toBe(401); |
| 107 | + }); |
| 108 | + |
| 109 | + it('returns the current user profile with valid token', async () => { |
| 110 | + mockUsersService.findOne.mockResolvedValue(mockUser); |
| 111 | + |
| 112 | + const token = getAccessToken(mockUser.id); |
| 113 | + |
| 114 | + const { status, body } = await request(app.getHttpServer()) |
| 115 | + .get('/v1/auth/profile') |
| 116 | + .set('Authorization', `Bearer ${token}`); |
| 117 | + |
| 118 | + expect(status).toBe(200); |
| 119 | + expect(body).toHaveProperty('id', mockUser.id); |
| 120 | + expect(body).toHaveProperty('email', mockUser.email); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments