Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
Mocking Passport authentication on protected endpoints

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.

about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!