jeudi 7 février 2019

signin test with mocha using passport as middleware

actually i dont know how to test this functionality, i have an endpoint called signin

const passportSignIn = passport.authenticate('local', { session: false });

router.post('/signin', passportSignIn, async (req, res) => {
  try {
    const signUser = await Userdal.signIn(req.user);
    return res.status(httpStatus.OK).send({ token: signUser });
  } catch (error) {
    return res.status(httpStatus.FORBIDDEN).send({ error: error.message });
  }
});

and i don't know how to test it, i'm using supertest-as-promised to do the consults. the middleware with the passport local statrategy is this:

passport.use('local', new LocalStrategy({
  usernameField: 'email',
}, async (email, password, done) => {
  try {
    const user = await UserModel.findOne({ email });
    if (!user) {
      return done(null, false);
    }
    const passwordHash = cryptoPassword.validatePassword(password, user.salt);
    return passwordHash === user.password ? done(null, user) : done(null, false);
  } catch (error) {
    return done(error, false);
  }
}));

export default passport;

the test that im doing is (i know that is wrong):

it('should return a user signed', (done) => {
      user.user.email = 'nxccc@hotmail.com';
      request(app)
        .post('/api/v1/users/signin/')
        .send({ email: user.user.email, password: user.user.password })
        .expect(httpStatus.OK);
      done();
    });

the test never calls to the endpoint, so i dont know how can i do it. please help me

Aucun commentaire:

Enregistrer un commentaire