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
);
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();
31 getHeader: sinon
.stub(),
34 setHeader: sinon
.stub(),
43 afterEach(function () {
47 it('instantiates', function () {
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
);
62 describe('maybeIngestBody', function () {
63 beforeEach(function () {
64 sinon
.stub(service
, 'bodyData');
65 sinon
.stub(service
, 'parseBody').returns();
67 it('covers no body', async
function() {
68 service
.bodyData
.resolves();
69 await service
.maybeIngestBody(req
, res
, ctx
);
71 it('covers body', async
function() {
72 service
.bodyData
.resolves('data');
73 await service
.maybeIngestBody(req
, res
, ctx
);
75 }); // maybeIngestBody
77 describe('handlerPostRoot', function () {
78 it('covers public mode', async
function () {
79 await service
.handlerPostRoot(req
, res
, ctx
);
80 assert(service
.manager
.postRoot
.called
);
82 }); // handlerPostRoot
84 describe('handlerGetRoot', function () {
85 it('covers', async
function () {
86 await service
.handlerGetRoot(req
, res
, ctx
);
87 assert(service
.manager
.getRoot
.called
);
91 describe('handlerGetHealthcheck', function () {
92 it('covers', async
function () {
93 await service
.handlerGetHealthcheck(req
, res
, ctx
);
94 assert(service
.manager
.getHealthcheck
.called
);
96 it('cover errors', async
function () {
97 const expectedException
= 'blah';
98 service
.manager
.getHealthcheck
.rejects(expectedException
);
100 await service
.handlerGetHealthcheck(req
, res
, ctx
);
101 assert
.fail('did not get expected exception');
103 assert
.strictEqual(e
.name
, expectedException
, 'did not get expected exception');
105 assert(service
.manager
.getHealthcheck
.called
);
107 }); // handlerGetHealthcheck
109 describe('handlerGetInfo', function () {
110 it('covers', async
function () {
111 await service
.handlerGetInfo(req
, res
, ctx
);
112 assert(service
.manager
.getInfo
.called
);
114 }); // handlerGetInfo
116 describe('handlerGetHistorySVG', function () {
117 it('covers', async
function () {
118 await service
.handlerGetHistorySVG(req
, res
, ctx
);
119 assert(service
.manager
.getHistorySVG
.called
);
121 }); // handlerGetHistorySVG
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
);
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
);
136 }); // handlerGetAdminOverview
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
);
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
);
151 }); // handlerGetAdminTopicDetails
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
);
160 }); // handlerPostAdminProcess
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
);
169 }); // handlerUpdateTopic
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
);
178 }); // handlerUpdateSubscription
180 describe('handlerGetAdminLogin', function () {
181 it('covers', async
function () {
182 await service
.handlerGetAdminLogin(req
, res
, ctx
);
183 assert(service
.sessionManager
.getAdminLogin
.called
);
185 }); // handlerGetAdminLogin
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
);
193 }); // handlerPostAdminLogin
195 describe('handlerGetAdminLogout', function () {
196 it('covers', async
function () {
197 await service
.handlerGetAdminLogout(req
, res
, ctx
);
198 assert(service
.sessionManager
.getAdminLogout
.called
);
200 }); // handlerGetAdminLogout
202 describe('handlerGetAdminIA', function () {
203 it('covers', async
function () {
204 await service
.handlerGetAdminIA(req
, res
, ctx
);
205 assert(service
.sessionManager
.getAdminIA
.called
);
207 }); // handlerGetAdminIA