@velocitycareerlabs/tests-helpers 1.22.0-dev-build.14f2539c1 → 1.22.0-dev-build.1478d9e84
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +0 -1
- package/package.json +7 -21
- package/src/build-fastify.js +0 -295
package/index.js
CHANGED
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@velocitycareerlabs/tests-helpers",
|
3
|
-
"version": "1.22.0-dev-build.
|
3
|
+
"version": "1.22.0-dev-build.1478d9e84",
|
4
4
|
"description": "Generic helpers for tests",
|
5
5
|
"repository": "https://github.com/velocitycareerlabs/packages",
|
6
6
|
"main": "index.js",
|
@@ -15,28 +15,14 @@
|
|
15
15
|
"author": "Itay Podhajcer",
|
16
16
|
"license": "Apache-2.0",
|
17
17
|
"dependencies": {
|
18
|
-
"@fastify/jwt": "7.2.4",
|
19
|
-
"@fastify/routes": "~5.1.0",
|
20
|
-
"@fastify/swagger": "~8.15.0",
|
21
|
-
"@fastify/swagger-ui": "~3.1.0",
|
22
|
-
"@spencejs/spence-events": "^0.10.2",
|
23
18
|
"@spencejs/spence-mongo-repos": "^0.10.2",
|
24
|
-
"@velocitycareerlabs/common-functions": "1.22.0-dev-build.
|
25
|
-
"@velocitycareerlabs/
|
26
|
-
"@velocitycareerlabs/
|
27
|
-
"@velocitycareerlabs/
|
28
|
-
"@velocitycareerlabs/jwt": "1.22.0-dev-build.14f2539c1",
|
29
|
-
"@velocitycareerlabs/logger": "1.22.0-dev-build.14f2539c1",
|
30
|
-
"@velocitycareerlabs/rest-queries": "1.22.0-dev-build.14f2539c1",
|
31
|
-
"@velocitycareerlabs/test-regexes": "1.22.0-dev-build.14f2539c1",
|
32
|
-
"ajv-formats": "3.0.1",
|
19
|
+
"@velocitycareerlabs/common-functions": "1.22.0-dev-build.1478d9e84",
|
20
|
+
"@velocitycareerlabs/crypto": "1.22.0-dev-build.1478d9e84",
|
21
|
+
"@velocitycareerlabs/jwt": "1.22.0-dev-build.1478d9e84",
|
22
|
+
"@velocitycareerlabs/test-regexes": "1.22.0-dev-build.1478d9e84",
|
33
23
|
"dotenv": "^16.0.0",
|
34
24
|
"env-var": "^7.0.0",
|
35
|
-
"
|
36
|
-
"fastify-plugin": "~4.5.0",
|
37
|
-
"form-data": "^4.0.0",
|
38
|
-
"http-errors": "~2.0.0",
|
39
|
-
"lodash": "~4.17.20",
|
25
|
+
"lodash": "~4.17.21",
|
40
26
|
"mongodb": "~6.8.0",
|
41
27
|
"nanoid": "~3.3.1"
|
42
28
|
},
|
@@ -54,5 +40,5 @@
|
|
54
40
|
"nanoid": "3.3.7",
|
55
41
|
"prettier": "2.8.8"
|
56
42
|
},
|
57
|
-
"gitHead": "
|
43
|
+
"gitHead": "641abb797f2379b534e707ba87d0da538eeff965"
|
58
44
|
}
|
package/src/build-fastify.js
DELETED
@@ -1,295 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright 2023 Velocity Team
|
3
|
-
*
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
-
* you may not use this file except in compliance with the License.
|
6
|
-
* You may obtain a copy of the License at
|
7
|
-
*
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
9
|
-
*
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
-
* See the License for the specific language governing permissions and
|
14
|
-
* limitations under the License.
|
15
|
-
*/
|
16
|
-
|
17
|
-
const { isEmpty } = require('lodash/fp');
|
18
|
-
const FormData = require('form-data');
|
19
|
-
const fastify = require('fastify');
|
20
|
-
const fp = require('fastify-plugin');
|
21
|
-
const newError = require('http-errors');
|
22
|
-
const { loggerProvider } = require('@velocitycareerlabs/logger');
|
23
|
-
const { mongodbPlugin, bindRepo } = require('@spencejs/spence-mongo-repos');
|
24
|
-
const { initEvents } = require('@spencejs/spence-events');
|
25
|
-
const { idKeyMapper } = require('@velocitycareerlabs/common-functions');
|
26
|
-
const {
|
27
|
-
errorsPlugin,
|
28
|
-
rpcProviderPlugin,
|
29
|
-
cachePlugin,
|
30
|
-
} = require('@velocitycareerlabs/fastify-plugins');
|
31
|
-
const { clone, map } = require('lodash/fp');
|
32
|
-
const fastifySwagger = require('@fastify/swagger');
|
33
|
-
const fastifySwaggerUI = require('@fastify/swagger-ui');
|
34
|
-
const ajvFormatsPlugin = require('ajv-formats');
|
35
|
-
|
36
|
-
const {
|
37
|
-
addressSchema,
|
38
|
-
dateSchema,
|
39
|
-
locationSchema,
|
40
|
-
multilingualStringSchema,
|
41
|
-
emailSchema,
|
42
|
-
phoneSchema,
|
43
|
-
} = require('@velocitycareerlabs/common-schemas');
|
44
|
-
const {
|
45
|
-
customFastifyQueryStringParser,
|
46
|
-
} = require('@velocitycareerlabs/rest-queries');
|
47
|
-
|
48
|
-
const {
|
49
|
-
testRegistrarSuperUser: defaultOAuthUser,
|
50
|
-
} = require('./test-oauth-user');
|
51
|
-
|
52
|
-
const defaultOverrides = { reqConfig: (x) => x };
|
53
|
-
|
54
|
-
const testAuthPlugin = fp((server, options, next) => {
|
55
|
-
server.decorate('verifyAdmin', async (req) => {
|
56
|
-
if (req.headers['x-override-auth-unauthorized'] != null) {
|
57
|
-
throw newError.Unauthorized('Unauthorized');
|
58
|
-
}
|
59
|
-
const groupId = req.headers['x-override-auth-user-group-id'];
|
60
|
-
|
61
|
-
req.user = {
|
62
|
-
...(groupId
|
63
|
-
? { groupId: req.headers['x-override-auth-user-group-id'] }
|
64
|
-
: {}),
|
65
|
-
user: 'velocity.admin@example.com',
|
66
|
-
};
|
67
|
-
});
|
68
|
-
next();
|
69
|
-
});
|
70
|
-
|
71
|
-
const testOAuthPlugin = fp((server, options, next) => {
|
72
|
-
server.decorate('verifyAccessToken', () => async (req) => {
|
73
|
-
if (req.headers['x-override-oauth-unauthorized'] != null) {
|
74
|
-
throw newError.Unauthorized('Unauthorized');
|
75
|
-
} else if (req.headers['x-override-oauth-user'] != null) {
|
76
|
-
// eslint-disable-next-line better-mutation/no-mutation
|
77
|
-
req.user = JSON.parse(req.headers['x-override-oauth-user']);
|
78
|
-
} else {
|
79
|
-
// eslint-disable-next-line better-mutation/no-mutation
|
80
|
-
req.user = defaultOAuthUser;
|
81
|
-
}
|
82
|
-
});
|
83
|
-
next();
|
84
|
-
});
|
85
|
-
|
86
|
-
const testVnfAuthenticationPlugin = fp(
|
87
|
-
(server, options, next) => {
|
88
|
-
server
|
89
|
-
.decorate('vnfAuthenticate', () => 'TOKEN')
|
90
|
-
.decorateRequest('vnfBlockchainAuthenticate', null)
|
91
|
-
.addHook('preValidation', async (req) => {
|
92
|
-
req.vnfBlockchainAuthenticate = () => 'TOKEN';
|
93
|
-
});
|
94
|
-
next();
|
95
|
-
},
|
96
|
-
{ name: 'velocityIdp' }
|
97
|
-
);
|
98
|
-
|
99
|
-
const buildFastify = (
|
100
|
-
config,
|
101
|
-
initServer,
|
102
|
-
mongoConnection,
|
103
|
-
defaultHeaders = {}
|
104
|
-
) => {
|
105
|
-
const log = loggerProvider(config);
|
106
|
-
const fastifyOptions = {
|
107
|
-
maxParamLength: 2000,
|
108
|
-
logger: log,
|
109
|
-
trustProxy: true,
|
110
|
-
// @ts-ignore
|
111
|
-
caseSensitive: false,
|
112
|
-
ignoreTrailingSlash: true,
|
113
|
-
querystringParser: customFastifyQueryStringParser,
|
114
|
-
...buildAjvOptions(config),
|
115
|
-
...buildHttpOptions(config),
|
116
|
-
};
|
117
|
-
|
118
|
-
// eslint-disable-next-line better-mutation/no-mutation
|
119
|
-
config.mongoConnection = mongoConnection;
|
120
|
-
|
121
|
-
const server = fastify(fastifyOptions)
|
122
|
-
.decorate('config', config)
|
123
|
-
.decorate('overrides', clone(defaultOverrides))
|
124
|
-
.decorate('resetOverrides', () => {
|
125
|
-
server.overrides = clone(defaultOverrides);
|
126
|
-
});
|
127
|
-
|
128
|
-
// eslint-disable-next-line better-mutation/no-mutation
|
129
|
-
server.mockRequest = {
|
130
|
-
request: jest.fn(),
|
131
|
-
get: jest.fn(),
|
132
|
-
post: jest.fn(),
|
133
|
-
patch: jest.fn(),
|
134
|
-
put: jest.fn(),
|
135
|
-
del: jest.fn(),
|
136
|
-
};
|
137
|
-
|
138
|
-
initEvents({
|
139
|
-
log: server.log,
|
140
|
-
errorHandler: (event, context, error) => {
|
141
|
-
server.log.error({ context, event }, 'Error Context');
|
142
|
-
server.log.error(error);
|
143
|
-
},
|
144
|
-
});
|
145
|
-
|
146
|
-
server
|
147
|
-
.addSchema(multilingualStringSchema)
|
148
|
-
.addSchema(dateSchema)
|
149
|
-
.addSchema(locationSchema)
|
150
|
-
.addSchema(addressSchema)
|
151
|
-
.addSchema(emailSchema)
|
152
|
-
.addSchema(phoneSchema)
|
153
|
-
.decorateRequest('config', null)
|
154
|
-
.addHook('onRequest', async (req) => {
|
155
|
-
req.config = server.overrides.reqConfig(server.config);
|
156
|
-
})
|
157
|
-
.addHook('preParsing', async (req) => {
|
158
|
-
req.traceId = req.headers['x-trace-id'] || `!${req.id}`;
|
159
|
-
req.log = req.log.child({
|
160
|
-
traceId: req.traceId,
|
161
|
-
});
|
162
|
-
})
|
163
|
-
.register(errorsPlugin)
|
164
|
-
.decorateRequest('repos', null)
|
165
|
-
.addHook('preValidation', async (req) => {
|
166
|
-
req.repos = bindRepo(req);
|
167
|
-
})
|
168
|
-
.addHook('preSerialization', async (req, reply, payload) => {
|
169
|
-
if (payload == null) {
|
170
|
-
return null;
|
171
|
-
}
|
172
|
-
|
173
|
-
if (Array.isArray(payload)) {
|
174
|
-
return map(idKeyMapper, payload);
|
175
|
-
}
|
176
|
-
|
177
|
-
return idKeyMapper(payload);
|
178
|
-
})
|
179
|
-
.addHook('onError', async (req, reply, error) => {
|
180
|
-
const { body } = req;
|
181
|
-
|
182
|
-
if (error.validation) {
|
183
|
-
req.log.warn(
|
184
|
-
{ req: req.raw, body, res: reply.raw, err: error },
|
185
|
-
error && error.message
|
186
|
-
);
|
187
|
-
} else {
|
188
|
-
req.log.error(
|
189
|
-
{ req: req.raw, body, res: reply.raw, err: error },
|
190
|
-
error && error.message
|
191
|
-
);
|
192
|
-
}
|
193
|
-
})
|
194
|
-
.register(cachePlugin)
|
195
|
-
.register(mongodbPlugin)
|
196
|
-
.register(testAuthPlugin)
|
197
|
-
.register(testOAuthPlugin)
|
198
|
-
.register(testVnfAuthenticationPlugin)
|
199
|
-
.register(rpcProviderPlugin)
|
200
|
-
.register(fastifySwagger, {
|
201
|
-
openapi: {
|
202
|
-
...server.config.swaggerInfo,
|
203
|
-
},
|
204
|
-
exposeRoute: true,
|
205
|
-
})
|
206
|
-
.register(fastifySwaggerUI, {
|
207
|
-
uiConfig: {
|
208
|
-
deepLinking: true,
|
209
|
-
},
|
210
|
-
initOAuth: {},
|
211
|
-
});
|
212
|
-
// eslint-disable-next-line better-mutation/no-mutation
|
213
|
-
server.injectJson = async ({ method, url, userId, payload, headers }) => {
|
214
|
-
const response = await server.inject({
|
215
|
-
method,
|
216
|
-
url,
|
217
|
-
payload,
|
218
|
-
headers: {
|
219
|
-
'content-type': 'application/json',
|
220
|
-
Accept: 'application/json',
|
221
|
-
'x-user-id': userId || '',
|
222
|
-
...defaultHeaders,
|
223
|
-
...headers,
|
224
|
-
},
|
225
|
-
});
|
226
|
-
if (
|
227
|
-
isEmpty(response.body) ||
|
228
|
-
!response.headers['content-type'].startsWith('application/json')
|
229
|
-
) {
|
230
|
-
return { ...response, json: {} };
|
231
|
-
}
|
232
|
-
|
233
|
-
return { ...response, json: JSON.parse(response.body) };
|
234
|
-
};
|
235
|
-
|
236
|
-
// eslint-disable-next-line better-mutation/no-mutation
|
237
|
-
server.injectMultipart = async ({
|
238
|
-
method,
|
239
|
-
url,
|
240
|
-
userId,
|
241
|
-
payload,
|
242
|
-
headers,
|
243
|
-
}) => {
|
244
|
-
const form = new FormData();
|
245
|
-
// eslint-disable-next-line no-restricted-syntax, guard-for-in
|
246
|
-
for (const key in payload) {
|
247
|
-
form.append(key, payload[key]);
|
248
|
-
}
|
249
|
-
return server.inject({
|
250
|
-
method,
|
251
|
-
url,
|
252
|
-
payload: form.getBuffer(),
|
253
|
-
headers: {
|
254
|
-
'Content-Type': `multipart/form-data; boundary=${form.getBoundary()}`,
|
255
|
-
'x-user-id': userId || '',
|
256
|
-
...defaultHeaders,
|
257
|
-
...headers,
|
258
|
-
},
|
259
|
-
});
|
260
|
-
};
|
261
|
-
|
262
|
-
return initServer(server);
|
263
|
-
};
|
264
|
-
|
265
|
-
const buildAjvOptions = (config) => {
|
266
|
-
return {
|
267
|
-
ajv: {
|
268
|
-
customOptions: {
|
269
|
-
...config.ajvOptions,
|
270
|
-
},
|
271
|
-
plugins: [[ajvFormatsPlugin, config.ajvFormats]],
|
272
|
-
},
|
273
|
-
};
|
274
|
-
};
|
275
|
-
|
276
|
-
const buildHttpOptions = (config) => {
|
277
|
-
const { enableHttp2, serverCertificate, serverCertificateKey } = config;
|
278
|
-
const isSSLMode = !!serverCertificate && !!serverCertificateKey;
|
279
|
-
const opts = {};
|
280
|
-
if (isSSLMode) {
|
281
|
-
opts.https = {
|
282
|
-
key: serverCertificateKey,
|
283
|
-
cert: serverCertificate,
|
284
|
-
};
|
285
|
-
if (enableHttp2) {
|
286
|
-
opts.https.allowHTTP1 = true;
|
287
|
-
}
|
288
|
-
}
|
289
|
-
if (enableHttp2) {
|
290
|
-
opts.http2 = true;
|
291
|
-
}
|
292
|
-
return opts;
|
293
|
-
};
|
294
|
-
|
295
|
-
module.exports = { buildFastify };
|