X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Fsrc%2Fservice.js;h=31365794992b578cbb33b687ac9848d831fd1d6a;hb=e7436419933940f90769c8be1d63acccdd947b92;hp=48f0c0bfd5311c5fc92337a67a66f2bfccd3fbce;hpb=b0103b0d496262c438b40bc20304081dbfe41e73;p=squeep-indie-auther diff --git a/test/src/service.js b/test/src/service.js index 48f0c0b..3136579 100644 --- a/test/src/service.js +++ b/test/src/service.js @@ -5,20 +5,26 @@ const assert = require('assert'); const sinon = require('sinon'); // eslint-disable-line node/no-unpublished-require +const { AsyncLocalStorage } = require('node:async_hooks'); -const stubDb = require('../stub-db'); -const stubLogger = require('../stub-logger'); +const StubDb = require('../stub-db'); +const StubLogger = require('../stub-logger'); const Service = require('../../src/service'); const Config = require('../../config'); describe('Service', function () { - let service, options; + let service, stubLogger, stubDb, options, asyncLocalStorage; let req, res, ctx; beforeEach(function () { + asyncLocalStorage = new AsyncLocalStorage(); options = new Config('test'); - service = new Service(stubLogger, stubDb, options); + stubDb = new StubDb(); + stubDb._reset(); + stubLogger = new StubLogger(); + stubLogger._reset(); + service = new Service(stubLogger, stubDb, options, asyncLocalStorage); sinon.stub(service.manager); sinon.stub(service.sessionManager); sinon.stub(service.authenticator); @@ -66,11 +72,15 @@ describe('Service', function () { }); }); // initialize - describe('preHandler', function () { + describe('preHandler', async function () { it('persists url into context', async function () { req.url = 'https://example.com/foo'; sinon.stub(service.__proto__.__proto__, 'preHandler').resolves(); - await service.preHandler(req, res, ctx); + await service.asyncLocalStorage.run({}, async () => { + await service.preHandler(req, res, ctx); + const logObject = service.asyncLocalStorage.getStore(); + assert('requestId' in logObject); + }); assert.strictEqual(ctx.url, req.url); }); }); // preHandler @@ -181,21 +191,27 @@ describe('Service', function () { }); // handlerGetHealthcheck describe('handlerInternalServerError', function () { + let ServiceClass, DingusClass; + before(function () { + ServiceClass = Object.getPrototypeOf(service); + DingusClass = Object.getPrototypeOf(ServiceClass); + }); it('covers no redirect', async function () { - sinon.stub(service.__proto__.__proto__, 'handlerInternalServerError'); + sinon.stub(DingusClass, 'handlerInternalServerError'); await service.handlerInternalServerError(req, res, ctx); - assert(service.__proto__.__proto__.handlerInternalServerError.called); + assert(DingusClass.handlerInternalServerError.called); }); it('covers redirect', async function () { - sinon.stub(service.__proto__.__proto__, 'handlerInternalServerError'); + sinon.stub(DingusClass, 'handlerInternalServerError'); ctx.session = { redirectUri: new URL('https://client.example.com/app'), clientIdentifier: new URL('https://client.exmaple.com/'), state: '123456', }; await service.handlerInternalServerError(req, res, ctx); - assert(!service.__proto__.__proto__.handlerInternalServerError.called); - assert(res.setHeader); + assert(!DingusClass.handlerInternalServerError.called); + assert(res.setHeader.called); + assert.strictEqual(res.statusCode, 302); }); }); // handlerInternalServerError