Javascript 如何测试passport.js身份验证
我想测试我的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.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()回调中调用测试。但我不希望我的测试成为中间件的一部分。