Hapi Lab - how do I have shared state between lab experiments

微笑、不失礼 提交于 2019-12-13 00:15:20

问题


So I made up the user domain and I've done I think a good suite test it's like this:

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

    });

});

now I made up the product domain as well and there is some routes needed access token.

How can I test the product domain using the 'globals'

var userId, payload, decoded, token;

or what's the way to turn in this case ?

Update

after @Matt Harrison answer

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

    });


});

it workish I've an other problem with

CONSTRAINT products_ibfk_1 FOREIGN KEY (userId) REFERENCES users (id) ON UPDATE CASCADE) stack: Error: ER_NO_REFERENCED_ROW


回答1:


Not sure I have understood your question correctly but I think you're asking:

How do I have shared state between lab experiments?

The simple answer is to move var userId, payload, decoded, token; into the upper "global" scope so they can be accessed everywhere.

However my advice is don't do this. Your test cases should be independent from each other. You should be able to add tests, reorder them and remove them without it affecting other test cases. Otherwise you're creating a maintenance nightmare for yourself.

If you need a resource in multiple test cases, just create a new one for each test:

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

If you need to change a resource and then test something, that's a single test case:

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

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


来源:https://stackoverflow.com/questions/31807290/hapi-lab-how-do-i-have-shared-state-between-lab-experiments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!