Hapi Lab - 我如何在实验室实验之间共享状态

时间:2015-08-04 10:48:52

标签: hapijs lab

所以我组成了用户域 我已经完成了我认为一套好套房 测试它是这样的:

var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var server = require('../../');
var Jwt = require('jsonwebtoken');
var Nconf = require('nconf');
var apiConfig =  Nconf.get('api');

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

    var userId, payload, decoded, token;

    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) {

            token = response.result.token;
            payload = options.payload;
            Code.expect(response.statusCode).to.equal(201);
            try {
                decoded = Jwt.verify(token, apiConfig.secret);
            }
            catch(err) {
            }
            console.log(decoded.scope);
            Code.expect(decoded.username).to.equal(payload.username);
            Code.expect(decoded.scope).to.be.an.array();
            userId = decoded.jti;
            done();
        });

    });

    lab.test('create sequelize unique', 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 result = response.result;
            Code.expect(result.message).to.equal('username must be unique');
            Code.expect(response.statusCode).to.equal(422);
            done();
        });

    });

    lab.test('update at least one required', function (done) {

        var options = {
            method: 'PUT',
            headers:{'Authorization' : 'Bearer ' + token},
            url: '/api/users/'+userId
        };

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

    });

    lab.test('update no password', function (done) {

        var options = {
            method: 'PUT',
            headers:{'Authorization' : 'Bearer ' + token},
            url: '/api/users/'+userId,
            payload: {
                password: 'mysecret_update'
            }
        };

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

    });

    lab.test('update not owner', function (done) {

        var options = {
            method: 'PUT',
            headers:{'Authorization' : 'Bearer ' + token},
            url: '/api/users/'+userId +1,
            payload: {
                firstname: 'Fabio_update'
            }
        };

        server.inject(options, function(response) {
            Code.expect(response.statusCode).to.equal(403);
            done();
        });

    });

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

        var updatevalue = Math.random().toString(36).slice(2);
        var options = {
            method: 'PUT',
            headers:{'Authorization' : 'Bearer ' + token},
            url: '/api/users/'+userId,
            payload: {
                firstname: 'Fabio_'+updatevalue,
                lastname: 'Bedini_'+updatevalue,
                username: 'whisher_'+updatevalue,
                email: 'me_'+updatevalue+'@ilwebdifabio.it'
            }
        };

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

    });

    lab.test('findById not owner', function (done) {

        var options = {
            method: 'GET',
            headers:{'Authorization' : 'Bearer ' + token},
            url: '/api/users/'+userId +1,
        };

        server.inject(options, function(response) {
            Code.expect(response.statusCode).to.equal(403);
            done();
        });

    });

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

        var options = {
            method: 'GET',
            headers:{'Authorization' : 'Bearer ' + token},
            url: '/api/users/'+userId
        };

        server.inject(options, function(response) {
            var result = response.result;
            Code.expect(response.statusCode).to.equal(200);
            Code.expect(result).to.be.instanceof(Object);
            Code.expect(Object.keys(result)).to.have.length(8);
            done();
        });

    });

    lab.test('destroy not owner', function (done) {

        var options = {
            method: 'DELETE',
            headers:{'Authorization' : 'Bearer ' + token},
            url: '/api/users/'+userId+1
        };

        server.inject(options, function(response) {
            Code.expect(response.statusCode).to.equal(403);
            done();
        });

    });

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

        var options = {
            method: 'DELETE',
            headers:{'Authorization' : 'Bearer ' + token},
            url: '/api/users/'+userId
        };

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

    });

});

现在我也在那里制作了产品领域 是一些需要访问令牌的路由。

如何使用'globals'

测试产品域
  

var userId,payload,decoding,token;

或者在这种情况下转向的方式是什么?

更新

在@Matt Harrison回答之后

var getToken = function(){
    var user = {id:1,username:'abcdefghijklmnopqrstuvwxyz'};
    var token = JwtUtil.getUserToken(user).token;
    return token;
}
lab.experiment('Product', function () {



    lab.test('create', function (done) {
        var token = getToken();
        console.log(token);
        var options = {
            method: 'POST',
             headers:{'Authorization' : 'Bearer ' + token},
            url: '/api/products',
            payload: {
                title: 'myproduct'
            }
        };

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

    });


});

它可行我还有另一个问题 与

  

约束products_ibfk_1外键(userId)参考users   (id)ON UPDATE CASCADE)堆栈:错误:ER_NO_REFERENCED_ROW

1 个答案:

答案 0 :(得分:2)

我不确定我是否正确理解了您的问题,但我认为您在问:

  

我如何在实验室实验之间共享状态?

简单的答案是将var userId, payload, decoded, token;移到上层"全球"范围,以便随处可访问。

但我的建议是不要这样做。您的测试用例应该彼此独立。您应该能够添加测试,重新排序并删除它们,而不会影响其他测试用例。否则你就会为自己制造维护噩梦。

如果您需要多个测试用例中的资源,只需为每个测试创建一个新资源:

lab.test('test the thing', function (done) {

    var token = getToken();
    expect(token).to.equal(...);
});

lab.test('test the other thing', function (done) {

    var token = getToken();
    expect(token).to.equal(...);
});

如果您需要更改资源然后测试某些内容,那就是单个测试用例:

lab.test('change something and then test it', function (done) {

    var token = getToken();
    token.prop = 'value';
    expect(token.prop).to.equal('value');
});