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');
15 describe('Service', function () {
19 beforeEach(function () {
20 options
= new Config('test');
21 service
= new Service(stubLogger
, stubDb
, options
);
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('maybeIngestBody', function () {
50 beforeEach(function () {
51 sinon
.stub(service
, 'bodyData');
52 sinon
.stub(service
, 'parseBody').returns();
54 it('covers no body', async
function() {
55 service
.bodyData
.resolves();
56 await service
.maybeIngestBody(req
, res
, ctx
);
58 it('covers body', async
function() {
59 service
.bodyData
.resolves('data');
60 await service
.maybeIngestBody(req
, res
, ctx
);
62 }); // maybeIngestBody
64 describe('setHeadHandler', function () {
65 it('covers', function () {
66 const origEnd
= res
.end
;
67 sinon
.stub(Service
.__proto__
, 'setHeadHandler');
68 ctx
.responseBody
= 'data';
70 Service
.setHeadHandler(req
, res
, ctx
);
72 assert(Service
.__proto__
.setHeadHandler
.called
);
73 assert(origEnd
.called
);
74 assert(!('responseBody' in ctx
));
78 describe('handlerPostRoot', function () {
79 it('covers public mode', async
function () {
80 await service
.handlerPostRoot(req
, res
, ctx
);
81 assert(service
.manager
.postRoot
.called
);
83 }); // handlerPostRoot
85 describe('handlerGetRoot', function () {
86 it('covers', async
function () {
87 await service
.handlerGetRoot(req
, res
, ctx
);
88 assert(service
.manager
.getRoot
.called
);
92 describe('handlerGetHealthcheck', function () {
93 it('covers', async
function () {
94 await service
.handlerGetHealthcheck(req
, res
, ctx
);
95 assert(service
.manager
.getHealthcheck
.called
);
97 it('cover errors', async
function () {
98 const expectedException
= 'blah';
99 service
.manager
.getHealthcheck
.rejects(expectedException
);
101 await service
.handlerGetHealthcheck(req
, res
, ctx
);
102 assert
.fail('did not get expected exception');
104 assert
.strictEqual(e
.name
, expectedException
, 'did not get expected exception');
106 assert(service
.manager
.getHealthcheck
.called
);
108 }); // handlerGetHealthcheck
110 describe('handlerGetInfo', function () {
111 it('covers', async
function () {
112 await service
.handlerGetInfo(req
, res
, ctx
);
113 assert(service
.manager
.getInfo
.called
);
115 }); // handlerGetInfo
117 describe('handlerGetHistorySVG', function () {
118 it('covers', async
function () {
119 await service
.handlerGetHistorySVG(req
, res
, ctx
);
120 assert(service
.manager
.getHistorySVG
.called
);
122 }); // handlerGetHistorySVG
124 describe('handlerGetAdminOverview', function () {
125 it('covers', async
function () {
126 await service
.handlerGetAdminOverview(req
, res
, ctx
);
127 assert(service
.authenticator
.sessionRequired
.called
);
128 assert(service
.manager
.getAdminOverview
.called
);
130 }); // handlerGetAdminOverview
132 describe('handlerGetAdminTopicDetails', function () {
133 it('covers', async
function () {
134 await service
.handlerGetAdminTopicDetails(req
, res
, ctx
);
135 assert(service
.authenticator
.sessionRequired
.called
);
136 assert(service
.manager
.getTopicDetails
.called
);
138 }); // handlerGetAdminTopicDetails
140 describe('handlerPostAdminProcess', function () {
141 it('covers', async
function () {
142 service
.serveFile
.resolves();
143 await service
.handlerPostAdminProcess(req
, res
, ctx
);
144 assert(service
.authenticator
.apiRequiredLocal
.called
);
145 assert(service
.manager
.processTasks
.called
);
147 }); // handlerPostAdminProcess
149 describe('handlerUpdateTopic', function () {
150 it('covers', async
function () {
151 sinon
.stub(service
, 'bodyData').resolves();
152 await service
.handlerUpdateTopic(req
, res
, ctx
);
153 assert(service
.authenticator
.apiRequiredLocal
.called
);
154 assert(service
.manager
.updateTopic
.called
);
156 }); // handlerUpdateTopic
158 describe('handlerUpdateSubscription', function () {
159 it('covers', async
function () {
160 sinon
.stub(service
, 'bodyData').resolves();
161 await service
.handlerUpdateSubscription(req
, res
, ctx
);
162 assert(service
.authenticator
.apiRequiredLocal
.called
);
163 assert(service
.manager
.updateSubscription
.called
);
165 }); // handlerUpdateSubscription
167 describe('handlerGetAdminLogin', function () {
168 it('covers', async
function () {
169 await service
.handlerGetAdminLogin(req
, res
, ctx
);
170 assert(service
.sessionManager
.getAdminLogin
.called
);
172 }); // handlerGetAdminLogin
174 describe('handlerPostAdminLogin', function () {
175 it('covers', async
function () {
176 sinon
.stub(service
, 'bodyData').resolves();
177 await service
.handlerPostAdminLogin(req
, res
, ctx
);
178 assert(service
.sessionManager
.postAdminLogin
.called
);
180 }); // handlerPostAdminLogin
182 describe('handlerGetAdminLogout', function () {
183 it('covers', async
function () {
184 await service
.handlerGetAdminLogout(req
, res
, ctx
);
185 assert(service
.sessionManager
.getAdminLogout
.called
);
187 }); // handlerGetAdminLogout
189 describe('handlerGetAdminIA', function () {
190 it('covers', async
function () {
191 await service
.handlerGetAdminIA(req
, res
, ctx
);
192 assert(service
.sessionManager
.getAdminIA
.called
);
194 }); // handlerGetAdminIA