use got instead of axios, some cleanup, problem with async context being lost for...
[websub-hub] / test / src / service.js
index bb93d2e7cc463efcc7b9193ba14306e9ba2ddd99..2de3b52c4595772834636f38be58b10fb54f9f6a 100644 (file)
@@ -10,15 +10,17 @@ 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);
     sinon.stub(service.manager);
     sinon.stub(service.sessionManager);
     sinon.stub(service.authenticator);
@@ -46,6 +48,17 @@ describe('Service', function () {
     assert(service);
   });
 
+  describe('preHandler', function () {
+    it('logs requestId', async () => {
+      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);
+      });
+    });
+  }); // preHandler
+
   describe('maybeIngestBody', function () {
     beforeEach(function () {
       sinon.stub(service, 'bodyData');
@@ -94,33 +107,54 @@ 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.required.called);
+      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.required.called);
+      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 () {
     it('covers', async function () {
       service.serveFile.resolves();
       await service.handlerPostAdminProcess(req, res, ctx);
-      assert(service.authenticator.requiredLocal.called);
+      assert(service.authenticator.apiRequiredLocal.called);
       assert(service.manager.processTasks.called);
     });
   }); // handlerPostAdminProcess
@@ -129,7 +163,7 @@ describe('Service', function () {
     it('covers', async function () {
       sinon.stub(service, 'bodyData').resolves();
       await service.handlerUpdateTopic(req, res, ctx);
-      assert(service.authenticator.requiredLocal.called);
+      assert(service.authenticator.apiRequiredLocal.called);
       assert(service.manager.updateTopic.called);
     });
   }); // handlerUpdateTopic
@@ -138,7 +172,7 @@ describe('Service', function () {
     it('covers', async function () {
       sinon.stub(service, 'bodyData').resolves();
       await service.handlerUpdateSubscription(req, res, ctx);
-      assert(service.authenticator.requiredLocal.called);
+      assert(service.authenticator.apiRequiredLocal.called);
       assert(service.manager.updateSubscription.called);
     });
   }); // handlerUpdateSubscription