Skip to content

Commit

Permalink
test(logging): add tests for masking option of LoggingInterceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
amarlankri committed Dec 5, 2023
1 parent f0c4fb6 commit 7aa4f48
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion packages/logging-interceptor/test/logging.interceptor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('Logging interceptor', () => {
});
});

describe('Masking options', () => {
describe('@Log - Masking options', () => {
const placeholder = '****';

it('allows to mask given properties of the request body', async () => {
Expand Down Expand Up @@ -397,4 +397,78 @@ describe('Logging interceptor', () => {
]);
});
});

describe('LoggingInterceptor - Masking options', () => {
const placeholder = '****';

it('allows to mask the whole content of a request header', async () => {
const interceptor = app.get(ApplicationConfig).getGlobalInterceptors()[0] as LoggingInterceptor;
interceptor.setMask({ requestHeader: { authorization: true } });
const logSpy: jest.SpyInstance = jest.spyOn(Logger.prototype, 'log');
const url: string = `/cats/ok`;

await request(app.getHttpServer()).get(url).set('authorization', 'access-token').expect(HttpStatus.OK);

expect(logSpy.mock.calls[0][0].headers.authorization).toEqual(placeholder);
});

it('allows to mask a request header with a specific handler function', async () => {
const interceptor = app.get(ApplicationConfig).getGlobalInterceptors()[0] as LoggingInterceptor;
interceptor.setMask({
requestHeader: {
authorization: (header: string | string[]) => {
if (typeof header === 'string') {
const [type, value] = header.split(' ');

return { type, value };
} else {
return header;
}
},
},
});
const logSpy: jest.SpyInstance = jest.spyOn(Logger.prototype, 'log');
const url: string = `/cats/ok`;

await request(app.getHttpServer()).get(url).set('authorization', 'Bearer JWT').expect(HttpStatus.OK);

expect(logSpy.mock.calls[0][0].headers.authorization).toEqual({
type: 'Bearer',
value: 'JWT',
});
});

it('should not mask a request header if the corresponding mask is undefined', async () => {
const interceptor = app.get(ApplicationConfig).getGlobalInterceptors()[0] as LoggingInterceptor;
interceptor.setMask({ requestHeader: {} });
const logSpy: jest.SpyInstance = jest.spyOn(Logger.prototype, 'log');
const url: string = `/cats/ok`;

await request(app.getHttpServer()).get(url).set('authorization', 'Bearer JWT').expect(HttpStatus.OK);

expect(logSpy.mock.calls[0][0].headers.authorization).toBe('Bearer JWT');
});

it('should not mask a request header if the corresponding mask is false', async () => {
const interceptor = app.get(ApplicationConfig).getGlobalInterceptors()[0] as LoggingInterceptor;
interceptor.setMask({ requestHeader: { authorization: false } });
const logSpy: jest.SpyInstance = jest.spyOn(Logger.prototype, 'log');
const url: string = `/cats/ok`;

await request(app.getHttpServer()).get(url).set('authorization', 'Bearer JWT').expect(HttpStatus.OK);

expect(logSpy.mock.calls[0][0].headers.authorization).toBe('Bearer JWT');
});

it('should not modify the request header if it is not passed with the request but defined in masking option', async () => {
const interceptor = app.get(ApplicationConfig).getGlobalInterceptors()[0] as LoggingInterceptor;
interceptor.setMask({ requestHeader: { authorization: true } });
const logSpy: jest.SpyInstance = jest.spyOn(Logger.prototype, 'log');
const url: string = `/cats/ok`;

await request(app.getHttpServer()).get(url).expect(HttpStatus.OK);

expect(logSpy.mock.calls[0][0].headers.authorization).toBeUndefined();
});
});
});

0 comments on commit 7aa4f48

Please sign in to comment.