I'm using passport local strategy to authenticate users and I'm having issues with continuous integration testing on protected endpoints. I used this tutorial to get my authentication up and running. I'm using mocha/chai for testing and I can't figure out how to test an endpoint with a user logged during testing. I understand that I may have to use a mock strategy but I'm not sure how to do that. Here is an example endpoint, I need the user to be logged in so that I have access to the req.user.id variable that gets set when I a user is logged in.
router.post('/user/draft/delete', (req, res) => {
if(!req.user) {
return res.redirect('/login');
}
User
.findById(req.user.id)
.exec()
.then(user => {
user.drafts.pull(req.body.draftid);
user.save();
res.status(202).redirect('/dashboard');
})
.catch(err => {
console.error(err);
res.status(500).redirect('/dashboard');
});
});
I'm not sure what other information might be required but I'm happy to update this post with any code needed.