lint cleanup
[websub-hub] / test / src / service.js
index cafe1d16922ff9e4e0f48b19921a572e39d7ea2f..576859f834156b09759c2b64f7b29cee31de0e53 100644 (file)
@@ -10,15 +10,18 @@ const stubDb = require('../stub-db');
 const stubLogger = require('../stub-logger');
 const Service = require('../../src/service');
 const Config = require('../../config');
+const { AsyncLocalStorage } = require('node:async_hooks');
 
 
 describe('Service', function () {
-  let service, options;
+  let service, options, asyncLocalStorage;
   let req, res, ctx;
 
   beforeEach(function () {
+    asyncLocalStorage = new AsyncLocalStorage();
     options = new Config('test');
-    service = new Service(stubLogger, stubDb, options);
+    service = new Service(stubLogger, stubDb, options, asyncLocalStorage);
+    stubLogger._reset();
     sinon.stub(service.manager);
     sinon.stub(service.sessionManager);
     sinon.stub(service.authenticator);
@@ -46,6 +49,23 @@ describe('Service', function () {
     assert(service);
   });
 
+  describe('preHandler', function () {
+    it('logs requestId', async function () {
+      sinon.stub(service.__proto__.__proto__, 'preHandler').resolves();
+      await service.asyncLocalStorage.run({}, async () => {
+        await service.preHandler(req, res, ctx);
+        const logObject = service.asyncLocalStorage.getStore();
+        assert('requestId' in logObject);
+      });
+    });
+    it('covers weird async context failure', async function () {
+      sinon.stub(service.__proto__.__proto__, 'preHandler').resolves();
+      sinon.stub(service.asyncLocalStorage, 'getStore').returns();
+      await service.preHandler(req, res, ctx);
+      assert(service.logger.debug.called);
+    });
+  }); // preHandler
+
   describe('maybeIngestBody', function () {
     beforeEach(function () {
       sinon.stub(service, 'bodyData');
@@ -61,20 +81,6 @@ describe('Service', function () {
     });
   }); // maybeIngestBody
 
-  describe('setHeadHandler', function () {
-    it('covers', function () {
-      const origEnd = res.end;
-      sinon.stub(Service.__proto__, 'setHeadHandler');
-      ctx.responseBody = 'data';
-      req.method = 'HEAD';
-      Service.setHeadHandler(req, res, ctx);
-      res.end('foop');
-      assert(Service.__proto__.setHeadHandler.called);
-      assert(origEnd.called);
-      assert(!('responseBody' in ctx));
-    });
-  }); // setHeadHandler
-
   describe('handlerPostRoot', function () {
     it('covers public mode', async function () {
       await service.handlerPostRoot(req, res, ctx);
@@ -108,26 +114,47 @@ describe('Service', function () {
   }); // handlerGetHealthcheck
 
   describe('handlerGetInfo', function () {
-    it('covers', async function() {
+    it('covers', async function () {
       await service.handlerGetInfo(req, res, ctx);
       assert(service.manager.getInfo.called);
     });
   }); // handlerGetInfo
 
-  describe('handlerGetAdminOverview', function () {
+  describe('handlerGetHistorySVG', function () {
     it('covers', async function () {
+      await service.handlerGetHistorySVG(req, res, ctx);
+      assert(service.manager.getHistorySVG.called);
+    });
+  }); // handlerGetHistorySVG
+
+  describe('handlerGetAdminOverview', function () {
+    it('covers authenticated', async function () {
+      service.authenticator.sessionRequired.resolves(false);
+      await service.handlerGetAdminOverview(req, res, ctx);
+      assert(service.authenticator.sessionRequired.called);
+      assert(service.manager.getAdminOverview.notCalled);
+    });
+    it('covers unauthenticated', async function () {
+      service.authenticator.sessionRequired.resolves(true);
       await service.handlerGetAdminOverview(req, res, ctx);
       assert(service.authenticator.sessionRequired.called);
       assert(service.manager.getAdminOverview.called);
-    })
+    });
   }); // handlerGetAdminOverview
 
   describe('handlerGetAdminTopicDetails', function () {
-    it('covers', async function () {
+    it('covers unauthenticated', async function () {
+      service.authenticator.sessionRequired.resolves(false);
+      await service.handlerGetAdminTopicDetails(req, res, ctx);
+      assert(service.authenticator.sessionRequired.called);
+      assert(service.manager.getTopicDetails.notCalled);
+    });
+    it('covers authenticated', async function () {
+      service.authenticator.sessionRequired.resolves(true);
       await service.handlerGetAdminTopicDetails(req, res, ctx);
       assert(service.authenticator.sessionRequired.called);
       assert(service.manager.getTopicDetails.called);
-    })
+    });
   }); // handlerGetAdminTopicDetails
 
   describe('handlerPostAdminProcess', function () {
@@ -177,7 +204,7 @@ describe('Service', function () {
       await service.handlerGetAdminLogout(req, res, ctx);
       assert(service.sessionManager.getAdminLogout.called);
     });
-}); // handlerGetAdminLogout
+  }); // handlerGetAdminLogout
 
   describe('handlerGetAdminIA', function () {
     it('covers', async function () {