-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlogger.test.ts
More file actions
121 lines (108 loc) · 3.83 KB
/
logger.test.ts
File metadata and controls
121 lines (108 loc) · 3.83 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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,
},
AB_LAMBO_MODEL: {
description: 'Test harmless env var that is safe to log',
type: 'string',
sensitive: false,
},
API_PRIVATE_KEY: {
description: 'Test env var that a developer forgot to explicitly flag as sensitive',
type: 'string',
},
} satisfies SettingsDefinitionMap
process.env['API_KEY'] = 'mock-api-key'
process.env['AB_LAMBO_MODEL'] = 'revuelto'
process.env['API_PRIVATE_KEY'] = 'mock-private-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') })
t.deepEqual(censorList[1], {
key: 'API_PRIVATE_KEY',
// eslint-disable-next-line prefer-regex-literals
value: RegExp('mock\\-private\\-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('does not censor vars flagged as sensitive = false', async (t) => {
const redacted = censor({ abLamboModel: 'revuelto' }, CensorList.getAll())
t.deepEqual(redacted, { abLamboModel: 'revuelto' })
})
test('censor vars not explicitly flagged as sensitive', async (t) => {
const redacted = censor({ publicApiKey: 'mock-private-key' }, CensorList.getAll())
t.deepEqual(redacted, { publicApiKey: '[API_PRIVATE_KEY REDACTED]' })
})
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]')
})