Merge branch 'v1.3-dev'
[websub-hub] / test / src / service.js
index 7a97cf5366f88a70a32702170658b4f2e50c7c97..2cfe38d66ed4b55115ebb070dcc4301c1f7b9f1d 100644 (file)
@@ -1,25 +1,26 @@
-/* eslint-env mocha */
-/* eslint-disable capitalized-comments */
-
 'use strict';
 
-const assert = require('assert');
-const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require
+const assert = require('node:assert');
+const sinon = require('sinon');
 
 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);
     sinon.stub(service, 'setResponseType');
     sinon.stub(service, 'serveFile');
@@ -45,6 +46,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');
@@ -93,33 +111,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.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.required.called);
+      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.required.called);
+      assert(service.authenticator.apiRequiredLocal.called);
       assert(service.manager.processTasks.called);
     });
   }); // handlerPostAdminProcess
@@ -128,7 +167,7 @@ describe('Service', function () {
     it('covers', async function () {
       sinon.stub(service, 'bodyData').resolves();
       await service.handlerUpdateTopic(req, res, ctx);
-      assert(service.authenticator.required.called);
+      assert(service.authenticator.apiRequiredLocal.called);
       assert(service.manager.updateTopic.called);
     });
   }); // handlerUpdateTopic
@@ -137,9 +176,66 @@ describe('Service', function () {
     it('covers', async function () {
       sinon.stub(service, 'bodyData').resolves();
       await service.handlerUpdateSubscription(req, res, ctx);
-      assert(service.authenticator.required.called);
+      assert(service.authenticator.apiRequiredLocal.called);
       assert(service.manager.updateSubscription.called);
     });
   }); // handlerUpdateSubscription
 
+  describe('handlerGetAdminLogin', function () {
+    it('covers', async function () {
+      await service.handlerGetAdminLogin(req, res, ctx);
+      assert(service.sessionManager.getAdminLogin.called);
+    });
+  }); // handlerGetAdminLogin
+
+  describe('handlerGetAdminSettings', function () {
+    it('covers logged in', async function () {
+      service.authenticator.sessionRequiredLocal.resolves(true);
+      await service.handlerGetAdminSettings(req, res, ctx);
+      assert(service.sessionManager.getAdminSettings.called);
+    });
+    it('covers not logged in', async function () {
+      service.authenticator.sessionRequiredLocal.resolves(false);
+      await service.handlerGetAdminSettings(req, res, ctx);
+      assert(service.sessionManager.getAdminSettings.notCalled);
+    });
+  }); // handlerGetAdminSettings
+
+  describe('handlerPostAdminSettings', function () {
+    it('covers logged in', async function () {
+      service.authenticator.sessionRequiredLocal.resolves(true);
+      sinon.stub(service, 'bodyData').resolves();
+      await service.handlerPostAdminSettings(req, res, ctx);
+      assert(service.sessionManager.postAdminSettings.called);
+    });
+    it('covers logged outo', async function () {
+      service.authenticator.sessionRequiredLocal.resolves(false);
+      sinon.stub(service, 'bodyData').resolves();
+      await service.handlerPostAdminSettings(req, res, ctx);
+      assert(service.sessionManager.postAdminSettings.notCalled);
+    });
+  }); // handlerPostAdminSettings
+
+  describe('handlerPostAdminLogin', function () {
+    it('covers', async function () {
+      sinon.stub(service, 'bodyData').resolves();
+      await service.handlerPostAdminLogin(req, res, ctx);
+      assert(service.sessionManager.postAdminLogin.called);
+    });
+  }); // handlerPostAdminLogin
+
+  describe('handlerGetAdminLogout', function () {
+    it('covers', async function () {
+      await service.handlerGetAdminLogout(req, res, ctx);
+      assert(service.sessionManager.getAdminLogout.called);
+    });
+  }); // handlerGetAdminLogout
+
+  describe('handlerGetAdminIA', function () {
+    it('covers', async function () {
+      await service.handlerGetAdminIA(req, res, ctx);
+      assert(service.sessionManager.getAdminIA.called);
+    });
+  }); // handlerGetAdminIA
+
 });
\ No newline at end of file