-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlogger.test.ts
95 lines (84 loc) · 2.86 KB
/
logger.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import test from 'ava'
import { start } from '../src'
import { Adapter, AdapterEndpoint } from '../src/adapter'
import { AdapterConfig, SettingsDefinitionMap } from '../src/config'
import CensorList from '../src/util/censor/censor-list'
import { COLORS, censor, colorFactory } from '../src/util/logger'
import { NopTransport } from '../src/util/testing-utils'
test.before(async () => {
const customSettings = {
API_KEY: {
description: 'Test custom env var',
type: 'string',
sensitive: true,
},
} satisfies SettingsDefinitionMap
process.env['API_KEY'] = 'mock-api-key'
const config = new AdapterConfig(customSettings)
const adapter = new Adapter({
name: 'TEST',
config,
endpoints: [
new AdapterEndpoint({
name: 'test',
transport: new NopTransport(),
}),
],
})
await start(adapter)
})
test('properly builds censor list', async (t) => {
const censorList = CensorList.getAll()
// eslint-disable-next-line prefer-regex-literals
t.deepEqual(censorList[0], { key: 'API_KEY', value: RegExp('mock\\-api\\-key', 'gi') })
})
test('properly redacts API_KEY (string)', async (t) => {
const redacted = censor('mock-api-key', CensorList.getAll())
t.is(redacted, '[API_KEY REDACTED]')
})
test('properly redacts API_KEY (string with added text)', async (t) => {
const redacted = censor('Bearer mock-api-key', CensorList.getAll())
t.is(redacted, 'Bearer [API_KEY REDACTED]')
})
test('properly redacts API_KEY (object)', async (t) => {
const redacted = censor({ apiKey: 'mock-api-key' }, CensorList.getAll())
t.deepEqual(redacted, { apiKey: '[API_KEY REDACTED]' })
})
test('properly redacts API_KEY (object with added text)', async (t) => {
const redacted = censor({ apiKey: 'Bearer mock-api-key' }, CensorList.getAll())
t.deepEqual(redacted, { apiKey: 'Bearer [API_KEY REDACTED]' })
})
test('properly handles undefined property', async (t) => {
const redacted = censor({ apiKey: undefined }, CensorList.getAll())
t.deepEqual(redacted, {})
})
test('properly handles undefined', async (t) => {
const redacted = censor(undefined, CensorList.getAll())
t.deepEqual(redacted, undefined)
})
test('properly redacts API_KEY (multiple nested values)', async (t) => {
const redacted = censor(
{ apiKey: 'mock-api-key', config: { headers: { auth: 'mock-api-key' } } },
CensorList.getAll(),
)
t.deepEqual(redacted, {
apiKey: '[API_KEY REDACTED]',
config: { headers: { auth: '[API_KEY REDACTED]' } },
})
})
test('Test color factory', async (t) => {
const nextColor = colorFactory(COLORS)
for (let i = 0; i < COLORS.length; i++) {
t.is(nextColor(), COLORS[i])
}
// Test that the colors cycle back
t.is(nextColor(), COLORS[0])
})
test('properly handle circular references', async (t) => {
const a = {
b: {},
}
a.b = a
const log = censor(a, CensorList.getAll())
t.is(log, '[Unknown]')
})