1

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.

koninos
  • 4,969
  • 5
  • 28
  • 47
Saras Arya
  • 3,022
  • 8
  • 41
  • 71

1 Answers1

1

After seeing a lot of stuff and fidgeting around I think I finally cracked it. It would be harsh not to mention the answer here which introduced me to the concept of an agent. Which helped me crack this. Inside your describe block, or probably before the block you can have the following it.

var superagent = require('superagent');
var agent = superagent.agent();
it('should create a user session successfully', function(done) {
       agent
       .post('http://localhost:5223/user/authenticate/login')
       .send({
              username: 'whatever@example.com',
              password: 'ssh-its-a-secret'
        })
        .end(function(err, res) {
             console.log(res.statusCode);
             if (expect(res.statusCode).to.equal(200))
                 return done();
             else {
                 return done(new Error("The login is not happening"));
                    }
                });
        });

the agent variable holds the cookies for you, which are then used by PassportJS to authenticate you.

here is how you do it. So the agent variable is inside a describe. In the same describe inside another it.

it("should test analytics controller", function(done) {
agent.post('http://localhost:5040/user/analytics/driverData')
        .send({
            startDate: "",
            endDate: "",
            driverId: ""
        })
        .end(function(err, res) {
            if(!err)
            done();
        });
});

This function passes like a charm. This was one complete documentation that was missing.

Community
  • 1
  • 1
Saras Arya
  • 3,022
  • 8
  • 41
  • 71