meadow-endpoints 2.0.16 → 2.0.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "meadow-endpoints",
3
- "version": "2.0.16",
3
+ "version": "2.0.18",
4
4
  "description": "Automatic API endpoints for Meadow data.",
5
5
  "main": "source/Meadow-Endpoints.js",
6
6
  "scripts": {
@@ -44,6 +44,14 @@ var MeadowAuthorizers = function()
44
44
  _AuthorizerFunctions[pAuthorizerHash] = fAuthorizer;
45
45
  };
46
46
 
47
+ /**
48
+ * Get a specific authorizer.
49
+ */
50
+ var getAuthorizer = function(pAuthorizerHash)
51
+ {
52
+ return _AuthorizerFunctions[pAuthorizerHash];
53
+ };
54
+
47
55
 
48
56
  // Map in the default authorizers
49
57
  setAuthorizer('Allow', require(__dirname+'/authorizers/Meadow-Authorizer-Allow.js'));
@@ -175,6 +183,7 @@ var MeadowAuthorizers = function()
175
183
  var tmpNewMeadowAuthorizer = (
176
184
  {
177
185
  setAuthorizer: setAuthorizer,
186
+ getAuthorizer: getAuthorizer,
178
187
  authorize: authorize,
179
188
  authorizeRequest: authorizeRequest,
180
189
 
@@ -418,6 +418,17 @@ var MeadowEndpoints = function()
418
418
  return fCallback();
419
419
  };
420
420
 
421
+ var _InvokeSetupCallback;
422
+ var getInvokeSetupCallback = function()
423
+ {
424
+ return _InvokeSetupCallback;
425
+ };
426
+
427
+ var setInvokeSetupCallback = function(fCallback)
428
+ {
429
+ _InvokeSetupCallback = fCallback;
430
+ };
431
+
421
432
  /**
422
433
  * Invoke a meadow endpoint programmatically
423
434
  *
@@ -470,6 +481,10 @@ var MeadowEndpoints = function()
470
481
  //internal invoke mark as authenticated (because this is not called via webservice)
471
482
  pRequest.EndpointAuthenticated = true;
472
483
 
484
+ if (_InvokeSetupCallback && typeof(_InvokeSetupCallback) == 'function')
485
+ {
486
+ _InvokeSetupCallback(pRequest, pResponse, typeof(pOptions) === 'object' && pOptions);
487
+ }
473
488
  return fStageComplete();
474
489
  },
475
490
  function(fStageComplete)
@@ -514,6 +529,8 @@ var MeadowEndpoints = function()
514
529
  // Expose the DAL
515
530
  DAL: _Meadow,
516
531
 
532
+ getInvokeSetupCallback: getInvokeSetupCallback,
533
+ setInvokeSetupCallback: setInvokeSetupCallback,
517
534
  invokeEndpoint: invokeEndpoint,
518
535
 
519
536
  // Factory
@@ -544,6 +561,18 @@ var MeadowEndpoints = function()
544
561
  enumerable: true
545
562
  });
546
563
 
564
+ /**
565
+ * EndpointAuthenticators
566
+ *
567
+ * @property endpointAuthorizers
568
+ * @type object
569
+ */
570
+ Object.defineProperty(tmpNewMeadowEndpointObject, 'endpointAuthorizers',
571
+ {
572
+ get: function() { return _Authorizers; },
573
+ enumerable: true
574
+ });
575
+
547
576
  /**
548
577
  * EndpointAuthenticators
549
578
  *
@@ -465,6 +465,28 @@ suite
465
465
  }
466
466
  );
467
467
  test
468
+ (
469
+ 'read: define a custom authorization behavior',
470
+ function(fDone)
471
+ {
472
+ const defaultAuthorizer = _MeadowEndpoints.endpointAuthorizers.getAuthorizer('Allow');
473
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Allow', function(req, next) { req.MeadowAuthorization = false; return next(); });
474
+ _Orator.webServer.get('/CustomHotRodRoute/:IDRecord', _MeadowEndpoints.endpointAuthenticators.Read, _MeadowEndpoints.wireState, _MeadowEndpoints.endpoints.Read);
475
+ libSuperTest('http://localhost:9080/')
476
+ .get('CustomHotRodRoute/2')
477
+ .end(
478
+ function (pError, pResponse)
479
+ {
480
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Allow', defaultAuthorizer);
481
+ //TODO: it's weird that we don't get an error here for access denied...
482
+ var tmpResult = JSON.parse(pResponse.text);
483
+ Expect(tmpResult.Error).to.equal('UNAUTHORIZED ACCESS IS NOT ALLOWED');
484
+ fDone();
485
+ }
486
+ );
487
+ }
488
+ );
489
+ test
468
490
  (
469
491
  'read: define a custom route and get a record with it',
470
492
  function(fDone)
@@ -504,6 +526,32 @@ suite
504
526
  }
505
527
  );
506
528
  test
529
+ (
530
+ 'read: get a specific record which resolved to Deny authorization, but with a Deny authorizer that just allows',
531
+ function(fDone)
532
+ {
533
+ _Meadow.schemaFull.authorizer.Manager = {};
534
+ _Meadow.schemaFull.authorizer.Manager.Read = 'Deny';
535
+ const defaultAuthorizer = _MeadowEndpoints.endpointAuthorizers.getAuthorizer('Deny');
536
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Deny', function(req, next) { req.MeadowAuthorization = true; return next(); });
537
+
538
+ libSuperTest('http://localhost:9080/')
539
+ .get('1.0/FableTest/2')
540
+ .end(
541
+ function (pError, pResponse)
542
+ {
543
+ // Reset authorization
544
+ _Meadow.schemaFull.authorizer.Manager.Read = 'Allow';
545
+ _MeadowEndpoints.endpointAuthorizers.setAuthorizer('Deny', defaultAuthorizer);
546
+
547
+ const responseBody = JSON.parse(pResponse.text);
548
+ Expect(responseBody.IDAnimal).to.equal(2);
549
+ fDone();
550
+ }
551
+ );
552
+ }
553
+ );
554
+ test
507
555
  (
508
556
  'read: get a specific record with a bad parameter',
509
557
  function(fDone)
@@ -1490,6 +1538,35 @@ suite
1490
1538
  {
1491
1539
  var tmpCreatedRecordGUID;
1492
1540
 
1541
+ test
1542
+ (
1543
+ 'invoke: setup method is called',
1544
+ function(fDone)
1545
+ {
1546
+ _MockSessionValidUser.UserRoleIndex = 2;
1547
+ let setupCallCount = 0;
1548
+ let passedRequest, passedResponse, passedOriginalRequest;
1549
+ _MeadowEndpoints.setInvokeSetupCallback((req, res, origReq) =>
1550
+ {
1551
+ ++setupCallCount;
1552
+ passedRequest = req;
1553
+ passedResponse = res;
1554
+ passedOriginalRequest = origReq;
1555
+ });
1556
+ const originalRequest = {UserSession: _MockSessionValidUser};
1557
+ _MeadowEndpoints.invokeEndpoint('Read', {IDRecord: 2}, originalRequest,
1558
+ function(pError, pResponse)
1559
+ {
1560
+ Expect(setupCallCount).to.equal(1);
1561
+ Expect(passedOriginalRequest).to.equal(originalRequest);
1562
+ Expect(passedRequest).to.be.an('object');
1563
+ Expect(passedResponse).to.be.an('object');
1564
+
1565
+ fDone();
1566
+ }
1567
+ );
1568
+ }
1569
+ );
1493
1570
  test
1494
1571
  (
1495
1572
  'invoke create: create a record',