Hapi Lab检测到以下泄漏:lr

时间:2015-08-10 16:31:42

标签: hapijs

这里Hapi Lab why Test failed when all the tests are passed为什么测试失败。

在这个新问题中,为什么我检测到以下泄漏:lr ,即使代码中没有全局变量。

运行这个简单的测试

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var server = require('../../');


lab.experiment('Users', function () {

    lab.test('create joi required', function (done) {

        var options = {
            method: 'POST',
            url: '/api/users',
            payload: {
                lastname: 'Bedini',
                username: 'whisher',
                email: 'me@ilwebdifabio.it',
                password: 'mysecret'
            }
        };

        server.inject(options, function(response) {
            var result = response.result;
            Code.expect(response.statusCode).to.equal(422);
            Code.expect(result.message).to.equal('child "firstname" fails because ["firstname" is required]');
            done();
        });

    });

    lab.test('create', function (done) {

        var options = {
            method: 'POST',
            url: '/api/users',
            payload:{
                firstname: 'Fabio',
                lastname: 'Bedini',
                username: 'whisher',
                email: 'me@ilwebdifabio.it',
                password: 'mysecret'
            }
        };

        server.inject(options, function(response) {
            var token = response.result.token;
            var payload = options.payload;
            Code.expect(response.statusCode).to.equal(201);
            done();
        });

    });

});
  

2个测试完成

     

测试持续时间:363毫秒

     

检测到以下泄漏:lr

但我没有看到任何变种!

奇怪的是,如果我运行这个

  

payload.passdword

而不是

  

payload.password

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var server = require('../../');


lab.experiment('Users', function () {

    lab.test('create joi required', function (done) {

        var options = {
            method: 'POST',
            url: '/api/users',
            payload: {
                lastname: 'Bedini',
                username: 'whisher',
                email: 'me@ilwebdifabio.it',
                password: 'mysecret'
            }
        };

        server.inject(options, function(response) {
            var result = response.result;
            Code.expect(response.statusCode).to.equal(422);
            Code.expect(result.message).to.equal('child "firstname" fails because ["firstname" is required]');
            done();
        });

    });

    lab.test('create', function (done) {

        var options = {
            method: 'POST',
            url: '/api/users',
            payload:{
                firstname: 'Fabio',
                lastname: 'Bedini',
                username: 'whisher',
                email: 'me@ilwebdifabio.it',
                passdword: 'mysecret'
            }
        };

        server.inject(options, function(response) {
            var token = response.result.token;
            var payload = options.payload;
            Code.expect(response.statusCode).to.equal(201);
            done();
        });

    });

});

我有

  

2次测试中有1次失败

     

测试持续时间:73毫秒

     

未检测到全局变量泄漏

没有关于lr var。

的警告

所以我不知道转向哪种方式:(

可以帮助我吗?

更新

控制器

'use strict';

/**
 * Module dependencies.
 */
var BcryptUtil = require('../utils/bcrypt');
var JwtUtil = require('../utils/jwt');
var Models = require('../models');
var ReplyUtil = require('../utils/reply');
var  User = Models.users;

exports.create =  function create(request, reply) {

    var params = request.payload;
    params.password = BcryptUtil.generateHash(params.password);
    params.roles =JSON.stringify(['user']);
    User
        .create(params)
        .then(function(user) {
            var token = JwtUtil.getUserToken(user);
            var redisClient = request.server.plugins['hapi-redis'].client;
            redisClient.set('user_'+user.userId, token);
            return reply(ReplyUtil.ok(token)).created('/api/users/' + user.userId);
        })
        .catch(function(err){
            if(err instanceof Models.Sequelize.ValidationError){
       return reply(ReplyUtil.badData(err,params));
    }
    return reply(ReplyUtil.badImplementation(err));
        });

};

exports.findAll = function (request, reply) {

    User
        .findAll({
            order: [['createdAt','DESC']],
            attributes: ['userId', 'firstname', 'lastname', 'username', 'email']
        })
        .then(function(users) {
            return reply(ReplyUtil.ok(users));
        })
        .catch(function(err){
    return reply(ReplyUtil.badImplementation(err));
        });

};

exports.findById = function (request, reply) {

    var userId = request.params.userId;
    User
        .findById(
            userId,
            {
                attributes: ['userId', 'firstname', 'lastname', 'username', 'email']
            })
        .then(function(user) {
            if(!user){
       return reply(ReplyUtil.notFound({userId:userId}));
    }
    return reply(ReplyUtil.ok(user));
        })
        .catch(function(err){
            return reply(ReplyUtil.badImplementation(err));
        });

};

exports.update = function (request, reply) {

    var userId = request.params.userId;
    var params =request.payload;
    User
        .update(params,{
            where: {
       userId: userId
    }
        })
        .then(function(rows) {
            var affectedRows = rows.pop();
    if(!affectedRows){
       return reply(ReplyUtil.notFound({userId:userId}));
    }
    return reply(ReplyUtil.ok(affectedRows));
        })
        .catch(function(err){
            if(err instanceof Models.Sequelize.ValidationError){
       return reply(ReplyUtil.badData(err,params));
    }
    return reply(ReplyUtil.badImplementation(err));
        });

};

exports.destroy = function (request, reply) {

    var userId = request.params.userId;
    User
        .destroy({
            where: {
           userId: userId
    }
        })
        .then(function(rows) {
            if(!rows){
       return reply(ReplyUtil.notFound({userId:userId}));
    }
    return reply(ReplyUtil.ok(rows));
        })
        .catch(function(err){
            return reply(ReplyUtil.badImplementation(err));
        });

};

exports.signIn = function (request, reply) {

    var params = request.payload;
    User
        .findOne({
            where: {
                email: params.email
            }
        })
        .then(function(user) {
            if(!user){
                return reply(ReplyUtil.invalidPassword());
            }
            if(BcryptUtil.authenticate(params.password, user.password)){
                var token = JwtUtil.getUserToken(user);
                var redisClient = request.server.plugins['hapi-redis'].client;
                redisClient.set('user_'+user.userId, token);
                return reply(ReplyUtil.ok(token));
            }
            return reply(ReplyUtil.invalidPassword());

        })
        .catch(function(err){
            return reply(ReplyUtil.badImplementation(err));
        });

};

exports.logOut = function (request, reply) {
    var userId = request.auth.credentials.jti;
    var redisClient = request.server.plugins['hapi-redis'].client;
    redisClient.del('user_'+userId);
    return reply();
};

exports.methodNotAllowed = function (request, reply) {
    return reply( ReplyUtil.methodNotAllowed() );
};

路线

'use strict';

/**
 * Module dependencies.
 */
var User      = require('../controllers/users');
var Validator = require('../validations/users');

/**
 * Resource configuration.
 */
var internals = {};
internals.resourcePath = '/users';

module.exports = function() {
    return [
        {
            method: 'POST',
            path:  internals.resourcePath,
            config : {
                handler: User.create,
                validate: Validator.create
            }
        },
        {
            method: 'GET',
        path:  internals.resourcePath,
        config : {
               handler : User.findAll,
               auth: {
                    strategy: 'token',
                    scope: ['admin']
                }
        }
        },
        {
            method: 'GET',
        path:  internals.resourcePath + '/{userId}',
        config : {
                handler : User.findById,
                validate: Validator.findById,
                auth: {
                    strategy: 'token',
                    scope: ['user']
                }
        }
        },
        {
            method: 'PUT',
        path:  internals.resourcePath + '/{userId}',
        config : {
               handler: User.update,
               validate: Validator.update,
               auth: {
                    strategy: 'token',
                    scope: ['user']
                }
        }
        },
        {
            method: 'DELETE',
        path:  internals.resourcePath + '/{userId}',
        config : {
               handler: User.destroy,
               validate: Validator.destroy,
               auth: {
                    strategy: 'token',
                    scope: ['user']
                }
        }
        },
        {
            method: 'POST',
            path:  internals.resourcePath + '/signin',
            config : {
               handler: User.signIn,
               validate: Validator.signIn
            }
        },
        {
            method: 'GET',
            path:  internals.resourcePath + '/logout',
            config : {
                handler : User.logOut,
                auth: {
                    strategy: 'token',
                    scope: ['user']
                }
            }
        },
        {
            method: '*',
            path: internals.resourcePath + '/{somethingss*}',
            config : {
                handler: User.methodNotAllowed
            }
        }
    ];
}();

1 个答案:

答案 0 :(得分:2)

我不是有点迟,但以防其他人有这个问题。这是bcrypt的一个问题。我有一个类似的问题,每当我使用bcrypt-nodejs它会给我The following leaks were detected:lr, password,但当我改为reqular bycrypt它没有泄漏。尝试更新您的bycrypt版本。