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