initial commit
[urlittler] / test / src / service.js
1 /* eslint-env mocha */
2 /* eslint-disable capitalized-comments */
3
4 'use strict';
5
6 const assert = require('assert');
7 // eslint-disable-next-line node/no-unpublished-require
8 const sinon = require('sinon');
9
10 const Service = require('../../src/service');
11 const { ServeStaticFile } = require('../../src/errors');
12
13
14 describe('service', function () {
15 let service, logger, options;
16 let req, res, ctx;
17 const dbStub = {};
18
19 beforeEach(function () {
20 options = {};
21 logger = {};
22 // logger = console;
23 service = new Service(logger, dbStub, options);
24 sinon.stub(service.manager);
25 sinon.stub(service.authenticator);
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 });
39
40 afterEach(function () {
41 sinon.restore();
42 });
43
44 it('instantiates', function () {
45 assert(service);
46 });
47
48 it('covers no options', function () {
49 const r = new Service(logger, dbStub);
50 assert(r);
51 });
52
53 describe('renderError', function () {
54 it('covers default', function () {
55 const contentType = 'text/plain';
56 const err = {
57 statusCode: 418,
58 errorMessage: 'I am a teapot',
59 details: 'what are you',
60 };
61 const result = service.renderError(contentType, err);
62 assert.strictEqual(result, 'I am a teapot\r\nwhat are you');
63 });
64 }); // renderError
65
66 describe('parseBody', function () {
67 it('covers default', function () {
68 const contentType = 'application/json';
69 const someData = { foo: 'bar', quux: 3 };
70 ctx.rawBody = JSON.stringify(someData);
71 service.parseBody(contentType, ctx);
72 assert.deepStrictEqual(ctx.parsedBody, someData);
73 });
74 }); // parseBody
75
76 describe('preHandler', function () {
77 it('covers default', async function () {
78 await service.preHandler(req, res, ctx);
79 });
80 }); // preHandler
81
82 describe('_endHandler', function () {
83 beforeEach(function () {
84 req = {};
85 res = {};
86 ctx = {};
87 });
88 it('covers', function() {
89 service._endHandler(req, res, ctx);
90 assert(service.authenticator.signResponse.called);
91 });
92 }); // _endHandler
93
94 describe('handlerPostRoot', function () {
95 it('covers public mode', async function () {
96 sinon.stub(service, '_postRootAuth');
97
98 await service.handlerPostRoot(req, res, ctx);
99 assert(service.manager.postRoot.called);
100 });
101 it('covers private mode', async function () {
102 service = new Service(logger, dbStub, { createRequiresAuth: true });
103 sinon.stub(service.manager);
104 sinon.stub(service.authenticator);
105 sinon.stub(service, '_postRootAuth');
106 sinon.stub(service, 'setResponseType');
107 sinon.stub(service, 'serveFile');
108 sinon.stub(service, 'ingestBody').resolves();
109
110 await service.handlerPostRoot(req, res, ctx);
111 assert(service.manager.postRoot.called);
112 });
113 }); // handlerPostRoot
114
115 describe('handlerGetRoot', function () {
116 it('covers', async function () {
117 await service.handlerGetRoot(req, res, ctx);
118 assert(service.manager.getRoot.called);
119 });
120 }); // handlerGetRoot
121
122 describe('handlerGetId', function () {
123 it('covers', async function () {
124 await service.handlerGetId(req, res, ctx);
125 assert(service.manager.getById.called);
126 });
127 it('covers static', async function () {
128 service.manager.getById.rejects(new ServeStaticFile('this_file.txt'));
129 await service.handlerGetId(req, res, ctx);
130 assert(service.manager.getById.called);
131 assert(service.serveFile.called);
132 });
133 it('cover errors', async function () {
134 const expectedException = 'blah';
135 service.manager.getById.rejects(expectedException);
136 try {
137 await service.handlerGetId(req, res, ctx);
138 assert.fail('did not get expected exception');
139 } catch (e) {
140 assert.strictEqual(e.name, expectedException, 'did not get expected exception');
141 }
142 assert(service.manager.getById.called);
143 });
144 }); // handlerGetId
145
146 describe('handlerDeleteId', function () {
147 it('covers', async function () {
148 await service.handlerDeleteId(req, res, ctx);
149 assert(service.manager.deleteById.called);
150 });
151 }); // handlerDeleteId
152
153 describe('handlerPutId', function () {
154 it('covers', async function () {
155 await service.handlerPutId(req, res, ctx);
156 assert(service.manager.putById.called);
157 });
158 }); // handlerPutId
159
160 describe('handlerGetIdInfo', function () {
161 it('covers', async function () {
162 await service.handlerGetIdInfo(req, res, ctx);
163 assert(service.manager.getByIdInfo.called);
164 });
165 }); // handlerGetIdInfo
166
167 describe('handlerGetStatic', function () {
168 it('covers', async function () {
169 ctx.params = {
170 file: '',
171 };
172 await service.handlerGetStatic(req, res, ctx);
173 assert(service.serveFile.called);
174 });
175 }); // handlerGetStatic
176
177 describe('handlerGetAdminReport', function () {
178 it('covers', async function () {
179 await service.handlerGetAdminReport(req, res, ctx);
180 assert(service.authenticator.required.called);
181 assert(service.manager.getAdminReport.called);
182 });
183 });
184
185 });