Node.js superagent module not saving cookies with passport-open source projects visionmedia/superagent
So I’m trying to test a website backend I’m coding in Node.js. I need a testing method to save session cookies being managed by the Passport module. I was originally using the Unit.js httpAgent but I tried using superagent based on advice from this question, but when I threw in an assertion to test if it was storing Passport’s req.user field, it failed:
assert(req.user, "Error! User is not defined"); //this should not fail
output:
app.js
127.0.0.1 - - [Tue, 06 Jan 2015 23:55:45 GMT] "POST /login HTTP/1.1" 302 40 "-" "node-superagent/0.21.0"
127.0.0.1 - - [Tue, 06 Jan 2015 23:55:45 GMT] "GET /login HTTP/1.1" 200 2188 "-" "node-superagent/0.21.0"
✓ should use cookies at login (43ms)
127.0.0.1 - - [Tue, 06 Jan 2015 23:55:45 GMT] "GET / HTTP/1.1" 200 1598 "-" "node-superagent/0.21.0"
✓ should GET /
AssertionError: Error! User is not defined
at app.post.submission (/home/matthew/Documents/git/libr/web/app.js:201:3)
at callbacks (/home/matthew/Documents/git/libr/web/node_modules/express/lib/router/index.js:164:37)
at param (/home/matthew/Documents/git/libr/web/node_modules/express/lib/router/index.js:138:11)
at pass (/home/matthew/Documents/git/libr/web/node_modules/express/lib/router/index.js:145:5)
at Router._dispatch (/home/matthew/Documents/git/libr/web/node_modules/express/lib/router/index.js:173:5)
at Object.router (/home/matthew/Documents/git/libr/web/node_modules/express/lib/router/index.js:33:10)
at next (/home/matthew/Documents/git/libr/web/node_modules/express/node_modules/connect/lib/proto.js:174:15)
at SessionStrategy.strategy.pass (/home/matthew/Documents/git/libr/web/node_modules/passport/lib/middleware/authenticate.js:318:9)
at SessionStrategy.authenticate (/home/matthew/Documents/git/libr/web/node_modules/passport/lib/strategies/session.js:67:10)
at attempt (/home/matthew/Documents/git/libr/web/node_modules/passport/lib/middleware/authenticate.js:341:16)
at Object.authenticate [as handle] (/home/matthew/Documents/git/libr/web/node_modules/passport/lib/middleware/authenticate.js:342:7)
at next (/home/matthew/Documents/git/libr/web/node_modules/express/node_modules/connect/lib/proto.js:174:15)
at Object.initialize [as handle] (/home/matthew/Documents/git/libr/web/node_modules/passport/lib/middleware/initialize.js:62:5)
at next (/home/matthew/Documents/git/libr/web/node_modules/express/node_modules/connect/lib/proto.js:174:15)
at Object.handle (/home/matthew/Documents/git/libr/web/node_modules/connect-flash/lib/flash.js:21:5)
at next (/home/matthew/Documents/git/libr/web/node_modules/express/node_modules/connect/lib/proto.js:174:15)
at Object. (/home/matthew/Documents/git/libr/web/node_modules/express/node_modules/connect/node_modules/express-session/index.js:385:7)
at Object.immediate._onImmediate (timers.js:372:16)
at processImmediate [as _immediateCallback] (timers.js:354:15)
Is there anything I’m doing wrong here? I’m using done callbacks in my test program (with mocha), so I’m pretty sure things are happening in series.
tests:
describe('app.js', function(){
it('should use cookies at login', function(done){
user1
.post('http://localhost:5000/login')
.send({ user: 'bob', password: 'secret' })
.end(function(err, res) {
if(err){
done(err.message);
}
should.exist(res.headers['set-cookie']);
done();
// user1 will manage its own cookies
// res.redirects contains an Array of redirects
});
});
it('should GET /', function(done){
user1
.get('http://localhost:5000/')
.end(function(err, res){
if(err){
done(err);
return;
}
done();
});
});
it('POST event data works', function(done){
user1
.post('http://localhost:5000/dataEntry')
.send({
start_date_time: '2014-08-24T11:11',
end_date_time: '2014-08-24T12:00',
state_intensity: 1,
activity: 'Sample',
event: 'Sample',
submitButton: 'Submit'
})
.end(function(err, res){
if (err) {
done(err);
return;
}
done();
});
});
});