From 285b940d329435f6d4078ddcfc153d7d512ef468 Mon Sep 17 00:00:00 2001 From: Vedanta Krishna <37595282+vedkribhu@users.noreply.github.com> Date: Sat, 18 Nov 2023 15:01:43 +0530 Subject: [PATCH] Added support for accessing execution location through script (#1336) --- CHANGELOG.yaml | 6 + lib/runner/extensions/event.command.js | 6 +- package-lock.json | 14 +- package.json | 4 +- test/integration/sandbox-libraries/pm.test.js | 498 ++++++++++++++++++ 5 files changed, 518 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.yaml b/CHANGELOG.yaml index 2cc30cae1..69e124115 100644 --- a/CHANGELOG.yaml +++ b/CHANGELOG.yaml @@ -1,3 +1,9 @@ +unreleased: + new features: + - GH-1336 Added support for fetching execution location context through script + chores: + - Updated dependencies + 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/package-lock.json b/package-lock.json index 836a19893..d671c7033 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6101,9 +6101,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", @@ -6158,12 +6158,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", diff --git a/test/integration/sandbox-libraries/pm.test.js b/test/integration/sandbox-libraries/pm.test.js index f83324661..bc38007a4 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', + null // FIXME: should be empty string + ]]); + }); + + 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'); + }); + }); + }); + }); + }); });