samedi 28 septembre 2019

How to test passport LocalStrategy with jest

Stock with express passport LocalStrategy test, probably on mocking request.

test('should Test password Login auth', async (done) => {
  const response = jest.fn((arg)=> console.log('args', arg));
  const next = jest.fn();
  let mockReq = {body: { username: "test@gmail.com", password: "tets"}}
  let mockRes = {send: function(ats){console.log("mockResFunc", ats), response()}};

  passport.authenticate('local', ()=> response())(mockReq, mockRes);

  expect(next).not.toHaveBeenCalled();
  expect(response).toHaveBeenCalled();

but callback is never called as well as i didn't found password and username goes to passport function. Does anyone ideas how to mock credentials using jest(i think here is clue)?

passport.use(new LocalStrategy(
    async function(username, password, done) {
            const existingUser = await User.findOne({ 'email' :  username })
            console.log('credits', username, password, existingUser.email)
    if (existingUser) {
        let validUsr = existingUser.validPassword(password);
        if (existingUser && validUsr) {
            console.log('passport',existingUser.email)
          return done(null, existingUser);
        }
    }
    return done(null, false, { message: 'Wrong credentials.' });
    }
  ));

Aucun commentaire:

Enregistrer un commentaire