-
Notifications
You must be signed in to change notification settings - Fork 0
/
moat.js
570 lines (554 loc) · 16.7 KB
/
moat.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
/*
* MOAT js Testing Library for Node.js
*
* Copyright 2013 © Inventit Inc.
*/
var assert = require('assert'),
crypto = require('crypto');
/**
* MOAT.init()
*/
exports.init = function(sinonObject) {
if (sinonObject) {
// recorded state (stub objects)
exports.replay = false;
exports.state = record(sinonObject, loadPackageJson());
return exports.state;
} else {
exports.replay = true;
return replay();
}
};
/**
* Returns a factory object to instantiate SinonJS stub object
*
* @param object sinon
* @param object packageJson
*/
function record(sinon, packageJson) {
return (function() {
var session = (function() {
var stub = sinon.stub({
setWaitingForResultNotification: function(tf) {},
commit: function(commitState, block) {},
notifySync: function(entity, block) {},
notifyAsync: function(entity) {},
fetchUrlSync: function(url, params, block) {},
findPackage: function(objectName) {},
setDmjobArgument: function(name, value) {}
});
// applicationId is NOT included in package.json.
stub.applicationId = 'applicationId';
stub.packageId = packageJson.name;
stub.log = function(tag, message) {
console.log('[' + tag + ']:' + message);
};
stub.digest = function(algorithm, encoding, value) {
assert(algorithm, 'Set the algorithm, one of MD5, SHA1, or SHA256 is available.');
assert(algorithm === 'MD5' || algorithm === 'SHA1' || algorithm === 'SHA256',
'Set the algorithm, one of MD5, SHA1, or SHA256 is available.');
assert(encoding, 'Set the encoding, one of hex, b64 or plain is available.');
assert(encoding === 'hex' || encoding === 'b64' || encoding === 'plain',
'Set the encoding, one of hex, b64 or plain is available.');
assert(value, 'value is missing.');
assert(typeof(value) === 'string', 'value should be string.');
var shasum = crypto.createHash(algorithm);
shasum.update(value);
var digest = null;
switch (encoding) {
default:
case 'hex':
digest = shasum.digest('hex');
break;
case 'b64':
digest = shasum.digest('base64');
break;
case 'plain':
digest = shasum.digest('binary').toString('utf8');
break;
}
return digest;
};
stub.hmac = function(algorithm, encoding, secret, value) {
assert(algorithm, 'Set the algorithm, one of MD5, SHA1, or SHA256 is available.');
assert(algorithm === 'MD5' || algorithm === 'SHA1' || algorithm === 'SHA256',
'Set the algorithm, one of MD5, SHA1, or SHA256 is available.');
assert(encoding, 'Set the encoding, one of hex, b64 or plain is available.');
assert(encoding == 'hex' || encoding == 'b64' || encoding == 'plain',
'Set the encoding, one of hex, b64 or plain is available.');
assert(secret, 'secret is missing.');
assert(typeof(secret) === 'string', 'secret should be string.');
assert(value, 'value is missing');
assert(typeof(value) === 'string', 'value should be string.');
var shasum = crypto.createHmac(algorithm, secret);
shasum.update(value);
var digest = null;
switch (encoding) {
default:
case 'hex':
digest = shasum.digest('hex');
break;
case 'b64':
digest = shasum.digest('base64');
break;
case 'plain':
digest = shasum.digest('binary').toString('utf8');
break;
}
return digest;
};
stub.hex2b64 = function(hex) {
assert(hex, 'hex is missing.');
assert(typeof(hex) === 'string', 'hex should be string.');
var buf = new Buffer(hex, 'hex');
return buf.toString('base64');
};
stub.b642hex = function(b64) {
assert(b64, 'b64 is missing.');
assert(typeof(b64) === 'string', 'b64 should be string.');
var buf = new Buffer(b64, 'base64');
return buf.toString('hex');
};
function text2_(target, arg1, arg2) {
var text = null,
encoding = 'utf8',
format = target;
if (arg2) {
text = arg2;
encoding = arg1;
} else {
assert(arg1, 'text is missing.');
text = arg1;
}
assert(typeof(text) === 'string', 'text should be string.');
if (format === 'b64') {
format = 'base64';
}
var buf = new Buffer(text, encoding);
return buf.toString(format);
}
function textFrom_(target, arg1, arg2) {
var str = null,
encoding = 'utf8',
format = target;
if (arg2) {
str = arg2;
encoding = arg1;
} else {
assert(arg1, target + ' is missing.');
str = arg1;
}
assert(typeof(str) === 'string', target + ' should be string.');
if (format === 'b64') {
format = 'base64';
}
var buf = new Buffer(str, format);
return buf.toString(encoding);
}
stub.text2b64 = function(arg1, arg2) {
return text2_('b64', arg1, arg2);
};
stub.b642text = function(arg1, arg2) {
return textFrom_('b64', arg1, arg2);
};
stub.text2hex = function(arg1, arg2) {
return text2_('hex', arg1, arg2);
};
var mapperHash = [];
var modelArrayHash = [];
stub.newModelMapperStub = function(type) {
if (!packageJson.models[type]) {
throwError(type + ' is not defined in package.json!');
}
var mapper = mapperHash[type];
if (mapper) {
return mapper;
}
mapper = sinon.stub({
add: function(entity, block) {},
update: function(entity, block) {},
updateFields: function(entity, fields, block) {},
remove: function(uid, block) {},
findByUid: function(uid, block) {},
findAllUids: function(block) {},
count: function(block) {}
});
mapper.newModelStub = function() {
var modelArray = modelArrayHash[type];
if (exports.replay) {
if (!modelArray || modelArray.length == 0) {
console.log('No record for the model!, type:' + type);
throw "No record for the model!";
} else {
return modelArray.shift();
}
} else {
if (!modelArray) {
modelArray = [];
modelArrayHash[type] = modelArray;
}
var model = sinon.stub();
model.__type = function() {
return type;
};
modelArray.push(model);
return model;
}
};
mapperHash[type] = mapper;
return mapper;
};
return stub;
})();
var clientRequest = {
objects: null,
device: null,
dmjob: null
};
var database = sinon.stub({
insert: function(entity) {},
update: function(entity) {},
remove: function(type, uids) {},
query: function(type, offsetOrToken, limit) {},
queryByUids: function(type, uids, f, r) {},
querySharedByUids: function(type, uids, f, r) {}
});
var context = {
/**
* MessageSession stub object.
*/
session: session,
/**
* Returns ClientRequest stub object.
*
* @return object (ClientRequest)
*/
clientRequest: clientRequest,
/**
* Returns Database stub object.
*
* @return object (Database)
*/
database: database,
// packageJson (internal property)
packageJson: packageJson,
// For command operations
addCommand: function(entity, name, event) {
var type = entity.__type();
if (!type) {
throwError('entity must be a return value of newModelStub().');
}
var descriptor = packageJson.models[type];
if (!descriptor) {
throwError('Unknown type:' + type);
}
var commands = descriptor.commands;
if (!commands || !commands[name]) {
throwError('Command :' + name + ' is NOT defined in the type:' + type);
}
entity[name] = function(session, parameter, block) {
session.commit();
if (block && block[event.id]) {
block[event.id].apply({}, event.args);
}
};
},
// clientRequest attributes manipulation
/**
* A setter method for Device object.
*
* @param Array objects
* @return objects (Array)
*/
setObjects: function(objects) {
clientRequest.objects = objects;
return objects;
},
/**
* A setter method for Device object.
*
* @param String uid
* @param String deviceId
* @param String name
* @param String status
* @param String clientVerion
* @param Number rev
* @return object (Device)
*/
setDevice: function(uid, deviceId, name, status, clientVersion, rev) {
if (uid) {
clientRequest.device = {
uid: uid,
deviceId: deviceId,
name: name,
status: status,
clientVersion: clientVersion,
rev: rev
};
} else {
clientRequest.device = null;
}
return clientRequest.device;
},
/**
* A setter method for Dmjob object.
*
* @param object uid
* @param object deviceId
* @param object name
* @param object status
* @param object jobServiceId
* @param object sessionId
* @param object arguments
* @param object createdAt
* @param object activatedAt
* @param object startedAt
* @param object expiredAt
* @param object notificationType
* @param object notificationUri
* @return object (Dmjob)
*/
setDmjob: function(uid, deviceId, name, status, jobServiceId, sessionId, arguments, createdAt, activatedAt, startedAt, expiredAt, notificationType, notificationUri) {
if (uid) {
clientRequest.dmjob = {
uid: uid,
deviceId: deviceId,
name: name,
status: status,
jobServiceId: jobServiceId,
sessionId: sessionId,
arguments: arguments,
createdAt: createdAt,
activatedAt: activatedAt,
startedAt: startedAt,
expiredAt: expiredAt,
notificationType: notificationType,
notificationUri: notificationUri
};
} else {
clientRequest.dmjob = null;
}
return clientRequest.dmjob;
},
// factory function
/**
* A factory method for an event object used for addCommand().
*
* @param Boolean async
* @param Array array
* @return object
*/
newSuccessfulCommandEvent: function(async, array) {
return {
id: 'success',
args: [{
success: true,
async: async,
array: array
}]
};
},
/**
* A factory method for an event object used for addCommand().
*
* @param String code
* @param String type
* @return object
*/
newErrorCommandEvent: function(code, type) {
return {
id: 'error',
args: [code, type]
};
},
/**
* A factory method for an event object used for addCommand().
*
* @param String eventId
* @return object
*/
newIntrruptCommandEvent: function(eventId) {
return {
id: 'interrupt',
args: [eventId]
};
},
/**
* A factory method for QueryResult stub object.
*
* @param Array array
* @param String offset
* @param int limit
* @return object (QueryResult)
*/
newQueryResult: function(array, offset, limit) {
return {
array: array,
offset: offset,
limit: limit
};
}
};
return context;
})();
};
function loadPackageJson() {
var path = require('path');
return require(path.resolve('./package.json'));
}
function throwError(message) {
console.trace('[ERROR] ' + message);
throw message;
}
/**
* Returns the root stub object.
* This function should be invoked after tweaking sinon stubs.
*
* @param object sinonContext
*/
function replay() {
var blocks = [];
var tokens = [];
function prepare(token, block) {
if (block) {
console.log('token-->' + token + ', block == null? => ' + (block == null));
blocks.push(block);
tokens.push(token);
}
return token;
}
var state = exports.state;
if (!state) {
throw "Invalid State. Invoke init(sinon) at first!";
}
var stub = state.session;
state.session = {
applicationId: stub.applicationId,
packageId: stub.packageId,
setWaitingForResultNotification: function(tf) {
return stub.setWaitingForResultNotification(tf);
},
notifySync: function(entity, block) {
return stub.notifySync(entity, block);
},
notifyAsync: function(entity) {
return stub.notifyAsync(entity);
},
fetchUrlSync: function(url, params, block) {
return stub.fetchUrlSync(url, params, block);
},
commit: function(commitState, methodBlock) {
var result = stub.commit(commitState, methodBlock);
if (!result) {
console.log('INFO: commit() returns undefined.');
return null;
}
var interrupt = (result['event'] != null);
for (var i = 0; i < blocks.length; i++) {
var lb = blocks[i];
if (lb) {
var obj = result[tokens[i]];
if (!obj) {
console.log('INFO: Missing object for the token['
+ tokens[i] + '] in result[' + JSON.stringify(result) + ']');
}
if (interrupt) {
if (lb.interrupt) {
lb.interrupt(result['event']);
continue;
}
} else if (obj.errorType || obj.errorCode) {
if (lb.error) {
lb.error(obj.errorType, obj.errorCode);
continue;
}
} else {
if (lb.success) {
lb.success(obj);
continue;
}
}
}
}
if (methodBlock) {
if (interrupt) {
if (methodBlock.interrupt) {
return methodBlock.interrupt(result['event']);
}
} else {
if (methodBlock.success) {
return methodBlock.success(result);
}
}
} else {
if (interrupt) {
console.log('INTERRUPT: event => ' + result['event']);
throw result['event'];
}
}
blocks = [];
tokens = [];
return result;
},
log: function(tag, message) {
console.log('[' + tag + ']:' + message);
},
findPackage: function(objectName) {
return stub.findPackage(objectName);
},
setDmjobArgument: function(name, value) {
return stub.setDmjobArgument(name, value);
},
digest: function(algorithm, encoding, value) {
return stub.digest(algorithm, encoding, value);
},
hmac: function(algorithm, encoding, secret, value) {
return stub.hmac(algorithm, encoding, secret, value);
},
hex2b64: function(hex) {
return stub.hex2b64(hex);
},
b642hex: function(b64) {
return stub.b642hex(b64);
},
text2hex: function(arg1, arg2) {
return stub.text2hex(arg1, arg2);
},
text2b64: function(arg1, arg2) {
return stub.text2b64(arg1, arg2);
},
b642text: function(arg1, arg2) {
return stub.b642text(arg1, arg2);
},
newModelMapperStub: function(type) {
var mapper = stub.newModelMapperStub(type);
return {
add: function(entity, block) {
return prepare(mapper.add(entity, block), block);
},
update: function(entity, block) {
return prepare(mapper.update(entity, block), block);
},
updateFields: function(entity, fields, block) {
return prepare(mapper.updateFields(entity, fields, block), block);
},
remove: function(uid, block) {
return prepare(mapper.remove(uid, block), block);
},
findByUid: function(uid, block) {
return prepare(mapper.findByUid(uid, block), block);
},
findAllUids: function(block) {
return prepare(mapper.findAllUids(block), block);
},
count: function(block) {
return prepare(mapper.count(block), block);
},
newModelStub: function() {
return mapper.newModelStub();
}
};
}
};
return state;
};