@villedemontreal/jwt-validator
Version:
Module to validate JWT (JSON Web Tokens)
112 lines • 5.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const chai = require("chai");
const chai_1 = require("chai");
const chai_as_promised_1 = require("chai-as-promised");
const node_mocks_http_1 = require("node-mocks-http");
const sinon_1 = require("sinon");
const jwtValidator_1 = require("../jwtValidator");
const jwtMiddleware_1 = require("./jwtMiddleware");
chai.use(chai_as_promised_1.default);
describe('#jwtValidationMiddleware', () => {
let middleware;
let nextFunction = (0, sinon_1.spy)();
let verifyMethod;
let jwt;
let authorizationHeader;
let request;
let response;
before(() => {
nextFunction = (0, sinon_1.spy)();
jwt = {
sub: '1234567890',
name: 'Peter Neighbor',
iat: 1694264949,
};
authorizationHeader = `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IlBldGVyIE5laWdoYm9yIiwiaWF0IjoxNjk0MjY0OTQ5fQ.ncai230HG-KbDL2ximBZz29Smt-yOFBgZYrJTmQreqA`;
});
beforeEach(() => {
nextFunction.resetHistory();
request = (0, node_mocks_http_1.createRequest)({
method: 'GET',
url: '/',
headers: { Authorization: authorizationHeader },
});
response = {};
});
afterEach(() => {
verifyMethod?.restore();
});
const DELAY = 50;
it('should invoke #verifyAuthorizationHeader with authorization header', async function () {
// Cf. https://mochajs.org/api/mocha#slow
this.slow(2 * DELAY + 10 /* budgeted test duration */);
// GIVEN
middleware = (0, jwtMiddleware_1.jwtValidationMiddleware)();
request = (0, node_mocks_http_1.createRequest)({
method: 'GET',
url: '/',
headers: { Authorization: authorizationHeader },
});
verifyMethod = (0, sinon_1.stub)(jwtValidator_1.jwtValidator, 'verifyAuthorizationHeader').returns(delay(DELAY).then(() => jwt));
// WHEN
await middleware(request, response, nextFunction);
// THEN
(0, chai_1.expect)(verifyMethod.callCount).to.equal(1);
(0, chai_1.expect)(verifyMethod.args[0]).to.deep.equal([authorizationHeader]);
(0, chai_1.expect)(nextFunction.callCount).to.equal(1);
(0, chai_1.expect)(nextFunction.args[0]).to.deep.equal([]);
(0, chai_1.expect)(request.jwt).to.equal(jwt);
});
it('should *NOT* invoke #verifyAuthorizationHeader *WITHOUT* authorization header and "mandatory" trigger being *OFF*', async () => {
// GIVEN
middleware = (0, jwtMiddleware_1.jwtValidationMiddleware)(false);
request.headers = {}; // ∅
verifyMethod = (0, sinon_1.stub)(jwtValidator_1.jwtValidator, 'verifyAuthorizationHeader').returns(delay(50).then(() => jwt));
// WHEN
await middleware(request, response, nextFunction);
// THEN
(0, chai_1.expect)(verifyMethod.callCount).to.equal(0);
(0, chai_1.expect)(nextFunction.callCount).to.equal(1);
(0, chai_1.expect)(nextFunction.args[0]).to.deep.equal([]);
(0, chai_1.expect)(request.jwt).to.be.undefined;
});
it('should fail synchronously if #verifyAuthorizationHeader does so', async () => {
// GIVEN
middleware = (0, jwtMiddleware_1.jwtValidationMiddleware)();
const error = new Error('💣');
verifyMethod = (0, sinon_1.stub)(jwtValidator_1.jwtValidator, 'verifyAuthorizationHeader').throws(error);
// WHEN
const operation = () => middleware(request, response, nextFunction);
// THEN
(0, chai_1.expect)(operation).not.to.throw();
(0, chai_1.expect)(verifyMethod.callCount).to.equal(1);
(0, chai_1.expect)(verifyMethod.args[0]).to.deep.equal([authorizationHeader]);
(0, chai_1.expect)(nextFunction.callCount).to.equal(1);
(0, chai_1.expect)(nextFunction.args[0]).to.deep.equal([error]);
(0, chai_1.expect)(request.jwt).to.be.undefined;
});
it('should fail *ASYNCHRONOUSLY* if #verifyAuthorizationHeader does so', async () => {
// GIVEN
middleware = (0, jwtMiddleware_1.jwtValidationMiddleware)();
const error = new Error('💣');
verifyMethod = (0, sinon_1.stub)(jwtValidator_1.jwtValidator, 'verifyAuthorizationHeader').returns(delay(50).then(() => {
throw error;
}));
// WHEN
const promise = middleware(request, response, nextFunction);
// THEN
await (0, chai_1.expect)(promise).not.to.be.eventually.rejected;
(0, chai_1.expect)(verifyMethod.callCount).to.equal(1);
(0, chai_1.expect)(verifyMethod.args[0]).to.deep.equal([authorizationHeader]);
(0, chai_1.expect)(nextFunction.callCount).to.equal(1);
(0, chai_1.expect)(nextFunction.args[0]).to.deep.equal([error]);
(0, chai_1.expect)(request.jwt).to.be.undefined;
});
});
async function delay(duration) {
return new Promise((resolve) => {
setTimeout(resolve, duration);
});
}
//# sourceMappingURL=jwtMiddleware.test.js.map