-
Notifications
You must be signed in to change notification settings - Fork 19
/
test.mjs
421 lines (356 loc) · 8.26 KB
/
test.mjs
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* eslint-disable import/no-extraneous-dependencies */
// eslint-disable-next-line import/no-unresolved
import test from 'ava';
import hapi from '@hapi/hapi';
import defer from 'p-defer';
import * as Sentry from '@sentry/node';
// eslint-disable-next-line import/extensions
import plugin from './index.js';
const dsn = 'https://[email protected]/0';
test.beforeEach(t => {
delete global.__SENTRY__;
t.context.server = new hapi.Server();
});
test('requires a dsn or a Scope (sentry opts vs. sentry client)', async t => {
const { server } = t.context;
const err = await t.throwsAsync(() => server.register({
plugin,
options: {
client: {},
},
}), {
name: 'ZodError',
message: /invalid_union/,
});
// check that the "client" path is invalid
t.assert(err.issues.some(issue => issue.path.join('.') === 'client'));
});
test('allows initialization without dsn (opts.dsn to be false)', async t => {
const { server } = t.context;
server.route({
method: 'GET',
path: '/',
handler() {
throw new Error('Oh no!');
},
});
const deferred = defer();
await t.notThrowsAsync(() => server.register({
plugin,
options: {
client: {
dsn: false,
beforeSend: deferred.resolve,
},
},
}));
await server.inject({
method: 'GET',
url: '/',
});
let eventCaptured = false;
deferred.promise.then(() => {
eventCaptured = true;
});
// wait for sentry event possibly to be sent
await new Promise(resolve => {
setTimeout(resolve, 20);
});
// still captured https://github.com/hydra-newmedia/hapi-sentry/issues/144
t.is(eventCaptured, true);
});
test('uses a custom sentry client', async t => {
const { server } = t.context;
const error = new Error('Error to be thrown');
server.route({
method: 'GET',
path: '/route',
handler() {
throw error;
},
});
const deferred = defer();
const customSentry = {
Scope: class Scope {},
getCurrentHub() {
return {
getScope() {
return {};
},
};
},
// arity needed to pass joi validation
Handlers: { parseRequest: (x, y) => { } }, // eslint-disable-line no-unused-vars
withScope: cb => cb({ addEventProcessor: () => { } }),
captureException: deferred.resolve,
configureScope: () => { },
};
// check exposing of custom client
await server.register({
plugin,
options: {
client: customSentry,
},
});
// t.deepEqual(server.plugins['hapi-sentry'].client, customSentry);
// check if custom sentry is used per request
await server.inject({
method: 'GET',
url: '/route',
});
const event = await deferred.promise;
t.is(event, error);
});
test('Pass in current Sentry client', async t => {
const { server } = t.context;
await t.notThrowsAsync(() => server.register({
plugin,
options: {
client: Sentry,
},
}));
});
test('exposes the sentry client', async t => {
const { server } = t.context;
await server.register({
plugin,
options: {
client: { dsn },
},
});
t.is(typeof server.plugins['hapi-sentry'].client.captureException, 'function');
});
test('exposes a per-request scope', async t => {
const { server } = t.context;
server.route({
method: 'GET',
path: '/',
handler(request) {
t.is(typeof request.sentryScope.setTag, 'function');
return null;
},
});
await server.register({
plugin,
options: {
client: { dsn },
},
});
await server.inject({
method: 'GET',
url: '/',
});
});
test('captures request errors', async t => {
const { server } = t.context;
server.route({
method: 'GET',
path: '/',
handler() {
throw new Error('Oh no!');
},
});
const deferred = defer();
await server.register({
plugin,
options: {
client: {
dsn,
beforeSend: deferred.resolve,
},
},
});
await server.inject({
method: 'GET',
url: '/',
});
const event = await deferred.promise;
t.is(event.exception.values[0].value, 'Oh no!');
t.is(event.exception.values[0].type, 'Error');
});
test('parses request metadata', async t => {
const { server } = t.context;
server.route({
method: 'GET',
path: '/route',
handler() {
throw new Error('Oh no!');
},
});
const deferred = defer();
await server.register({
plugin,
options: {
client: {
dsn,
beforeSend: deferred.resolve,
},
},
});
await server.inject({
method: 'GET',
url: '/route',
});
const { request } = await deferred.promise;
t.is(request.method, 'GET');
t.is(typeof request.headers, 'object');
t.is(request.url, `http://${request.headers.host}/route`);
});
test('sanitizes user info from auth', async t => {
const { server } = t.context;
server.auth.scheme('mock', () => ({
authenticate(request, h) {
return h.authenticated({
credentials: {
username: 'me',
password: 'open sesame',
pw: 'os',
secret: 'abc123',
},
});
},
}));
server.auth.strategy('mock', 'mock');
server.route({
method: 'GET',
path: '/',
handler() {
throw new Error('Oh no!');
},
config: {
auth: 'mock',
},
});
const deferred = defer();
await server.register({
plugin,
options: {
client: {
dsn,
beforeSend: deferred.resolve,
},
},
});
await server.inject({
method: 'GET',
url: '/',
});
const event = await deferred.promise;
t.deepEqual(event.user, { username: 'me' });
});
test('process \'app\' channel events with default tags', async t => {
const { server } = t.context;
server.route({
method: 'GET',
path: '/route',
handler(request) {
request.log(['error', 'foo'], new Error('Oh no!'));
return null;
},
});
const deferred = defer();
await server.register({
plugin,
options: {
client: {
dsn,
beforeSend: deferred.resolve,
},
catchLogErrors: true,
},
});
await server.inject({
method: 'GET',
url: '/route',
});
const event = await deferred.promise;
t.is(event.exception.values[0].value, 'Oh no!');
t.is(event.exception.values[0].type, 'Error');
});
test('process \'app\' channel events with `catchLogErrors` tags', async t => {
const { server } = t.context;
server.route({
method: 'GET',
path: '/route',
handler(request) {
request.log('exception', new Error('Oh no!'));
return null;
},
});
const deferred = defer();
await server.register({
plugin,
options: {
client: {
dsn,
beforeSend: deferred.resolve,
},
catchLogErrors: ['exception', 'failure'],
},
});
await server.inject({
method: 'GET',
url: '/route',
});
const event = await deferred.promise;
t.is(event.exception.values[0].value, 'Oh no!');
t.is(event.exception.values[0].type, 'Error');
});
test('process \'log\' events with default tags', async t => {
const { server } = t.context;
server.route({
method: 'GET',
path: '/route',
handler() {
server.log(['error', 'foo'], new Error('Oh no!'));
return null;
},
});
const deferred = defer();
await server.register({
plugin,
options: {
client: {
dsn,
beforeSend: deferred.resolve,
},
catchLogErrors: true,
},
});
await server.inject({
method: 'GET',
url: '/route',
});
const event = await deferred.promise;
t.is(event.exception.values[0].value, 'Oh no!');
t.is(event.exception.values[0].type, 'Error');
});
test('process \'log\' events with `catchLogErrors` tags', async t => {
const { server } = t.context;
server.route({
method: 'GET',
path: '/route',
handler() {
server.log('exception', new Error('Oh no!'));
return null;
},
});
const deferred = defer();
await server.register({
plugin,
options: {
client: {
dsn,
beforeSend: deferred.resolve,
},
catchLogErrors: ['exception', 'failure'],
},
});
await server.inject({
method: 'GET',
url: '/route',
});
const event = await deferred.promise;
t.is(event.exception.values[0].value, 'Oh no!');
t.is(event.exception.values[0].type, 'Error');
});