2 /* eslint-disable capitalized-comments */
6 const assert
= require('assert');
7 // eslint-disable-next-line node/no-unpublished-require
8 const sinon
= require('sinon');
10 const Service
= require('../../src/service');
11 const { ServeStaticFile
} = require('../../src/errors');
14 describe('service', function () {
15 let service
, logger
, options
;
19 beforeEach(function () {
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();
30 getHeader: sinon
.stub(),
33 setHeader: sinon
.stub(),
40 afterEach(function () {
44 it('instantiates', function () {
48 it('covers no options', function () {
49 const r
= new Service(logger
, dbStub
);
53 describe('renderError', function () {
54 it('covers default', function () {
55 const contentType
= 'text/plain';
58 errorMessage: 'I am a teapot',
59 details: 'what are you',
61 const result
= service
.renderError(contentType
, err
);
62 assert
.strictEqual(result
, 'I am a teapot\r\nwhat are you');
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
);
76 describe('preHandler', function () {
77 it('covers default', async
function () {
78 await service
.preHandler(req
, res
, ctx
);
82 describe('_endHandler', function () {
83 beforeEach(function () {
88 it('covers', function() {
89 service
._endHandler(req
, res
, ctx
);
90 assert(service
.authenticator
.signResponse
.called
);
94 describe('handlerPostRoot', function () {
95 it('covers public mode', async
function () {
96 sinon
.stub(service
, '_postRootAuth');
98 await service
.handlerPostRoot(req
, res
, ctx
);
99 assert(service
.manager
.postRoot
.called
);
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();
110 await service
.handlerPostRoot(req
, res
, ctx
);
111 assert(service
.manager
.postRoot
.called
);
113 }); // handlerPostRoot
115 describe('handlerGetRoot', function () {
116 it('covers', async
function () {
117 await service
.handlerGetRoot(req
, res
, ctx
);
118 assert(service
.manager
.getRoot
.called
);
120 }); // handlerGetRoot
122 describe('handlerGetId', function () {
123 it('covers', async
function () {
124 await service
.handlerGetId(req
, res
, ctx
);
125 assert(service
.manager
.getById
.called
);
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
);
133 it('cover errors', async
function () {
134 const expectedException
= 'blah';
135 service
.manager
.getById
.rejects(expectedException
);
137 await service
.handlerGetId(req
, res
, ctx
);
138 assert
.fail('did not get expected exception');
140 assert
.strictEqual(e
.name
, expectedException
, 'did not get expected exception');
142 assert(service
.manager
.getById
.called
);
146 describe('handlerDeleteId', function () {
147 it('covers', async
function () {
148 await service
.handlerDeleteId(req
, res
, ctx
);
149 assert(service
.manager
.deleteById
.called
);
151 }); // handlerDeleteId
153 describe('handlerPutId', function () {
154 it('covers', async
function () {
155 await service
.handlerPutId(req
, res
, ctx
);
156 assert(service
.manager
.putById
.called
);
160 describe('handlerGetIdInfo', function () {
161 it('covers', async
function () {
162 await service
.handlerGetIdInfo(req
, res
, ctx
);
163 assert(service
.manager
.getByIdInfo
.called
);
165 }); // handlerGetIdInfo
167 describe('handlerGetStatic', function () {
168 it('covers', async
function () {
172 await service
.handlerGetStatic(req
, res
, ctx
);
173 assert(service
.serveFile
.called
);
175 }); // handlerGetStatic
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
);