|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { getRepositoryToken } from '@nestjs/typeorm'; |
| 3 | +import { UnauthorizedException } from '@nestjs/common'; |
| 4 | +import { JwtStrategy, JwtPayload } from './jwt.strategy'; |
| 5 | +import { User, UserStatus } from '../users/entities/user.entity'; |
| 6 | + |
| 7 | +describe('JwtStrategy', () => { |
| 8 | + let strategy: JwtStrategy; |
| 9 | + |
| 10 | + const mockUserRepo = { |
| 11 | + findOneBy: jest.fn(), |
| 12 | + createQueryBuilder: jest.fn(), |
| 13 | + }; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + const module: TestingModule = await Test.createTestingModule({ |
| 17 | + providers: [JwtStrategy, { provide: getRepositoryToken(User), useValue: mockUserRepo }], |
| 18 | + }).compile(); |
| 19 | + |
| 20 | + strategy = module.get<JwtStrategy>(JwtStrategy); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => jest.clearAllMocks()); |
| 24 | + |
| 25 | + it('should be defined', () => { |
| 26 | + expect(strategy).toBeDefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('validate', () => { |
| 30 | + const payload: JwtPayload = { |
| 31 | + sub: 'user-1', |
| 32 | + email: 'test@example.com', |
| 33 | + roles: [], |
| 34 | + permissions: [], |
| 35 | + }; |
| 36 | + |
| 37 | + const mockUser = { |
| 38 | + id: 'user-1', |
| 39 | + email: 'test@example.com', |
| 40 | + status: UserStatus.ACTIVE, |
| 41 | + }; |
| 42 | + |
| 43 | + const mockUserWithRolesAndPermissions = { |
| 44 | + ...mockUser, |
| 45 | + roles: [ |
| 46 | + { |
| 47 | + name: 'student', |
| 48 | + permissions: [{ resource: 'course', action: 'read' }], |
| 49 | + }, |
| 50 | + ], |
| 51 | + }; |
| 52 | + |
| 53 | + it('should successfully validate and return payload with roles and permissions if user is active', async () => { |
| 54 | + mockUserRepo.findOneBy.mockResolvedValue(mockUser); |
| 55 | + |
| 56 | + const mockQueryBuilder = { |
| 57 | + leftJoinAndSelect: jest.fn().mockReturnThis(), |
| 58 | + where: jest.fn().mockReturnThis(), |
| 59 | + getOne: jest.fn().mockResolvedValue(mockUserWithRolesAndPermissions), |
| 60 | + }; |
| 61 | + mockUserRepo.createQueryBuilder.mockReturnValue(mockQueryBuilder); |
| 62 | + |
| 63 | + const result = await strategy.validate(payload); |
| 64 | + |
| 65 | + expect(mockUserRepo.findOneBy).toHaveBeenCalledWith({ id: 'user-1' }); |
| 66 | + expect(result).toEqual({ |
| 67 | + sub: 'user-1', |
| 68 | + email: 'test@example.com', |
| 69 | + roles: ['student'], |
| 70 | + permissions: ['course:read'], |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should throw UnauthorizedException if the user is suspended', async () => { |
| 75 | + mockUserRepo.findOneBy.mockResolvedValue({ |
| 76 | + ...mockUser, |
| 77 | + status: UserStatus.SUSPENDED, |
| 78 | + }); |
| 79 | + |
| 80 | + await expect(strategy.validate(payload)).rejects.toThrow(UnauthorizedException); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should throw UnauthorizedException if the user is inactive', async () => { |
| 84 | + mockUserRepo.findOneBy.mockResolvedValue({ |
| 85 | + ...mockUser, |
| 86 | + status: UserStatus.INACTIVE, |
| 87 | + }); |
| 88 | + |
| 89 | + await expect(strategy.validate(payload)).rejects.toThrow(UnauthorizedException); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should throw an error if the user is not found', async () => { |
| 93 | + mockUserRepo.findOneBy.mockResolvedValue(null); |
| 94 | + |
| 95 | + await expect(strategy.validate(payload)).rejects.toThrow(Error); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments