登录后,我无法运行经过身份验证的测试(服务器返回401 Unauthenticated
)。
var should = require('should'),
_ = require('lodash'),
app = require('../../../server'),
mongoose = require('mongoose'),
User = mongoose.model('User'),
request = require('supertest');
var user
, user1;
describe('GET /api/jobs', function () {
before(function (done) {
user = new User({
provider: 'local',
name: 'Fake User',
email: 'test@test.com',
password: 'password'
});
// Clear users before testing
User.remove().exec();
request(app)
.post('/api/users')
.send(user)
// end handles the response
.end(function(err, res) {
if (err) {
throw err;
}
res.should.have.status(200);
res.body._id.should.exist;
user1 = request.agent(app); //user1 will be used in all subsequent tests since he's supposed to be authenticated
user1
.post('/api/session')
.send({ email: user.email, password: user.password })
.end(function(err, res) {
if ( err ) throw err;
// user1 will manage its own cookies
// res.redirects contains an Array of redirects
res.should.have.status(200);
done();
});
});
});
afterEach(function (done) {
User.remove().exec();
done();
});
it('should create a job by user1', function (done) {
var job = {
//jobs stuff
};
user1
.post('/api/jobs')
.send(job)
.expect(200) //It fails here, getting 401 Unauthenticated. Session is using passport local strategy
.expect('Content-Type', /json/)
.end(function (err, res) {
if (err) return done(err);
res.body.should.be.instanceof(Object);
done();
});
});
});
会话正在设置这样的cookie:
if (req.user) {
res.cookie('user', JSON.stringify(req.user.userInfo));
}