Derocs
Derocs

Reputation: 73

Mocha test cases throwing Error: expected 200 "OK", got 403 "Forbidden"

I am using mochajs for my mean applications server side test cases.The test cases are showing an error, Error: expected 200 "OK", got 403 "Forbidden". I tried using console logs inside the functions that I am trying to test, but they never get executed. I am unable to find out the issue. Can someone help me with this.

My controller

exports.signin = function(req, res) {
   req.body.username = new Buffer(req.body.username, base64').toString('ascii');
    User.findOne({
        username: req.body.username.toLowerCase()
    }, function(err, user) {
        if (err) {
            //do something
        } else if (!user) {
            //do something
        } else {
            //do something
        }
    });
};

My test case

it('should be able to signin if the credentials are valid', function(done) {
    var obj = {
        username: new Buffer('demo').toString('base64'),
        password: new Buffer('demouser').toString('base64')
        };
    agent.post('/auth/signin')
        .send(obj)
        .expect(200)
        .end(function(signinErr, res) {
                if (signinErr){
                      console.log(signinErr);
                    done(signinErr);
                }
                else{
                    var temp = res.body;
                    console.log(temp);
            done();
                }
    });
});

And my route

app.route('/auth/signin').post(users.signin);

Upvotes: 3

Views: 2601

Answers (1)

Pranjal Diwedi
Pranjal Diwedi

Reputation: 1178

I was facing the same issue. I found out that we were forcing SSL in our application using the middleware express-force-ssl by app.use(force-ssl). We used a flag to control this behavior. When I made that false, the test cases ran fine. So if you are not controlling it with the flag. Try removing that module and running your test cases.

Upvotes: 3

Related Questions