Skip to content

Commit

Permalink
feat(APIM-344): unit test for logger class ConsoleLoggerWithRedact
Browse files Browse the repository at this point in the history
  • Loading branch information
avaitonis committed Jul 19, 2023
1 parent 3a3b646 commit 4950a04
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/helpers/redact-strings-in-log-args.helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ describe('Redact errors helper', () => {

describe('redactStringsInLogArgs', () => {
const domain = valueGenerator.httpsUrl();
const otherSensitivefield = valueGenerator.word();
const message = `ConnectionError: Failed to connect to ${domain}, ${otherSensitivefield}`;
const otherSensitiveField = valueGenerator.word();
const message = `ConnectionError: Failed to connect to ${domain}, ${otherSensitiveField}`;
const redactedMessage = `ConnectionError: Failed to connect to [RedactedDomain], [Redacted]`;
const redactStrings = [
{ searchValue: domain, replaceValue: '[RedactedDomain]' },
{ searchValue: otherSensitivefield, replaceValue: '[Redacted]' },
{ searchValue: otherSensitiveField, replaceValue: '[Redacted]' },
];
const args = [
{
Expand Down
42 changes: 42 additions & 0 deletions src/logging/console-logger-with-redact.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ConsoleLogger } from '@nestjs/common';
import { RandomValueGenerator } from '@ukef-test/support/generator/random-value-generator';

import { ConsoleLoggerWithRedact } from './console-logger-with-redact';

const mockedSuperError = jest.spyOn(ConsoleLogger.prototype, 'error');

describe('ConsoleLoggerWithRedact', () => {
const valueGenerator = new RandomValueGenerator();
const domain = valueGenerator.httpsUrl();
const otherSensitiveField = valueGenerator.word();
const message = `ConnectionError: Failed to connect to ${domain}, ${otherSensitiveField}`;
const redactedMessage = `ConnectionError: Failed to connect to [RedactedDomain], [Redacted]`;
const redactStrings = [
{ searchValue: domain, replaceValue: '[RedactedDomain]' },
{ searchValue: otherSensitiveField, replaceValue: '[Redacted]' },
];
const logger = new ConsoleLoggerWithRedact(redactStrings);

beforeEach(() => {
mockedSuperError.mockClear();
});

it('redacts sensitive data in `message`', () => {
logger.error(message);

expect(mockedSuperError).toHaveBeenCalledWith(redactedMessage, undefined, undefined);
});

it('redacts sensitive data in `message` and second parameter `stack`', () => {
logger.error(message, message);

expect(mockedSuperError).toHaveBeenCalledWith(redactedMessage, redactedMessage, undefined);
});

it('redacts sensitive data in `message` and second parameter `stack`, also passes context', () => {
const context = valueGenerator.word();
logger.error(message, message, context);

expect(mockedSuperError).toHaveBeenCalledWith(redactedMessage, redactedMessage, context);
});
});

0 comments on commit 4950a04

Please sign in to comment.