I am a newbie in testing in Javascript and trying to test my NodeJS backend using mocha and chai. How all of my routes are filled with a middleware which does not allow people to go forward if they aren't logged in.
Something like this
app.post('/user/analytics/driverdata', checkauth, function(req, res) {
analytics.driverData(req, res);
});
where checkauth
is
var checkauth = function(req, res, next) {
console.log("In checkauth");
if (req.isAuthenticated()) {
next();
} else {
console.log("Doesn't authenticate");
res.status(401);
res.set('Content-Type', 'application/json');
res.end(JSON.stringify({
'success': false
}));
}
};
The isAuthenticated parameter is attached to request by PassportJS, when it deserializes a request. What I want to do is write a test for
app.post('/user/analytics/driverdata', checkauth, function(req, res) {
analytics.driverData(req, res);
});
this API. in which I am failing as I am not logged in hence unable to reach there.
So I wrote a beforeEach to login the user beforeEach it
. It goes like this.
var expect = require('chai').expect;
var request = require('superagent');
beforeEach(function(done){
//login into the system
request
.post("http:localhost:5223/user/authenticate/login")
.send({username : "saras.arya@gmail.com", password : "saras"})
.end(function assert(err, res){
if(err){
console.log(err);
done();
}
else{
done();
}
});
});
I don't know what am I doing wrong and the internet has failed me. Any help to point out where am I going wrong will be appreciated.