|
| 1 | +/** |
| 2 | + * ResponseSanitizerInterceptor — unit tests |
| 3 | + * |
| 4 | + * Covers: |
| 5 | + * - Strips privateKey from response body |
| 6 | + * - Strips encryptedSecret from response body |
| 7 | + * - Strips nested sensitive fields |
| 8 | + * - Passes through non-sensitive fields unchanged |
| 9 | + * - Handles null/undefined responses |
| 10 | + * - Handles array responses |
| 11 | + */ |
| 12 | +import { ResponseSanitizerInterceptor } from './response-sanitizer.interceptor'; |
| 13 | +import { ExecutionContext, CallHandler } from '@nestjs/common'; |
| 14 | +import { of } from 'rxjs'; |
| 15 | +import { firstValueFrom } from 'rxjs'; |
| 16 | + |
| 17 | +describe('ResponseSanitizerInterceptor', () => { |
| 18 | + let interceptor: ResponseSanitizerInterceptor; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + interceptor = new ResponseSanitizerInterceptor(); |
| 22 | + }); |
| 23 | + |
| 24 | + function mockContext(): ExecutionContext { |
| 25 | + return { |
| 26 | + switchToHttp: () => ({ |
| 27 | + getRequest: () => ({}), |
| 28 | + getResponse: () => ({}), |
| 29 | + }), |
| 30 | + getHandler: () => ({}), |
| 31 | + getClass: () => ({}), |
| 32 | + } as ExecutionContext; |
| 33 | + } |
| 34 | + |
| 35 | + function callHandler(responseBody: unknown): CallHandler { |
| 36 | + return { handle: () => of(responseBody) }; |
| 37 | + } |
| 38 | + |
| 39 | + it('strips privateKey from the response body', async () => { |
| 40 | + const body = { |
| 41 | + wallet: { id: 'wallet-1', publicKey: 'GABC' }, |
| 42 | + privateKey: 'S-secret-key', |
| 43 | + isNewWallet: true, |
| 44 | + }; |
| 45 | + |
| 46 | + const result = await firstValueFrom( |
| 47 | + interceptor.intercept(mockContext(), callHandler(body)), |
| 48 | + ); |
| 49 | + |
| 50 | + expect(result.privateKey).toBe('[REDACTED]'); |
| 51 | + expect(result.wallet.id).toBe('wallet-1'); |
| 52 | + expect(result.isNewWallet).toBe(true); |
| 53 | + }); |
| 54 | + |
| 55 | + it('strips encryptedSecret from the response body', async () => { |
| 56 | + const body = { |
| 57 | + wallet: { |
| 58 | + id: 'wallet-1', |
| 59 | + encryptedSecret: 'enc-very-secret', |
| 60 | + publicKey: 'GABC', |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + const result = await firstValueFrom( |
| 65 | + interceptor.intercept(mockContext(), callHandler(body)), |
| 66 | + ); |
| 67 | + |
| 68 | + expect(result.wallet.encryptedSecret).toBe('[REDACTED]'); |
| 69 | + expect(result.wallet.publicKey).toBe('GABC'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('strips nested sensitive fields deep in the response', async () => { |
| 73 | + const body = { |
| 74 | + data: { |
| 75 | + items: [ |
| 76 | + { privateKey: 'S-key-1', name: 'item1' }, |
| 77 | + { encryptedSecret: 'enc-2', name: 'item2' }, |
| 78 | + ], |
| 79 | + }, |
| 80 | + }; |
| 81 | + |
| 82 | + const result = await firstValueFrom( |
| 83 | + interceptor.intercept(mockContext(), callHandler(body)), |
| 84 | + ); |
| 85 | + |
| 86 | + expect(result.data.items[0].privateKey).toBe('[REDACTED]'); |
| 87 | + expect(result.data.items[0].name).toBe('item1'); |
| 88 | + expect(result.data.items[1].encryptedSecret).toBe('[REDACTED]'); |
| 89 | + expect(result.data.items[1].name).toBe('item2'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('passes through non-sensitive fields unchanged', async () => { |
| 93 | + const body = { |
| 94 | + wallet: { id: 'w-1', publicKey: 'GABC', status: 'ACTIVE' }, |
| 95 | + isNewWallet: false, |
| 96 | + idempotencyKey: 'key-123', |
| 97 | + }; |
| 98 | + |
| 99 | + const result = await firstValueFrom( |
| 100 | + interceptor.intercept(mockContext(), callHandler(body)), |
| 101 | + ); |
| 102 | + |
| 103 | + expect(result.wallet.id).toBe('w-1'); |
| 104 | + expect(result.wallet.publicKey).toBe('GABC'); |
| 105 | + expect(result.wallet.status).toBe('ACTIVE'); |
| 106 | + expect(result.isNewWallet).toBe(false); |
| 107 | + expect(result.idempotencyKey).toBe('key-123'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('handles null and undefined responses', async () => { |
| 111 | + const nullResult = await firstValueFrom( |
| 112 | + interceptor.intercept(mockContext(), callHandler(null)), |
| 113 | + ); |
| 114 | + expect(nullResult).toBeNull(); |
| 115 | + |
| 116 | + const undefinedResult = await firstValueFrom( |
| 117 | + interceptor.intercept(mockContext(), callHandler(undefined)), |
| 118 | + ); |
| 119 | + expect(undefinedResult).toBeUndefined(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('handles array responses', async () => { |
| 123 | + const body = [ |
| 124 | + { privateKey: 'S-key-1', publicKey: 'GABC' }, |
| 125 | + { privateKey: 'S-key-2', publicKey: 'GDEF' }, |
| 126 | + ]; |
| 127 | + |
| 128 | + const result = await firstValueFrom( |
| 129 | + interceptor.intercept(mockContext(), callHandler(body)), |
| 130 | + ); |
| 131 | + |
| 132 | + expect(result[0].privateKey).toBe('[REDACTED]'); |
| 133 | + expect(result[0].publicKey).toBe('GABC'); |
| 134 | + expect(result[1].privateKey).toBe('[REDACTED]'); |
| 135 | + expect(result[1].publicKey).toBe('GDEF'); |
| 136 | + }); |
| 137 | +}); |
0 commit comments