576859f834156b09759c2b64f7b29cee31de0e53
2 /* eslint-disable capitalized-comments */
6 const assert
= require('assert');
7 const sinon
= require('sinon'); // eslint-disable-line node/no-unpublished-require
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');
16 describe('Service', function () {
17 let service
, options
, asyncLocalStorage
;
20 beforeEach(function () {
21 asyncLocalStorage
= new AsyncLocalStorage();
22 options
= new Config('test');
23 service
= new Service(stubLogger
, stubDb
, options
, asyncLocalStorage
);
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();
32 getHeader: sinon
.stub(),
35 setHeader: sinon
.stub(),
44 afterEach(function () {
48 it('instantiates', function () {
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
);
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
);
69 describe('maybeIngestBody', function () {
70 beforeEach(function () {
71 sinon
.stub(service
, 'bodyData');
72 sinon
.stub(service
, 'parseBody').returns();
74 it('covers no body', async
function() {
75 service
.bodyData
.resolves();
76 await service
.maybeIngestBody(req
, res
, ctx
);
78 it('covers body', async
function() {
79 service
.bodyData
.resolves('data');
80 await service
.maybeIngestBody(req
, res
, ctx
);
82 }); // maybeIngestBody
84 describe('handlerPostRoot', function () {
85 it('covers public mode', async
function () {
86 await service
.handlerPostRoot(req
, res
, ctx
);
87 assert(service
.manager
.postRoot
.called
);
89 }); // handlerPostRoot
91 describe('handlerGetRoot', function () {
92 it('covers', async
function () {
93 await service
.handlerGetRoot(req
, res
, ctx
);
94 assert(service
.manager
.getRoot
.called
);
98 describe('handlerGetHealthcheck', function () {
99 it('covers', async
function () {
100 await service
.handlerGetHealthcheck(req
, res
, ctx
);
101 assert(service
.manager
.getHealthcheck
.called
);
103 it('cover errors', async
function () {
104 const expectedException
= 'blah';
105 service
.manager
.getHealthcheck
.rejects(expectedException
);
107 await service
.handlerGetHealthcheck(req
, res
, ctx
);
108 assert
.fail('did not get expected exception');
110 assert
.strictEqual(e
.name
, expectedException
, 'did not get expected exception');
112 assert(service
.manager
.getHealthcheck
.called
);
114 }); // handlerGetHealthcheck
116 describe('handlerGetInfo', function () {
117 it('covers', async
function () {
118 await service
.handlerGetInfo(req
, res
, ctx
);
119 assert(service
.manager
.getInfo
.called
);
121 }); // handlerGetInfo
123 describe('handlerGetHistorySVG', function () {
124 it('covers', async
function () {
125 await service
.handlerGetHistorySVG(req
, res
, ctx
);
126 assert(service
.manager
.getHistorySVG
.called
);
128 }); // handlerGetHistorySVG
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
);
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
);
143 }); // handlerGetAdminOverview
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
);
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
);
158 }); // handlerGetAdminTopicDetails
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
);
167 }); // handlerPostAdminProcess
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
);
176 }); // handlerUpdateTopic
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
);
185 }); // handlerUpdateSubscription
187 describe('handlerGetAdminLogin', function () {
188 it('covers', async
function () {
189 await service
.handlerGetAdminLogin(req
, res
, ctx
);
190 assert(service
.sessionManager
.getAdminLogin
.called
);
192 }); // handlerGetAdminLogin
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
);
200 }); // handlerPostAdminLogin
202 describe('handlerGetAdminLogout', function () {
203 it('covers', async
function () {
204 await service
.handlerGetAdminLogout(req
, res
, ctx
);
205 assert(service
.sessionManager
.getAdminLogout
.called
);
207 }); // handlerGetAdminLogout
209 describe('handlerGetAdminIA', function () {
210 it('covers', async
function () {
211 await service
.handlerGetAdminIA(req
, res
, ctx
);
212 assert(service
.sessionManager
.getAdminIA
.called
);
214 }); // handlerGetAdminIA