diff --git a/test/unit/pm-variables.test.js b/test/unit/pm-variables.test.js index 55e1167f..b5d12a40 100644 --- a/test/unit/pm-variables.test.js +++ b/test/unit/pm-variables.test.js @@ -28,6 +28,19 @@ describe('pm.variables', function () { `, done); }); + it('should not respect variables set using pm.vault', function (done) { + const executionId = '1'; + + ctx.on('execution.vault.' + executionId, (id, _, k, v) => { + ctx.dispatch('execution.vault.' + executionId, id, k, v); + }); + ctx.execute(` + var assert = require('assert'); + await pm.vault.set('key-1', 'value-1'); + assert.strictEqual(pm.variables.get('key-1'), undefined); + `, { id: executionId }, done); + }); + describe('.set', function () { before(function (done) { var globalVarList = new sdk.VariableList(null, { key: 'key-1', value: 'value-1' }), diff --git a/test/unit/sandbox-libraries/pm.test.js b/test/unit/sandbox-libraries/pm.test.js index c9687458..453dd11a 100644 --- a/test/unit/sandbox-libraries/pm.test.js +++ b/test/unit/sandbox-libraries/pm.test.js @@ -267,71 +267,111 @@ describe('sandbox library - pm api', function () { describe('vault', function () { it('should only have get, set and unset properties', function (done) { context.execute(` - var assert = require('assert'); - assert.deepEqual(Object.keys(pm.vault), ['get', 'set', 'unset']); + const assert = require('assert'); + function allKeys(obj) { + if (!Object.isObject(obj)) return []; + const keys = []; + for (var key in obj) keys.push(key); + return keys; + } + + assert.deepEqual(allKeys(pm.vault), ['get', 'set', 'unset']); + assert.deepEqual(typeof pm.vault.get, 'function'); + assert.deepEqual(typeof pm.vault.set, 'function'); + assert.deepEqual(typeof pm.vault.unset, 'function'); `, {}, done); }); - it('should send correct events and receive data for pm.vault operations', function (done) { - const executionId = '2', - getErrorEventHandler = function (callback) { - // errors from the execute callback are catched here as well - // so, call mocha `done` callback with an error - // @todo this is not supposed to happen, fix this - return function () { - callback(new Error('Assertion Error')); - }; - }, - executionError = sinon.spy(getErrorEventHandler(done)); + it('should be a readonly property', function (done) { + context.execute(` + var assert = require('assert'), + _vault; - context.on('execution.error', executionError); - // eslint-disable-next-line no-unused-vars - context.on('execution.vault.' + executionId, (eventId, _cmd, ..._args) => { - context.dispatch(`execution.vault.${executionId}`, eventId, null, 'value2'); + _vault = pm.vault; + pm.vault = []; + + assert.strictEqual(pm.vault, _vault, 'property stays unchanged'); + `, done); + }); + + it('should dispatch and wait for `execution.vault.id` event when pm.vault.get is called', function (done) { + const executionId = '2'; + + context.on('execution.error', done); + context.on('execution.assertion', function (cursor, assertion) { + assertion.forEach(function (ass) { + expect(ass).to.deep.include({ + passed: true, + error: null + }); + }); + done(); + }); + context.on('execution.vault.' + executionId, (eventId, cmd, k) => { + expect(eventId).to.be.ok; + expect(cmd).to.eql('get'); + expect(k).to.eql('key'); + + context.dispatch(`execution.vault.${executionId}`, eventId, null, 'value'); }); context.execute(` - var assert = require('assert'); - const val = await pm.vault.get('key1'); - assert.equal(val, "value2"); - `, { - id: executionId - }, function (err) { - if (err) { - return done(err); - } + const val = await pm.vault.get('key'); + pm.test('vault.get', function () { + pm.expect(val).to.equal('value'); + }); + `, { id: executionId }); + }); - done(); + it('should dispatch and wait for `execution.vault.id` event when pm.vault.set is called', function (done) { + const executionId = '2'; + + context.on('execution.error', done); + context.on('execution.vault.' + executionId, (eventId, cmd, k, v) => { + expect(eventId).to.be.ok; + expect(cmd).to.eql('set'); + expect(k).to.eql('key'); + expect(v).to.eql('val'); + + context.dispatch(`execution.vault.${executionId}`, eventId, null); + }); + context.execute(` + await pm.vault.set('key', 'val'); + `, { id: executionId }, done); + }); + + it('should dispatch and wait for `execution.vault.id` event when pm.vault.unset called', function (done) { + const executionId = '2'; + + context.on('execution.error', done); + context.on('execution.vault.' + executionId, (eventId, cmd, k) => { + expect(eventId).to.be.ok; + expect(cmd).to.eql('unset'); + expect(k).to.eql('key'); + + context.dispatch(`execution.vault.${executionId}`, eventId, null); }); + context.execute(` + const val = await pm.vault.unset('key'); + `, { id: executionId }, done); }); - it('should throw error if pm.vault. promise rejects', function (done) { + + it('should trigger `execution.error` event if pm.vault. promise rejects', function (done) { const executionId = '2', executionError = sinon.spy(); context.on('execution.error', (...args) => { executionError(args); }); - // eslint-disable-next-line no-unused-vars - context.on('execution.vault.' + executionId, (eventId, _cmd, ..._args) => { + context.on('execution.vault.' + executionId, (eventId) => { context.dispatch(`execution.vault.${executionId}`, eventId, new Error('Vault access denied')); }); context.execute(` - var assert = require('assert'); const val = await pm.vault.get('key1'); - assert.equal(val, "value2"); `, { id: executionId }, function () { expect(executionError.calledOnce).to.be.true; - expect(executionError.firstCall.args[0]).to.eql([ - { - execution: '2' - }, - { - message: 'Vault access denied', - name: 'Error', - type: 'Error' - } - ]); + expect(executionError.firstCall.args[0][1]).to.have.property('message', 'Vault access denied'); done(); }); }); diff --git a/test/unit/vault.test.js b/test/unit/vault.test.js deleted file mode 100644 index 84a9d585..00000000 --- a/test/unit/vault.test.js +++ /dev/null @@ -1,166 +0,0 @@ -const { VaultExternal, Vault } = require('../../lib/sandbox/vault'); - -describe('vault', function () { - describe('Vault', function () { - it('should set up vault', function () { - const bridge = { - on: sinon.spy() - }, - timers = {}, - vault = new Vault('', bridge, timers); - - expect(vault._event).to.eql('execution.vault.'); - }); - it('should close connection on calling dispose()', function () { - const bridge = { - on: sinon.spy(), - off: sinon.spy() - }, - timers = {}, - vault = new Vault('', bridge, timers); - - vault.dispose(); - - expect(bridge.off.calledWith('execution.vault.')).to.be.true; - }); - it('should call bridge dispath on calling exec', async function () { - const bridge = { - on: sinon.spy(), - off: sinon.spy(), - dispatch: sinon.spy() - }, - timers = { - setEvent: (fn) => { return fn(); } - }, - vault = new Vault('', bridge, timers); - - await vault.exec('', ''); - - expect(bridge.dispatch.called).to.be.true; - }); - it('should return value on calling exec', async function () { - const bridge = { - on: sinon.spy(), - off: sinon.spy(), - dispatch: () => { return 0; } - }, - timers = { - setEvent: (fn) => { return fn(null, ''); } - }, - vault = new Vault('', bridge, timers), - - res = await vault.exec('', ''); - - expect(res).to.eql(''); - }); - it('should trigger handler to return value', async function () { - let callHandler, promise, resolve; - - promise = new Promise((res) => { - resolve = res; - }); - - const bridge = { - on: (event, handler) => { - callHandler = () => { return handler(); }; - }, - off: sinon.spy(), - dispatch: () => { return 0; } - }, - timers = { - // setEvent: (fn) => { return fn(null, ''); } - setEvent: async (fn) => { - await promise; - - return fn(null, ''); - }, - clearEvent: () => { - resolve(''); - } - }, - vault = new Vault('', bridge, timers); - - vault._dispatch = callHandler; - - // eslint-disable-next-line one-var - const res = await vault.exec('', ''); - - expect(res).to.eql(''); - }); - it('should handle thrown error', async function () { - const bridge = { - on: sinon.spy(), - off: sinon.spy(), - dispatch: () => { return 0; } - }, - timers = { - setEvent: (fn) => { return fn(new Error('Failed')); } - }, - vault = new Vault('', bridge, timers); - - let errorMsg; - - try { - await vault.exec('', ''); - } - catch (err) { - errorMsg = err.message; - } - - expect(errorMsg).to.eql('Failed'); - }); - it('should handle error', async function () { - const bridge = { - on: sinon.spy(), - off: sinon.spy(), - dispatch: () => { return 0; } - }, - timers = { - setEvent: (fn) => { return fn('Failed'); } - }, - vault = new Vault('', bridge, timers); - - let errorMsg; - - try { - await vault.exec('', ''); - } - catch (err) { - errorMsg = err.message; - } - - expect(errorMsg).to.eql('Failed'); - }); - }); - - describe('VaultExternal', function () { - it('VaultExternal#get', function () { - const vaultFn = sinon.spy(), - - vault = VaultExternal(vaultFn); - - vault.get(''); - - expect(vaultFn.calledWith('get', '')).to.be.true; - }); - it('VaultExternal#set', function () { - const vaultFn = sinon.spy(), - - vault = VaultExternal(vaultFn); - - vault.set('', ''); - - expect(vaultFn.calledWith('set', '', '')).to.be.true; - }); - - it('VaultExternal#unset', function () { - const vaultFn = sinon.spy(), - - vault = VaultExternal(vaultFn); - - vault.unset(''); - - expect(vaultFn.calledWith('unset', '')).to.be.true; - }); - }); -});