This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
594 lines (550 loc) · 16 KB
/
index.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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// Require Node.js Dependencies
const { createReadStream } = require("fs");
const { basename } = require("path");
const { performance, PerformanceObserver } = require('perf_hooks');
const assert = require("assert");
const events = require('events');
const readline = require('readline');
// Require Third-party Dependencies
const { has, get, cloneDeep, merge, isEqual } = require("lodash");
const request = require("request-promise");
const is = require("@slimio/is");
const mime = require("mime-types");
const chalk = require("chalk");
// Assign Chalk color shortHand!
const {
yellow: warn,
greenBright: fOk,
green: ok,
blueBright: info,
red: error,
gray,
white
} = chalk.bold;
// create readline Interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Variable REGEXP
const variableRegexp = /\${([a-zA-Z0-9._-]+)}/g;
const IDefaultRequest = {
method: "GET",
resolveWithFullResponse: true,
json: true
};
/**
* @function getSourceDB
* @memberof helpers
* @param {CollectionManager} model Loopback model
* @returns {MongoDB.DB} Return MongoDB Connector
*/
function getSourceDB(model) {
return new Promise((resolve, reject) => {
model.getDataSource().connector.connect(function connectDB(err, db) {
if (err) {
return reject(err);
}
resolve(db);
});
});
}
/**
* @class loopbackTest
* @classdesc Run unit tests
*
* @property {any} app
* @property {Object} headers
* @property {any[]} tests
* @property {String} basePath
* @property {Object} context
*/
class loopbackTest extends events {
/**
* @constructor
* @param {*} app Loopback server application
* @param {String} basePath API BasePath
* @param {String=} baseModelName baseModelName (used to get dataSource in after/before)
*
* @throws {TypeError}
*/
constructor(app, basePath = "api", baseModelName) {
super();
if (is.nullOrUndefined(app)) {
throw new TypeError("App argument cannot be undefined!");
}
this.app = app;
this.payload = {};
this.context = {};
this.tests = [];
this.basePath = basePath;
this.extension = new Map();
this.app.on('started', async() => {
let DB;
if(is(baseModelName) === "string") {
DB = await getSourceDB(this.app.models[baseModelName]);
if(!is.nullOrUndefined(this._before)) {
try {
await this._before(DB);
}
catch(E) {
this.emit('error', E.message);
}
}
}
try {
await this.run();
}
catch(E) {
Reflect.deleteProperty(E, 'response');
Reflect.deleteProperty(E, 'options');
this.emit('error', E);
}
if(!is.nullOrUndefined(this._after)) {
try {
await this._after(DB);
}
catch(E) {
this.emit('error', E.message);
}
}
process.exit(0);
});
process.nextTick(() => {
this.app.start();
});
}
/**
* @public
* @method setVar
* @desc Add a new variable into the context
* @memberof loopbackTest#
* @param {!String} varName Variable name
* @param {!any} varValue Variable value
* @returns {this} return this
*
* @throws {TypeError}
*/
setVar(varName, varValue) {
if(is(varName) !== 'string') {
throw new TypeError('varName argument should be a string!')
}
if(is.nullOrUndefined(varValue)) {
throw new TypeError('varValue argument cannot be undefined');
}
Reflect.set(this.context, varName, varValue);
return this;
}
/**
* @memberof loopbackTest
* @public
* @param {Function} fn function
*/
set before(fn) {
if(!is.asyncFunction(fn)) {
throw new TypeError("before property should be an AsyncFunction");
}
this._before = fn;
}
/**
* @memberof loopbackTest
* @public
* @param {Function} fn function
*/
set after(fn) {
if(!is.asyncFunction(fn)) {
throw new TypeError("after property should be an AsyncFunction");
}
this._after = fn;
}
/**
* @private
* @method _checkBodyExpectation
* @desc Check returned body (it should match our expectation)
* @memberof loopbackTest#
* @param {!Object} body Request body object!
* @param {!Object} expected Expected Object
* @returns {void}
*
* @throws {Error}
* @throws {TypeError}
*/
_checkBodyExpectation(body, { bodyType, properties }) {
const isType = is(body).toLowerCase();
bodyType = bodyType.toLowerCase();
assert.equal(isType, bodyType,
`Invalid type for the returned response body. Should be ${ok(bodyType)} but detected as ${error(isType)}`
);
console.log(`├── ${white("bodyType")}: ${ok(bodyType)}`);
console.log("│");
// Check properties keys if the returned type is an Object!
if (isType !== "object" || is(properties) !== "Object") {
return;
}
console.log(`├── [ ${warn("Body properties")} ]`);
for(const key of Object.keys(properties)) {
if (!has(body, key)) {
throw new Error(`Missing property key ${key} in the response body`);
}
let propertyType = Reflect.get(properties, key);
const propertyIs = is(propertyType);
const bodyValue = get(body, key);
const bodyType = is(bodyValue).toLowerCase();
if(propertyIs === "string") {
propertyType = propertyType.toLowerCase();
if (propertyType === "any") {
continue;
}
if (bodyType !== propertyType) {
throw new TypeError(`Property ${info(key)} should be ${ok(propertyType)} but the returned property was ${warn(bodyType)}`);
}
console.log(`│ ├── ${white(key)}: ${ok(propertyType)}`);
}
else if(propertyIs === "Object") {
let { type, value } = propertyType;
type = type.toLowerCase();
if (type === "any") {
continue;
}
if (bodyType !== type) {
throw new TypeError(`Property ${info(key)} should be typeof ${ok(type)} but the returned property was typeof ${warn(bodyType)}`);
}
if(is(value) === "string") {
value = this._getContextVariable(value);
}
const valueIsNotDefined = is.nullOrUndefined(value);
if(valueIsNotDefined === false && !isEqual(bodyValue, value)) {
throw new Error(`Variable ${info(key)} value should be ${ok(value)} but was detected as ${error(bodyValue)}`);
}
console.log(`│ ├── ${white(key)}: ${ok(type)} -> ${info(!valueIsNotDefined ? value : 'N/D')}`);
}
}
console.log("│");
}
/**
* @private
* @method _checkBodyExpectation
* @desc Check returned body (it should match our expectation)
* @memberof loopbackTest#
* @param {!Object} headers Request headers Object
* @param {!Object} expected Expected Object
* @returns {void}
*
* @throws {Error}
*/
_checkHeadersExpectation(headers, expected) {
console.log(`├── [ ${warn("Headers properties")} ]`);
for(let headerKey of Object.keys(expected.headers)) {
headerKey = headerKey.toLowerCase();
if (!Reflect.has(headers, headerKey)) {
throw new Error(`Key ${ok(headerKey)} is not present in the response headers!`);
}
assert.equal(
headers[headerKey].includes(expected.headers[headerKey]),
true,
`Invalid headers value for the key ${info(headerKey)}. Should be (or contains) ${ok(expected.headers[headerKey])} but was ${error(headers[headerKey])}}`
);
console.log(`│ ├── ${white(headerKey)}: ${ok(expected.headers[headerKey])}`);
}
console.log("│");
}
/**
* @private
* @method _checkAndAssignVariables
* @desc Check and Assign body (keys/values) into the Context!
* @memberof loopbackTest#
* @param {!Object} body Request body object!
* @param {String[]} variables Request variables Object
* @returns {void}
*
* @throws {Error}
*/
_checkAndAssignVariables(body, variables) {
for(const completeVarStr of variables) {
const [varName, varFix] = completeVarStr.split(':');
if(!has(body, varName)) {
throw new Error(`Variable ${ok(varName)} is missing from the response body. Cannot be applied to the test Context!`);
}
const varValue = get(body, varName);
const finalVarName = varFix || varName;
this.context[finalVarName] = varValue;
console.log(
`├── Assign new variable ${ok(finalVarName)} with value ${chalk.bold.cyan(varValue)} into the context!`
);
}
console.log("│");
}
/**
* @private
* @method _dump
* @desc Dump object!
* @memberof loopbackTest#
* @param {!String} strTitle Dump title
* @param {!Object} payload Dump payload
* @returns {void}
*/
_dump(strTitle, payload) {
console.log(`├── ${white(strTitle)}:`);
console.log(`\n${gray(JSON.stringify(payload, null, 2))}\n`);
}
/**
* @private
* @method _getContextVariable
* @desc Get variable in Regexp (shorthand private method)
* @param {!String} varStr variable to handle!
* @returns {void|String} Context variable!
*
* @throws {TypeError}
*/
_getContextVariable(varStr) {
if(is.nullOrUndefined(varStr)) {
throw new TypeError("varStr argument cannot be undefined or null!");
}
varStr.replace(variableRegexp, (match, matchValue) => {
if (!Reflect.has(this.context, matchValue)) {
return;
}
varStr = varStr.replace(
new RegExp(`\\${match}`, "g"),
Reflect.get(this.context, matchValue)
);
});
return varStr;
}
/**
* @private
* @method _hydrateObject
* @desc Hydrate an object
* @param {any!} focusObject Object to hydrate
* @returns {void} Return void
*/
_hydrateObject(focusObject) {
for(const key of Object.keys(focusObject)) {
const value = Reflect.get(focusObject, key);
if (is(value) !== "string") {
continue;
}
Reflect.set(focusObject, key, this._getContextVariable(value));
}
}
/**
* @public
* @async
* @method run
* @memberof loopbackTest#
* @returns {void}
*/
async run() {
const baseUrl = this.app.get("url").replace(/\/$/, "");
let testIndex = 0;
runTests: for(const test of this.tests) {
testIndex++;
performance.mark(`start_${testIndex}`);
console.log(`\n\n${white("Running test: id")} ${ok(testIndex)} - ${fOk(test.title) || "[NO TITLE]"}`);
console.log(gray("─────────────────────────────────────────────────────"));
// Skip test!
if (test.skip) {
console.log(`├── ${warn("Test skipped...")}`);
continue;
}
const { expect = {}, file, debug = false, variables } = test;
if (is.nullOrUndefined(expect.statusCode)) {
Reflect.set(expect, "statusCode", 200);
}
test.url = this._getContextVariable(test.url);
// Define the HTTP Request!
let reqOptions = {
method: test.method,
url: `${baseUrl}/${this.basePath}${is(test.model) === "string" ? `/${test.model}` : ""}/${test.url}`,
body: test.body,
qs: test.qs
};
// Hydrate context for headers keys!
if (is(test.headers) === 'Object') {
this._hydrateObject(test.headers);
Reflect.set(reqOptions, 'headers', test.headers);
}
if(is(test.form) === 'Object') {
this._hydrateObject(test.form);
Reflect.set(reqOptions, 'form', test.form);
}
if(is(test.formData) === 'Object') {
this._hydrateObject(test.formData);
Reflect.set(reqOptions, 'formData', test.formData);
}
reqOptions = Object.assign(cloneDeep(IDefaultRequest), reqOptions);
// Upload a file!
uploadFile: if (is(file) === "Object") {
if (is.nullOrUndefined(file.path)) {
break uploadFile;
}
const formName = is.nullOrUndefined(file.form_name) ? "file" : file.form_name;
const name = basename(file.path);
reqOptions.formData = Object.assign(reqOptions.formData || {}, {
name,
[formName]: {
value: createReadStream(file.path),
options: {
filename: name,
contentType: mime.lookup(name)
}
}
});
}
// Debug the request options !
if (debug) {
console.log(`├── [ ${chalk.magenta.bold("DEBUG ON")} ]`)
this._dump("Request options", reqOptions);
this._dump("Context", this.context);
}
else {
console.log(`├── ${white("url")}: [${warn(reqOptions.method || 'GET')}] ${ok(reqOptions.url)}`);
}
// Make the request !
let body, statusCode, headers;
try {
const response = await request(reqOptions);
body = response.body;
statusCode = response.statusCode;
headers = response.headers;
if (debug) {
this._dump("Body", body);
this._dump("Headers", headers);
}
}
catch(E) {
statusCode = E.statusCode;
body = E.response.body;
headers = E.response.headers;
if(statusCode === 200) {
this._dump("Request options", reqOptions);
this._dump("Context", this.context);
this._dump("Body", body);
this._dump("Headers", headers);
throw E;
}
else if(debug) {
this._dump("Body", body);
this._dump("Headers", headers);
}
}
// Check expected response statusCode
assert.equal(statusCode, expect.statusCode,
`Invalid response statusCode. Should be ${ok(expect.statusCode)} but returned code ${error(statusCode)}`
);
console.log(`├── ${white("statusCode")}: ${ok(expect.statusCode)}`);
// Check expected bodyType returned by the Request !
if (is(expect.bodyType) === "string") {
this._checkBodyExpectation(body, expect);
}
// Check expected headers!
if (is(expect.headers) === "Object") {
this._checkHeadersExpectation(headers, expect);
}
// Check body(keys/values) and assign variables!
if (is(variables) === "Array") {
this._checkAndAssignVariables(body, variables);
}
performance.mark(`end_${testIndex}`);
const measure = await new Promise((resolve) => {
const obs = new PerformanceObserver((items, observer) => {
resolve(items.getEntries()[0]);
performance.clearMarks();
observer.disconnect();
});
obs.observe({ entryTypes: ['measure'] });
performance.measure(`execDuration_${testIndex}`, `start_${testIndex}`, `end_${testIndex}`);
});
if(Reflect.has(expect, 'duration')) {
if(measure.duration > expect.duration) {
throw new Error(
`${white("Expect baseline execution duration")}: ${ok(`${expect.duration}ms`)} but was ${error(measure.duration)}`
);
}
}
console.log(`├── ${white("Execution time (ms)")}: ${warn(`${measure.duration}ms`)}`);
// Break
if(test.break) {
const breakRet = await new Promise((resolve) => {
rl.question(`\n${error('Do you want to continue the test ? (Y/N) ')}`, (answer) => {
answer = answer.toLowerCase();
rl.close();
resolve(answer);
});
});
if(breakRet !== "y") {
console.log(warn('Test breaked!'));
break runTests;
}
}
}
console.log(`\n\n${ok("All tests passed!")}`);
}
/**
* @public
* @method defaultPayload
* @desc Add default payload for each tests!
* @memberof loopbackTest#
* @param {!Object} payload payload
* @returns {this} return this
*
* @throws {TypeError}
*/
defaultPayload(payload) {
if (is(payload) !== 'Object') {
throw new TypeError("payload argument have to be typeof Object");
}
Object.assign(this.payload, cloneDeep(payload));
return this;
}
/**
* @public
* @method extend
* @desc Add a new code extension
* @memberof loopbackTest#
* @param {!String} name extension name
* @param {!Object} payload payload
* @returns {this} return this
*
* @throws {TypeError}
*/
extend(name, payload) {
if (is(name) !== 'string') {
throw new TypeError('name argument should be a string');
}
if (is(payload) !== 'Object') {
throw new TypeError("payload argument have to be typeof Object");
}
this.extension.set(name, cloneDeep(payload));
return this;
}
/**
* @public
* @method run
* @memberof loopbackTest#
* @param {!Array<Object>} testArr Test payload to load on the application!
* @returns {loopbackTest} Return self
*/
load(testArr) {
if (is.nullOrUndefined(testArr)) {
throw new TypeError("test argument cant be undefined");
}
testArr.forEach((test) => {
if(Reflect.has(test, 'extends')) {
const extendArray = Reflect.get(test, 'extends');
if(is(extendArray) === 'Array') {
for(const ext of extendArray) {
if(this.extension.has(ext)) {
test = merge(cloneDeep(this.extension.get(ext)), test);
}
}
}
Reflect.deleteProperty(test, 'extends');
}
this.tests.push(merge(cloneDeep(this.payload), test));
});
return this;
}
}
// Export loopbackModelTester handler
module.exports = loopbackTest;