如何在使用mocha / chai进行unit testing时覆盖护照authentication?
我尝试设置app.request.isAuthenticated = function(){return true;}
在另一个答案build议,但这不适合我。
这是在我想要validation的路由上用作中间件的身份validation方法:
const authenticate = [passport.authenticate(['bearer'], {session: false}), (req, res, next) => { if (req.isAuthenticated()) { return next(null); } else { return res.status(401).send('Unauthorized'); } }];
典型的安全调用看起来像这样
router.get('/securedInfo', authenticate, function(req,res,next){ // do stuff });
我也不想在这个testing中进行身份validation,因为我想单独testing我的RESTful APItesting和我的身份validation逻辑(因为后者很可能随时间而改变)。