From 8726b9d1fdc147a6c62cf8643e3dfa8e9fcb803b Mon Sep 17 00:00:00 2001 From: Vedanta Krishna Date: Thu, 16 Nov 2023 17:52:30 +0530 Subject: [PATCH 1/3] feat: add support to get path of current request in sandbox scripts --- CHANGELOG.yaml | 4 + lib/runner/extensions/event.command.js | 6 +- test/integration/sandbox-libraries/pm.test.js | 498 ++++++++++++++++++ 3 files changed, 507 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 2cc30cae1..9bf25521c 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,3 +1,7 @@ +unreleased: + new features: + - GH-1336 Added support for fetching execution location context through script + 7.35.0: date: 2023-11-02 new features: diff --git a/lib/runner/extensions/event.command.js b/lib/runner/extensions/event.command.js index 23dc1f9a3..74b14e0a5 100644 --- a/lib/runner/extensions/event.command.js +++ b/lib/runner/extensions/event.command.js @@ -457,6 +457,8 @@ module.exports = { shouldSkipExecution = true; }); + const currentEventItem = event.parent && event.parent(); + // finally execute the script this.host.execute(event, { id: executionId, @@ -468,7 +470,9 @@ module.exports = { // legacy options legacy: { _itemId: item.id, - _itemName: item.name + _itemName: item.name, + _itemPath: item.getPath && item.getPath(), + _eventItemName: currentEventItem && currentEventItem.name } }, function (err, result) { this.host.removeAllListeners(EXECUTION_REQUEST_EVENT_BASE + executionId); diff --git a/test/integration/sandbox-libraries/pm.test.js b/test/integration/sandbox-libraries/pm.test.js index f83324661..4794a3c40 100644 --- a/test/integration/sandbox-libraries/pm.test.js +++ b/test/integration/sandbox-libraries/pm.test.js @@ -578,4 +578,502 @@ describe('sandbox library - pm api', function () { }); }); }); + + describe('pm.execution', function () { + describe('.location ', function () { + describe('for requests without valid name property', function () { + before(function (done) { + this.run({ + collection: { + info: { + _postman_id: 'collection-id', + name: 'collection-name' + }, + event: [ + { + listen: 'prerequest', + script: { + exec: [ + 'console.log("path", pm.execution.location)', + 'console.log("current", pm.execution.location.current)' + ], + type: 'text/javascript' + } + } + ], + item: [{ + request: { + auth: { + type: 'noauth' + }, + method: 'GET', + header: [ + { + key: 'fs', + value: '', + type: 'text' + } + ], + url: { + raw: 'postman-echo.com/get', + host: [ + 'postman-echo', + 'com' + ], + path: [ + 'get' + ] + } + }, + response: [], + id: 'request-id' + }] + } + } + , function (_err, results) { + testrun = results; + expect(testrun).to.be.ok; + done(); + }); + }); + + it('should complete the run', function () { + sinon.assert.calledOnce(testrun.start); + sinon.assert.calledOnce(testrun.done); + sinon.assert.calledWith(testrun.done.getCall(0), null); + }); + + it('should correct path property value', function () { + const collPreConsole = testrun.console.getCall(0).args.slice(2); + + expect(collPreConsole).to.deep.include.ordered.members(['path', [ + 'collection-name', + '' + ]]); + }); + + it('should correct current property value', function () { + const collPreConsole = testrun.console.getCall(1).args.slice(2); + + expect(collPreConsole).to.deep.include.ordered.members(['current', 'collection-name']); + }); + }); + + describe('for requests with valid name properties', function () { + before(function (done) { + this.run({ + collection: { + info: { + _postman_id: '230937f7-2c1a-4196-8a19-cf962ae5d15c', + name: 'collection-0', + schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json', + _exporter_id: '767722', + _collection_link: '' + }, + item: [ + { + name: 'folder-1', + id: 'folder-1-id', + item: [ + { + name: 'folder-2', + id: 'folder-2-id', + item: [ + { + name: 'request-2', + id: 'request-2-id', + event: [ + { + listen: 'prerequest', + script: { + exec: [ + // eslint-disable-next-line max-len + 'console.log("request pre path", pm.execution.location)', + // eslint-disable-next-line max-len + 'console.log("request pre current", pm.execution.location.current)' + ], + type: 'text/javascript' + } + }, + { + listen: 'test', + script: { + exec: [ + // eslint-disable-next-line max-len + 'console.log("request test path", pm.execution.location)', + // eslint-disable-next-line max-len + 'console.log("request test current", pm.execution.location.current)' + ], + type: 'text/javascript' + } + } + ], + request: { + method: 'GET', + header: [], + url: { + raw: '{{URL}}', + host: [ + '{{URL}}' + ] + } + }, + response: [] + } + ], + event: [ + { + listen: 'prerequest', + script: { + type: 'text/javascript', + exec: [ + // eslint-disable-next-line max-len + 'console.log("folder pre path", pm.execution.location)', + // eslint-disable-next-line max-len + 'console.log("folder pre current", pm.execution.location.current)' + ] + } + }, + { + listen: 'test', + script: { + type: 'text/javascript', + exec: [ + // eslint-disable-next-line max-len + 'console.log("folder test path", pm.execution.location)', + // eslint-disable-next-line max-len + 'console.log("folder test current", pm.execution.location.current)' + ] + } + } + ] + } + ], + event: [ + { + listen: 'prerequest', + script: { + type: 'text/javascript', + exec: [ + 'console.log("folder pre path", pm.execution.location)', + 'console.log("folder pre current", pm.execution.location.current)' + ] + } + }, + { + listen: 'test', + script: { + type: 'text/javascript', + exec: [ + 'console.log("folder test path", pm.execution.location)', + 'console.log("folder test current", pm.execution.location.current)' + ] + } + } + ] + }, + { + name: 'request-0', + id: 'request-0-id', + event: [ + { + listen: 'prerequest', + script: { + exec: [ + 'console.log("request pre path", pm.execution.location)', + 'console.log("request pre current", pm.execution.location.current)' + ], + type: 'text/javascript' + } + }, + { + listen: 'test', + script: { + exec: [ + 'console.log("request test path", pm.execution.location)', + 'console.log("request test current", pm.execution.location.current)' + ], + type: 'text/javascript' + } + } + ], + request: { + method: 'GET', + header: [], + url: { + raw: '{{URL}}', + host: [ + '{{URL}}' + ] + } + }, + response: [ + { + name: 'New Request', + originalRequest: { + method: 'GET', + header: [], + url: { + raw: 'localhost:3000', + host: [ + 'localhost' + ], + port: '3000' + } + }, + _postman_previewlanguage: null, + header: null, + cookie: [], + body: null + } + ] + } + ], + event: [ + { + listen: 'prerequest', + script: { + type: 'text/javascript', + exec: [ + 'console.log("collection pre path", pm.execution.location)', + 'console.log("collection pre current", pm.execution.location.current)' + ] + } + }, + { + listen: 'test', + script: { + type: 'text/javascript', + exec: [ + 'console.log("collection test path", pm.execution.location)', + 'console.log("collection test current", pm.execution.location.current)' + ] + } + } + ] + } + }, function (_err, results) { + testrun = results; + expect(testrun).to.be.ok; + done(); + }); + }); + + it('should complete the run', function () { + sinon.assert.calledOnce(testrun.start); + sinon.assert.calledOnce(testrun.done); + sinon.assert.calledWith(testrun.done.getCall(0), null); + }); + + describe('when logged from collection pre request script', function () { + it('should log correct value for location', function () { + const collPreConsole = testrun.console.getCall(0).args.slice(2); + + expect(collPreConsole[0]).to.eql('collection pre path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'folder-1', + 'folder-2', + 'request-2' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(1).args.slice(2); + + expect(collPreConsole[0]).to.eql('collection pre current'); + expect(collPreConsole[1]).to.eql('collection-0'); + }); + }); + + + describe('when logged from 1 level nested folder pre request script', function () { + it('should log correct location property value', function () { + const collPreConsole = testrun.console.getCall(2).args.slice(2); + + expect(collPreConsole[0]).to.eql('folder pre path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'folder-1', + 'folder-2', + 'request-2' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(3).args.slice(2); + + expect(collPreConsole[0]).to.eql('folder pre current'); + expect(collPreConsole[1]).to.eql('folder-1'); + }); + }); + + + describe('when logged from 2 level nested folder pre request script', function () { + it('should log correct location property value', function () { + const collPreConsole = testrun.console.getCall(4).args.slice(2); + + expect(collPreConsole[0]).to.eql('folder pre path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'folder-1', + 'folder-2', + 'request-2' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(5).args.slice(2); + + expect(collPreConsole[0]).to.eql('folder pre current'); + expect(collPreConsole[1]).to.eql('folder-2'); + }); + }); + + describe('when logged from request in nested folder pre request script', function () { + it('should log correct location property value', function () { + const collPreConsole = testrun.console.getCall(6).args.slice(2); + + expect(collPreConsole[0]).to.eql('request pre path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'folder-1', + 'folder-2', + 'request-2' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(7).args.slice(2); + + expect(collPreConsole[0]).to.eql('request pre current'); + expect(collPreConsole[1]).to.eql('request-2'); + }); + }); + + + describe('when logged from collection test script', function () { + it('should log correct location property value', function () { + const collPreConsole = testrun.console.getCall(8).args.slice(2); + + expect(collPreConsole[0]).to.eql('collection test path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'folder-1', + 'folder-2', + 'request-2' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(9).args.slice(2); + + expect(collPreConsole[0]).to.eql('collection test current'); + expect(collPreConsole[1]).to.eql('collection-0'); + }); + }); + + describe('when logged from 1 level nested folder test script', function () { + it('should log correct location property value', function () { + const collPreConsole = testrun.console.getCall(10).args.slice(2); + + expect(collPreConsole[0]).to.eql('folder test path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'folder-1', + 'folder-2', + 'request-2' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(11).args.slice(2); + + expect(collPreConsole[0]).to.eql('folder test current'); + expect(collPreConsole[1]).to.eql('folder-1'); + }); + }); + + describe('when logged from 2 level nested folder test script', function () { + it('should log correct location property value', function () { + const collPreConsole = testrun.console.getCall(12).args.slice(2); + + expect(collPreConsole[0]).to.eql('folder test path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'folder-1', + 'folder-2', + 'request-2' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(13).args.slice(2); + + expect(collPreConsole[0]).to.eql('folder test current'); + expect(collPreConsole[1]).to.eql('folder-2'); + }); + }); + + describe('when logged from nested request test script', function () { + it('should log correct location property value', function () { + const collPreConsole = testrun.console.getCall(14).args.slice(2); + + expect(collPreConsole[0]).to.eql('request test path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'folder-1', + 'folder-2', + 'request-2' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(15).args.slice(2); + + expect(collPreConsole[0]).to.eql('request test current'); + expect(collPreConsole[1]).to.eql('request-2'); + }); + }); + + describe('when logged from (non-nested) request pre request script', function () { + it('should log correct location property value', function () { + const collPreConsole = testrun.console.getCall(18).args.slice(2); + + expect(collPreConsole[0]).to.eql('request pre path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'request-0' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(19).args.slice(2); + + expect(collPreConsole[0]).to.eql('request pre current'); + expect(collPreConsole[1]).to.eql('request-0'); + }); + }); + + describe('when logged from (non-nested) request test script', function () { + it('should log correct location property value', function () { + const collPreConsole = testrun.console.getCall(22).args.slice(2); + + expect(collPreConsole[0]).to.eql('request test path'); + expect(collPreConsole[1]).to.eql([ + 'collection-0', + 'request-0' + ]); + }); + + it('should log correct value for current property', function () { + const collPreConsole = testrun.console.getCall(23).args.slice(2); + + expect(collPreConsole[0]).to.eql('request test current'); + expect(collPreConsole[1]).to.eql('request-0'); + }); + }); + }); + }); + }); }); From 25daf04a17233049ee8625ecf96803a5b388c102 Mon Sep 17 00:00:00 2001 From: Udit Vasu Date: Sat, 18 Nov 2023 14:46:35 +0530 Subject: [PATCH 2/3] Update dependencies --- CHANGELOG.yaml | 2 ++ package-lock.json | 14 +++++++------- package.json | 4 ++-- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 9bf25521c..69e124115 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,6 +1,8 @@ unreleased: new features: - GH-1336 Added support for fetching execution location context through script + chores: + - Updated dependencies 7.35.0: date: 2023-11-02 diff --git a/package-lock.json b/package-lock.json index fbb42f048..16c1b701d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6020,9 +6020,9 @@ } }, "postman-collection": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/postman-collection/-/postman-collection-4.2.1.tgz", - "integrity": "sha512-DFLt3/yu8+ldtOTIzmBUctoupKJBOVK4NZO0t68K2lIir9smQg7OdQTBjOXYy+PDh7u0pSDvD66tm93eBHEPHA==", + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/postman-collection/-/postman-collection-4.3.0.tgz", + "integrity": "sha512-QpmNOw1JhAVQTFWRz443/qpKs4/3T1MFrKqDZ84RS1akxOzhXXr15kD8+/+jeA877qyy9rfMsrFgLe2W7aCPjw==", "requires": { "@faker-js/faker": "5.5.3", "file-type": "3.9.0", @@ -6077,12 +6077,12 @@ } }, "postman-sandbox": { - "version": "4.3.0", - "resolved": "https://registry.npmjs.org/postman-sandbox/-/postman-sandbox-4.3.0.tgz", - "integrity": "sha512-CRfY4WbRjxWc6xKWJWMYgLHzvKudB4yz/n5Q/CyFYHIIqZ2boM3+/HoSW0uuw8r5i814lftc690doZ5vfvCnqQ==", + "version": "4.4.0", + "resolved": "https://registry.npmjs.org/postman-sandbox/-/postman-sandbox-4.4.0.tgz", + "integrity": "sha512-NvNodj44vqtZrgLvnqoqFxvqRL1Pw0MSLXGlzSu252iurmHCz59wUykNUQ8mQh0AXNM00gbN/lS1nrGfS1zqwA==", "requires": { "lodash": "4.17.21", - "postman-collection": "4.2.1", + "postman-collection": "4.3.0", "teleport-javascript": "1.0.0", "uvm": "2.1.1" } diff --git a/package.json b/package.json index 585a7d426..a1fd8512d 100644 --- a/package.json +++ b/package.json @@ -54,9 +54,9 @@ "node-forge": "1.3.1", "node-oauth1": "1.3.0", "performance-now": "2.1.0", - "postman-collection": "4.2.1", + "postman-collection": "4.3.0", "postman-request": "2.88.1-postman.33", - "postman-sandbox": "4.3.0", + "postman-sandbox": "4.4.0", "postman-url-encoder": "3.0.5", "serialised-error": "1.1.3", "strip-json-comments": "3.1.1", From 10141d439fa7c1a08622588cc1d197b15b814737 Mon Sep 17 00:00:00 2001 From: Udit Vasu Date: Sat, 18 Nov 2023 14:56:14 +0530 Subject: [PATCH 3/3] Fix test --- test/integration/sandbox-libraries/pm.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/sandbox-libraries/pm.test.js b/test/integration/sandbox-libraries/pm.test.js index 4794a3c40..bc38007a4 100644 --- a/test/integration/sandbox-libraries/pm.test.js +++ b/test/integration/sandbox-libraries/pm.test.js @@ -648,7 +648,7 @@ describe('sandbox library - pm api', function () { expect(collPreConsole).to.deep.include.ordered.members(['path', [ 'collection-name', - '' + null // FIXME: should be empty string ]]); });