Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何测试passport.js身份验证_Javascript_Node.js_Unit Testing_Sails.js_Passport.js - Fatal编程技术网

Javascript 如何测试passport.js身份验证

Javascript 如何测试passport.js身份验证,javascript,node.js,unit-testing,sails.js,passport.js,Javascript,Node.js,Unit Testing,Sails.js,Passport.js,我想测试我的passport.js身份验证中间件: passport.authenticate('jwt', { session: false }, (err, user, info) => { if (err) return res.serverError(err) if (!user) return res.forbidden(info) req.user = user next() })(req, res, next) 中间件正在工作。但我不知道如何在测试中等待回调

我想测试我的passport.js身份验证中间件:

passport.authenticate('jwt', { session: false }, (err, user, info) => {
  if (err) return res.serverError(err)
  if (!user) return res.forbidden(info)
  req.user = user
  next()
})(req, res, next)
中间件正在工作。但我不知道如何在测试中等待回调。测试如下所示:

const token = JwtAuthService.issue({ id: 1, email: 'test@email.com' })
const req = httpMocks.createRequest({ headers: { authorization: `bearer ${token}` } })
const res = { forbidden: sinon.spy() }
const next = sinon.spy()

isAuthenticated(req, res, next);

(res.forbidden.called).should.be.false();
(next.calledOnce).should.be.true();

由于
passport.authenticate(…)
不是承诺,我不能简单地等待响应。如果调用了
res.forbidden
next
,我如何在测试之前等待
passport.authenticate(…)
函数的回调。

只需替换内容,将测试放入函数中,然后在对
user
@KresimirPendic进行身份验证后,在auth块中调用它,如果您理解正确的话,您建议在passport.authenticate()回调中调用测试。但我不想让我的测试成为中间件的一部分。只需替换东西,将测试放入函数中,然后在auth块中调用它,在您对
user
@KresimirPendic进行身份验证后,如果您理解正确,建议在passport.authenticate()回调中调用测试。但我不希望我的测试成为中间件的一部分。