I\'m using Passport.js for authentication (Facebook strategy) and testing with Mocha and Supertest. How can I create a session and make authenticated requests with Supertest for
The general solution is to create a cookie jar that will be re-used between requests.
The following example isn't passport specific, but should work:
var request = require('request');
describe('POST /api/posts', function () {
// Create a new cookie jar
var j = request.jar();
var requestWithCookie = request.defaults({jar: j}),
// Authenticate, thus setting the cookie in the cookie jar
before(function(done) {
requestWithCookie.post('http://localhost/user', {user: 'foo', password: 'bar'}, done);
});
it('should get the user profile', function (done) {
requestWithCookie.get('http://localhost/user', function (err, res, user) {
assert.equal(user.login, 'foo');
done();
});
});
});