-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathpino_test.js
232 lines (204 loc) · 7.86 KB
/
pino_test.js
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
import { Writable } from 'node:stream';
import pino from 'pino';
import { config } from '../../../../src/shared/config.js';
import { monitoringTools } from '../../../../src/shared/infrastructure/monitoring-tools.js';
import * as pinoPlugin from '../../../../src/shared/infrastructure/plugins/pino.js';
import { expect, generateAuthenticatedUserRequestHeaders, HttpTestServer, sinon } from '../../../test-helper.js';
describe('Integration | Infrastructure | plugins | pino', function () {
let httpTestServer;
beforeEach(async function () {
httpTestServer = new HttpTestServer();
await httpTestServer.register({
register: (server) => {
server.route([
{
method: 'GET',
path: '/',
config: {
handler: () => {
monitoringTools.incrementInContext('metrics.knexQueryCount');
return { cou: 'cou' };
},
},
},
{
method: 'POST',
path: '/api/token',
config: {
handler: () => {
return {};
},
},
},
{
method: 'GET',
path: '/error',
config: {
handler: () => {
throw new Error('Manual throwed error');
},
},
},
]);
},
name: 'test-api',
});
});
async function registerWithPlugin(cb) {
const stream = new Writable({
write(chunk, encoding, ack) {
cb(JSON.parse(chunk.toString()));
ack();
return true;
},
});
const pinoPluginWithLogger = {
...pinoPlugin,
options: {
...pinoPlugin.options,
instance: pino(stream),
},
};
await httpTestServer.register([pinoPluginWithLogger]);
}
describe('Ensure that datadog configured log format is what we send', function () {
it('should log the error and the request result when there is an unexpected error', async function () {
// given
const messages = [];
await registerWithPlugin((data) => {
messages.push(data);
});
const method = 'GET';
const url = '/error';
// when
await httpTestServer.request(method, url);
expect(messages).to.have.lengthOf(2);
expect(messages[0].level).to.equal(50);
expect(messages[0].tags).to.deep.equal(['internal', 'error']);
expect(messages[0].err.message).to.equal('Manual throwed error');
expect(messages[0].msg).to.equal('request error');
expect(messages[1].msg).to.equal('request completed');
});
context('with request monitoring disabled', function () {
beforeEach(function () {
sinon.stub(config.hapi, 'enableRequestMonitoring').value(false);
monitoringTools.installHapiHook();
});
it('should log the message and version', async function () {
// given
const messages = [];
await registerWithPlugin((data) => {
messages.push(data);
});
const method = 'GET';
const url = '/';
// when
const response = await httpTestServer.request(method, url);
// then
expect(response.statusCode).to.equal(200);
expect(messages).to.have.lengthOf(1);
expect(messages[0].level).to.equal(30);
expect(messages[0].msg).to.equal('request completed');
expect(messages[0].req.version).to.equal('development');
expect(messages[0].req.user_id).to.be.undefined;
expect(messages[0].req.route).to.be.undefined;
expect(messages[0].req.metrics).to.be.undefined;
});
});
context('with request monitoring enabled', function () {
beforeEach(function () {
sinon.stub(config.hapi, 'enableRequestMonitoring').value(true);
monitoringTools.installHapiHook();
});
it('should log the message, version, user id, route and metrics', async function () {
// given
const messages = [];
await registerWithPlugin((data) => {
messages.push(data);
});
const method = 'GET';
const url = '/';
const headers = generateAuthenticatedUserRequestHeaders();
// when
const response = await httpTestServer.request(method, url, null, null, headers);
// then
expect(response.statusCode).to.equal(200);
expect(messages).to.have.lengthOf(1);
expect(messages[0].msg).to.equal('request completed');
expect(messages[0].req.version).to.equal('development');
expect(messages[0].req.user_id).to.equal(1234);
expect(messages[0].req.route).to.equal('/');
expect(messages[0].req.metrics).to.deep.equal({ knexQueryCount: 1 });
});
context('when calling /api/token', function () {
context('when there is a username', function () {
it('logs the message, version, user id, route, metrics and hashed username', async function () {
// given
const messages = [];
await registerWithPlugin((data) => {
messages.push(data);
});
const method = 'POST';
const url = '/api/token';
const payload = {
username: 'toto',
};
const headers = { 'x-forwarded-proto': 'https', 'x-forwarded-host': 'app.pix.org' };
// when
const response = await httpTestServer.request(method, url, payload, null, headers);
// then
expect(response.statusCode).to.equal(200);
expect(messages).to.have.lengthOf(1);
expect(messages[0].msg).to.equal('request completed');
expect(messages[0].req.version).to.equal('development');
expect(messages[0].req.user_id).to.equal('-');
expect(messages[0].req.route).to.equal('/api/token');
expect(messages[0].req.usernameHash).to.equal(
'31f7a65e315586ac198bd798b6629ce4903d0899476d5741a9f32e2e521b6a66', // echo -n 'toto'| shasum -a 256
);
});
});
context('when there is no username', function () {
it('logs the message, version, user id, route, metrics and default value for username', async function () {
// given
const messages = [];
await registerWithPlugin((data) => {
messages.push(data);
});
const method = 'POST';
const url = '/api/token';
const headers = { 'x-forwarded-proto': 'https', 'x-forwarded-host': 'app.pix.org' };
// when
const response = await httpTestServer.request(method, url, null, null, headers);
// then
expect(response.statusCode).to.equal(200);
expect(messages).to.have.lengthOf(1);
expect(messages[0].msg).to.equal('request completed');
expect(messages[0].req.version).to.equal('development');
expect(messages[0].req.user_id).to.equal('-');
expect(messages[0].req.route).to.equal('/api/token');
expect(messages[0].req.usernameHash).to.equal('-');
});
});
context('when there is no forwarded origin (no x-forwarded headers)', function () {
it('handles the ForwardedOriginError error', async function () {
// given
const messages = [];
await registerWithPlugin((data) => {
messages.push(data);
});
const method = 'POST';
const url = '/api/token';
const noForwardedHeaders = {};
// when
const response = await httpTestServer.request(method, url, null, null, noForwardedHeaders);
// then
expect(response.statusCode).to.equal(200);
expect(messages).to.have.lengthOf(1);
expect(messages[0].msg).to.equal('request completed');
});
});
});
});
});
});