I have a SailsJS setup and am using Mocha/Supertest/Superagent combination to run unit tests. I have searched around and read about supertest and how it now extends superagent.agent('url') in order to store sessions and cookies. I know that my /athlete/login and /athlete/current routes are working as I can test them using Postman and they return the correct values. However, when testing, I get a 200 status on login but a 404 on /athlete/current
Here is what I am currently working with:
1. Mocha Test to login and verify session of user logged in
var should = require('should'),
assert = require('assert'),
request = require('supertest');
it('/athlete/login should return 200 and athlete on success', function (done){
var athleteStub = AthleteStub(),
setPassword = athleteStub.password;
Athlete.create(athleteStub, function(err, newAthlete) {
var user = request.agent(sails.express.app);
user
.post('/athlete/login')
.send({ 'email': athleteStub.email, 'password': setPassword })
.expect('Content-Type', /json/)
.end(function (err, res) {
if(err) return done(err);
console.log('test', user );
// res.should.have.status(200);
// res.body.email.should.equal(athleteStub.email);
user
.get('/athlete/current')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if(err) return done(err);
done();
});
});
});
});
2. /Login and /current actions
login: function (req, res) {
var bcrypt = require('bcrypt');
Athlete.findOneByEmail(req.body.email).done(function (err, athlete) {
if (err) {
res.json({ error: 'DB error' }, 500);
}
if (athlete) {
if ( !athlete.isActive ){
res.json({ error: 'Your account is not active.' }, 500);
}
bcrypt.compare(req.body.password, athlete.password, function (err, match) {
if (err){
res.json({ error: 'Server error' }, 500);
}
if (match) {
// password match
req.session.athlete = athlete.id;
res.json(athlete);
} else {
// invalid password
if (req.session.athlete){
req.session.athlete = null;
}
res.json({ error: 'Invalid password' }, 403);
}
});
} else {
res.json({ error: 'User not found' }, 404);
}
});
},
current: function (req, res){
if(req.session.athlete){
Athlete.findOneById(req.session.athlete)
.where({ isActive: true })
.done(function(err, athlete) {
if (err) {
res.json({ error: 'No active athlete found with ID of '+req.params.id }, 404);
} else {
res.json(athlete);
}
});
}else{
res.json({ error: 'No active athlete currently logged in.' }, 404);
}
},
SOLUTION
I had changed some of the routing so basically put '/athlete/current/' from above is now '/athlete/me/'
it("/athlete/me should return user data if athlete is logged in", function(done){
var agent = request.agent(sails.hooks.http.app),
athleteStub = AthleteStub(),
setPassword = athleteStub.password;
agent
.post('/athlete')
.send( athleteStub )
.expect('Content-Type', /json/)
.end(function (err, res) {
should.not.exist(err);
res.should.have.status(200);
res.body.email.should.equal(athleteStub.email);
res.body.firstName.should.equal(athleteStub.firstName);
res.body.lastName.should.equal(athleteStub.lastName);
agent
.post('/athlete/login')
.send({ 'email': athleteStub.email, 'password': setPassword })
.expect('Content-Type', /json/)
.end(function (err, res) {
should.not.exist(err);
res.should.have.status(200);
agent
.get('/athlete/me')
.expect(200)
.end(function(err, res) {
should.not.exist(err);
res.body.email.should.equal(athleteStub.email);
res.should.have.status(200);
done();
});
});
});
});
Not sure if you found an answer to this or not but i was able to get the cookie to save with the following code:
var agent = request.agent(sails.hooks.http.app);
describe('Session', function(done) {
it("should be able to create", function(done) {
agent
.post("/session/create")
.send({email: "test.User1@gmail.com",password: "test", confirmation: "test"})
.expect('set-cookie', 'cookie=hey; Path=/', done)
.end(function(err, res) {
console.log('got a response!! '+ JSON.stringify(res.body));
done();
});
});
it("should be logged in", function(done) {
agent
.get("/user/index")
.end(function(err, res) {
console.log('got a response from user/index!! '+ JSON.stringify(res.body));
done();
});
});
});