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
.resourceAuthenticator
);
26 sinon
.stub(service
, 'setResponseType');
27 sinon
.stub(service
, 'serveFile');
28 sinon
.stub(service
, 'ingestBody').resolves();
30 getHeader: sinon
.stub(),
33 setHeader: sinon
.stub(),
42 afterEach(function () {
46 it('instantiates', function () {
50 it('instantiates with config coverage', async
function () {
51 options
.dingus
.selfBaseUrl
= 'https://example.com/';
52 service
= new Service(stubLogger
, stubDb
, options
);
56 it('instantiates with config coverage', async
function () {
57 delete options
.dingus
.selfBaseUrl
;
58 service
= new Service(stubLogger
, stubDb
, options
);
62 describe('initialize', function () {
63 it('covers', async
function () {
64 await service
.initialize();
65 assert(service
.manager
.initialize
.called
);
69 describe('preHandler', function () {
70 it('persists url into context', async
function () {
71 req
.url
= 'https://example.com/foo';
72 sinon
.stub(service
.__proto__
.__proto__
, 'preHandler').resolves();
73 await service
.preHandler(req
, res
, ctx
);
74 assert
.strictEqual(ctx
.url
, req
.url
);
78 describe('handlerGetAdminLogin', function () {
79 it('covers', async
function () {
80 await service
.handlerGetAdminLogin(req
, res
, ctx
);
81 assert(service
.sessionManager
.getAdminLogin
.called
);
83 }); // handlerGetAdminLogin
85 describe('handlerPostAdminLogin', function () {
86 it('covers', async
function () {
87 await service
.handlerPostAdminLogin(req
, res
, ctx
);
88 assert(service
.sessionManager
.postAdminLogin
.called
);
90 }); // handlerPostAdminLogin
92 describe('handlerGetAdminLogout', function () {
93 it('covers', async
function () {
94 await service
.handlerGetAdminLogout(req
, res
, ctx
);
95 assert(service
.sessionManager
.getAdminLogout
.called
);
97 }); // handlerGetAdminLogout
99 describe('handlerGetAdmin', function () {
100 it('covers authenticated', async
function () {
101 service
.authenticator
.sessionRequiredLocal
.resolves(true);
102 await service
.handlerGetAdmin(req
, res
, ctx
);
103 assert(service
.manager
.getAdmin
.called
);
105 it('covers unauthenticated', async
function () {
106 service
.authenticator
.sessionRequiredLocal
.resolves(false);
107 await service
.handlerGetAdmin(req
, res
, ctx
);
108 assert(service
.manager
.getAdmin
.notCalled
);
110 }); // handlerGetAdmin
112 describe('handlerPostAdmin', function () {
113 it('covers authenticated', async
function () {
114 service
.authenticator
.sessionRequiredLocal
.resolves(true);
115 await service
.handlerPostAdmin(req
, res
, ctx
);
116 assert(service
.manager
.postAdmin
.called
);
118 it('covers unauthenticated', async
function () {
119 service
.authenticator
.sessionRequiredLocal
.resolves(false);
120 await service
.handlerPostAdmin(req
, res
, ctx
);
121 assert(service
.manager
.getAdmin
.notCalled
);
123 }); // handlerPostAdmin
125 describe('handlerGetRoot', function () {
126 it('covers', async
function () {
127 await service
.handlerGetRoot(req
, res
, ctx
);
128 assert(service
.manager
.getRoot
.called
);
130 }); // handlerGetRoot
132 describe('handlerGetAdminTicket', function () {
133 it('covers authenticated', async
function () {
134 service
.authenticator
.sessionRequiredLocal
.resolves(true);
135 await service
.handlerGetAdminTicket(req
, res
, ctx
);
136 assert(service
.manager
.getAdminTicket
.called
);
138 it('covers unauthenticated', async
function () {
139 service
.authenticator
.sessionRequiredLocal
.resolves(false);
140 await service
.handlerGetAdminTicket(req
, res
, ctx
);
141 assert(service
.manager
.getAdminTicket
.notCalled
);
143 }); // handlerGetAdminTicket
145 describe('handlerPostAdminTicket', function () {
146 it('covers authenticated', async
function () {
147 service
.authenticator
.sessionRequiredLocal
.resolves(true);
148 await service
.handlerPostAdminTicket(req
, res
, ctx
);
149 assert(service
.manager
.postAdminTicket
.called
);
151 it('covers unauthenticated', async
function () {
152 service
.authenticator
.sessionRequiredLocal
.resolves(false);
153 await service
.handlerPostAdminTicket(req
, res
, ctx
);
154 assert(service
.manager
.postAdminTicket
.notCalled
);
156 }); // handlerPostAdminTicket
158 describe('handlerGetMeta', function () {
159 it('covers', async
function () {
160 await service
.handlerGetMeta(req
, res
, ctx
);
161 assert(service
.manager
.getMeta
.called
);
163 }); // handlerGetMeta
165 describe('handlerGetHealthcheck', function () {
166 it('covers', async
function () {
167 await service
.handlerGetHealthcheck(req
, res
, ctx
);
168 assert(service
.manager
.getHealthcheck
.called
);
170 it('cover errors', async
function () {
171 const expectedException
= 'blah';
172 service
.manager
.getHealthcheck
.rejects(expectedException
);
174 await service
.handlerGetHealthcheck(req
, res
, ctx
);
175 assert
.fail('did not get expected exception');
177 assert
.strictEqual(e
.name
, expectedException
, 'did not get expected exception');
179 assert(service
.manager
.getHealthcheck
.called
);
181 }); // handlerGetHealthcheck
183 describe('handlerInternalServerError', function () {
184 it('covers no redirect', async
function () {
185 sinon
.stub(service
.__proto__
.__proto__
, 'handlerInternalServerError');
186 await service
.handlerInternalServerError(req
, res
, ctx
);
187 assert(service
.__proto__
.__proto__
.handlerInternalServerError
.called
);
189 it('covers redirect', async
function () {
190 sinon
.stub(service
.__proto__
.__proto__
, 'handlerInternalServerError');
192 redirectUri: new URL('https://client.example.com/app'),
193 clientIdentifier: new URL('https://client.exmaple.com/'),
196 await service
.handlerInternalServerError(req
, res
, ctx
);
197 assert(!service
.__proto__
.__proto__
.handlerInternalServerError
.called
);
198 assert(res
.setHeader
);
200 }); // handlerInternalServerError
202 describe('handlerGetAuthorization', function () {
203 it('covers authenticated', async
function() {
204 service
.authenticator
.sessionRequiredLocal
.resolves(true);
205 await service
.handlerGetAuthorization(req
, res
, ctx
);
206 assert(service
.manager
.getAuthorization
.called
);
208 it('covers unauthenticated', async
function() {
209 service
.authenticator
.sessionRequiredLocal
.resolves(false);
210 await service
.handlerGetAuthorization(req
, res
, ctx
);
211 assert(service
.manager
.getAuthorization
.notCalled
);
213 }); // handlerGetAuthorization
215 describe('handlerPostAuthorization', function () {
216 it('covers', async
function () {
217 await service
.handlerPostAuthorization(req
, res
, ctx
);
218 assert(service
.manager
.postAuthorization
.called
);
220 }); // handlerPostAuthorization
222 describe('handlerPostConsent', function () {
223 it('covers', async
function () {
224 service
.serveFile
.resolves();
225 await service
.handlerPostConsent(req
, res
, ctx
);
226 assert(service
.manager
.postConsent
.called
);
228 }); // handlerPostConsent
230 describe('handlerPostToken', function () {
231 it('covers', async
function () {
232 await service
.handlerPostToken(req
, res
, ctx
);
233 assert(service
.manager
.postToken
.called
);
235 }); // handlerPostToken
237 describe('handlerPostTicket', function () {
238 it('covers', async
function () {
239 await service
.handlerPostTicket(req
, res
, ctx
);
240 assert(service
.manager
.postTicket
.called
);
242 }); // handlerPostTicket
244 describe('handlerPostIntrospection', function () {
245 it('covers', async
function () {
246 await service
.handlerPostIntrospection(req
, res
, ctx
);
247 assert(service
.manager
.postIntrospection
.called
);
249 }); // handlerPostIntrospection
251 describe('handlerPostRevocation', function () {
252 it('covers', async
function () {
253 await service
.handlerPostRevocation(req
, res
, ctx
);
254 assert(service
.manager
.postRevocation
.called
);
256 }); // handlerPostRevocation
258 describe('handlerPostUserInfo', function () {
259 it('covers', async
function () {
260 await service
.handlerPostUserInfo(req
, res
, ctx
);
261 assert(service
.manager
.postUserInfo
.called
);
263 }); // handlerPostUserInfo
265 describe('handlerGetAdminMaintenance', function () {
266 it('covers authenticated', async
function () {
267 service
.authenticator
.sessionRequiredLocal
.resolves(true);
268 await service
.handlerGetAdminMaintenance(req
, res
, ctx
);
269 assert(service
.manager
.getAdminMaintenance
.called
);
271 it('covers unauthenticated', async
function () {
272 service
.authenticator
.sessionRequiredLocal
.resolves(false);
273 await service
.handlerGetAdminMaintenance(req
, res
, ctx
);
274 assert(service
.manager
.getAdminMaintenance
.notCalled
);
276 }); // handlerGetAdminMaintenance