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