Merge branch 'v1.3-dev'
[websub-hub] / 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.authenticator);
24 sinon.stub(service, 'setResponseType');
25 sinon.stub(service, 'serveFile');
26 sinon.stub(service, 'ingestBody').resolves();
27 req = {
28 getHeader: sinon.stub(),
29 };
30 res = {
31 setHeader: sinon.stub(),
32 write: sinon.stub(),
33 end: sinon.stub(),
34 };
35 ctx = {
36 params: {},
37 };
38 });
39
40 afterEach(function () {
41 sinon.restore();
42 });
43
44 it('instantiates', function () {
45 assert(service);
46 });
47
48 describe('maybeIngestBody', function () {
49 beforeEach(function () {
50 sinon.stub(service, 'bodyData');
51 sinon.stub(service, 'parseBody').returns();
52 });
53 it('covers no body', async function() {
54 service.bodyData.resolves();
55 await service.maybeIngestBody(req, res, ctx);
56 });
57 it('covers body', async function() {
58 service.bodyData.resolves('data');
59 await service.maybeIngestBody(req, res, ctx);
60 });
61 }); // maybeIngestBody
62
63 describe('handlerRedirect', function () {
64 it('covers', async function () {
65 await service.handlerRedirect(req, res, ctx, '/');
66 assert(res.end.called);
67 assert.strictEqual(res.statusCode, 307);
68 });
69 }); // handlerRedirect
70
71 describe('handlerPostRoot', function () {
72 it('covers public mode', async function () {
73 await service.handlerPostRoot(req, res, ctx);
74 assert(service.manager.postRoot.called);
75 });
76 }); // handlerPostRoot
77
78 describe('handlerGetRoot', function () {
79 it('covers', async function () {
80 await service.handlerGetRoot(req, res, ctx);
81 assert(service.manager.getRoot.called);
82 });
83 }); // handlerGetRoot
84
85 describe('handlerGetHealthcheck', function () {
86 it('covers', async function () {
87 await service.handlerGetHealthcheck(req, res, ctx);
88 assert(service.manager.getHealthcheck.called);
89 });
90 it('cover errors', async function () {
91 const expectedException = 'blah';
92 service.manager.getHealthcheck.rejects(expectedException);
93 try {
94 await service.handlerGetHealthcheck(req, res, ctx);
95 assert.fail('did not get expected exception');
96 } catch (e) {
97 assert.strictEqual(e.name, expectedException, 'did not get expected exception');
98 }
99 assert(service.manager.getHealthcheck.called);
100 });
101 }); // handlerGetHealthcheck
102
103 describe('handlerGetInfo', function () {
104 it('covers', async function() {
105 await service.handlerGetInfo(req, res, ctx);
106 assert(service.manager.getInfo.called);
107 });
108 }); // handlerGetInfo
109
110 describe('handlerGetAdminOverview', function () {
111 it('covers', async function () {
112 await service.handlerGetAdminOverview(req, res, ctx);
113 assert(service.authenticator.required.called);
114 assert(service.manager.getAdminOverview.called);
115 })
116 }); // handlerGetAdminOverview
117
118 describe('handlerGetAdminTopicDetails', function () {
119 it('covers', async function () {
120 await service.handlerGetAdminTopicDetails(req, res, ctx);
121 assert(service.authenticator.required.called);
122 assert(service.manager.getTopicDetails.called);
123 })
124 }); // handlerGetAdminTopicDetails
125
126 describe('handlerGetStaticFile', function () {
127 it('covers', async function () {
128 service.serveFile.resolves();
129 await service.handlerGetStaticFile(req, res, ctx);
130 assert(service.serveFile.called);
131 });
132 }); // handlerGetStaticFile
133
134 describe('handlerPostAdminProcess', function () {
135 it('covers', async function () {
136 service.serveFile.resolves();
137 await service.handlerPostAdminProcess(req, res, ctx);
138 assert(service.authenticator.required.called);
139 assert(service.manager.processTasks.called);
140 });
141 }); // handlerPostAdminProcess
142
143 describe('handlerUpdateTopic', function () {
144 it('covers', async function () {
145 sinon.stub(service, 'bodyData').resolves();
146 await service.handlerUpdateTopic(req, res, ctx);
147 assert(service.authenticator.required.called);
148 assert(service.manager.updateTopic.called);
149 });
150 }); // handlerUpdateTopic
151
152 describe('handlerUpdateSubscription', function () {
153 it('covers', async function () {
154 sinon.stub(service, 'bodyData').resolves();
155 await service.handlerUpdateSubscription(req, res, ctx);
156 assert(service.authenticator.required.called);
157 assert(service.manager.updateSubscription.called);
158 });
159 }); // handlerUpdateSubscription
160
161 });