meadow-endpoints 2.0.13 → 3.0.1

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.
@@ -0,0 +1,1731 @@
1
+ /**
2
+ * Unit tests for the MeadowEndpoints Server in trusted session data mode.
3
+ *
4
+ * @license MIT
5
+ *
6
+ * @author Alex Decker <alex.decker@headlght.com>
7
+ */
8
+
9
+ const Chai = require('chai');
10
+ const Expect = Chai.expect;
11
+ const Assert = Chai.assert;
12
+
13
+ const libSuperTest = require('supertest');
14
+
15
+ const libMySQL = require('mysql2');
16
+ const libAsync = require('async');
17
+
18
+ let tmpFableSettings = (
19
+ {
20
+ Product: 'MockOratorAlternate',
21
+ ProductVersion: '0.0.0',
22
+
23
+ UnauthorizedRequestDelay: 10,
24
+ MeadowEndpointsSessionDataSource: 'Header',
25
+
26
+ MeadowAuthenticationMode: 'LoggedIn',
27
+ MeadowAuthorizationMode: 'SimpleOwnership',
28
+
29
+ APIServerPort: 9081,
30
+
31
+ MySQL:
32
+ {
33
+ // This is queued up for Travis defaults.
34
+ Server: 'localhost',
35
+ Port: 3306,
36
+ User: process.env.DEV_MYSQL_USER || 'root',
37
+ Password: process.env.DEV_MYSQL_PASS || '123456789',
38
+ Database: 'FableTest',
39
+ ConnectionPoolLimit: 20
40
+ },
41
+ ConfigFile: + `${__dirname}/../MeadowTest-Settings.json`,
42
+ });
43
+
44
+ const libFable = require('fable').new(tmpFableSettings);
45
+ tmpFableSettings = libFable.settings;
46
+
47
+ libFable.MeadowMySQLConnectionPool = libMySQL.createPool
48
+ (
49
+ {
50
+ connectionLimit: libFable.settings.MySQL.ConnectionPoolLimit,
51
+ host: libFable.settings.MySQL.Server,
52
+ port: libFable.settings.MySQL.Port,
53
+ user: libFable.settings.MySQL.User,
54
+ password: libFable.settings.MySQL.Password,
55
+ database: libFable.settings.MySQL.Database,
56
+ namedPlaceholders: true
57
+ }
58
+ );
59
+
60
+ const _MockSessionValidUser = (
61
+ {
62
+ SessionID: '0000-VALID',
63
+ UserID: 37,
64
+ UserRole: 'User',
65
+ UserRoleIndex: 1,
66
+ LoggedIn: true,
67
+ DeviceID: 'TEST-HARNESS',
68
+ CustomerID: 1
69
+ });
70
+
71
+ let _Meadow;
72
+ let _MeadowEndpoints;
73
+
74
+ const _AnimalSchema = require('./Animal.json');
75
+
76
+ // Now that we have some test data, wire up the endpoints!
77
+
78
+ // Load up a Meadow (pointing at the Animal database)
79
+ _Meadow = require('meadow')
80
+ .new(libFable, 'FableTest')
81
+ .setProvider('MySQL')
82
+ .setSchema(_AnimalSchema.Schema)
83
+ .setJsonSchema(_AnimalSchema.JsonSchema)
84
+ .setDefaultIdentifier(_AnimalSchema.DefaultIdentifier)
85
+ .setDefault(_AnimalSchema.DefaultObject)
86
+ .setAuthorizer(_AnimalSchema.Authorization);
87
+ // Instantiate the endpoints
88
+ _MeadowEndpoints = require('../source/Meadow-Endpoints.js').new(_Meadow);
89
+
90
+ suite
91
+ (
92
+ 'Meadow-Endpoints with Trusted Header Auth',
93
+ function()
94
+ {
95
+ // TODO: Abstract this so it can be run again and again.
96
+ let _SpooledUp = false;
97
+ let _Orator;
98
+
99
+ const getAnimalInsert = function(pName, pType)
100
+ {
101
+ return "INSERT INTO `FableTest` (`IDAnimal`, `GUIDAnimal`, `CreateDate`, `CreatingIDUser`, `UpdateDate`, `UpdatingIDUser`, `Deleted`, `DeleteDate`, `DeletingIDUser`, `Name`, `Type`, `IDCustomer`) VALUES (NULL, '00000000-0000-0000-0000-000000000000', NOW(), 1, NOW(), 1, 0, NULL, 0, '"+pName+"', '"+pType+"', 1); ";
102
+ };
103
+
104
+ setup
105
+ (
106
+ function(fDone)
107
+ {
108
+ // Only do this for the first test, so we persiste database state across suites
109
+ if (!_SpooledUp)
110
+ {
111
+ _Orator = require('orator').new(tmpFableSettings);
112
+ _Orator.enabledModules.CORS = true;
113
+ _Orator.enabledModules.FullResponse = true;
114
+ _Orator.enabledModules.Body = false;
115
+
116
+
117
+ const _SQLConnectionPool = libMySQL.createPool
118
+ (
119
+ {
120
+ connectionLimit: tmpFableSettings.MySQL.ConnectionPoolLimit,
121
+ host: tmpFableSettings.MySQL.Server,
122
+ port: tmpFableSettings.MySQL.Port,
123
+ user: tmpFableSettings.MySQL.User,
124
+ password: tmpFableSettings.MySQL.Password,
125
+ database: tmpFableSettings.MySQL.Database
126
+ }
127
+ );
128
+
129
+ // Tear down previous test data, rebuild records
130
+ libAsync.waterfall(
131
+ [
132
+ function(fCallBack)
133
+ {
134
+ _SQLConnectionPool.query('DROP TABLE IF EXISTS FableTest',
135
+ function(pErrorUpdate, pResponse) { fCallBack(null); });
136
+ },
137
+ function(fCallBack)
138
+ {
139
+ _SQLConnectionPool.query("CREATE TABLE IF NOT EXISTS FableTest (IDAnimal INT UNSIGNED NOT NULL AUTO_INCREMENT, GUIDAnimal CHAR(36) NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000', CreateDate DATETIME, CreatingIDUser INT NOT NULL DEFAULT '0', UpdateDate DATETIME, UpdatingIDUser INT NOT NULL DEFAULT '0', Deleted TINYINT NOT NULL DEFAULT '0', DeleteDate DATETIME, DeletingIDUser INT NOT NULL DEFAULT '0', Name CHAR(128) NOT NULL DEFAULT '', Type CHAR(128) NOT NULL DEFAULT '', IDCustomer INT NOT NULL DEFAULT '0', PRIMARY KEY (IDAnimal) );",
140
+ function(pErrorUpdate, pResponse) { fCallBack(null); });
141
+ },
142
+ function(fCallBack)
143
+ {
144
+ _SQLConnectionPool.query(getAnimalInsert('Foo Foo', 'Bunny'),
145
+ function(pErrorUpdate, pResponse) { fCallBack(null); });
146
+ },
147
+ function(fCallBack)
148
+ {
149
+ _SQLConnectionPool.query(getAnimalInsert('Red Riding Hood', 'Girl'),
150
+ function(pErrorUpdate, pResponse) { fCallBack(null); });
151
+ },
152
+ function(fCallBack)
153
+ {
154
+ _SQLConnectionPool.query(getAnimalInsert('Red', 'Dog'),
155
+ function(pErrorUpdate, pResponse) { fCallBack(null); });
156
+ },
157
+ function(fCallBack)
158
+ {
159
+ _SQLConnectionPool.query(getAnimalInsert('Spot', 'Dog'),
160
+ function(pErrorUpdate, pResponse) { fCallBack(null); });
161
+ },
162
+ function(fCallBack)
163
+ {
164
+ _SQLConnectionPool.query(getAnimalInsert('Gertrude', 'Frog'),
165
+ function(pErrorUpdate, pResponse) { fCallBack(null); });
166
+ },
167
+ function(fCallBack)
168
+ {
169
+ // Start the web server
170
+ _MeadowEndpoints.setEndpointAuthorization
171
+ (
172
+ 'Create',
173
+ 2
174
+ );
175
+ _MeadowEndpoints.setEndpointAuthenticator ('Reads');
176
+ _MeadowEndpoints.setEndpointAuthenticator
177
+ (
178
+ 'Reads',
179
+ function(pRequest, pResponse, fNext)
180
+ {
181
+ pRequest.EndpointAuthenticated = true;
182
+ fNext();
183
+ }
184
+ );
185
+ _MeadowEndpoints.setEndpoint('Randomize');
186
+ _MeadowEndpoints.setEndpoint('Randomize', function() {});
187
+
188
+ _MeadowEndpoints.behaviorModifications.setTemplate('ListQuery', '<%= MyData %>');
189
+ //_MeadowEndpoints.behaviorModifications.setTemplate('SelectList', '<%= Name %>|<%= Type %>');
190
+
191
+ // Wire the endpoints up
192
+ _MeadowEndpoints.connectRoutes(_Orator.webServer);
193
+ _Orator.startWebServer (function() { fCallBack(null); });
194
+ }
195
+ ],
196
+ function(pError, pResult)
197
+ {
198
+ // Now continue the tests.
199
+ _SpooledUp = true;
200
+ fDone();
201
+ }
202
+ );
203
+ }
204
+ else
205
+ {
206
+ fDone();
207
+ }
208
+ }
209
+ );
210
+
211
+ setup
212
+ (
213
+ function()
214
+ {
215
+ }
216
+ );
217
+
218
+ suite
219
+ (
220
+ 'Object Sanity',
221
+ function()
222
+ {
223
+ test
224
+ (
225
+ 'initialize should build a happy little object',
226
+ function()
227
+ {
228
+ Expect(_MeadowEndpoints).to.be.an('object', 'MeadowEndpoints should initialize as an object directly from the require statement.');
229
+ }
230
+ );
231
+ }
232
+ );
233
+ suite
234
+ (
235
+ 'Behavior Modifications',
236
+ function()
237
+ {
238
+ test
239
+ (
240
+ 'instantiate a behavior modification object',
241
+ function()
242
+ {
243
+ const tmpBehaviorMods = require('../source/Meadow-BehaviorModifications.js').new(libFable);
244
+ Expect(tmpBehaviorMods).to.be.an('object');
245
+ }
246
+ );
247
+ test
248
+ (
249
+ 'exercise the templates api',
250
+ function()
251
+ {
252
+ const tmpBehaviorMods = require('../source/Meadow-BehaviorModifications.js').new(libFable);
253
+
254
+ let tmpCrossBehaviorState = 0;
255
+
256
+ Expect(tmpBehaviorMods.runBehavior('NoBehaviorsHere', {}, function() {})).to.equal(undefined, 'nonexistant behaviors should just execute');
257
+ tmpBehaviorMods.setBehavior('BigBehavior', function() { tmpCrossBehaviorState++ });
258
+ Expect(tmpCrossBehaviorState).to.equal(0);
259
+ Expect(tmpBehaviorMods.runBehavior('BigBehavior', {}, function() {})).to.equal(undefined, 'existant behaviors should just execute');
260
+ Expect(tmpCrossBehaviorState).to.equal(1);
261
+ }
262
+ );
263
+ test
264
+ (
265
+ 'exercise the behavior modification api',
266
+ function()
267
+ {
268
+ const tmpBehaviorMods = require('../source/Meadow-BehaviorModifications.js').new(libFable);
269
+ Expect(tmpBehaviorMods.getTemplateFunction('NoTemplatesHere')).to.equal(false, 'empty template hashes on empty sets should return false');
270
+ Expect(tmpBehaviorMods.getTemplate('NoTemplatesHere')).to.equal(false,'emtpy template sets should be false');
271
+ tmpBehaviorMods.setTemplate('AnimalFormatter', '<p>An animal (id <%= Number %> is here</p>');
272
+ Expect(tmpBehaviorMods.getTemplate('AnimalFormatter')).to.contain('An animal');
273
+ Expect(tmpBehaviorMods.processTemplate('AnimalFormatter', {Number:5})).to.contain('id 5');
274
+ Expect(tmpBehaviorMods.processTemplate('FriendFormatter', {Number:5}, 'blit <%= Number %>')).to.contain('blit 5');
275
+ Expect(tmpBehaviorMods.processTemplate('Blank', {Number:5})).to.equal('');
276
+ tmpBehaviorMods.setTemplate('SimpleTemplate', 'Not so simple.');
277
+ Expect(tmpBehaviorMods.processTemplate('SimpleTemplate')).to.equal('Not so simple.');
278
+ }
279
+ );
280
+ test
281
+ (
282
+ 'exercise the security modification api',
283
+ function()
284
+ {
285
+ const tmpAuthorizers = require('../source/Meadow-Authorizers.js').new(libFable);
286
+ tmpAuthorizers.setAuthorizer('AlwaysAuthorize',
287
+ function(pRequest, fComplete)
288
+ {
289
+ pRequest.MeadowAuthorization = true;
290
+ });
291
+ const tmpMockRequest = {MeadowAuthorization: 'Green'};
292
+ tmpAuthorizers.authorize('BadHash', tmpMockRequest,
293
+ function()
294
+ {
295
+ Expect(tmpMockRequest.MeadowAuthorization).to.equal('Green');
296
+ });
297
+ tmpAuthorizers.authorize('AlwaysAuthorize', tmpMockRequest,
298
+ function()
299
+ {
300
+ Expect(tmpMockRequest.MeadowAuthorization).to.equal(true);
301
+ });
302
+ tmpAuthorizers.authorize('Allow', tmpMockRequest,
303
+ function()
304
+ {
305
+ Expect(tmpMockRequest.MeadowAuthorization).to.equal(true);
306
+ });
307
+ tmpAuthorizers.authorize('Deny', tmpMockRequest,
308
+ function()
309
+ {
310
+ Expect(tmpMockRequest.MeadowAuthorization).to.equal(false);
311
+ });
312
+ }
313
+ );
314
+ test
315
+ (
316
+ 'exercise the security modification authenticators',
317
+ function()
318
+ {
319
+ const tmpAuthorizers = require('../source/Meadow-Authorizers.js').new(libFable);
320
+ const tmpMockFullRequest =
321
+ {
322
+ UserSession:
323
+ {
324
+ CustomerID: 10,
325
+ UserID: 1
326
+ },
327
+ Record:
328
+ {
329
+ IDCustomer: 10,
330
+ CreatingIDUser: 1
331
+ }
332
+ };
333
+ // Mine and MyCustomer should both work
334
+ tmpAuthorizers.authorize('Mine', tmpMockFullRequest,
335
+ function()
336
+ {
337
+ Expect(tmpMockFullRequest.MeadowAuthorization).to.equal(true);
338
+ });
339
+ tmpAuthorizers.authorize('MyCustomer', tmpMockFullRequest,
340
+ function()
341
+ {
342
+ Expect(tmpMockFullRequest.MeadowAuthorization).to.equal(true);
343
+ });
344
+ tmpMockFullRequest.UserSession.CustomerID = 100;
345
+ tmpMockFullRequest.UserSession.UserID = 100;
346
+ // Now they should both fail
347
+ tmpAuthorizers.authorize('Mine', tmpMockFullRequest,
348
+ function()
349
+ {
350
+ //If record does not have matching CreatingIDUser, then it should fail
351
+ Expect(tmpMockFullRequest.MeadowAuthorization).to.equal(false);
352
+ });
353
+ tmpAuthorizers.authorize('MyCustomer', tmpMockFullRequest,
354
+ function()
355
+ {
356
+ //If record does not have matching CustomerID, then it should fail
357
+ Expect(tmpMockFullRequest.MeadowAuthorization).to.equal(false);
358
+ });
359
+ }
360
+ );
361
+ }
362
+ );
363
+ suite
364
+ (
365
+ 'Basic Server Routes',
366
+ function()
367
+ {
368
+ test
369
+ (
370
+ 'create: create a record',
371
+ function(fDone)
372
+ {
373
+ const tmpRecord = {Name:'BatBrains', Type:'Mammoth'};
374
+ _MockSessionValidUser.UserRoleIndex = 2;
375
+ libSuperTest('http://localhost:9081/')
376
+ .post('1.0/FableTest')
377
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
378
+ .send(tmpRecord)
379
+ .end(
380
+ function(pError, pResponse)
381
+ {
382
+ // Expect response to be the record we just created.
383
+ const tmpResult = JSON.parse(pResponse.text);
384
+ Expect(tmpResult.Type).to.equal('Mammoth');
385
+ Expect(tmpResult.CreatingIDUser).to.equal(37);
386
+ fDone();
387
+ }
388
+ );
389
+ }
390
+ );
391
+ test
392
+ (
393
+ 'create: create a record',
394
+ function(fDone)
395
+ {
396
+ const tmpRecord = {Name:'BatBrains', Type:'Mammoth'};
397
+ _MockSessionValidUser.UserRoleIndex = 1;
398
+ libSuperTest('http://localhost:9081/')
399
+ .post('1.0/FableTest')
400
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
401
+ .send(tmpRecord)
402
+ .end(
403
+ function(pError, pResponse)
404
+ {
405
+ // Expect response to be the record we just created.
406
+ const tmpResult = JSON.parse(pResponse.text);
407
+ Expect(tmpResult.Error).to.contain('authenticated');
408
+ fDone();
409
+ }
410
+ );
411
+ }
412
+ );
413
+ test
414
+ (
415
+ 'create: create a record with a bad record passed in',
416
+ function(fDone)
417
+ {
418
+ const tmpRecord = ' ';
419
+ _MockSessionValidUser.UserRoleIndex = 2;
420
+ libSuperTest('http://localhost:9081/')
421
+ .post('1.0/FableTest')
422
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
423
+ .send(tmpRecord)
424
+ .end(
425
+ function(pError, pResponse)
426
+ {
427
+ // Expect response to be the record we just created.
428
+ const tmpResult = JSON.parse(pResponse.text);
429
+ Expect(tmpResult.Error).to.not.be.null;
430
+ fDone();
431
+ }
432
+ );
433
+ }
434
+ );
435
+ test
436
+ (
437
+ 'read: get a specific record',
438
+ function(fDone)
439
+ {
440
+ libSuperTest('http://localhost:9081/')
441
+ .get('1.0/FableTest/2')
442
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
443
+ .end(
444
+ function (pError, pResponse)
445
+ {
446
+ const tmpResult = JSON.parse(pResponse.text);
447
+ Expect(tmpResult.Type).to.equal('Girl');
448
+ fDone();
449
+ }
450
+ );
451
+ }
452
+ );
453
+ test
454
+ (
455
+ 'read: define a custom route and get a record with it',
456
+ function(fDone)
457
+ {
458
+ _Orator.webServer.get('/CustomHotRodRoute/:IDRecord', _MeadowEndpoints.endpointAuthenticators.Read, _MeadowEndpoints.wireState, _MeadowEndpoints.endpoints.Read);
459
+ libSuperTest('http://localhost:9081/')
460
+ .get('CustomHotRodRoute/2')
461
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
462
+ .end(
463
+ function (pError, pResponse)
464
+ {
465
+ const tmpResult = JSON.parse(pResponse.text);
466
+ Expect(tmpResult.Type).to.equal('Girl');
467
+ fDone();
468
+ }
469
+ );
470
+ }
471
+ );
472
+ test
473
+ (
474
+ 'read: get a specific record but be denied by security',
475
+ function(fDone)
476
+ {
477
+ _Meadow.schemaFull.authorizer.Manager = {};
478
+ _Meadow.schemaFull.authorizer.Manager.Read = 'Deny';
479
+
480
+ libSuperTest('http://localhost:9081/')
481
+ .get('1.0/FableTest/2')
482
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
483
+ .end(
484
+ function (pError, pResponse)
485
+ {
486
+ Expect(pResponse.text).to.contain('UNAUTHORIZED ACCESS IS NOT ALLOWED');
487
+ // Reset authorization
488
+ _Meadow.schemaFull.authorizer.Manager.Read = 'Allow';
489
+ fDone();
490
+ }
491
+ );
492
+ }
493
+ );
494
+ test
495
+ (
496
+ 'read: get a specific record with a bad parameter',
497
+ function(fDone)
498
+ {
499
+ libSuperTest('http://localhost:9081/')
500
+ .get('1.0/FableTest/')
501
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
502
+ .end(
503
+ function (pError, pResponse)
504
+ {
505
+ const tmpResult = JSON.parse(pResponse.text);
506
+ Expect(tmpResult.Error).to.equal('Error retreiving a record. Record not found');
507
+ fDone();
508
+ }
509
+ );
510
+ }
511
+ );
512
+ test
513
+ (
514
+ 'reads: get all records',
515
+ function(fDone)
516
+ {
517
+ libSuperTest('http://localhost:9081/')
518
+ .get('1.0/FableTests')
519
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
520
+ .end(
521
+ function (pError, pResponse)
522
+ {
523
+ const tmpResults = JSON.parse(pResponse.text);
524
+ Expect(tmpResults.length).to.equal(6);
525
+ Expect(tmpResults[0].Type).to.equal('Bunny');
526
+ Expect(tmpResults[4].Name).to.equal('Gertrude');
527
+ fDone();
528
+ }
529
+ );
530
+ }
531
+ );
532
+ test
533
+ (
534
+ 'readsLiteExtended: get all records',
535
+ function(fDone)
536
+ {
537
+ libSuperTest('http://localhost:9081/')
538
+ .get('1.0/FableTests/LiteExtended/Type,Name')
539
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
540
+ .end(
541
+ function (pError, pResponse)
542
+ {
543
+ const tmpResults = JSON.parse(pResponse.text);
544
+ Expect(tmpResults.length).to.equal(6);
545
+ Expect(tmpResults[0].IDAnimal).to.equal(1);
546
+ Expect(tmpResults[4].IDAnimal).to.equal(5);
547
+ Expect(tmpResults[4].Type).to.equal('Frog');
548
+ fDone();
549
+ }
550
+ );
551
+ }
552
+ );
553
+ test
554
+ (
555
+ 'readsby: get all records by Type',
556
+ function(fDone)
557
+ {
558
+ libSuperTest('http://localhost:9081/')
559
+ .get('1.0/FableTests/By/Type/Dog')
560
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
561
+ .end(
562
+ function (pError, pResponse)
563
+ {
564
+ const tmpResults = JSON.parse(pResponse.text);
565
+ Expect(tmpResults.length).to.equal(2);
566
+ Expect(tmpResults[0].Type).to.equal('Dog');
567
+ fDone();
568
+ }
569
+ );
570
+ }
571
+ );
572
+ test
573
+ (
574
+ 'readsby: get all records by Type IN LIST',
575
+ function(fDone)
576
+ {
577
+ libSuperTest('http://localhost:9081/')
578
+ .get('1.0/FableTests/By/Type/Mammoth%2C%20WithComma,Dog')
579
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
580
+ .end(
581
+ function (pError, pResponse)
582
+ {
583
+ const tmpResults = JSON.parse(pResponse.text);
584
+ Expect(tmpResults.length).to.equal(2);
585
+ Expect(tmpResults[0].Type).to.equal('Dog');
586
+ fDone();
587
+ }
588
+ );
589
+ }
590
+ );
591
+ test
592
+ (
593
+ 'countby: get count of records by Type',
594
+ function(fDone)
595
+ {
596
+ libSuperTest('http://localhost:9081/')
597
+ .get('1.0/FableTests/Count/By/Type/Dog')
598
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
599
+ .end(
600
+ function (pError, pResponse)
601
+ {
602
+ const tmpResults = JSON.parse(pResponse.text);
603
+ Expect(tmpResults.Count).to.equal(2);
604
+ fDone();
605
+ }
606
+ );
607
+ }
608
+ );
609
+ test
610
+ (
611
+ 'countby: get count of records by multiple Types',
612
+ function(fDone)
613
+ {
614
+ libSuperTest('http://localhost:9081/')
615
+ .get('1.0/FableTests/Count/By/Type/Dog,Mammoth')
616
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
617
+ .end(
618
+ function (pError, pResponse)
619
+ {
620
+ const tmpResults = JSON.parse(pResponse.text);
621
+ Expect(tmpResults.Count).to.equal(3);
622
+ fDone();
623
+ }
624
+ );
625
+ }
626
+ );
627
+ test
628
+ (
629
+ 'readsby: get paged records by Type',
630
+ function(fDone)
631
+ {
632
+ libSuperTest('http://localhost:9081/')
633
+ .get('1.0/FableTests/By/Type/Dog/1/1')
634
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
635
+ .end(
636
+ function (pError, pResponse)
637
+ {
638
+ const tmpResults = JSON.parse(pResponse.text);
639
+ Expect(tmpResults.length).to.equal(1);
640
+ Expect(tmpResults[0].Name).to.equal('Spot');
641
+ fDone();
642
+ }
643
+ );
644
+ }
645
+ );
646
+ test
647
+ (
648
+ 'readselect: get a page of filtered records by date',
649
+ function(fDone)
650
+ {
651
+ let today = new Date();
652
+ today = today.toISOString().substring(0, 10);
653
+
654
+ libSuperTest('http://localhost:9081/')
655
+ .get(`1.0/FableTestSelect/FilteredTo/FBD~UpdateDate~EQ~${today}/0/1`)
656
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
657
+ .end(
658
+ function (pError, pResponse)
659
+ {
660
+ console.log(pResponse.text)
661
+ const tmpResults = JSON.parse(pResponse.text);
662
+ Expect(tmpResults.length).to.equal(1);
663
+ Expect(tmpResults[0].Value).to.equal('FableTest #1');
664
+ fDone();
665
+ }
666
+ );
667
+ }
668
+ );
669
+ test
670
+ (
671
+ 'readselect: get all records',
672
+ function(fDone)
673
+ {
674
+ libSuperTest('http://localhost:9081/')
675
+ .get('1.0/FableTestSelect')
676
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
677
+ .end(
678
+ function (pError, pResponse)
679
+ {
680
+ console.log(pResponse.text)
681
+ const tmpResults = JSON.parse(pResponse.text);
682
+ Expect(tmpResults.length).to.equal(6);
683
+ Expect(tmpResults[4].Value).to.equal('FableTest #5');
684
+ fDone();
685
+ }
686
+ );
687
+ }
688
+ );
689
+ test
690
+ (
691
+ 'readselect: get a page of records',
692
+ function(fDone)
693
+ {
694
+ libSuperTest('http://localhost:9081/')
695
+ .get('1.0/FableTestSelect/2/2')
696
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
697
+ .end(
698
+ function (pError, pResponse)
699
+ {
700
+ console.log(pResponse.text)
701
+ const tmpResults = JSON.parse(pResponse.text);
702
+ Expect(tmpResults.length).to.equal(2);
703
+ Expect(tmpResults[1].Value).to.equal('FableTest #4');
704
+ fDone();
705
+ }
706
+ );
707
+ }
708
+ );
709
+ test
710
+ (
711
+ 'readselect: get a page of records',
712
+ function(fDone)
713
+ {
714
+ libSuperTest('http://localhost:9081/')
715
+ .get('1.0/FableTestSelect/2/2')
716
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
717
+ .end(
718
+ function (pError, pResponse)
719
+ {
720
+ console.log(pResponse.text)
721
+ const tmpResults = JSON.parse(pResponse.text);
722
+ Expect(tmpResults.length).to.equal(2);
723
+ Expect(tmpResults[1].Value).to.equal('FableTest #4');
724
+ fDone();
725
+ }
726
+ );
727
+ }
728
+ );
729
+ test
730
+ (
731
+ 'readselect: get filtered records',
732
+ function(fDone)
733
+ {
734
+ libSuperTest('http://localhost:9081/')
735
+ .get('1.0/FableTestSelect/FilteredTo/FBV~Type~EQ~Dog')
736
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
737
+ .end(
738
+ function (pError, pResponse)
739
+ {
740
+ const tmpResults = JSON.parse(pResponse.text);
741
+ Expect(tmpResults.length).to.equal(2);
742
+ Expect(tmpResults[0].Value).to.equal('FableTest #3');
743
+ fDone();
744
+ }
745
+ );
746
+ }
747
+ );
748
+ test
749
+ (
750
+ 'readselect: get a page of filtered records',
751
+ function(fDone)
752
+ {
753
+ libSuperTest('http://localhost:9081/')
754
+ .get('1.0/FableTestSelect/FilteredTo/FBV~Type~EQ~Dog/1/1')
755
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
756
+ .end(
757
+ function (pError, pResponse)
758
+ {
759
+ console.log(pResponse.text)
760
+ const tmpResults = JSON.parse(pResponse.text);
761
+ Expect(tmpResults.length).to.equal(1);
762
+ Expect(tmpResults[0].Value).to.equal('FableTest #4');
763
+ fDone();
764
+ }
765
+ );
766
+ }
767
+ );
768
+ test
769
+ (
770
+ 'readselect: get an empty page of records',
771
+ function(fDone)
772
+ {
773
+ libSuperTest('http://localhost:9081/')
774
+ .get('1.0/FableTestSelect/200/200')
775
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
776
+ .end(
777
+ function (pError, pResponse)
778
+ {
779
+ console.log(pResponse.text)
780
+ const tmpResults = JSON.parse(pResponse.text);
781
+ Expect(tmpResults.length).to.equal(0);
782
+ fDone();
783
+ }
784
+ );
785
+ }
786
+ );
787
+ test
788
+ (
789
+ 'reads: get a page of records',
790
+ function(fDone)
791
+ {
792
+ libSuperTest('http://localhost:9081/')
793
+ // Get page 2, 2 records per page.
794
+ .get('1.0/FableTests/2/2')
795
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
796
+ .end(
797
+ function (pError, pResponse)
798
+ {
799
+ const tmpResults = JSON.parse(pResponse.text);
800
+ Expect(tmpResults.length).to.equal(2);
801
+ Expect(tmpResults[0].Type).to.equal('Dog');
802
+ Expect(tmpResults[1].Name).to.equal('Spot');
803
+ fDone();
804
+ }
805
+ );
806
+ }
807
+ );
808
+ test
809
+ (
810
+ 'reads: get a filtered set of records',
811
+ function(fDone)
812
+ {
813
+ libSuperTest('http://localhost:9081/')
814
+ .get('1.0/FableTests/FilteredTo/FBV~Type~EQ~Frog')
815
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
816
+ .end(
817
+ function (pError, pResponse)
818
+ {
819
+ const tmpResults = JSON.parse(pResponse.text);
820
+ Expect(tmpResults.length).to.equal(1);
821
+ Expect(tmpResults[0].Type).to.equal('Frog');
822
+ fDone();
823
+ }
824
+ );
825
+ }
826
+ );
827
+ test
828
+ (
829
+ 'reads: get distinct values for a column',
830
+ function(fDone)
831
+ {
832
+ libSuperTest('http://localhost:9081/')
833
+ .get('1.0/FableTests/Distinct/Type')
834
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
835
+ .end(
836
+ function (pError, pResponse)
837
+ {
838
+ const tmpResults = JSON.parse(pResponse.text);
839
+ Expect(tmpResults.length).to.equal(5);
840
+ const types = tmpResults.map((r) => r.Type);
841
+ Expect(types).to.have.members(['Bunny', 'Girl', 'Dog', 'Frog', 'Mammoth']);
842
+ fDone();
843
+ }
844
+ );
845
+ }
846
+ );
847
+ test
848
+ (
849
+ 'reads: get distinct values for a column with filter',
850
+ function(fDone)
851
+ {
852
+ libSuperTest('http://localhost:9081/')
853
+ .get('1.0/FableTests/Distinct/Type/FilteredTo/FBV~IDAnimal~LT~3')
854
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
855
+ .end(
856
+ function (pError, pResponse)
857
+ {
858
+ const tmpResults = JSON.parse(pResponse.text);
859
+ Expect(tmpResults.length).to.equal(2);
860
+ const types = new Set(tmpResults.map((r) => r.Type));
861
+ Expect(types.size).to.equal(2);
862
+ fDone();
863
+ }
864
+ );
865
+ }
866
+ );
867
+ test
868
+ (
869
+ 'reads: get distinct values for a column with filter and pagination',
870
+ function(fDone)
871
+ {
872
+ libSuperTest('http://localhost:9081/')
873
+ .get('1.0/FableTests/Distinct/Type/FilteredTo/FBV~IDAnimal~LT~3/0/1')
874
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
875
+ .end(
876
+ function (pError, pResponse)
877
+ {
878
+ const tmpResults = JSON.parse(pResponse.text);
879
+ Expect(tmpResults.length).to.equal(1);
880
+ fDone();
881
+ }
882
+ );
883
+ }
884
+ );
885
+ test
886
+ (
887
+ 'reads: get distinct values for a column with pagination',
888
+ function(fDone)
889
+ {
890
+ libSuperTest('http://localhost:9081/')
891
+ .get('1.0/FableTests/Distinct/Type/2/2')
892
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
893
+ .end(
894
+ function (pError, pResponse)
895
+ {
896
+ const tmpResults = JSON.parse(pResponse.text);
897
+ Expect(tmpResults.length).to.equal(2);
898
+ const types = new Set(tmpResults.map((r) => r.Type));
899
+ Expect(types.size).to.equal(2);
900
+ fDone();
901
+ }
902
+ );
903
+ }
904
+ );
905
+ test
906
+ (
907
+ 'reads: get a filtered paged set of records',
908
+ function(fDone)
909
+ {
910
+ libSuperTest('http://localhost:9081/')
911
+ // Skip one record, 2 records per page.
912
+ .get('1.0/FableTests/FilteredTo/FBV~Type~EQ~Dog/1/2')
913
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
914
+ .end(
915
+ function (pError, pResponse)
916
+ {
917
+ const tmpResults = JSON.parse(pResponse.text);
918
+ Expect(tmpResults.length).to.equal(1);
919
+ Expect(tmpResults[0].Type).to.equal('Dog');
920
+ fDone();
921
+ }
922
+ );
923
+ }
924
+ );
925
+ test
926
+ (
927
+ 'update: update a record',
928
+ function(fDone)
929
+ {
930
+ // Change animal 4 ("Spot") to a Corgi
931
+ const tmpRecord = {IDAnimal:4, Type:'Corgi'};
932
+ libSuperTest('http://localhost:9081/')
933
+ .put('1.0/FableTest')
934
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
935
+ .send(tmpRecord)
936
+ .end(
937
+ function(pError, pResponse)
938
+ {
939
+ // Expect response to be the record we just created.
940
+ const tmpResult = JSON.parse(pResponse.text);
941
+ Expect(tmpResult.Type).to.equal('Corgi');
942
+ Expect(tmpResult.CreatingIDUser).to.equal(1);
943
+ Expect(tmpResult.UpdatingIDUser).to.equal(37);
944
+ fDone();
945
+ }
946
+ );
947
+ }
948
+ );
949
+ test
950
+ (
951
+ 'delete: delete a record',
952
+ function(fDone)
953
+ {
954
+ // Delete animal 3 ("Red")
955
+ const tmpRecord = {IDAnimal:3};
956
+ libSuperTest('http://localhost:9081/')
957
+ .del('1.0/FableTest')
958
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
959
+ .send(tmpRecord)
960
+ .end(
961
+ function(pError, pResponse)
962
+ {
963
+ // Expect response to be the count of deleted records.
964
+ const tmpResult = JSON.parse(pResponse.text);
965
+ Expect(tmpResult.Count).to.equal(1);
966
+ fDone();
967
+ }
968
+ );
969
+ }
970
+ );
971
+ test
972
+ (
973
+ 'delete: delete a record with a bad parameter',
974
+ function(fDone)
975
+ {
976
+ // Delete animal 3 ("Red")
977
+ const tmpRecord = {IDAnimal:{MyStuff:4}};
978
+ libSuperTest('http://localhost:9081/')
979
+ .del('1.0/FableTest')
980
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
981
+ .send(tmpRecord)
982
+ .end(
983
+ function(pError, pResponse)
984
+ {
985
+ // Expect response to be the count of deleted records.
986
+ const tmpResult = JSON.parse(pResponse.text);
987
+ Expect(tmpResult.Error).to.contain('a valid record ID is required');
988
+ fDone();
989
+ }
990
+ );
991
+ }
992
+ );
993
+ test
994
+ (
995
+ 'count: get the count of records',
996
+ function(fDone)
997
+ {
998
+ libSuperTest('http://localhost:9081/')
999
+ .get('1.0/FableTests/Count')
1000
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1001
+ .end(
1002
+ function (pError, pResponse)
1003
+ {
1004
+ const tmpResults = JSON.parse(pResponse.text);
1005
+ Expect(tmpResults.Count).to.equal(5);
1006
+ fDone();
1007
+ }
1008
+ );
1009
+ }
1010
+ );
1011
+ test
1012
+ (
1013
+ 'count: get the count of filtered records',
1014
+ function(fDone)
1015
+ {
1016
+ libSuperTest('http://localhost:9081/')
1017
+ .get('1.0/FableTests/Count/FilteredTo/FBV~Type~EQ~Girl')
1018
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1019
+ .end(
1020
+ function (pError, pResponse)
1021
+ {
1022
+ const tmpResults = JSON.parse(pResponse.text);
1023
+ Expect(tmpResults.Count).to.equal(1);
1024
+ fDone();
1025
+ }
1026
+ );
1027
+ }
1028
+ );
1029
+ test
1030
+ (
1031
+ 'schema: get the schema of a record',
1032
+ function(fDone)
1033
+ {
1034
+ libSuperTest('http://localhost:9081/')
1035
+ .get('1.0/FableTest/Schema')
1036
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1037
+ .end(
1038
+ function (pError, pResponse)
1039
+ {
1040
+ const tmpResults = JSON.parse(pResponse.text);
1041
+ //console.log('SCHEMA --> '+JSON.stringify(tmpResults, null, 4))
1042
+ Expect(tmpResults.title).to.equal('Animal');
1043
+ Expect(tmpResults.description).to.contain('creature that lives in');
1044
+ fDone();
1045
+ }
1046
+ );
1047
+ }
1048
+ );
1049
+ test
1050
+ (
1051
+ 'new: get a new empty record',
1052
+ function(fDone)
1053
+ {
1054
+ libSuperTest('http://localhost:9081/')
1055
+ .get('1.0/FableTest/Schema/New')
1056
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1057
+ .end(
1058
+ function (pError, pResponse)
1059
+ {
1060
+ const tmpResults = JSON.parse(pResponse.text);
1061
+ //console.log(JSON.stringify(tmpResults, null, 4))
1062
+ Expect(tmpResults.IDAnimal).to.equal(null);
1063
+ Expect(tmpResults.Name).to.equal('Unknown');
1064
+ Expect(tmpResults.Type).to.equal('Unclassified');
1065
+ fDone();
1066
+ }
1067
+ );
1068
+ }
1069
+ );
1070
+ test
1071
+ (
1072
+ 'validate: validate an invalid record',
1073
+ function(fDone)
1074
+ {
1075
+ const tmpRecord = {IDAnimal:4, Type:'Corgi'};
1076
+ libSuperTest('http://localhost:9081/')
1077
+ .post('1.0/FableTest/Schema/Validate')
1078
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1079
+ .send(tmpRecord)
1080
+ .end(
1081
+ function(pError, pResponse)
1082
+ {
1083
+ // Expect response to be the record we just created.
1084
+ const tmpResult = JSON.parse(pResponse.text);
1085
+ //console.log(JSON.stringify(tmpResult, null, 4))
1086
+ Expect(tmpResult.Valid).to.equal(false);
1087
+ Expect(tmpResult.Errors[0].field).to.equal('data.Name');
1088
+ Expect(tmpResult.Errors[0].message).to.equal('is required');
1089
+ fDone();
1090
+ }
1091
+ );
1092
+ }
1093
+ );
1094
+ test
1095
+ (
1096
+ 'validate: validate a valid record',
1097
+ function(fDone)
1098
+ {
1099
+ const tmpRecord = {IDAnimal:4, Type:'Corgi', Name:'Doofer', CreatingIDUser:10};
1100
+ libSuperTest('http://localhost:9081/')
1101
+ .post('1.0/FableTest/Schema/Validate')
1102
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1103
+ .send(tmpRecord)
1104
+ .end(
1105
+ function(pError, pResponse)
1106
+ {
1107
+ // Expect response to be the record we just created.
1108
+ const tmpResult = JSON.parse(pResponse.text);
1109
+ //console.log(JSON.stringify(tmpResult, null, 4))
1110
+ Expect(tmpResult.Valid).to.equal(true);
1111
+ fDone();
1112
+ }
1113
+ );
1114
+ }
1115
+ );
1116
+ test
1117
+ (
1118
+ 'validate: validate bad data',
1119
+ function(fDone)
1120
+ {
1121
+ const tmpRecord = 'IAMBAD';
1122
+ libSuperTest('http://localhost:9081/')
1123
+ .post('1.0/FableTest/Schema/Validate')
1124
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1125
+ .send(tmpRecord)
1126
+ .end(
1127
+ function(pError, pResponse)
1128
+ {
1129
+ // Expect response to be the record we just created.
1130
+ const tmpResult = JSON.parse(pResponse.text);
1131
+ //console.log(JSON.stringify(tmpResult, null, 4))
1132
+ Expect(tmpResult.Valid).to.be.false;
1133
+ fDone();
1134
+ }
1135
+ );
1136
+ }
1137
+ );
1138
+ }
1139
+ );
1140
+ suite
1141
+ (
1142
+ 'Unauthorized server routes',
1143
+ function()
1144
+ {
1145
+ test
1146
+ (
1147
+ 'read: get a specific record',
1148
+ function(fDone)
1149
+ {
1150
+ _MockSessionValidUser.UserRoleIndex = -1;
1151
+ libSuperTest('http://localhost:9081/')
1152
+ .get('1.0/FableTest/2')
1153
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1154
+ .end(
1155
+ function (pError, pResponse)
1156
+ {
1157
+ const tmpResult = JSON.parse(pResponse.text);
1158
+ Expect(tmpResult.Error).to.contain('You must be appropriately authenticated');
1159
+ _MockSessionValidUser.UserRoleIndex = 1;
1160
+ fDone();
1161
+ }
1162
+ );
1163
+ }
1164
+ );
1165
+ }
1166
+ );
1167
+ suite
1168
+ (
1169
+ 'Bad user server routes',
1170
+ function()
1171
+ {
1172
+ test
1173
+ (
1174
+ 'create: create a record',
1175
+ function(fDone)
1176
+ {
1177
+ _MockSessionValidUser.UserID = 0;
1178
+ const tmpRecord = {Name:'BatBrains', Type:'Mammoth'};
1179
+ libSuperTest('http://localhost:9081/')
1180
+ .post('1.0/FableTest')
1181
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1182
+ .send(tmpRecord)
1183
+ .end(
1184
+ function(pError, pResponse)
1185
+ {
1186
+ // Expect response to be the record we just created.
1187
+ const tmpResult = JSON.parse(pResponse.text);
1188
+ Expect(tmpResult.Error).to.contain('authenticated');
1189
+ fDone();
1190
+ }
1191
+ );
1192
+ }
1193
+ );
1194
+ test
1195
+ (
1196
+ 'read: get a specific record',
1197
+ function(fDone)
1198
+ {
1199
+ libSuperTest('http://localhost:9081/')
1200
+ .get('1.0/FableTest/2')
1201
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1202
+ .end(
1203
+ function (pError, pResponse)
1204
+ {
1205
+ const tmpResult = JSON.parse(pResponse.text);
1206
+ Expect(tmpResult.Error).to.contain('authenticated');
1207
+ fDone();
1208
+ }
1209
+ );
1210
+ }
1211
+ );
1212
+ test
1213
+ (
1214
+ 'readselect: get all records',
1215
+ function(fDone)
1216
+ {
1217
+ libSuperTest('http://localhost:9081/')
1218
+ .get('1.0/FableTestSelect')
1219
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1220
+ .end(
1221
+ function (pError, pResponse)
1222
+ {
1223
+ console.log(pResponse.text)
1224
+ const tmpResults = JSON.parse(pResponse.text);
1225
+ Expect(tmpResults.Error).to.contain('authenticated');
1226
+ fDone();
1227
+ }
1228
+ );
1229
+ }
1230
+ );
1231
+ test
1232
+ (
1233
+ 'update: update a record',
1234
+ function(fDone)
1235
+ {
1236
+ // Change animal 4 ("Spot") to a Corgi
1237
+ const tmpRecord = {IDAnimal:4, Type:'Corgi'};
1238
+ libSuperTest('http://localhost:9081/')
1239
+ .put('1.0/FableTest')
1240
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1241
+ .send(tmpRecord)
1242
+ .end(
1243
+ function(pError, pResponse)
1244
+ {
1245
+ // Expect response to be the record we just created.
1246
+ const tmpResult = JSON.parse(pResponse.text);
1247
+ Expect(tmpResult.Error).to.contain('authenticated');
1248
+ fDone();
1249
+ }
1250
+ );
1251
+ }
1252
+ );
1253
+ test
1254
+ (
1255
+ 'schema: get the schema of a record',
1256
+ function(fDone)
1257
+ {
1258
+ libSuperTest('http://localhost:9081/')
1259
+ .get('1.0/FableTest/Schema')
1260
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1261
+ .end(
1262
+ function (pError, pResponse)
1263
+ {
1264
+ const tmpResults = JSON.parse(pResponse.text);
1265
+ //console.log('SCHEMA --> '+JSON.stringify(tmpResults, null, 4))
1266
+ Expect(tmpResults.Error).to.contain('authenticated');
1267
+ fDone();
1268
+ }
1269
+ );
1270
+ }
1271
+ );
1272
+ test
1273
+ (
1274
+ 'new: get a new empty record',
1275
+ function(fDone)
1276
+ {
1277
+ libSuperTest('http://localhost:9081/')
1278
+ .get('1.0/FableTest/Schema/New')
1279
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1280
+ .end(
1281
+ function (pError, pResponse)
1282
+ {
1283
+ const tmpResults = JSON.parse(pResponse.text);
1284
+ //console.log(JSON.stringify(tmpResults, null, 4))
1285
+ Expect(tmpResults.Error).to.contain('authenticated');
1286
+ fDone();
1287
+ }
1288
+ );
1289
+ }
1290
+ );
1291
+ test
1292
+ (
1293
+ 'validate: validate an invalid record',
1294
+ function(fDone)
1295
+ {
1296
+ const tmpRecord = {IDAnimal:4, Type:'Corgi'};
1297
+ libSuperTest('http://localhost:9081/')
1298
+ .post('1.0/FableTest/Schema/Validate')
1299
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1300
+ .send(tmpRecord)
1301
+ .end(
1302
+ function(pError, pResponse)
1303
+ {
1304
+ // Expect response to be the record we just created.
1305
+ const tmpResult = JSON.parse(pResponse.text);
1306
+ //console.log(JSON.stringify(tmpResult, null, 4))
1307
+ Expect(tmpResult.Error).to.contain('authenticated');
1308
+ fDone();
1309
+
1310
+ }
1311
+ );
1312
+ }
1313
+ );
1314
+ test
1315
+ (
1316
+ 'count: get the count of records',
1317
+ function(fDone)
1318
+ {
1319
+ libSuperTest('http://localhost:9081/')
1320
+ .get('1.0/FableTests/Count')
1321
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1322
+ .end(
1323
+ function (pError, pResponse)
1324
+ {
1325
+ const tmpResult = JSON.parse(pResponse.text);
1326
+ Expect(tmpResult.Error).to.contain('authenticated');
1327
+ fDone();
1328
+ }
1329
+ );
1330
+ }
1331
+ );
1332
+ test
1333
+ (
1334
+ 'delete: delete a record',
1335
+ function(fDone)
1336
+ {
1337
+ // Delete animal 3 ("Red")
1338
+ const tmpRecord = {IDAnimal:3};
1339
+ libSuperTest('http://localhost:9081/')
1340
+ .del('1.0/FableTest')
1341
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1342
+ .send(tmpRecord)
1343
+ .end(
1344
+ function(pError, pResponse)
1345
+ {
1346
+ // Expect response to be the count of deleted records.
1347
+ const tmpResult = JSON.parse(pResponse.text);
1348
+ Expect(tmpResult.Error).to.contain('authenticated');
1349
+ _MockSessionValidUser.UserID = 10;
1350
+ fDone();
1351
+ }
1352
+ );
1353
+ }
1354
+ );
1355
+ }
1356
+ );
1357
+ suite
1358
+ (
1359
+ 'Not logged in server routes',
1360
+ function()
1361
+ {
1362
+ test
1363
+ (
1364
+ 'read: get a specific record',
1365
+ function(fDone)
1366
+ {
1367
+ _MockSessionValidUser.LoggedIn = false;
1368
+ libSuperTest('http://localhost:9081/')
1369
+ .get('1.0/FableTest/2')
1370
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1371
+ .end(
1372
+ function (pError, pResponse)
1373
+ {
1374
+ const tmpResult = JSON.parse(pResponse.text);
1375
+ Expect(tmpResult.Error).to.contain('You must be authenticated');
1376
+ _MockSessionValidUser.LoggedIn = true;
1377
+ fDone();
1378
+ }
1379
+ );
1380
+ }
1381
+ );
1382
+ }
1383
+ );
1384
+ suite
1385
+ (
1386
+ 'Filter parser',
1387
+ function()
1388
+ {
1389
+ test
1390
+ (
1391
+ 'Filter parse',
1392
+ function(fDone)
1393
+ {
1394
+ const tmpQuery = _MeadowEndpoints.DAL.query;
1395
+ _MeadowEndpoints.parseFilter('FBV~UUIDAnimal~EQ~1000000', tmpQuery);
1396
+ Expect(tmpQuery.parameters.filter[0].Column).to.equal('UUIDAnimal');
1397
+ fDone();
1398
+ }
1399
+ );
1400
+ }
1401
+ );
1402
+ suite
1403
+ (
1404
+ 'Changing route requirement',
1405
+ function()
1406
+ {
1407
+ test
1408
+ (
1409
+ 'read: get a specific record',
1410
+ function(fDone)
1411
+ {
1412
+ Expect(_MeadowEndpoints.endpointAuthorizationLevels.Read).to.equal(0);
1413
+ fDone();
1414
+ }
1415
+ );
1416
+ }
1417
+ );
1418
+ suite
1419
+ (
1420
+ 'Behavior modifications',
1421
+ function()
1422
+ {
1423
+ test
1424
+ (
1425
+ 'read: modified get of a specific record',
1426
+ function(fDone)
1427
+ {
1428
+ // Override the query configuration
1429
+ _MeadowEndpoints.behaviorModifications.setBehavior('Read-QueryConfiguration', [
1430
+ function(pRequest, fComplete)
1431
+ {
1432
+ //implicitly test behvaior-cascade
1433
+ return fComplete(false);
1434
+ },
1435
+ function(pRequest, fComplete)
1436
+ {
1437
+ // Turn up logging on the request.
1438
+ pRequest.Query.setLogLevel(5);
1439
+ fComplete(false);
1440
+ } ]);
1441
+ libSuperTest('http://localhost:9081/')
1442
+ .get('1.0/FableTest/2')
1443
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1444
+ .end(
1445
+ function (pError, pResponse)
1446
+ {
1447
+ const tmpResult = JSON.parse(pResponse.text);
1448
+ //console.log(JSON.stringify(tmpResult, null, 4))
1449
+ Expect(tmpResult.Name).to.equal('Red Riding Hood');
1450
+ fDone();
1451
+ }
1452
+ );
1453
+ }
1454
+ );
1455
+ test
1456
+ (
1457
+ 'read: inject data into the record',
1458
+ function(fDone)
1459
+ {
1460
+ // Override the query configuration
1461
+ _MeadowEndpoints.behaviorModifications.setBehavior('Read-PostOperation',
1462
+ function(pRequest, fComplete)
1463
+ {
1464
+ // Create a custom property on the record.
1465
+ pRequest.Record.CustomProperty = 'Custom '+pRequest.Record.Type+' ID '+pRequest.Record.IDAnimal;
1466
+ fComplete(false);
1467
+ });
1468
+ _MockSessionValidUser.LoggedIn = true;
1469
+ libSuperTest('http://localhost:9081/')
1470
+ .get('1.0/FableTest/2')
1471
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1472
+ .end(
1473
+ function (pError, pResponse)
1474
+ {
1475
+ const tmpResult = JSON.parse(pResponse.text);
1476
+ //console.log(JSON.stringify(tmpResult, null, 4))
1477
+ Expect(tmpResult.CustomProperty).to.equal('Custom Girl ID 2');
1478
+ fDone();
1479
+ }
1480
+ );
1481
+ }
1482
+ );
1483
+ test
1484
+ (
1485
+ 'read-max: get the max record ID',
1486
+ function(fDone)
1487
+ {
1488
+ libSuperTest('http://localhost:9081/')
1489
+ .get('1.0/FableTest/Max/IDAnimal')
1490
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1491
+ .end(
1492
+ function (pError, pResponse)
1493
+ {
1494
+ const tmpResult = JSON.parse(pResponse.text);
1495
+ Expect(tmpResult.IDAnimal).to.equal(6);
1496
+ fDone();
1497
+ }
1498
+ );
1499
+ }
1500
+ );
1501
+ test
1502
+ (
1503
+ 'read-max: get the max name',
1504
+ function(fDone)
1505
+ {
1506
+ libSuperTest('http://localhost:9081/')
1507
+ .get('1.0/FableTest/Max/Name')
1508
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1509
+ .end(
1510
+ function (pError, pResponse)
1511
+ {
1512
+ const tmpResult = JSON.parse(pResponse.text);
1513
+ Expect(tmpResult.Name).to.equal('Spot');
1514
+ fDone();
1515
+ }
1516
+ );
1517
+ }
1518
+ );
1519
+ }
1520
+ );
1521
+
1522
+ suite
1523
+ (
1524
+ 'Endpoint Security - Deny',
1525
+ function()
1526
+ {
1527
+ test
1528
+ (
1529
+ 'bulk creates',
1530
+ function(fDone)
1531
+ {
1532
+ const tmpRecords = [
1533
+ {Name:'Billy', Type:'Cat'},
1534
+ {Name:'Jim', Type:'Cat'},
1535
+ {Name:'Janet', Type:'Cat'},
1536
+ {Name:'Sweeps', Type:'Cat'},
1537
+ {Name:'Stakes', Type:'Dog'},
1538
+ {Name:'Sally', Type:'Dog'},
1539
+ {Name:'Bill', Type:'Dog'},
1540
+ {Name:'Chris', Type:'Dog'},
1541
+ {Name:'Haji', Type:'Snake'}
1542
+ ];
1543
+ _MockSessionValidUser.UserRoleIndex = 2;
1544
+ libSuperTest('http://localhost:9081/')
1545
+ .post('1.0/FableTests')
1546
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1547
+ .send(tmpRecords)
1548
+ .end(
1549
+ function(pError, pResponse)
1550
+ {
1551
+ // Expect response to be the record we just created.
1552
+ const tmpResult = JSON.parse(pResponse.text);
1553
+ //console.log(JSON.stringify(tmpResult,null,4));
1554
+ Expect(tmpResult[0].Name).to.equal('Billy');
1555
+ Expect(tmpResult[5].Type).to.equal('Dog');
1556
+ fDone();
1557
+ }
1558
+ );
1559
+ }
1560
+ );
1561
+ test
1562
+ (
1563
+ 'bulk create with a bad record',
1564
+ function(fDone)
1565
+ {
1566
+ const tmpRecords = [
1567
+ {Name:'Astro', Type:'Cartoon'},
1568
+ {Name:'Boy', Type:'Cartoon'},
1569
+ {License:'Whoops', Type:'Cat'},
1570
+ {Name:'Froggy', Type:'Cartoon'}
1571
+ ];
1572
+ _MockSessionValidUser.UserRoleIndex = 2;
1573
+ libSuperTest('http://localhost:9081/')
1574
+ .post('1.0/FableTests')
1575
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1576
+ .send(tmpRecords)
1577
+ .end(
1578
+ function(pError, pResponse)
1579
+ {
1580
+ // Expect response to be the record we just created.
1581
+ const tmpResult = JSON.parse(pResponse.text);
1582
+ Expect(tmpResult[0].Type).to.equal('Cartoon');
1583
+ fDone();
1584
+ }
1585
+ );
1586
+ }
1587
+ );
1588
+ test
1589
+ (
1590
+ 'bulk updates',
1591
+ function(fDone)
1592
+ {
1593
+ const tmpRecords = [
1594
+ {IDAnimal: 11, Type:'Hoss'},
1595
+ {IDAnimal: 12, Type:'Hoss'},
1596
+ {IDAnimal: 14, Type:'Hoss'},
1597
+ {IDAnimal: 15, Type:'Hoss'}
1598
+ ];
1599
+ _MockSessionValidUser.UserRoleIndex = 2;
1600
+ libSuperTest('http://localhost:9081/')
1601
+ .put('1.0/FableTests')
1602
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1603
+ .send(tmpRecords)
1604
+ .end(
1605
+ function(pError, pResponse)
1606
+ {
1607
+ // Expect response to be the record we just created.
1608
+ const tmpResult = JSON.parse(pResponse.text);
1609
+ //console.log(JSON.stringify(tmpResult,null,4));
1610
+ Expect(tmpResult[0].IDAnimal).to.equal(11);
1611
+ Expect(tmpResult[0].Type).to.equal('Hoss');
1612
+ Expect(tmpResult[1].Type).to.equal('Hoss');
1613
+ Expect(tmpResult[2].Type).to.equal('Hoss');
1614
+ Expect(tmpResult[3].Type).to.equal('Hoss');
1615
+ fDone();
1616
+ }
1617
+ );
1618
+ }
1619
+ );
1620
+ test
1621
+ (
1622
+ 'bulk updates with bad record',
1623
+ function(fDone)
1624
+ {
1625
+ const tmpRecords = [
1626
+ {IDAnimal: 11, Type:'Horsse'},
1627
+ {IDAnimal: 12, Type:'Horsse'},
1628
+ {IDAnimal: 14, Genus:'Hosse'},
1629
+ {IDAnimal: 15, Type:'Hosses'}
1630
+ ];
1631
+ _MockSessionValidUser.UserRoleIndex = 2;
1632
+ libSuperTest('http://localhost:9081/')
1633
+ .put('1.0/FableTests')
1634
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1635
+ .send(tmpRecords)
1636
+ .end(
1637
+ function(pError, pResponse)
1638
+ {
1639
+ // Expect response to be the record we just created.
1640
+ const tmpResult = JSON.parse(pResponse.text);
1641
+ //console.log(JSON.stringify(tmpResult,null,4));
1642
+ Expect(tmpResult[3].Type).to.equal('Hosses');
1643
+ fDone();
1644
+ }
1645
+ );
1646
+ }
1647
+ );
1648
+ test
1649
+ (
1650
+ 'upsert: create a record',
1651
+ function(fDone)
1652
+ {
1653
+ const tmpRecord = {GUIDAnimal:'0xHAXXXX', Name:'Jason', Type:'Tyranosaurus'};
1654
+ _MockSessionValidUser.UserRoleIndex = 2;
1655
+ libSuperTest('http://localhost:9081/')
1656
+ .put('1.0/FableTest/Upsert')
1657
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1658
+ .send(tmpRecord)
1659
+ .end(
1660
+ function(pError, pResponse)
1661
+ {
1662
+ // Expect response to be the record we just created.
1663
+ //console.log(pResponse.text)
1664
+ const tmpResult = JSON.parse(pResponse.text);
1665
+ Expect(tmpResult.Type).to.equal('Tyranosaurus');
1666
+ Expect(tmpResult.CreatingIDUser).to.equal(10);
1667
+ fDone();
1668
+ }
1669
+ );
1670
+ }
1671
+ );
1672
+ test
1673
+ (
1674
+ 'upsert: Update a record',
1675
+ function(fDone)
1676
+ {
1677
+ const tmpRecord = {GUIDAnimal:'0xHAXXXX', Type:'Stegosaurus'};
1678
+ _MockSessionValidUser.UserRoleIndex = 2;
1679
+ libSuperTest('http://localhost:9081/')
1680
+ .put('1.0/FableTest/Upsert')
1681
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1682
+ .send(tmpRecord)
1683
+ .end(
1684
+ function(pError, pResponse)
1685
+ {
1686
+ // Expect response to be the record we just created.
1687
+ console.log(pResponse.text)
1688
+ const tmpResult = JSON.parse(pResponse.text);
1689
+ Expect(tmpResult.Type).to.equal('Stegosaurus');
1690
+ Expect(tmpResult.Name).to.equal('Jason');
1691
+ fDone();
1692
+ }
1693
+ );
1694
+ }
1695
+ );
1696
+ test
1697
+ (
1698
+ 'bulk upserts',
1699
+ function(fDone)
1700
+ {
1701
+ _MeadowEndpoints.behaviorModifications.setTemplate('SelectList', '<%= Record.Name %>|<%=Record.Type%>');
1702
+ const tmpRecords = [
1703
+ {GUIDAnimal:'0xHAXXXX', Type:'Triceratops'},
1704
+ {GUIDAnimal:'0xDavison', Name:'Davison', Type:'Dog'},
1705
+ {GUIDAnimal:'0xMartino', Name:'Martin', Type:'Dog'},
1706
+ {Name:'Chino', Type:'Cat'}
1707
+ ];
1708
+ _MockSessionValidUser.UserRoleIndex = 2;
1709
+ libSuperTest('http://localhost:9081/')
1710
+ .put('1.0/FableTest/Upserts')
1711
+ .set('x-trusted-session', JSON.stringify(_MockSessionValidUser))
1712
+ .send(tmpRecords)
1713
+ .end(
1714
+ function(pError, pResponse)
1715
+ {
1716
+ // Expect response to be the record we just created.
1717
+ const tmpResult = JSON.parse(pResponse.text);
1718
+ console.log(JSON.stringify(tmpResult,null,4));
1719
+ Expect(tmpResult[0].Value).to.equal('Jason|Triceratops');
1720
+ Expect(tmpResult[1].Value).to.equal('Davison|Dog');
1721
+ Expect(tmpResult[2].Value).to.equal('Martin|Dog');
1722
+ Expect(tmpResult[3].Value).to.equal('Chino|Cat');
1723
+ fDone();
1724
+ }
1725
+ );
1726
+ }
1727
+ );
1728
+ }
1729
+ );
1730
+ }
1731
+ );