ab6646f19ae9ecccb817388cb8bbb71a18396e82
[squeep-indie-auther] / test / src / service.js
1 /* eslint-env mocha */
2 /* eslint-disable capitalized-comments */
3
4 'use strict';
5
6 const assert = require('assert');
7 const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require
8 const { AsyncLocalStorage } = require('node:async_hooks');
9
10 const StubDb = require('../stub-db');
11 const StubLogger = require('../stub-logger');
12 const Service = require('../../src/service');
13 const Config = require('../../config');
14
15
16 describe('Service', function () {
17 let service, stubLogger, stubDb, options, asyncLocalStorage;
18 let req, res, ctx;
19
20 beforeEach(function () {
21 asyncLocalStorage = new AsyncLocalStorage();
22 options = new Config('test');
23 stubDb = new StubDb();
24 stubDb._reset();
25 stubLogger = new StubLogger();
26 stubLogger._reset();
27 service = new Service(stubLogger, stubDb, options, asyncLocalStorage);
28 sinon.stub(service.manager);
29 sinon.stub(service.sessionManager);
30 sinon.stub(service.authenticator);
31 sinon.stub(service.resourceAuthenticator);
32 sinon.stub(service, 'setResponseType');
33 sinon.stub(service, 'serveFile');
34 sinon.stub(service, 'ingestBody').resolves();
35 req = {
36 getHeader: sinon.stub(),
37 };
38 res = {
39 setHeader: sinon.stub(),
40 write: sinon.stub(),
41 end: sinon.stub(),
42 };
43 ctx = {
44 params: {},
45 };
46 });
47
48 afterEach(function () {
49 sinon.restore();
50 });
51
52 it('instantiates', function () {
53 assert(service);
54 });
55
56 it('instantiates with config coverage', async function () {
57 options.dingus.selfBaseUrl = 'https://example.com/';
58 service = new Service(stubLogger, stubDb, options);
59 assert(service);
60 });
61
62 it('instantiates with config coverage', async function () {
63 delete options.dingus.selfBaseUrl;
64 service = new Service(stubLogger, stubDb, options);
65 assert(service);
66 });
67
68 describe('initialize', function () {
69 it('covers', async function () {
70 await service.initialize();
71 assert(service.manager.initialize.called);
72 });
73 }); // initialize
74
75 describe('preHandler', async function () {
76 it('persists url into context', async function () {
77 req.url = 'https://example.com/foo';
78 sinon.stub(service.__proto__.__proto__, 'preHandler').resolves();
79 await service.asyncLocalStorage.run({}, async () => {
80 await service.preHandler(req, res, ctx);
81 const logObject = service.asyncLocalStorage.getStore();
82 assert('requestId' in logObject);
83 });
84 assert.strictEqual(ctx.url, req.url);
85 });
86 }); // preHandler
87
88 describe('handlerGetAdminLogin', function () {
89 it('covers', async function () {
90 await service.handlerGetAdminLogin(req, res, ctx);
91 assert(service.sessionManager.getAdminLogin.called);
92 });
93 }); // handlerGetAdminLogin
94
95 describe('handlerPostAdminLogin', function () {
96 it('covers', async function () {
97 await service.handlerPostAdminLogin(req, res, ctx);
98 assert(service.sessionManager.postAdminLogin.called);
99 });
100 }); // handlerPostAdminLogin
101
102 describe('handlerGetAdminLogout', function () {
103 it('covers', async function () {
104 await service.handlerGetAdminLogout(req, res, ctx);
105 assert(service.sessionManager.getAdminLogout.called);
106 });
107 }); // handlerGetAdminLogout
108
109 describe('handlerGetAdmin', function () {
110 it('covers authenticated', async function () {
111 service.authenticator.sessionRequiredLocal.resolves(true);
112 await service.handlerGetAdmin(req, res, ctx);
113 assert(service.manager.getAdmin.called);
114 });
115 it('covers unauthenticated', async function () {
116 service.authenticator.sessionRequiredLocal.resolves(false);
117 await service.handlerGetAdmin(req, res, ctx);
118 assert(service.manager.getAdmin.notCalled);
119 });
120 }); // handlerGetAdmin
121
122 describe('handlerPostAdmin', function () {
123 it('covers authenticated', async function () {
124 service.authenticator.sessionRequiredLocal.resolves(true);
125 await service.handlerPostAdmin(req, res, ctx);
126 assert(service.manager.postAdmin.called);
127 });
128 it('covers unauthenticated', async function () {
129 service.authenticator.sessionRequiredLocal.resolves(false);
130 await service.handlerPostAdmin(req, res, ctx);
131 assert(service.manager.getAdmin.notCalled);
132 });
133 }); // handlerPostAdmin
134
135 describe('handlerGetRoot', function () {
136 it('covers', async function () {
137 await service.handlerGetRoot(req, res, ctx);
138 assert(service.manager.getRoot.called);
139 });
140 }); // handlerGetRoot
141
142 describe('handlerGetAdminTicket', function () {
143 it('covers authenticated', async function () {
144 service.authenticator.sessionRequiredLocal.resolves(true);
145 await service.handlerGetAdminTicket(req, res, ctx);
146 assert(service.manager.getAdminTicket.called);
147 });
148 it('covers unauthenticated', async function () {
149 service.authenticator.sessionRequiredLocal.resolves(false);
150 await service.handlerGetAdminTicket(req, res, ctx);
151 assert(service.manager.getAdminTicket.notCalled);
152 });
153 }); // handlerGetAdminTicket
154
155 describe('handlerPostAdminTicket', function () {
156 it('covers authenticated', async function () {
157 service.authenticator.sessionRequiredLocal.resolves(true);
158 await service.handlerPostAdminTicket(req, res, ctx);
159 assert(service.manager.postAdminTicket.called);
160 });
161 it('covers unauthenticated', async function () {
162 service.authenticator.sessionRequiredLocal.resolves(false);
163 await service.handlerPostAdminTicket(req, res, ctx);
164 assert(service.manager.postAdminTicket.notCalled);
165 });
166 }); // handlerPostAdminTicket
167
168 describe('handlerGetMeta', function () {
169 it('covers', async function () {
170 await service.handlerGetMeta(req, res, ctx);
171 assert(service.manager.getMeta.called);
172 });
173 }); // handlerGetMeta
174
175 describe('handlerGetHealthcheck', function () {
176 it('covers', async function () {
177 await service.handlerGetHealthcheck(req, res, ctx);
178 assert(service.manager.getHealthcheck.called);
179 });
180 it('cover errors', async function () {
181 const expectedException = 'blah';
182 service.manager.getHealthcheck.rejects(expectedException);
183 try {
184 await service.handlerGetHealthcheck(req, res, ctx);
185 assert.fail('did not get expected exception');
186 } catch (e) {
187 assert.strictEqual(e.name, expectedException, 'did not get expected exception');
188 }
189 assert(service.manager.getHealthcheck.called);
190 });
191 }); // handlerGetHealthcheck
192
193 describe('handlerInternalServerError', function () {
194 let ServiceClass, DingusClass;
195 before(function () {
196 ServiceClass = Object.getPrototypeOf(service);
197 DingusClass = Object.getPrototypeOf(ServiceClass);
198 });
199 it('covers no redirect', async function () {
200 sinon.stub(DingusClass, 'handlerInternalServerError');
201 await service.handlerInternalServerError(req, res, ctx);
202 assert(DingusClass.handlerInternalServerError.called);
203 });
204 it('covers redirect', async function () {
205 sinon.stub(DingusClass, 'handlerInternalServerError');
206 ctx.session = {
207 redirectUri: new URL('https://client.example.com/app'),
208 clientIdentifier: new URL('https://client.exmaple.com/'),
209 state: '123456',
210 };
211 await service.handlerInternalServerError(req, res, ctx);
212 assert(!DingusClass.handlerInternalServerError.called);
213 assert(res.setHeader.called);
214 assert.strictEqual(res.statusCode, 302);
215 });
216 }); // handlerInternalServerError
217
218 describe('handlerGetAuthorization', function () {
219 it('covers authenticated', async function() {
220 service.authenticator.sessionRequiredLocal.resolves(true);
221 await service.handlerGetAuthorization(req, res, ctx);
222 assert(service.manager.getAuthorization.called);
223 });
224 it('covers unauthenticated', async function() {
225 service.authenticator.sessionRequiredLocal.resolves(false);
226 await service.handlerGetAuthorization(req, res, ctx);
227 assert(service.manager.getAuthorization.notCalled);
228 });
229 }); // handlerGetAuthorization
230
231 describe('handlerPostAuthorization', function () {
232 it('covers', async function () {
233 await service.handlerPostAuthorization(req, res, ctx);
234 assert(service.manager.postAuthorization.called);
235 });
236 }); // handlerPostAuthorization
237
238 describe('handlerPostConsent', function () {
239 it('covers', async function () {
240 service.serveFile.resolves();
241 await service.handlerPostConsent(req, res, ctx);
242 assert(service.manager.postConsent.called);
243 });
244 }); // handlerPostConsent
245
246 describe('handlerPostToken', function () {
247 it('covers', async function () {
248 await service.handlerPostToken(req, res, ctx);
249 assert(service.manager.postToken.called);
250 });
251 }); // handlerPostToken
252
253 describe('handlerPostTicket', function () {
254 it('covers', async function () {
255 await service.handlerPostTicket(req, res, ctx);
256 assert(service.manager.postTicket.called);
257 });
258 }); // handlerPostTicket
259
260 describe('handlerPostIntrospection', function () {
261 it('covers', async function () {
262 await service.handlerPostIntrospection(req, res, ctx);
263 assert(service.manager.postIntrospection.called);
264 });
265 }); // handlerPostIntrospection
266
267 describe('handlerPostRevocation', function () {
268 it('covers', async function () {
269 await service.handlerPostRevocation(req, res, ctx);
270 assert(service.manager.postRevocation.called);
271 });
272 }); // handlerPostRevocation
273
274 describe('handlerPostUserInfo', function () {
275 it('covers', async function () {
276 await service.handlerPostUserInfo(req, res, ctx);
277 assert(service.manager.postUserInfo.called);
278 });
279 }); // handlerPostUserInfo
280
281 describe('handlerGetAdminMaintenance', function () {
282 it('covers authenticated', async function () {
283 service.authenticator.sessionRequiredLocal.resolves(true);
284 await service.handlerGetAdminMaintenance(req, res, ctx);
285 assert(service.manager.getAdminMaintenance.called);
286 });
287 it('covers unauthenticated', async function () {
288 service.authenticator.sessionRequiredLocal.resolves(false);
289 await service.handlerGetAdminMaintenance(req, res, ctx);
290 assert(service.manager.getAdminMaintenance.notCalled);
291 });
292 }); // handlerGetAdminMaintenance
293
294 describe('handlerWhaGwan', function () {
295 it('covers', async function () {
296 await assert.rejects(() => service.handlerWhaGwan(req. res, ctx));
297 });
298 }); // handlerWhaGwan
299
300 });