update dependency, fixes for logger update
[websub-hub] / test / src / service.js
1 /* eslint-env mocha */
2 /* eslint-disable capitalized-comments */
3
4 'use strict';
5
6 const assert = require('assert');
7 const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require
8
9 const stubDb = require('../stub-db');
10 const stubLogger = require('../stub-logger');
11 const Service = require('../../src/service');
12 const Config = require('../../config');
13 const { AsyncLocalStorage } = require('node:async_hooks');
14
15
16 describe('Service', function () {
17 let service, options, asyncLocalStorage;
18 let req, res, ctx;
19
20 beforeEach(function () {
21 asyncLocalStorage = new AsyncLocalStorage();
22 options = new Config('test');
23 service = new Service(stubLogger, stubDb, options, asyncLocalStorage);
24 sinon.stub(service.manager);
25 sinon.stub(service.sessionManager);
26 sinon.stub(service.authenticator);
27 sinon.stub(service, 'setResponseType');
28 sinon.stub(service, 'serveFile');
29 sinon.stub(service, 'ingestBody').resolves();
30 req = {
31 getHeader: sinon.stub(),
32 };
33 res = {
34 setHeader: sinon.stub(),
35 write: sinon.stub(),
36 end: sinon.stub(),
37 };
38 ctx = {
39 params: {},
40 };
41 });
42
43 afterEach(function () {
44 sinon.restore();
45 });
46
47 it('instantiates', function () {
48 assert(service);
49 });
50
51 describe('preHandler', function () {
52 it('logs requestId', async () => {
53 sinon.stub(service.__proto__.__proto__, 'preHandler').resolves();
54 await service.asyncLocalStorage.run({}, async () => {
55 await service.preHandler(req, res, ctx);
56 const logObject = service.asyncLocalStorage.getStore();
57 assert('requestId' in logObject);
58 });
59 });
60 }); // preHandler
61
62 describe('maybeIngestBody', function () {
63 beforeEach(function () {
64 sinon.stub(service, 'bodyData');
65 sinon.stub(service, 'parseBody').returns();
66 });
67 it('covers no body', async function() {
68 service.bodyData.resolves();
69 await service.maybeIngestBody(req, res, ctx);
70 });
71 it('covers body', async function() {
72 service.bodyData.resolves('data');
73 await service.maybeIngestBody(req, res, ctx);
74 });
75 }); // maybeIngestBody
76
77 describe('handlerPostRoot', function () {
78 it('covers public mode', async function () {
79 await service.handlerPostRoot(req, res, ctx);
80 assert(service.manager.postRoot.called);
81 });
82 }); // handlerPostRoot
83
84 describe('handlerGetRoot', function () {
85 it('covers', async function () {
86 await service.handlerGetRoot(req, res, ctx);
87 assert(service.manager.getRoot.called);
88 });
89 }); // handlerGetRoot
90
91 describe('handlerGetHealthcheck', function () {
92 it('covers', async function () {
93 await service.handlerGetHealthcheck(req, res, ctx);
94 assert(service.manager.getHealthcheck.called);
95 });
96 it('cover errors', async function () {
97 const expectedException = 'blah';
98 service.manager.getHealthcheck.rejects(expectedException);
99 try {
100 await service.handlerGetHealthcheck(req, res, ctx);
101 assert.fail('did not get expected exception');
102 } catch (e) {
103 assert.strictEqual(e.name, expectedException, 'did not get expected exception');
104 }
105 assert(service.manager.getHealthcheck.called);
106 });
107 }); // handlerGetHealthcheck
108
109 describe('handlerGetInfo', function () {
110 it('covers', async function () {
111 await service.handlerGetInfo(req, res, ctx);
112 assert(service.manager.getInfo.called);
113 });
114 }); // handlerGetInfo
115
116 describe('handlerGetHistorySVG', function () {
117 it('covers', async function () {
118 await service.handlerGetHistorySVG(req, res, ctx);
119 assert(service.manager.getHistorySVG.called);
120 });
121 }); // handlerGetHistorySVG
122
123 describe('handlerGetAdminOverview', function () {
124 it('covers authenticated', async function () {
125 service.authenticator.sessionRequired.resolves(false);
126 await service.handlerGetAdminOverview(req, res, ctx);
127 assert(service.authenticator.sessionRequired.called);
128 assert(service.manager.getAdminOverview.notCalled);
129 });
130 it('covers unauthenticated', async function () {
131 service.authenticator.sessionRequired.resolves(true);
132 await service.handlerGetAdminOverview(req, res, ctx);
133 assert(service.authenticator.sessionRequired.called);
134 assert(service.manager.getAdminOverview.called);
135 });
136 }); // handlerGetAdminOverview
137
138 describe('handlerGetAdminTopicDetails', function () {
139 it('covers unauthenticated', async function () {
140 service.authenticator.sessionRequired.resolves(false);
141 await service.handlerGetAdminTopicDetails(req, res, ctx);
142 assert(service.authenticator.sessionRequired.called);
143 assert(service.manager.getTopicDetails.notCalled);
144 });
145 it('covers authenticated', async function () {
146 service.authenticator.sessionRequired.resolves(true);
147 await service.handlerGetAdminTopicDetails(req, res, ctx);
148 assert(service.authenticator.sessionRequired.called);
149 assert(service.manager.getTopicDetails.called);
150 });
151 }); // handlerGetAdminTopicDetails
152
153 describe('handlerPostAdminProcess', function () {
154 it('covers', async function () {
155 service.serveFile.resolves();
156 await service.handlerPostAdminProcess(req, res, ctx);
157 assert(service.authenticator.apiRequiredLocal.called);
158 assert(service.manager.processTasks.called);
159 });
160 }); // handlerPostAdminProcess
161
162 describe('handlerUpdateTopic', function () {
163 it('covers', async function () {
164 sinon.stub(service, 'bodyData').resolves();
165 await service.handlerUpdateTopic(req, res, ctx);
166 assert(service.authenticator.apiRequiredLocal.called);
167 assert(service.manager.updateTopic.called);
168 });
169 }); // handlerUpdateTopic
170
171 describe('handlerUpdateSubscription', function () {
172 it('covers', async function () {
173 sinon.stub(service, 'bodyData').resolves();
174 await service.handlerUpdateSubscription(req, res, ctx);
175 assert(service.authenticator.apiRequiredLocal.called);
176 assert(service.manager.updateSubscription.called);
177 });
178 }); // handlerUpdateSubscription
179
180 describe('handlerGetAdminLogin', function () {
181 it('covers', async function () {
182 await service.handlerGetAdminLogin(req, res, ctx);
183 assert(service.sessionManager.getAdminLogin.called);
184 });
185 }); // handlerGetAdminLogin
186
187 describe('handlerPostAdminLogin', function () {
188 it('covers', async function () {
189 sinon.stub(service, 'bodyData').resolves();
190 await service.handlerPostAdminLogin(req, res, ctx);
191 assert(service.sessionManager.postAdminLogin.called);
192 });
193 }); // handlerPostAdminLogin
194
195 describe('handlerGetAdminLogout', function () {
196 it('covers', async function () {
197 await service.handlerGetAdminLogout(req, res, ctx);
198 assert(service.sessionManager.getAdminLogout.called);
199 });
200 }); // handlerGetAdminLogout
201
202 describe('handlerGetAdminIA', function () {
203 it('covers', async function () {
204 await service.handlerGetAdminIA(req, res, ctx);
205 assert(service.sessionManager.getAdminIA.called);
206 });
207 }); // handlerGetAdminIA
208
209 });