哈皮实验室 - 实验室实验之间我怎么都共享状态(Hapi Lab - how do I have s

2019-10-24 11:44发布

所以,我提出了用户域,我已经做了我认为一个好的测试套件是这样的:

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();
        });

    });

});

现在我做了产品领域,以及与有需要的访问令牌部分航线。

我如何使用“全局”测试产品领域

VAR用户id,有效负载解码,令牌;

或者什么是在这种情况下打开的方式吗?

更新

之后@马特·哈里森回答

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();
        });

    });


});

它workish我有一个其他问题

约束products_ibfk_1外键( userId )参考文献usersid )ON UPDATE CASCADE)栈:错误:ER_NO_REFERENCED_ROW

Answer 1:

不知道我理解正确你的问题,但我想你问:

如何有共同的实验室实验之间的状态?

简单的答案是将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');
});


文章来源: Hapi Lab - how do I have shared state between lab experiments
标签: hapijs lab