|
| 1 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 2 | +import { ExecutionContext, UnauthorizedException } from '@nestjs/common'; |
| 3 | +import { JwtService } from '@nestjs/jwt'; |
| 4 | +import { ConfigService } from '@nestjs/config'; |
| 5 | +import { WsJwtGuard } from './ws-jwt.guard'; |
| 6 | +import { Socket } from 'socket.io'; |
| 7 | + |
| 8 | +describe('WsJwtGuard', () => { |
| 9 | + let guard: WsJwtGuard; |
| 10 | + let jwt_service: JwtService; |
| 11 | + let config_service: ConfigService; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + const module: TestingModule = await Test.createTestingModule({ |
| 15 | + providers: [ |
| 16 | + WsJwtGuard, |
| 17 | + { |
| 18 | + provide: JwtService, |
| 19 | + useValue: { |
| 20 | + verify: jest.fn(), |
| 21 | + }, |
| 22 | + }, |
| 23 | + { |
| 24 | + provide: ConfigService, |
| 25 | + useValue: { |
| 26 | + get: jest.fn().mockReturnValue('test-secret'), |
| 27 | + }, |
| 28 | + }, |
| 29 | + ], |
| 30 | + }).compile(); |
| 31 | + |
| 32 | + guard = module.get<WsJwtGuard>(WsJwtGuard); |
| 33 | + jwt_service = module.get<JwtService>(JwtService); |
| 34 | + config_service = module.get<ConfigService>(ConfigService); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + jest.clearAllMocks(); |
| 39 | + jest.restoreAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('canActivate', () => { |
| 43 | + it('should be defined', () => { |
| 44 | + expect(guard).toBeDefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return true for non-WebSocket contexts', () => { |
| 48 | + const context = { |
| 49 | + getType: jest.fn().mockReturnValue('http'), |
| 50 | + } as any as ExecutionContext; |
| 51 | + |
| 52 | + const result = guard.canActivate(context); |
| 53 | + |
| 54 | + expect(result).toBe(true); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should validate token and attach user to client for WebSocket context', () => { |
| 58 | + const mock_user = { id: '123', email: 'test@example.com' }; |
| 59 | + const mock_client = { |
| 60 | + handshake: { |
| 61 | + headers: { |
| 62 | + authorization: 'Bearer valid-token', |
| 63 | + }, |
| 64 | + }, |
| 65 | + data: {}, |
| 66 | + } as any as Socket; |
| 67 | + |
| 68 | + const context = { |
| 69 | + getType: jest.fn().mockReturnValue('ws'), |
| 70 | + switchToWs: jest.fn().mockReturnValue({ |
| 71 | + getClient: jest.fn().mockReturnValue(mock_client), |
| 72 | + }), |
| 73 | + } as any as ExecutionContext; |
| 74 | + |
| 75 | + jest.spyOn(jwt_service, 'verify').mockReturnValue(mock_user); |
| 76 | + |
| 77 | + const result = guard.canActivate(context); |
| 78 | + |
| 79 | + expect(result).toBe(true); |
| 80 | + expect(mock_client.data.user).toEqual(mock_user); |
| 81 | + expect(jwt_service.verify).toHaveBeenCalledWith('valid-token', { |
| 82 | + secret: 'test-secret', |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return false when validateToken returns null', () => { |
| 87 | + const mock_client = { |
| 88 | + handshake: { |
| 89 | + headers: { |
| 90 | + authorization: 'Bearer invalid-token', |
| 91 | + }, |
| 92 | + }, |
| 93 | + data: {}, |
| 94 | + } as any as Socket; |
| 95 | + |
| 96 | + const context = { |
| 97 | + getType: jest.fn().mockReturnValue('ws'), |
| 98 | + switchToWs: jest.fn().mockReturnValue({ |
| 99 | + getClient: jest.fn().mockReturnValue(mock_client), |
| 100 | + }), |
| 101 | + } as any as ExecutionContext; |
| 102 | + |
| 103 | + jest.spyOn(WsJwtGuard, 'validateToken').mockReturnValue(null); |
| 104 | + |
| 105 | + const result = guard.canActivate(context); |
| 106 | + |
| 107 | + expect(result).toBe(false); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('validateToken', () => { |
| 112 | + it('should validate and return user payload for valid token', () => { |
| 113 | + const mock_user = { id: '123', email: 'test@example.com' }; |
| 114 | + const mock_client = { |
| 115 | + handshake: { |
| 116 | + headers: { |
| 117 | + authorization: 'Bearer valid-token', |
| 118 | + }, |
| 119 | + }, |
| 120 | + } as any as Socket; |
| 121 | + |
| 122 | + (jwt_service.verify as jest.Mock).mockReturnValue(mock_user); |
| 123 | + |
| 124 | + const result = WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 125 | + |
| 126 | + expect(result).toEqual(mock_user); |
| 127 | + expect(jwt_service.verify).toHaveBeenCalledWith('valid-token', { |
| 128 | + secret: 'test-secret', |
| 129 | + }); |
| 130 | + expect(config_service.get).toHaveBeenCalledWith('JWT_TOKEN_SECRET'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should throw UnauthorizedException when authorization header is missing', () => { |
| 134 | + const mock_client = { |
| 135 | + handshake: { |
| 136 | + headers: {}, |
| 137 | + }, |
| 138 | + } as any as Socket; |
| 139 | + |
| 140 | + expect(() => { |
| 141 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 142 | + }).toThrow(UnauthorizedException); |
| 143 | + |
| 144 | + expect(() => { |
| 145 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 146 | + }).toThrow('No authorization header'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should throw UnauthorizedException when authorization header is undefined', () => { |
| 150 | + const mock_client = { |
| 151 | + handshake: { |
| 152 | + headers: { |
| 153 | + authorization: undefined, |
| 154 | + }, |
| 155 | + }, |
| 156 | + } as any as Socket; |
| 157 | + |
| 158 | + expect(() => { |
| 159 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 160 | + }).toThrow(UnauthorizedException); |
| 161 | + |
| 162 | + expect(() => { |
| 163 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 164 | + }).toThrow('No authorization header'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should throw UnauthorizedException when token is missing', () => { |
| 168 | + const mock_client = { |
| 169 | + handshake: { |
| 170 | + headers: { |
| 171 | + authorization: 'Bearer', |
| 172 | + }, |
| 173 | + }, |
| 174 | + } as any as Socket; |
| 175 | + |
| 176 | + expect(() => { |
| 177 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 178 | + }).toThrow(UnauthorizedException); |
| 179 | + |
| 180 | + expect(() => { |
| 181 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 182 | + }).toThrow('Invalid authorization format'); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should throw UnauthorizedException when token after Bearer is empty', () => { |
| 186 | + const mock_client = { |
| 187 | + handshake: { |
| 188 | + headers: { |
| 189 | + authorization: 'Bearer ', |
| 190 | + }, |
| 191 | + }, |
| 192 | + } as any as Socket; |
| 193 | + |
| 194 | + expect(() => { |
| 195 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 196 | + }).toThrow(UnauthorizedException); |
| 197 | + |
| 198 | + expect(() => { |
| 199 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 200 | + }).toThrow('Invalid authorization format'); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should throw UnauthorizedException when authorization format is invalid', () => { |
| 204 | + const mock_client = { |
| 205 | + handshake: { |
| 206 | + headers: { |
| 207 | + authorization: 'InvalidFormat', |
| 208 | + }, |
| 209 | + }, |
| 210 | + } as any as Socket; |
| 211 | + |
| 212 | + expect(() => { |
| 213 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 214 | + }).toThrow(UnauthorizedException); |
| 215 | + |
| 216 | + expect(() => { |
| 217 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 218 | + }).toThrow('Invalid authorization format'); |
| 219 | + }); |
| 220 | + |
| 221 | + it('should throw UnauthorizedException when JWT verification fails', () => { |
| 222 | + const mock_client = { |
| 223 | + handshake: { |
| 224 | + headers: { |
| 225 | + authorization: 'Bearer invalid-token', |
| 226 | + }, |
| 227 | + }, |
| 228 | + } as any as Socket; |
| 229 | + |
| 230 | + (jwt_service.verify as jest.Mock).mockImplementation(() => { |
| 231 | + throw new Error('Invalid token'); |
| 232 | + }); |
| 233 | + |
| 234 | + expect(() => { |
| 235 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 236 | + }).toThrow(UnauthorizedException); |
| 237 | + |
| 238 | + expect(() => { |
| 239 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 240 | + }).toThrow('Invalid token'); |
| 241 | + }); |
| 242 | + |
| 243 | + it('should throw UnauthorizedException when JWT is expired', () => { |
| 244 | + const mock_client = { |
| 245 | + handshake: { |
| 246 | + headers: { |
| 247 | + authorization: 'Bearer expired-token', |
| 248 | + }, |
| 249 | + }, |
| 250 | + } as any as Socket; |
| 251 | + |
| 252 | + (jwt_service.verify as jest.Mock).mockImplementation(() => { |
| 253 | + throw new Error('jwt expired'); |
| 254 | + }); |
| 255 | + |
| 256 | + expect(() => { |
| 257 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 258 | + }).toThrow(UnauthorizedException); |
| 259 | + }); |
| 260 | + |
| 261 | + it('should handle malformed JWT tokens', () => { |
| 262 | + const mock_client = { |
| 263 | + handshake: { |
| 264 | + headers: { |
| 265 | + authorization: 'Bearer malformed.token', |
| 266 | + }, |
| 267 | + }, |
| 268 | + } as any as Socket; |
| 269 | + |
| 270 | + (jwt_service.verify as jest.Mock).mockImplementation(() => { |
| 271 | + throw new Error('jwt malformed'); |
| 272 | + }); |
| 273 | + |
| 274 | + expect(() => { |
| 275 | + WsJwtGuard.validateToken(mock_client, jwt_service, config_service); |
| 276 | + }).toThrow(UnauthorizedException); |
| 277 | + }); |
| 278 | + }); |
| 279 | +}); |
0 commit comments