d361df120b18b9891406f1bb7fe4e668f9124d75
[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, options, asyncLocalStorage;
18 let req, res, ctx;
19
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();
31 req = {
32 getHeader: sinon.stub(),
33 };
34 res = {
35 setHeader: sinon.stub(),
36 write: sinon.stub(),
37 end: sinon.stub(),
38 };
39 ctx = {
40 params: {},
41 };
42 });
43
44 afterEach(function () {
45 sinon.restore();
46 });
47
48 it('instantiates', function () {
49 assert(service);
50 });
51
52 it('instantiates with config coverage', async function () {
53 options.dingus.selfBaseUrl = 'https://example.com/';
54 service = new Service(stubLogger, stubDb, options);
55 assert(service);
56 });
57
58 it('instantiates with config coverage', async function () {
59 delete options.dingus.selfBaseUrl;
60 service = new Service(stubLogger, stubDb, options);
61 assert(service);
62 });
63
64 describe('initialize', function () {
65 it('covers', async function () {
66 await service.initialize();
67 assert(service.manager.initialize.called);
68 });
69 }); // initialize
70
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);
79 });
80 assert.strictEqual(ctx.url, req.url);
81 });
82 }); // preHandler
83
84 describe('handlerGetAdminLogin', function () {
85 it('covers', async function () {
86 await service.handlerGetAdminLogin(req, res, ctx);
87 assert(service.sessionManager.getAdminLogin.called);
88 });
89 }); // handlerGetAdminLogin
90
91 describe('handlerPostAdminLogin', function () {
92 it('covers', async function () {
93 await service.handlerPostAdminLogin(req, res, ctx);
94 assert(service.sessionManager.postAdminLogin.called);
95 });
96 }); // handlerPostAdminLogin
97
98 describe('handlerGetAdminLogout', function () {
99 it('covers', async function () {
100 await service.handlerGetAdminLogout(req, res, ctx);
101 assert(service.sessionManager.getAdminLogout.called);
102 });
103 }); // handlerGetAdminLogout
104
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);
110 });
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);
115 });
116 }); // handlerGetAdmin
117
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);
123 });
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);
128 });
129 }); // handlerPostAdmin
130
131 describe('handlerGetRoot', function () {
132 it('covers', async function () {
133 await service.handlerGetRoot(req, res, ctx);
134 assert(service.manager.getRoot.called);
135 });
136 }); // handlerGetRoot
137
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);
143 });
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);
148 });
149 }); // handlerGetAdminTicket
150
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);
156 });
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);
161 });
162 }); // handlerPostAdminTicket
163
164 describe('handlerGetMeta', function () {
165 it('covers', async function () {
166 await service.handlerGetMeta(req, res, ctx);
167 assert(service.manager.getMeta.called);
168 });
169 }); // handlerGetMeta
170
171 describe('handlerGetHealthcheck', function () {
172 it('covers', async function () {
173 await service.handlerGetHealthcheck(req, res, ctx);
174 assert(service.manager.getHealthcheck.called);
175 });
176 it('cover errors', async function () {
177 const expectedException = 'blah';
178 service.manager.getHealthcheck.rejects(expectedException);
179 try {
180 await service.handlerGetHealthcheck(req, res, ctx);
181 assert.fail('did not get expected exception');
182 } catch (e) {
183 assert.strictEqual(e.name, expectedException, 'did not get expected exception');
184 }
185 assert(service.manager.getHealthcheck.called);
186 });
187 }); // handlerGetHealthcheck
188
189 describe('handlerInternalServerError', function () {
190 let ServiceClass, DingusClass;
191 before(function () {
192 ServiceClass = Object.getPrototypeOf(service);
193 DingusClass = Object.getPrototypeOf(ServiceClass);
194 });
195 it('covers no redirect', async function () {
196 sinon.stub(DingusClass, 'handlerInternalServerError');
197 await service.handlerInternalServerError(req, res, ctx);
198 assert(DingusClass.handlerInternalServerError.called);
199 });
200 it('covers redirect', async function () {
201 sinon.stub(DingusClass, 'handlerInternalServerError');
202 ctx.session = {
203 redirectUri: new URL('https://client.example.com/app'),
204 clientIdentifier: new URL('https://client.exmaple.com/'),
205 state: '123456',
206 };
207 await service.handlerInternalServerError(req, res, ctx);
208 assert(!DingusClass.handlerInternalServerError.called);
209 assert(res.setHeader.called);
210 assert.strictEqual(res.statusCode, 302);
211 });
212 }); // handlerInternalServerError
213
214 describe('handlerGetAuthorization', function () {
215 it('covers authenticated', async function() {
216 service.authenticator.sessionRequiredLocal.resolves(true);
217 await service.handlerGetAuthorization(req, res, ctx);
218 assert(service.manager.getAuthorization.called);
219 });
220 it('covers unauthenticated', async function() {
221 service.authenticator.sessionRequiredLocal.resolves(false);
222 await service.handlerGetAuthorization(req, res, ctx);
223 assert(service.manager.getAuthorization.notCalled);
224 });
225 }); // handlerGetAuthorization
226
227 describe('handlerPostAuthorization', function () {
228 it('covers', async function () {
229 await service.handlerPostAuthorization(req, res, ctx);
230 assert(service.manager.postAuthorization.called);
231 });
232 }); // handlerPostAuthorization
233
234 describe('handlerPostConsent', function () {
235 it('covers', async function () {
236 service.serveFile.resolves();
237 await service.handlerPostConsent(req, res, ctx);
238 assert(service.manager.postConsent.called);
239 });
240 }); // handlerPostConsent
241
242 describe('handlerPostToken', function () {
243 it('covers', async function () {
244 await service.handlerPostToken(req, res, ctx);
245 assert(service.manager.postToken.called);
246 });
247 }); // handlerPostToken
248
249 describe('handlerPostTicket', function () {
250 it('covers', async function () {
251 await service.handlerPostTicket(req, res, ctx);
252 assert(service.manager.postTicket.called);
253 });
254 }); // handlerPostTicket
255
256 describe('handlerPostIntrospection', function () {
257 it('covers', async function () {
258 await service.handlerPostIntrospection(req, res, ctx);
259 assert(service.manager.postIntrospection.called);
260 });
261 }); // handlerPostIntrospection
262
263 describe('handlerPostRevocation', function () {
264 it('covers', async function () {
265 await service.handlerPostRevocation(req, res, ctx);
266 assert(service.manager.postRevocation.called);
267 });
268 }); // handlerPostRevocation
269
270 describe('handlerPostUserInfo', function () {
271 it('covers', async function () {
272 await service.handlerPostUserInfo(req, res, ctx);
273 assert(service.manager.postUserInfo.called);
274 });
275 }); // handlerPostUserInfo
276
277 describe('handlerGetAdminMaintenance', function () {
278 it('covers authenticated', async function () {
279 service.authenticator.sessionRequiredLocal.resolves(true);
280 await service.handlerGetAdminMaintenance(req, res, ctx);
281 assert(service.manager.getAdminMaintenance.called);
282 });
283 it('covers unauthenticated', async function () {
284 service.authenticator.sessionRequiredLocal.resolves(false);
285 await service.handlerGetAdminMaintenance(req, res, ctx);
286 assert(service.manager.getAdminMaintenance.notCalled);
287 });
288 }); // handlerGetAdminMaintenance
289
290 });