The function checks if the route is accessible or not
function isSessionCookieValid(req, res, next) {
if (!isValid(req.session)) {
return res.status(401).json({
isLoggedIn: false
});
}
return next();
}
In another file I do a post request which is a protected route using the above function
app
.route('/url')
.post(utils.isSessionCookieValid, (req, res, next) => {})
TESTING part
The problem is I don't know how to mock the isSessionCookieValid since it requires a next but am not able to pass the next callback in my test:
describe('testing routes', () => {
it('should enter the route body', (done) => {
utils.isSessionCookieValid(req, res, 'next should be here...');
chai.request(server)
.post('/url')
.end(function (error, response, body) {
if (error) {
done(error);
} else {
done();
}
});
});
});
ERROR: TypeError: next is not a function