bb93d2e7cc463efcc7b9193ba14306e9ba2ddd99
[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('handlerPostRoot', function () {
65 it('covers public mode', async function () {
66 await service.handlerPostRoot(req, res, ctx);
67 assert(service.manager.postRoot.called);
68 });
69 }); // handlerPostRoot
70
71 describe('handlerGetRoot', function () {
72 it('covers', async function () {
73 await service.handlerGetRoot(req, res, ctx);
74 assert(service.manager.getRoot.called);
75 });
76 }); // handlerGetRoot
77
78 describe('handlerGetHealthcheck', function () {
79 it('covers', async function () {
80 await service.handlerGetHealthcheck(req, res, ctx);
81 assert(service.manager.getHealthcheck.called);
82 });
83 it('cover errors', async function () {
84 const expectedException = 'blah';
85 service.manager.getHealthcheck.rejects(expectedException);
86 try {
87 await service.handlerGetHealthcheck(req, res, ctx);
88 assert.fail('did not get expected exception');
89 } catch (e) {
90 assert.strictEqual(e.name, expectedException, 'did not get expected exception');
91 }
92 assert(service.manager.getHealthcheck.called);
93 });
94 }); // handlerGetHealthcheck
95
96 describe('handlerGetInfo', function () {
97 it('covers', async function() {
98 await service.handlerGetInfo(req, res, ctx);
99 assert(service.manager.getInfo.called);
100 });
101 }); // handlerGetInfo
102
103 describe('handlerGetAdminOverview', function () {
104 it('covers', async function () {
105 await service.handlerGetAdminOverview(req, res, ctx);
106 assert(service.authenticator.required.called);
107 assert(service.manager.getAdminOverview.called);
108 })
109 }); // handlerGetAdminOverview
110
111 describe('handlerGetAdminTopicDetails', function () {
112 it('covers', async function () {
113 await service.handlerGetAdminTopicDetails(req, res, ctx);
114 assert(service.authenticator.required.called);
115 assert(service.manager.getTopicDetails.called);
116 })
117 }); // handlerGetAdminTopicDetails
118
119 describe('handlerPostAdminProcess', function () {
120 it('covers', async function () {
121 service.serveFile.resolves();
122 await service.handlerPostAdminProcess(req, res, ctx);
123 assert(service.authenticator.requiredLocal.called);
124 assert(service.manager.processTasks.called);
125 });
126 }); // handlerPostAdminProcess
127
128 describe('handlerUpdateTopic', function () {
129 it('covers', async function () {
130 sinon.stub(service, 'bodyData').resolves();
131 await service.handlerUpdateTopic(req, res, ctx);
132 assert(service.authenticator.requiredLocal.called);
133 assert(service.manager.updateTopic.called);
134 });
135 }); // handlerUpdateTopic
136
137 describe('handlerUpdateSubscription', function () {
138 it('covers', async function () {
139 sinon.stub(service, 'bodyData').resolves();
140 await service.handlerUpdateSubscription(req, res, ctx);
141 assert(service.authenticator.requiredLocal.called);
142 assert(service.manager.updateSubscription.called);
143 });
144 }); // handlerUpdateSubscription
145
146 describe('handlerGetAdminLogin', function () {
147 it('covers', async function () {
148 await service.handlerGetAdminLogin(req, res, ctx);
149 assert(service.sessionManager.getAdminLogin.called);
150 });
151 }); // handlerGetAdminLogin
152
153 describe('handlerPostAdminLogin', function () {
154 it('covers', async function () {
155 sinon.stub(service, 'bodyData').resolves();
156 await service.handlerPostAdminLogin(req, res, ctx);
157 assert(service.sessionManager.postAdminLogin.called);
158 });
159 }); // handlerPostAdminLogin
160
161 describe('handlerGetAdminLogout', function () {
162 it('covers', async function () {
163 await service.handlerGetAdminLogout(req, res, ctx);
164 assert(service.sessionManager.getAdminLogout.called);
165 });
166 }); // handlerGetAdminLogout
167
168 describe('handlerGetAdminIA', function () {
169 it('covers', async function () {
170 await service.handlerGetAdminIA(req, res, ctx);
171 assert(service.sessionManager.getAdminIA.called);
172 });
173 }); // handlerGetAdminIA
174
175 });