3 const { StubLogger
} = require('@squeep/test-helper');
4 const assert
= require('node:assert');
5 const sinon
= require('sinon');
7 const Service
= require('../../src/service');
8 const { ServeStaticFile
} = require('../../src/errors');
11 describe('service', function () {
12 let service
, logger
, options
, asyncLocalStorage
;
16 beforeEach(function () {
18 logger
= new StubLogger(sinon
);
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();
29 getHeader: sinon
.stub(),
32 setHeader: sinon
.stub(),
39 afterEach(function () {
43 it('instantiates', function () {
47 it('covers no options', function () {
48 const r
= new Service(logger
, dbStub
);
52 describe('renderError', function () {
53 it('covers default', function () {
54 const contentType
= 'text/plain';
57 errorMessage: 'I am a teapot',
58 details: 'what are you',
60 const result
= service
.renderError(contentType
, err
);
61 assert
.strictEqual(result
, 'I am a teapot\r\nwhat are you');
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
);
75 describe('preHandler', function () {
76 it('covers default', async
function () {
77 await service
.preHandler(req
, res
, ctx
);
81 describe('_endHandler', function () {
82 beforeEach(function () {
87 it('covers', function() {
88 service
._endHandler(req
, res
, ctx
);
89 assert(service
.authenticator
.signResponse
.called
);
93 describe('handlerPostRoot', function () {
94 it('covers public mode', async
function () {
95 sinon
.stub(service
, '_postRootAuth');
97 await service
.handlerPostRoot(req
, res
, ctx
);
98 assert(service
.manager
.postRoot
.called
);
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();
109 await service
.handlerPostRoot(req
, res
, ctx
);
110 assert(service
.manager
.postRoot
.called
);
112 }); // handlerPostRoot
114 describe('handlerGetRoot', function () {
115 it('covers', async
function () {
116 await service
.handlerGetRoot(req
, res
, ctx
);
117 assert(service
.manager
.getRoot
.called
);
119 }); // handlerGetRoot
121 describe('handlerGetId', function () {
122 it('covers', async
function () {
123 await service
.handlerGetId(req
, res
, ctx
);
124 assert(service
.manager
.getById
.called
);
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
);
132 it('cover errors', async
function () {
133 const expectedException
= 'blah';
134 service
.manager
.getById
.rejects(expectedException
);
136 await service
.handlerGetId(req
, res
, ctx
);
137 assert
.fail('did not get expected exception');
139 assert
.strictEqual(e
.name
, expectedException
, 'did not get expected exception');
141 assert(service
.manager
.getById
.called
);
145 describe('handlerDeleteId', function () {
146 it('covers', async
function () {
147 await service
.handlerDeleteId(req
, res
, ctx
);
148 assert(service
.manager
.deleteById
.called
);
150 }); // handlerDeleteId
152 describe('handlerPutId', function () {
153 it('covers', async
function () {
154 await service
.handlerPutId(req
, res
, ctx
);
155 assert(service
.manager
.putById
.called
);
159 describe('handlerGetIdInfo', function () {
160 it('covers', async
function () {
161 await service
.handlerGetIdInfo(req
, res
, ctx
);
162 assert(service
.manager
.getByIdInfo
.called
);
164 }); // handlerGetIdInfo
166 describe('handlerGetStatic', function () {
167 it('covers', async
function () {
171 await service
.handlerGetStatic(req
, res
, ctx
);
172 assert(service
.serveFile
.called
);
174 }); // handlerGetStatic
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
);