48f0c0bfd5311c5fc92337a67a66f2bfccd3fbce
[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
9 const stubDb = require('../stub-db');
10 const stubLogger = require('../stub-logger');
11 const Service = require('../../src/service');
12 const Config = require('../../config');
13
14
15 describe('Service', function () {
16 let service, options;
17 let req, res, ctx;
18
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();
29 req = {
30 getHeader: sinon.stub(),
31 };
32 res = {
33 setHeader: sinon.stub(),
34 write: sinon.stub(),
35 end: sinon.stub(),
36 };
37 ctx = {
38 params: {},
39 };
40 });
41
42 afterEach(function () {
43 sinon.restore();
44 });
45
46 it('instantiates', function () {
47 assert(service);
48 });
49
50 it('instantiates with config coverage', async function () {
51 options.dingus.selfBaseUrl = 'https://example.com/';
52 service = new Service(stubLogger, stubDb, options);
53 assert(service);
54 });
55
56 it('instantiates with config coverage', async function () {
57 delete options.dingus.selfBaseUrl;
58 service = new Service(stubLogger, stubDb, options);
59 assert(service);
60 });
61
62 describe('initialize', function () {
63 it('covers', async function () {
64 await service.initialize();
65 assert(service.manager.initialize.called);
66 });
67 }); // initialize
68
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);
75 });
76 }); // preHandler
77
78 describe('handlerGetAdminLogin', function () {
79 it('covers', async function () {
80 await service.handlerGetAdminLogin(req, res, ctx);
81 assert(service.sessionManager.getAdminLogin.called);
82 });
83 }); // handlerGetAdminLogin
84
85 describe('handlerPostAdminLogin', function () {
86 it('covers', async function () {
87 await service.handlerPostAdminLogin(req, res, ctx);
88 assert(service.sessionManager.postAdminLogin.called);
89 });
90 }); // handlerPostAdminLogin
91
92 describe('handlerGetAdminLogout', function () {
93 it('covers', async function () {
94 await service.handlerGetAdminLogout(req, res, ctx);
95 assert(service.sessionManager.getAdminLogout.called);
96 });
97 }); // handlerGetAdminLogout
98
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);
104 });
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);
109 });
110 }); // handlerGetAdmin
111
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);
117 });
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);
122 });
123 }); // handlerPostAdmin
124
125 describe('handlerGetRoot', function () {
126 it('covers', async function () {
127 await service.handlerGetRoot(req, res, ctx);
128 assert(service.manager.getRoot.called);
129 });
130 }); // handlerGetRoot
131
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);
137 });
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);
142 });
143 }); // handlerGetAdminTicket
144
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);
150 });
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);
155 });
156 }); // handlerPostAdminTicket
157
158 describe('handlerGetMeta', function () {
159 it('covers', async function () {
160 await service.handlerGetMeta(req, res, ctx);
161 assert(service.manager.getMeta.called);
162 });
163 }); // handlerGetMeta
164
165 describe('handlerGetHealthcheck', function () {
166 it('covers', async function () {
167 await service.handlerGetHealthcheck(req, res, ctx);
168 assert(service.manager.getHealthcheck.called);
169 });
170 it('cover errors', async function () {
171 const expectedException = 'blah';
172 service.manager.getHealthcheck.rejects(expectedException);
173 try {
174 await service.handlerGetHealthcheck(req, res, ctx);
175 assert.fail('did not get expected exception');
176 } catch (e) {
177 assert.strictEqual(e.name, expectedException, 'did not get expected exception');
178 }
179 assert(service.manager.getHealthcheck.called);
180 });
181 }); // handlerGetHealthcheck
182
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);
188 });
189 it('covers redirect', async function () {
190 sinon.stub(service.__proto__.__proto__, 'handlerInternalServerError');
191 ctx.session = {
192 redirectUri: new URL('https://client.example.com/app'),
193 clientIdentifier: new URL('https://client.exmaple.com/'),
194 state: '123456',
195 };
196 await service.handlerInternalServerError(req, res, ctx);
197 assert(!service.__proto__.__proto__.handlerInternalServerError.called);
198 assert(res.setHeader);
199 });
200 }); // handlerInternalServerError
201
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);
207 });
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);
212 });
213 }); // handlerGetAuthorization
214
215 describe('handlerPostAuthorization', function () {
216 it('covers', async function () {
217 await service.handlerPostAuthorization(req, res, ctx);
218 assert(service.manager.postAuthorization.called);
219 });
220 }); // handlerPostAuthorization
221
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);
227 });
228 }); // handlerPostConsent
229
230 describe('handlerPostToken', function () {
231 it('covers', async function () {
232 await service.handlerPostToken(req, res, ctx);
233 assert(service.manager.postToken.called);
234 });
235 }); // handlerPostToken
236
237 describe('handlerPostTicket', function () {
238 it('covers', async function () {
239 await service.handlerPostTicket(req, res, ctx);
240 assert(service.manager.postTicket.called);
241 });
242 }); // handlerPostTicket
243
244 describe('handlerPostIntrospection', function () {
245 it('covers', async function () {
246 await service.handlerPostIntrospection(req, res, ctx);
247 assert(service.manager.postIntrospection.called);
248 });
249 }); // handlerPostIntrospection
250
251 describe('handlerPostRevocation', function () {
252 it('covers', async function () {
253 await service.handlerPostRevocation(req, res, ctx);
254 assert(service.manager.postRevocation.called);
255 });
256 }); // handlerPostRevocation
257
258 describe('handlerPostUserInfo', function () {
259 it('covers', async function () {
260 await service.handlerPostUserInfo(req, res, ctx);
261 assert(service.manager.postUserInfo.called);
262 });
263 }); // handlerPostUserInfo
264
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);
270 });
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);
275 });
276 }); // handlerGetAdminMaintenance
277
278 });