2 /* eslint-disable capitalized-comments */
6 const assert
= require('assert');
7 const sinon
= require('sinon'); // eslint-disable-line node/no-unpublished-require
8 const { AsyncLocalStorage
} = require('node:async_hooks');
10 const stubDb
= require('../stub-db');
11 const stubLogger
= require('../stub-logger');
12 const Service
= require('../../src/service');
13 const Config
= require('../../config');
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
.resourceAuthenticator
);
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 it('instantiates with config coverage', async
function () {
53 options
.dingus
.selfBaseUrl
= 'https://example.com/';
54 service
= new Service(stubLogger
, stubDb
, options
);
58 it('instantiates with config coverage', async
function () {
59 delete options
.dingus
.selfBaseUrl
;
60 service
= new Service(stubLogger
, stubDb
, options
);
64 describe('initialize', function () {
65 it('covers', async
function () {
66 await service
.initialize();
67 assert(service
.manager
.initialize
.called
);
71 describe('preHandler', async
function () {
72 it('persists url into context', async
function () {
73 req
.url
= 'https://example.com/foo';
74 sinon
.stub(service
.__proto__
.__proto__
, 'preHandler').resolves();
75 await service
.asyncLocalStorage
.run({}, async () => {
76 await service
.preHandler(req
, res
, ctx
);
77 const logObject
= service
.asyncLocalStorage
.getStore();
78 assert('requestId' in logObject
);
80 assert
.strictEqual(ctx
.url
, req
.url
);
84 describe('handlerGetAdminLogin', function () {
85 it('covers', async
function () {
86 await service
.handlerGetAdminLogin(req
, res
, ctx
);
87 assert(service
.sessionManager
.getAdminLogin
.called
);
89 }); // handlerGetAdminLogin
91 describe('handlerPostAdminLogin', function () {
92 it('covers', async
function () {
93 await service
.handlerPostAdminLogin(req
, res
, ctx
);
94 assert(service
.sessionManager
.postAdminLogin
.called
);
96 }); // handlerPostAdminLogin
98 describe('handlerGetAdminLogout', function () {
99 it('covers', async
function () {
100 await service
.handlerGetAdminLogout(req
, res
, ctx
);
101 assert(service
.sessionManager
.getAdminLogout
.called
);
103 }); // handlerGetAdminLogout
105 describe('handlerGetAdmin', function () {
106 it('covers authenticated', async
function () {
107 service
.authenticator
.sessionRequiredLocal
.resolves(true);
108 await service
.handlerGetAdmin(req
, res
, ctx
);
109 assert(service
.manager
.getAdmin
.called
);
111 it('covers unauthenticated', async
function () {
112 service
.authenticator
.sessionRequiredLocal
.resolves(false);
113 await service
.handlerGetAdmin(req
, res
, ctx
);
114 assert(service
.manager
.getAdmin
.notCalled
);
116 }); // handlerGetAdmin
118 describe('handlerPostAdmin', function () {
119 it('covers authenticated', async
function () {
120 service
.authenticator
.sessionRequiredLocal
.resolves(true);
121 await service
.handlerPostAdmin(req
, res
, ctx
);
122 assert(service
.manager
.postAdmin
.called
);
124 it('covers unauthenticated', async
function () {
125 service
.authenticator
.sessionRequiredLocal
.resolves(false);
126 await service
.handlerPostAdmin(req
, res
, ctx
);
127 assert(service
.manager
.getAdmin
.notCalled
);
129 }); // handlerPostAdmin
131 describe('handlerGetRoot', function () {
132 it('covers', async
function () {
133 await service
.handlerGetRoot(req
, res
, ctx
);
134 assert(service
.manager
.getRoot
.called
);
136 }); // handlerGetRoot
138 describe('handlerGetAdminTicket', function () {
139 it('covers authenticated', async
function () {
140 service
.authenticator
.sessionRequiredLocal
.resolves(true);
141 await service
.handlerGetAdminTicket(req
, res
, ctx
);
142 assert(service
.manager
.getAdminTicket
.called
);
144 it('covers unauthenticated', async
function () {
145 service
.authenticator
.sessionRequiredLocal
.resolves(false);
146 await service
.handlerGetAdminTicket(req
, res
, ctx
);
147 assert(service
.manager
.getAdminTicket
.notCalled
);
149 }); // handlerGetAdminTicket
151 describe('handlerPostAdminTicket', function () {
152 it('covers authenticated', async
function () {
153 service
.authenticator
.sessionRequiredLocal
.resolves(true);
154 await service
.handlerPostAdminTicket(req
, res
, ctx
);
155 assert(service
.manager
.postAdminTicket
.called
);
157 it('covers unauthenticated', async
function () {
158 service
.authenticator
.sessionRequiredLocal
.resolves(false);
159 await service
.handlerPostAdminTicket(req
, res
, ctx
);
160 assert(service
.manager
.postAdminTicket
.notCalled
);
162 }); // handlerPostAdminTicket
164 describe('handlerGetMeta', function () {
165 it('covers', async
function () {
166 await service
.handlerGetMeta(req
, res
, ctx
);
167 assert(service
.manager
.getMeta
.called
);
169 }); // handlerGetMeta
171 describe('handlerGetHealthcheck', function () {
172 it('covers', async
function () {
173 await service
.handlerGetHealthcheck(req
, res
, ctx
);
174 assert(service
.manager
.getHealthcheck
.called
);
176 it('cover errors', async
function () {
177 const expectedException
= 'blah';
178 service
.manager
.getHealthcheck
.rejects(expectedException
);
180 await service
.handlerGetHealthcheck(req
, res
, ctx
);
181 assert
.fail('did not get expected exception');
183 assert
.strictEqual(e
.name
, expectedException
, 'did not get expected exception');
185 assert(service
.manager
.getHealthcheck
.called
);
187 }); // handlerGetHealthcheck
189 describe('handlerInternalServerError', function () {
190 it('covers no redirect', async
function () {
191 sinon
.stub(service
.__proto__
.__proto__
, 'handlerInternalServerError');
192 await service
.handlerInternalServerError(req
, res
, ctx
);
193 assert(service
.__proto__
.__proto__
.handlerInternalServerError
.called
);
195 it('covers redirect', async
function () {
196 sinon
.stub(service
.__proto__
.__proto__
, 'handlerInternalServerError');
198 redirectUri: new URL('https://client.example.com/app'),
199 clientIdentifier: new URL('https://client.exmaple.com/'),
202 await service
.handlerInternalServerError(req
, res
, ctx
);
203 assert(!service
.__proto__
.__proto__
.handlerInternalServerError
.called
);
204 assert(res
.setHeader
);
206 }); // handlerInternalServerError
208 describe('handlerGetAuthorization', function () {
209 it('covers authenticated', async
function() {
210 service
.authenticator
.sessionRequiredLocal
.resolves(true);
211 await service
.handlerGetAuthorization(req
, res
, ctx
);
212 assert(service
.manager
.getAuthorization
.called
);
214 it('covers unauthenticated', async
function() {
215 service
.authenticator
.sessionRequiredLocal
.resolves(false);
216 await service
.handlerGetAuthorization(req
, res
, ctx
);
217 assert(service
.manager
.getAuthorization
.notCalled
);
219 }); // handlerGetAuthorization
221 describe('handlerPostAuthorization', function () {
222 it('covers', async
function () {
223 await service
.handlerPostAuthorization(req
, res
, ctx
);
224 assert(service
.manager
.postAuthorization
.called
);
226 }); // handlerPostAuthorization
228 describe('handlerPostConsent', function () {
229 it('covers', async
function () {
230 service
.serveFile
.resolves();
231 await service
.handlerPostConsent(req
, res
, ctx
);
232 assert(service
.manager
.postConsent
.called
);
234 }); // handlerPostConsent
236 describe('handlerPostToken', function () {
237 it('covers', async
function () {
238 await service
.handlerPostToken(req
, res
, ctx
);
239 assert(service
.manager
.postToken
.called
);
241 }); // handlerPostToken
243 describe('handlerPostTicket', function () {
244 it('covers', async
function () {
245 await service
.handlerPostTicket(req
, res
, ctx
);
246 assert(service
.manager
.postTicket
.called
);
248 }); // handlerPostTicket
250 describe('handlerPostIntrospection', function () {
251 it('covers', async
function () {
252 await service
.handlerPostIntrospection(req
, res
, ctx
);
253 assert(service
.manager
.postIntrospection
.called
);
255 }); // handlerPostIntrospection
257 describe('handlerPostRevocation', function () {
258 it('covers', async
function () {
259 await service
.handlerPostRevocation(req
, res
, ctx
);
260 assert(service
.manager
.postRevocation
.called
);
262 }); // handlerPostRevocation
264 describe('handlerPostUserInfo', function () {
265 it('covers', async
function () {
266 await service
.handlerPostUserInfo(req
, res
, ctx
);
267 assert(service
.manager
.postUserInfo
.called
);
269 }); // handlerPostUserInfo
271 describe('handlerGetAdminMaintenance', function () {
272 it('covers authenticated', async
function () {
273 service
.authenticator
.sessionRequiredLocal
.resolves(true);
274 await service
.handlerGetAdminMaintenance(req
, res
, ctx
);
275 assert(service
.manager
.getAdminMaintenance
.called
);
277 it('covers unauthenticated', async
function () {
278 service
.authenticator
.sessionRequiredLocal
.resolves(false);
279 await service
.handlerGetAdminMaintenance(req
, res
, ctx
);
280 assert(service
.manager
.getAdminMaintenance
.notCalled
);
282 }); // handlerGetAdminMaintenance