I have a middleware function which checks a session token to see if the user is an admin user. The function does not return anything if all checks pass but simply calls next().
How can I wait for the internal asynchronous Promise (adminPromise) to resolve before making assertions on the next() callback which is a Sinon spy? The test will currently fail because the assertion in the test is made before the resolution of the promise in AdminMiddleware.prototype.run.
The function is:
AdminMiddleware.prototype.run = function (req, res, next) {
let token = req.header(sessionTokenHeader);
let adminPromise = new admin().send()
adminPromise.then(function (authResponse) {
let adminBoolean = JSON.parse(authResponse).payload.admin;
if (adminBoolean !== true) {
return new responseTypes().clientError(res, {
'user': 'validation.admin'
}, 403);
};
next();
});
};
And the test:
it('should call next once if admin', function (done) {
stub = sinon.stub(admin.prototype, 'send');
stub.resolves(JSON.stringify({success : true, payload : {admin : true}}));
let nextSpy = sinon.spy();
AdminMiddleware.prototype.run({header: function () {}}, {}, nextSpy);
expect(nextSpy.calledOnce).to.be.true;
done();
});
At the moment I am wrapping the expectation like below, which will cause the test to pass, but seems like a hack. Furthermore, if it were to fail it would cause an unhandled promise rejection error and timeout due to done() not being called.
it('should call next once if admin', function (done) {
stub = sinon.stub(admin.prototype, 'send');
stub.resolves(JSON.stringify({success : true, payload : {admin : true}}));
let nextSpy = sinon.spy();
AdminMiddleware.prototype.run({header: function () {}}, {}, nextSpy);
stub().then(function () {
expect(nextSpy.calledOnce).to.be.true;
done();
});
});