Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sandbox to work with collection variables by exposing collectionVariables in execution #217

Merged
merged 6 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/sandbox/execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Execution = function (id, event, context, options) {
context.environment : new sdk.VariableScope(context.environment);
this.globals = sdk.VariableScope.isVariableScope(context.globals) ?
context.globals : new sdk.VariableScope(context.globals);
this.collectionVariables = sdk.VariableScope.isVariableScope(context.collectionVariables) ?
context.collectionVariables : new sdk.VariableScope(context.collectionVariables);

if (TARGETS_WITH_REQUEST[this.target] || _.has(context, PROPERTY.REQUEST)) {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Postman = function Postman (bridge, execution, onRequest) {
* @type {VariableScope}
*/
// eslint-disable-next-line max-len
variables: new VariableScope(null, [iterationData.values, execution.environment.values, execution.globals.values]),
variables: new VariableScope(null, [iterationData.values, execution.environment.values, execution.collectionVariables.values, execution.globals.values]),

/**
* @type {VariableScope}
Expand Down
89 changes: 89 additions & 0 deletions test/unit/sandbox-libraries/pm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ describe('sandbox library - pm api', function () {
value: 2.5,
type: 'number'
}],
collectionVariables: [{
key: 'var1',
value: 'collection-var1',
type: 'string'
}, {
key: 'var3',
value: 'collection-var3'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason for skipping the type here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, I threw in some of these randomly to make sure legacy variables work

}],
data: {
'var1': 'one-data'
}
Expand Down Expand Up @@ -497,6 +505,86 @@ describe('sandbox library - pm api', function () {
assert.strictEqual(pm.variables.values.members[0].key, 'var1');
`, {context: sampleContextData}, done);
});

it('should get from collectionVariables', function (done) {
var contextData = {
collectionVariables: [{
key: 'var1',
value: 1,
type: 'number'
}, {
key: 'var2',
value: 'two'
}, {
key: 'var3',
value: 3,
type: 'number'
}]
};

context.execute(`
var assert = require('assert');

assert.strictEqual(pm.variables.get('var1'), 1);
assert.strictEqual(pm.variables.get('var2'), 'two');

// can set variables with same keys as collectionVariables
assert.strictEqual(pm.variables.get('var3'), 3);
pm.variables.set('var3', 'hola');
assert.strictEqual(pm.variables.get('var3'), 'hola');
`, {context: contextData}, done);
});

it('should maintain environment > collectionVariables > globals precedence', function (done) {
var contextData = {
environment: [{
key: 'var1',
value: 1,
type: 'number'
}, {
key: 'var2',
value: 'two-env'
}],
collectionVariables: [{
key: 'var1',
value: 2,
type: 'number'
}, {
key: 'var3',
value: 'three-collection'
}, {
key: 'var4',
value: 4,
type: 'number'
}],
globals: [{
key: 'var3',
value: 'three-global'
}, {
key: 'var5',
value: 'five-global'
}]
};

context.execute(`
var assert = require('assert');

assert.strictEqual(pm.variables.get('var1'), 1); // env overrides collection
assert.strictEqual(pm.variables.get('var2'), 'two-env'); // direct env
assert.strictEqual(pm.variables.get('var3'), 'three-collection'); // collection overrides global
assert.strictEqual(pm.variables.get('var4'), 4); // direct collection
assert.strictEqual(pm.variables.get('var5'), 'five-global'); // direct global
// @todo: unskip after VariableScope.prototype.toObject is fixed
// assert.deepEqual(pm.variables.toObject(), {
// var1: 1,
// var2: 'two-env',
// var3: 'three-collection',
// var4: 4,
// var5: 'five-global'
// });
`, {context: contextData}, done);
});

it('pm.variables.toObject must return a pojo', function (done) {
context.execute(`
var assert = require('assert');
Expand All @@ -507,6 +595,7 @@ describe('sandbox library - pm api', function () {
assert.deepEqual(pm.variables.toObject(), {
var1: 'one',
var2: 2,
var3: 'collection-var3',
foo: 'bar',
});
`, {context: sampleContextData}, done);
Expand Down