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