I have the following file called auth.js which is a middleware used for authenticationimport jwksRsa from 'jwks-rsa'; import jwt from 'express-jwt'; module.exports = app => { return { authenticate: () => { return jwt({ secret: jwksRsa.expressJwtSecret({ cache: true, rateLimit: true, jwksRequestsPerMinute: 5, jwksUri: `https://${app.libs.config.authDomain}/.well-known/jwks.json` }), audience: app.libs.config.auth0Audience, issuer: `https://${app.libs.config.authDomain}/`, algorithms: ["RS256"] }); }, authorize: (role) => { return function (req, res, next) { const roles = req.user["http://localhost:3000/roles"]; if (Array.isArray(roles) && roles.includes(role)) { return next(); } else { return res.status(401).send("User is not authorized"); } }; } }; }; and I'm trying to stub the function in my test file describe("GET Success 200", () => { before(() => { sinon.stub(app.auth, 'authenticate').callsFake((req, res, next) => { req.user = {"http://localhost:3000/roles": "sales_rep"}; next(); }); }); it("return customers", () => { request.get("/customers") .set('Authorization', `Bearer ASDFAD@#$@#$sdf2123123213`) .expect(200).end((err, res) => { expect(res.body.customers[0].name).to.eql("test"); }); }); }); then I read that I should stub the function before I reference it so I tried to do the following in my helper.js fileimport supertest from "supertest"; import chai from "chai"; import app from "../server.js"; import sinon from "sinon"; import auth from "../auth.js"; sinon.stub(auth, 'authenticate').callsFake((req, res, next) => { req.user = {"http://localhost:3000/roles": "sales_rep"}; next(); }); global.app = app; global.request = supertest(app); global.expect = chai.expect; still the original function get called and I can't find any reference about sinon stubbing anything
Submitted December 12, 2018 at 02:43PM by supermedo
No comments:
Post a Comment