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

Enhance pm.vault API with improved get and set methods #1059

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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: 1 addition & 1 deletion lib/sandbox/pmapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore,
* @name Vault#set
* @param {string} key -
* @param {string} value -
* @returns {Promise<void>}
* @returns {Promise<string>}
*/
/**
* Unset a value in the vault.
Expand Down
4 changes: 2 additions & 2 deletions lib/sandbox/vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class Vault {
const getVaultInterface = (vault) => {
return {
get: (key) => {
return vault('get', key);
return vault('get', key).then((value) => { return (value === null ? undefined : value); });
Copy link
Member

Choose a reason for hiding this comment

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

Why is this coming null in the first place? This looks like a patch rather than fixing the root cause

},

set: (key, value) => {
return vault('set', key, value);
return vault('set', key, value).then(() => { return value; });
Copy link
Member

Choose a reason for hiding this comment

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

Why are we making this change?

.set returns undefined for other variable scopes.

},

unset: (key) => {
Expand Down
77 changes: 77 additions & 0 deletions test/unit/sandbox-libraries/pm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,28 @@ describe('sandbox library - pm api', function () {
`, { id: executionId });
});

it('should return a promise for all vault operations', function (done) {
context.execute(`
var assert = require('assert');
assert.strictEqual(pm.vault.get('key') instanceof Promise, true);
assert.strictEqual(pm.vault.set('key', 'value') instanceof Promise, true);
assert.strictEqual(pm.vault.unset('key') instanceof Promise, true);
`, {
context: {
vaultSecrets: {}
}
}, done);
});

it('should not allow access to pm.vault when vaultSecrets is not set', function (done) {
context.execute(`
var assert = require('assert');
assert.strictEqual(typeof pm.vault, 'undefined');
`, {
context: {}
}, done);
});

it('should dispatch and wait for `execution.vault.id` event when pm.vault.set is called', function (done) {
const executionId = '2';

Expand Down Expand Up @@ -377,6 +399,61 @@ describe('sandbox library - pm api', function () {
});
});

it('should return undefined for null values in get method', function (done) {
const executionId = '3';

context.on('execution.vault.' + executionId, (eventId, cmd, k) => {
expect(eventId).to.be.ok;
expect(cmd).to.eql('get');
expect(k).to.eql('nullKey');

context.dispatch(`execution.vault.${executionId}`, eventId, null, null);
});
context.execute(`
const val = await pm.vault.get('nullKey');
pm.test('vault.get with null value', function () {
pm.expect(val).to.be.undefined;
});
`, { id: executionId }, done);
});

it('should return the value for non-null values in get method', function (done) {
const executionId = '4';

context.on('execution.vault.' + executionId, (eventId, cmd, k) => {
expect(eventId).to.be.ok;
expect(cmd).to.eql('get');
expect(k).to.eql('nonNullKey');

context.dispatch(`execution.vault.${executionId}`, eventId, null, 'nonNullValue');
});
context.execute(`
const val = await pm.vault.get('nonNullKey');
pm.test('vault.get with non-null value', function () {
pm.expect(val).to.equal('nonNullValue');
});
`, { id: executionId }, done);
});

it('should return the set value in set method', function (done) {
const executionId = '5';

context.on('execution.vault.' + executionId, (eventId, cmd, k, v) => {
expect(eventId).to.be.ok;
expect(cmd).to.eql('set');
expect(k).to.eql('testKey');
expect(v).to.eql('testValue');

context.dispatch(`execution.vault.${executionId}`, eventId, null);
});
context.execute(`
const val = await pm.vault.set('testKey', 'testValue');
pm.test('vault.set return value', function () {
pm.expect(val).to.equal('testValue');
});
`, { id: executionId }, done);
});

describe('request', function () {
it('should be defined as sdk Request object', function (done) {
context.execute(`
Expand Down
Loading