如何testingpassport.js进行身份validation
我想testing我的passport.jsauthentication中间件:
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)
中间件正在工作。 但是我不知道如何等待testing中的callback。 testing看起来像这样:
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(...)
是没有承诺,我不能简单地等待回应。 在testing之前如何等待passport.authenticate(...)
函数的callback,如果res.forbidden
或next
被调用。