forked from pinojs/pino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-redact.test.js
More file actions
185 lines (173 loc) · 4.03 KB
/
browser-redact.test.js
File metadata and controls
185 lines (173 loc) · 4.03 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict'
const test = require('tape')
const pino = require('../browser')
test('redact option with array of paths', ({ end, is, ok }) => {
const instance = pino({
redact: ['password'],
browser: {
write (o) {
is(o.user, 'test')
is(o.password, '[REDACTED]')
ok(o.time)
}
}
})
instance.info({ user: 'test', password: 'secret' })
end()
})
test('redact option with nested paths', ({ end, is }) => {
const instance = pino({
redact: ['user.password'],
browser: {
write (o) {
is(o.user.name, 'John')
is(o.user.password, '[REDACTED]')
}
}
})
instance.info({ user: { name: 'John', password: 'secret' } })
end()
})
test('redact option with custom censor', ({ end, is }) => {
const instance = pino({
redact: { paths: ['password'], censor: '***' },
browser: {
write (o) {
is(o.password, '***')
}
}
})
instance.info({ password: 'secret' })
end()
})
test('redact option with remove', ({ end, is, ok }) => {
const instance = pino({
redact: { paths: ['password'], remove: true },
browser: {
write (o) {
is(o.user, 'test')
ok(!('password' in o))
}
}
})
instance.info({ user: 'test', password: 'secret' })
end()
})
test('redact option with multiple paths', ({ end, is }) => {
const instance = pino({
redact: ['password', 'secret', 'token'],
browser: {
write (o) {
is(o.user, 'test')
is(o.password, '[REDACTED]')
is(o.secret, '[REDACTED]')
is(o.token, '[REDACTED]')
}
}
})
instance.info({ user: 'test', password: 'a', secret: 'b', token: 'c' })
end()
})
test('redact does not affect non-matching properties', ({ end, is }) => {
const instance = pino({
redact: ['password'],
browser: {
write (o) {
is(o.user, 'test')
is(o.email, 'test@test.com')
is(o.password, '[REDACTED]')
}
}
})
instance.info({ user: 'test', email: 'test@test.com', password: 'secret' })
end()
})
test('redact works with child loggers', ({ end, is }) => {
const instance = pino({
redact: ['password'],
browser: {
write (o) {
is(o.service, 'auth')
is(o.password, '[REDACTED]')
}
}
})
const child = instance.child({ service: 'auth' })
child.info({ password: 'secret' })
end()
})
test('redact works with wildcard paths', ({ end, is }) => {
const instance = pino({
redact: ['users[*].password'],
browser: {
write (o) {
is(o.users[0].name, 'John')
is(o.users[0].password, '[REDACTED]')
is(o.users[1].name, 'Jane')
is(o.users[1].password, '[REDACTED]')
}
}
})
instance.info({
users: [
{ name: 'John', password: 'pass1' },
{ name: 'Jane', password: 'pass2' }
]
})
end()
})
test('redact in non-asObject mode redacts object arguments', ({ end, is }) => {
const info = console.info
console.info = function (obj) {
is(obj.password, '[REDACTED]')
is(obj.user, 'test')
console.info = info
}
const instance = pino({
level: 'info',
redact: ['password']
})
instance.info({ user: 'test', password: 'secret' })
end()
})
test('redact with formatters', ({ end, is }) => {
const instance = pino({
redact: ['password'],
browser: {
formatters: {
level (label, number) {
return { label, level: number }
}
},
write (o) {
is(o.label, 'info')
is(o.password, '[REDACTED]')
}
}
})
instance.info({ password: 'secret' })
end()
})
test('redact throws if paths is not an array', ({ end, throws }) => {
throws(function () {
pino({ redact: { paths: 'not-an-array' } })
})
end()
})
test('redact with censor function', ({ end, is }) => {
const instance = pino({
redact: {
paths: ['password'],
censor: function (value) {
return value[0] + '***'
}
},
browser: {
write (o) {
is(o.password, 's***')
}
}
})
instance.info({ password: 'secret' })
end()
})