I'm trying to write some tests where I need to authenticate first. If I make multiple requests in "before()" I get connection refused. If I split it between "before()" and "it()" it works but I cannot acheive what I want.
Code I want to work:
var agent = request.agent(myExpressApp),
token;
before(function(done) {
async.series([
function(cb) {
agent
.post('/1/auth/login')
.send({
email: 'john@smith.com',
password: 'j0hn_Sm1TH'
})
.expect(200)
.end(cb);
}, function(cb) {
agent
.get('/1/auth/authorize')
.query({
response_type: 'token',
client_id: 'some id',
redirect_uri: 'http://ignore'
})
.expect(302)
.end(function(err, res) {
/* console.log(arguments) = { '0':
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' } }*/
if (err) return cb(err);
cb();
});
}
], done);
});
it('some authenticated task', function(done) {
// do something else
done();
});
Code that is working:
var agent = request.agent(myExpressApp),
token;
before(function(done) {
async.series([
function(cb) {
agent
.post('/1/auth/login')
.send({
email: 'john@smith.com',
password: 'j0hn_Sm1TH'
})
.expect(200)
.end(cb);
}, function(cb) {
cb();
}
], done);
});
it('some authenticated task', function(done) {
agent
.get('/1/auth/authorize')
.query({
response_type: 'token',
client_id: 'some id',
redirect_uri: 'http://ignore'
})
.expect(302)
.end(function(err, res) {
if (err) return done(err);
done();
});
});