7a5e349df4af710a90f2e18c9ee58eeebb008db3
[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 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);
194 });
195 it('covers redirect', async function () {
196 sinon.stub(service.__proto__.__proto__, 'handlerInternalServerError');
197 ctx.session = {
198 redirectUri: new URL('https://client.example.com/app'),
199 clientIdentifier: new URL('https://client.exmaple.com/'),
200 state: '123456',
201 };
202 await service.handlerInternalServerError(req, res, ctx);
203 assert(!service.__proto__.__proto__.handlerInternalServerError.called);
204 assert(res.setHeader);
205 });
206 }); // handlerInternalServerError
207
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);
213 });
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);
218 });
219 }); // handlerGetAuthorization
220
221 describe('handlerPostAuthorization', function () {
222 it('covers', async function () {
223 await service.handlerPostAuthorization(req, res, ctx);
224 assert(service.manager.postAuthorization.called);
225 });
226 }); // handlerPostAuthorization
227
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);
233 });
234 }); // handlerPostConsent
235
236 describe('handlerPostToken', function () {
237 it('covers', async function () {
238 await service.handlerPostToken(req, res, ctx);
239 assert(service.manager.postToken.called);
240 });
241 }); // handlerPostToken
242
243 describe('handlerPostTicket', function () {
244 it('covers', async function () {
245 await service.handlerPostTicket(req, res, ctx);
246 assert(service.manager.postTicket.called);
247 });
248 }); // handlerPostTicket
249
250 describe('handlerPostIntrospection', function () {
251 it('covers', async function () {
252 await service.handlerPostIntrospection(req, res, ctx);
253 assert(service.manager.postIntrospection.called);
254 });
255 }); // handlerPostIntrospection
256
257 describe('handlerPostRevocation', function () {
258 it('covers', async function () {
259 await service.handlerPostRevocation(req, res, ctx);
260 assert(service.manager.postRevocation.called);
261 });
262 }); // handlerPostRevocation
263
264 describe('handlerPostUserInfo', function () {
265 it('covers', async function () {
266 await service.handlerPostUserInfo(req, res, ctx);
267 assert(service.manager.postUserInfo.called);
268 });
269 }); // handlerPostUserInfo
270
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);
276 });
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);
281 });
282 }); // handlerGetAdminMaintenance
283
284 });