3 const assert
= require('node:assert');
4 const sinon
= require('sinon');
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');
13 describe('Service', function () {
14 let service
, options
, asyncLocalStorage
;
17 beforeEach(function () {
18 asyncLocalStorage
= new AsyncLocalStorage();
19 options
= new Config('test');
20 service
= new Service(stubLogger
, stubDb
, options
, asyncLocalStorage
);
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();
29 getHeader: sinon
.stub(),
32 setHeader: sinon
.stub(),
41 afterEach(function () {
45 it('instantiates', function () {
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
);
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
);
66 describe('maybeIngestBody', function () {
67 beforeEach(function () {
68 sinon
.stub(service
, 'bodyData');
69 sinon
.stub(service
, 'parseBody').returns();
71 it('covers no body', async
function() {
72 service
.bodyData
.resolves();
73 await service
.maybeIngestBody(req
, res
, ctx
);
75 it('covers body', async
function() {
76 service
.bodyData
.resolves('data');
77 await service
.maybeIngestBody(req
, res
, ctx
);
79 }); // maybeIngestBody
81 describe('handlerPostRoot', function () {
82 it('covers public mode', async
function () {
83 await service
.handlerPostRoot(req
, res
, ctx
);
84 assert(service
.manager
.postRoot
.called
);
86 }); // handlerPostRoot
88 describe('handlerGetRoot', function () {
89 it('covers', async
function () {
90 await service
.handlerGetRoot(req
, res
, ctx
);
91 assert(service
.manager
.getRoot
.called
);
95 describe('handlerGetHealthcheck', function () {
96 it('covers', async
function () {
97 await service
.handlerGetHealthcheck(req
, res
, ctx
);
98 assert(service
.manager
.getHealthcheck
.called
);
100 it('cover errors', async
function () {
101 const expectedException
= 'blah';
102 service
.manager
.getHealthcheck
.rejects(expectedException
);
104 await service
.handlerGetHealthcheck(req
, res
, ctx
);
105 assert
.fail('did not get expected exception');
107 assert
.strictEqual(e
.name
, expectedException
, 'did not get expected exception');
109 assert(service
.manager
.getHealthcheck
.called
);
111 }); // handlerGetHealthcheck
113 describe('handlerGetInfo', function () {
114 it('covers', async
function () {
115 await service
.handlerGetInfo(req
, res
, ctx
);
116 assert(service
.manager
.getInfo
.called
);
118 }); // handlerGetInfo
120 describe('handlerGetHistorySVG', function () {
121 it('covers', async
function () {
122 await service
.handlerGetHistorySVG(req
, res
, ctx
);
123 assert(service
.manager
.getHistorySVG
.called
);
125 }); // handlerGetHistorySVG
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
);
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
);
140 }); // handlerGetAdminOverview
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
);
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
);
155 }); // handlerGetAdminTopicDetails
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
);
164 }); // handlerPostAdminProcess
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
);
173 }); // handlerUpdateTopic
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
);
182 }); // handlerUpdateSubscription
184 describe('handlerGetAdminLogin', function () {
185 it('covers', async
function () {
186 await service
.handlerGetAdminLogin(req
, res
, ctx
);
187 assert(service
.sessionManager
.getAdminLogin
.called
);
189 }); // handlerGetAdminLogin
191 describe('handlerPostAdminLogin', function () {
192 it('covers', async
function () {
193 sinon
.stub(service
, 'bodyData').resolves();
194 await service
.handlerPostAdminLogin(req
, res
, ctx
);
195 assert(service
.sessionManager
.postAdminLogin
.called
);
197 }); // handlerPostAdminLogin
199 describe('handlerGetAdminLogout', function () {
200 it('covers', async
function () {
201 await service
.handlerGetAdminLogout(req
, res
, ctx
);
202 assert(service
.sessionManager
.getAdminLogout
.called
);
204 }); // handlerGetAdminLogout
206 describe('handlerGetAdminIA', function () {
207 it('covers', async
function () {
208 await service
.handlerGetAdminIA(req
, res
, ctx
);
209 assert(service
.sessionManager
.getAdminIA
.called
);
211 }); // handlerGetAdminIA