generated from UK-Export-Finance/nestjs-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(APIM-344): unit test for logger class ConsoleLoggerWithRedact
- Loading branch information
Showing
2 changed files
with
45 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |