Skip to content

Commit

Permalink
Merge pull request #217 from postmanlabs/feature/collection-level-var…
Browse files Browse the repository at this point in the history
…iables-sandbox

Allow sandbox to work with collection variables by exposing `collectionVariables` in execution
  • Loading branch information
kunagpal authored Oct 20, 2017
2 parents 880a243 + e73bf8a commit a26dbb6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
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'
}],
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

0 comments on commit a26dbb6

Please sign in to comment.