Node.js 在passport管理的API上使用mocha通过supertest发送经过身份验证的请求
我已经24小时没有出现这个问题了。我知道有很多类似的问题,特别是在SO上,但我不知道如何解决 我有具体要求:Node.js 在passport管理的API上使用mocha通过supertest发送经过身份验证的请求,node.js,authentication,mocha.js,passport.js,supertest,Node.js,Authentication,Mocha.js,Passport.js,Supertest,我已经24小时没有出现这个问题了。我知道有很多类似的问题,特别是在SO上,但我不知道如何解决 我有具体要求: 我有一些没有身份验证的路由要测试 我有一些特定用户的测试路线 我使用passport本地策略 所以我决定测试我的API,比如: var request = require('supertest'); ... describe('Routes', function () { var anAgent; var anotherAgent; before(function
- 我有一些没有身份验证的路由要测试
- 我有一些特定用户的测试路线
- 我使用passport本地策略
var request = require('supertest');
...
describe('Routes', function () {
var anAgent;
var anotherAgent;
before(function (done) {
anAgent = request.agent(app);
anotherAgent = request.agent(app);
async.parallel([
function (callback) {
anAgent
.post('/api/users/login')
.send({user:user, password:password})
.expect(200)
.end(callback);
},
function (callback) {
userAgent
.post('/api/users/login')
.send({user:anotherUser, password:anotherPassword})
.expect(200)
.end(callback);
}
], done);
});
describe('/superroute', function () {
it('should return the user', function (done) {
anAgent
.post('/api/superroute')
.send(params)
.expect(200)
.end(function (err, res) {
should.not.exist(err);
res.body.err.should.equal('Super route');
done();
});
});
...
});
...
});
路线/superroute
描述如下
express.Router().post('/superroute', auth.ensureAuthenticated, superCtrl.superroute);
其中ensureAuthenticated
中间件调用passport的req.isAuthenticated()
当我从一个简单的角度使用它时,这个API工作得很好,但是当我使用mocha运行测试时,不会调用passport.deserializeUser
方法,并且isAuthenticated
返回false
。由于用户已正确登录并从/login
调用中检索,我只知道这一点
因此,调用返回401而不是200
我可能错过了什么