|
| 1 | +import { ExecutionContext, UnauthorizedException } from '@nestjs/common'; |
| 2 | +import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants'; |
| 3 | +import { Authorization } from './authorization.decorator'; |
| 4 | +import Chance from 'chance'; |
| 5 | +import { AuthorizationOptions } from '../models'; |
| 6 | + |
| 7 | +interface ParamMetadata { |
| 8 | + index: number; |
| 9 | + factory: (data: any, ctx: ExecutionContext) => string | null; |
| 10 | +} |
| 11 | +describe('Authorization Decorator', () => { |
| 12 | + const chance = new Chance(); |
| 13 | + |
| 14 | + function getParamDecoratorFactory(options?: AuthorizationOptions) { |
| 15 | + class TestController { |
| 16 | + public getDecoratorValue(@Authorization(options) value: string) { |
| 17 | + return value; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + const args = Reflect.getMetadata( |
| 22 | + ROUTE_ARGS_METADATA, |
| 23 | + TestController, |
| 24 | + 'getDecoratorValue', |
| 25 | + ) as Record<string, ParamMetadata>; |
| 26 | + |
| 27 | + return args[Object.keys(args)[0]].factory; |
| 28 | + } |
| 29 | + |
| 30 | + let mockExecutionContext: ExecutionContext; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + mockExecutionContext = { |
| 34 | + switchToHttp: jest.fn().mockReturnValue({ |
| 35 | + getRequest: jest.fn(), |
| 36 | + }), |
| 37 | + } as unknown as ExecutionContext; |
| 38 | + }); |
| 39 | + |
| 40 | + it('should extract token with Bearer prefix', () => { |
| 41 | + const token = chance.string({ length: 20 }); |
| 42 | + const mockRequest = { |
| 43 | + headers: { authorization: `Bearer ${token}` }, |
| 44 | + }; |
| 45 | + |
| 46 | + ( |
| 47 | + mockExecutionContext.switchToHttp().getRequest as jest.Mock |
| 48 | + ).mockReturnValue(mockRequest); |
| 49 | + |
| 50 | + const factory = getParamDecoratorFactory(); |
| 51 | + const actualToken = factory(undefined, mockExecutionContext); |
| 52 | + |
| 53 | + expect(actualToken).toBe(token); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return null when no authorization header', () => { |
| 57 | + const mockRequest = { headers: {} }; |
| 58 | + |
| 59 | + ( |
| 60 | + mockExecutionContext.switchToHttp().getRequest as jest.Mock |
| 61 | + ).mockReturnValue(mockRequest); |
| 62 | + |
| 63 | + const factory = getParamDecoratorFactory(); |
| 64 | + const actualToken = factory(undefined, mockExecutionContext); |
| 65 | + |
| 66 | + expect(actualToken).toBeNull(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should extract token without prefix', () => { |
| 70 | + const token = chance.string({ length: 20 }); |
| 71 | + const mockRequest = { |
| 72 | + headers: { authorization: token }, |
| 73 | + }; |
| 74 | + |
| 75 | + ( |
| 76 | + mockExecutionContext.switchToHttp().getRequest as jest.Mock |
| 77 | + ).mockReturnValue(mockRequest); |
| 78 | + |
| 79 | + const factory = getParamDecoratorFactory(); |
| 80 | + const actualToken = factory(undefined, mockExecutionContext); |
| 81 | + |
| 82 | + expect(actualToken).toBe(token); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should throw when required and missing', () => { |
| 86 | + const mockRequest = { headers: {} }; |
| 87 | + |
| 88 | + ( |
| 89 | + mockExecutionContext.switchToHttp().getRequest as jest.Mock |
| 90 | + ).mockReturnValue(mockRequest); |
| 91 | + |
| 92 | + const factory = getParamDecoratorFactory({ required: true }); |
| 93 | + |
| 94 | + expect(() => factory({ required: true }, mockExecutionContext)).toThrow( |
| 95 | + UnauthorizedException, |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should handle custom prefix', () => { |
| 100 | + const token = chance.string({ length: 20 }); |
| 101 | + const mockRequest = { |
| 102 | + headers: { authorization: `Token ${token}` }, |
| 103 | + }; |
| 104 | + |
| 105 | + ( |
| 106 | + mockExecutionContext.switchToHttp().getRequest as jest.Mock |
| 107 | + ).mockReturnValue(mockRequest); |
| 108 | + |
| 109 | + const factory = getParamDecoratorFactory({ |
| 110 | + prefix: 'Token', |
| 111 | + }); |
| 112 | + const actualToken = factory({ prefix: 'Token' }, mockExecutionContext); |
| 113 | + |
| 114 | + expect(actualToken).toBe(token); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should return token as-is when no prefix matches', () => { |
| 118 | + const token = chance.string({ length: 20 }); |
| 119 | + const mockRequest = { |
| 120 | + headers: { authorization: `CustomPrefix ${token}` }, |
| 121 | + }; |
| 122 | + |
| 123 | + ( |
| 124 | + mockExecutionContext.switchToHttp().getRequest as jest.Mock |
| 125 | + ).mockReturnValue(mockRequest); |
| 126 | + |
| 127 | + const factory = getParamDecoratorFactory(); |
| 128 | + const actualToken = factory(undefined, mockExecutionContext); |
| 129 | + |
| 130 | + expect(actualToken).toBe(`CustomPrefix ${token}`); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle empty prefix option', () => { |
| 134 | + const token = chance.string({ length: 20 }); |
| 135 | + const mockRequest = { |
| 136 | + headers: { authorization: token }, |
| 137 | + }; |
| 138 | + |
| 139 | + ( |
| 140 | + mockExecutionContext.switchToHttp().getRequest as jest.Mock |
| 141 | + ).mockReturnValue(mockRequest); |
| 142 | + |
| 143 | + const factory = getParamDecoratorFactory({ prefix: '' }); |
| 144 | + const actualToken = factory({ prefix: '' }, mockExecutionContext); |
| 145 | + |
| 146 | + expect(actualToken).toBe(token); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should return empty when prefix matches but token part is empty', () => { |
| 150 | + const mockRequest = { |
| 151 | + headers: { authorization: 'Bearer' }, |
| 152 | + }; |
| 153 | + |
| 154 | + ( |
| 155 | + mockExecutionContext.switchToHttp().getRequest as jest.Mock |
| 156 | + ).mockReturnValue(mockRequest); |
| 157 | + |
| 158 | + const factory = getParamDecoratorFactory(); |
| 159 | + const actualToken = factory(undefined, mockExecutionContext); |
| 160 | + |
| 161 | + expect(actualToken).toBe(''); |
| 162 | + }); |
| 163 | +}); |
0 commit comments