keep response body out of logs during HEAD requests, by removing it from context
[websub-hub] / 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, 'setResponseType');
26 sinon.stub(service, 'serveFile');
27 sinon.stub(service, 'ingestBody').resolves();
28 req = {
29 getHeader: sinon.stub(),
30 };
31 res = {
32 setHeader: sinon.stub(),
33 write: sinon.stub(),
34 end: sinon.stub(),
35 };
36 ctx = {
37 params: {},
38 };
39 });
40
41 afterEach(function () {
42 sinon.restore();
43 });
44
45 it('instantiates', function () {
46 assert(service);
47 });
48
49 describe('maybeIngestBody', function () {
50 beforeEach(function () {
51 sinon.stub(service, 'bodyData');
52 sinon.stub(service, 'parseBody').returns();
53 });
54 it('covers no body', async function() {
55 service.bodyData.resolves();
56 await service.maybeIngestBody(req, res, ctx);
57 });
58 it('covers body', async function() {
59 service.bodyData.resolves('data');
60 await service.maybeIngestBody(req, res, ctx);
61 });
62 }); // maybeIngestBody
63
64 describe('setHeadHandler', function () {
65 it('covers', function () {
66 const origEnd = res.end;
67 sinon.stub(Service.__proto__, 'setHeadHandler');
68 ctx.responseBody = 'data';
69 req.method = 'HEAD';
70 Service.setHeadHandler(req, res, ctx);
71 res.end('foop');
72 assert(Service.__proto__.setHeadHandler.called);
73 assert(origEnd.called);
74 assert(!('responseBody' in ctx));
75 });
76 }); // setHeadHandler
77
78 describe('handlerPostRoot', function () {
79 it('covers public mode', async function () {
80 await service.handlerPostRoot(req, res, ctx);
81 assert(service.manager.postRoot.called);
82 });
83 }); // handlerPostRoot
84
85 describe('handlerGetRoot', function () {
86 it('covers', async function () {
87 await service.handlerGetRoot(req, res, ctx);
88 assert(service.manager.getRoot.called);
89 });
90 }); // handlerGetRoot
91
92 describe('handlerGetHealthcheck', function () {
93 it('covers', async function () {
94 await service.handlerGetHealthcheck(req, res, ctx);
95 assert(service.manager.getHealthcheck.called);
96 });
97 it('cover errors', async function () {
98 const expectedException = 'blah';
99 service.manager.getHealthcheck.rejects(expectedException);
100 try {
101 await service.handlerGetHealthcheck(req, res, ctx);
102 assert.fail('did not get expected exception');
103 } catch (e) {
104 assert.strictEqual(e.name, expectedException, 'did not get expected exception');
105 }
106 assert(service.manager.getHealthcheck.called);
107 });
108 }); // handlerGetHealthcheck
109
110 describe('handlerGetInfo', function () {
111 it('covers', async function() {
112 await service.handlerGetInfo(req, res, ctx);
113 assert(service.manager.getInfo.called);
114 });
115 }); // handlerGetInfo
116
117 describe('handlerGetAdminOverview', function () {
118 it('covers', async function () {
119 await service.handlerGetAdminOverview(req, res, ctx);
120 assert(service.authenticator.sessionRequired.called);
121 assert(service.manager.getAdminOverview.called);
122 })
123 }); // handlerGetAdminOverview
124
125 describe('handlerGetAdminTopicDetails', function () {
126 it('covers', async function () {
127 await service.handlerGetAdminTopicDetails(req, res, ctx);
128 assert(service.authenticator.sessionRequired.called);
129 assert(service.manager.getTopicDetails.called);
130 })
131 }); // handlerGetAdminTopicDetails
132
133 describe('handlerPostAdminProcess', function () {
134 it('covers', async function () {
135 service.serveFile.resolves();
136 await service.handlerPostAdminProcess(req, res, ctx);
137 assert(service.authenticator.apiRequiredLocal.called);
138 assert(service.manager.processTasks.called);
139 });
140 }); // handlerPostAdminProcess
141
142 describe('handlerUpdateTopic', function () {
143 it('covers', async function () {
144 sinon.stub(service, 'bodyData').resolves();
145 await service.handlerUpdateTopic(req, res, ctx);
146 assert(service.authenticator.apiRequiredLocal.called);
147 assert(service.manager.updateTopic.called);
148 });
149 }); // handlerUpdateTopic
150
151 describe('handlerUpdateSubscription', function () {
152 it('covers', async function () {
153 sinon.stub(service, 'bodyData').resolves();
154 await service.handlerUpdateSubscription(req, res, ctx);
155 assert(service.authenticator.apiRequiredLocal.called);
156 assert(service.manager.updateSubscription.called);
157 });
158 }); // handlerUpdateSubscription
159
160 describe('handlerGetAdminLogin', function () {
161 it('covers', async function () {
162 await service.handlerGetAdminLogin(req, res, ctx);
163 assert(service.sessionManager.getAdminLogin.called);
164 });
165 }); // handlerGetAdminLogin
166
167 describe('handlerPostAdminLogin', function () {
168 it('covers', async function () {
169 sinon.stub(service, 'bodyData').resolves();
170 await service.handlerPostAdminLogin(req, res, ctx);
171 assert(service.sessionManager.postAdminLogin.called);
172 });
173 }); // handlerPostAdminLogin
174
175 describe('handlerGetAdminLogout', function () {
176 it('covers', async function () {
177 await service.handlerGetAdminLogout(req, res, ctx);
178 assert(service.sessionManager.getAdminLogout.called);
179 });
180 }); // handlerGetAdminLogout
181
182 describe('handlerGetAdminIA', function () {
183 it('covers', async function () {
184 await service.handlerGetAdminIA(req, res, ctx);
185 assert(service.sessionManager.getAdminIA.called);
186 });
187 }); // handlerGetAdminIA
188
189 });