diff --git a/lib/sandbox/pmapi.js b/lib/sandbox/pmapi.js index 06b0f01e..002d6691 100644 --- a/lib/sandbox/pmapi.js +++ b/lib/sandbox/pmapi.js @@ -144,7 +144,7 @@ function Postman (execution, onRequest, onSkipRequest, onAssertion, cookieStore, * @name Vault#set * @param {string} key - * @param {string} value - - * @returns {Promise} + * @returns {Promise} */ /** * Unset a value in the vault. diff --git a/lib/sandbox/vault.js b/lib/sandbox/vault.js index dbf49d61..4135c131 100644 --- a/lib/sandbox/vault.js +++ b/lib/sandbox/vault.js @@ -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); }); }, set: (key, value) => { - return vault('set', key, value); + return vault('set', key, value).then(() => { return value; }); }, unset: (key) => { diff --git a/test/unit/sandbox-libraries/pm.test.js b/test/unit/sandbox-libraries/pm.test.js index 453dd11a..5bf01675 100644 --- a/test/unit/sandbox-libraries/pm.test.js +++ b/test/unit/sandbox-libraries/pm.test.js @@ -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'; @@ -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(`