node.bcrypt.js icon indicating copy to clipboard operation
node.bcrypt.js copied to clipboard

bcrypt.compare not executing when testing with mocha and chai-http

Open sysstas opened this issue 8 years ago • 5 comments

It hang up when testing with mocha, chai-http. when bcrypt-compare start execution. But it runs well on server without hanging up.

async function auth (req, res) { let { login, password } = req.body try { let payload = {} let result = {} await User.findOne({ where: { userEmail: login } }).then(user => { return result = user }) bcrypt.compare(password, result.dataValues.password, await function (err, data) { if (err) { throw Error(err) } if (result && data) { payload.isAdmin = result.dataValues.isAdmin payload.ID = result.dataValues.id let token = jwt.sign(payload, 'yoursecret') res.status(200).send({ token: token }) } else { res.status(401) } }) } catch (error) { res.sendStatus(500) } } test describe('Testing login', () => { it('should return status 200 when there is such user in DB and password is correct', (done) => { chai.request(server) .post('/login') .send({ login: '[email protected]', password: 'somepassword' }) .end((err, res) => { res.should.have.status(200) done() }) }) })

sysstas avatar Sep 18 '18 11:09 sysstas

@sysstas The way you use the await keyword is a bit odd. First this line :

let result = {} 
await User.findOne({ where: { userEmail: login } }).then(user => { return result = user })

With the await keyword, you will automatically return the result of the promise User.findOne({ where: { userEmail: login } }), no need of .then(user => { return result = user }) so you just could do :

const result = await User.findOne({ where: { userEmail: login } })

And in the second case of your await, it is a bit weird to use await on your callback function of bcrypt.compare, async/await are here to help you get rid of callback functions, so you could just do :

const data = await bcrypt.compare(password, result.dataValues.password)

So your full auth function shoud be something like this :

async function auth(req, res) {
    let { login, password } = req.body;
    try {
        let payload = {};
        const result = await User.findOne({ where: { userEmail: login } })
        if (result) {
            await bcrypt.compare(password, result.dataValues.password)
            payload.isAdmin = result.dataValues.isAdmin;
            payload.ID = result.dataValues.id;
            let token = jwt.sign(payload, "yoursecret");
            res.status(200).send({ token: token });
        } else {
            res.status(401);
        }
    } catch (error) {
        res.sendStatus(500);
    }
}

I think your bcrypt.compare is hanging, because your result variable is undefined in bcrypt.compare(password, result.dataValues.password since your User.findOne was not resolved.

AJRdev avatar Sep 18 '18 14:09 AJRdev

@AJRdev , Thank you. now code is much more clear. It runs as expected on server. But on tests it still hangs on. I can see that exactly bcrypt.compare is hanging on by adding console.log on every step.

sysstas avatar Sep 18 '18 15:09 sysstas

@sysstas are you sure that result.dataValues.password is defined ?

AJRdev avatar Sep 18 '18 15:09 AJRdev

Yes. It loged out.

sysstas avatar Sep 18 '18 15:09 sysstas

Wrong behaviour wasn't connected to chai/mocha/bcript. It was caused by following statement

} else {
    res.status(401)
}

correct code should be

} else {
    res.status(401).send(something)
}

or

} else {
    res.sendStatus(401)
}

I think Express waiting for .send() after .status() and keep connection without response

sysstas avatar Sep 19 '18 13:09 sysstas