Skip to content

Commit

Permalink
Removed logic to parse cli args and extract collection and environmen…
Browse files Browse the repository at this point in the history
…t uids and apikey from postman reporter.

This info will now be stored in options which is sent to the reporter.
Moved utility functions to extract the above from postman reporter to newman util.
Added test cases for the utility functions.
Cleanup for existing tests based on above.
  • Loading branch information
harsh-bansal97 committed Jun 6, 2022
1 parent 163404b commit 18e8fe3
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 283 deletions.
20 changes: 1 addition & 19 deletions lib/reporters/postman/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,5 @@ module.exports = {
/**
* Used as a fall back error message for the upload API call
*/
RESPONSE_FALLBACK_ERROR_MESSAGE: 'Error occurred while uploading newman run data to Postman',

/**
* Regex pattern to extract the collection id from the postman api collection url
*/
COLLECTION_UID_FROM_URL_EXTRACTION_PATTERN:
/https?:\/\/api\.(?:get)?postman.*\.com\/(?:collections)\/([A-Za-z0-9-]+)/,

/**
* Regex pattern to extract the environment id from the postman api environment url
*/
ENVIRONMENT_UID_FROM_URL_EXTRACTION_PATTERN:
/https?:\/\/api\.(?:get)?postman.*\.com\/(?:environments)\/([A-Za-z0-9-]+)/,

/**
* Regex pattern to extract the api key from the postman api collection url
*/
API_KEY_FROM_URL_EXTRACTION_PATTERN:
/https?:\/\/api.(?:get)?postman.com\/([a-z]+)s\/([a-z0-9-]+)\?apikey=([a-z0-9A-Z-]+)/
RESPONSE_FALLBACK_ERROR_MESSAGE: 'Error occurred while uploading newman run data to Postman'
};
127 changes: 0 additions & 127 deletions lib/reporters/postman/helpers/util.js

This file was deleted.

9 changes: 4 additions & 5 deletions lib/reporters/postman/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const _ = require('lodash'),

print = require('../../print'),
util = require('./helpers/util'),
{ RESPONSE_FALLBACK_ERROR_MESSAGE } = require('./helpers/constants'),
uploadUtil = require('./helpers/upload-run');

Expand All @@ -20,14 +20,13 @@ function PostmanReporter (newman, _reporterOptions, collectionRunOptions) {

// We get the collection id in the collectionRunOptions. But that collection id has the user id stripped off
// Hence, we try to parse the CLI args to extract the whole collection id from it.
const processArgs = process.argv,
{ collection, environment } = util.parseCLIArguments(processArgs),

const collection = _.get(collectionRunOptions, 'cachedArgs.collectionUID'),
environment = _.get(collectionRunOptions, 'cachedArgs.environmentUID'),
// If api key is not present in environment variables, we check if it has been passed
// seperately as CLI args else we try to get it from collection postman api url
// eslint-disable-next-line no-process-env
postmanApiKey = process.env.POSTMAN_API_KEY ||
collectionRunOptions.postmanApiKey || util.getAPIKeyFromCLIArguments(processArgs);
collectionRunOptions.postmanApiKey || _.get(collectionRunOptions, 'cachedArgs.postmanApiKey');

if (!collection) {
print.lf('Publishing run details to postman cloud is currently supported only for collections specified ' +
Expand Down
7 changes: 7 additions & 0 deletions lib/run/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ module.exports = function (options, callback) {
// allow insecure file read by default
options.insecureFileRead = Boolean(_.get(options, 'insecureFileRead', true));

// store collection, environment uid and postman api key in options if specified using postman public api link
options.cachedArgs = {
collectionUID: util.extractCollectionId(options.collection),
environmentUID: util.extractEnvironmentId(options.environment),
postmanApiKey: util.extractPostmanApiKey(options.collection)
};

config.get(options, { loaders: configLoaders, command: 'run' }, function (err, result) {
if (err) { return callback(err); }

Expand Down
84 changes: 83 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,19 @@ var fs = require('fs'),

// Matches valid Postman UID, case insensitive.
// Same used for validation on the Postman API side.
UID_REGEX = /^[0-9A-Z]+-[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
UID_REGEX = /^[0-9A-Z]+-[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,

// Regex pattern to extract the collection id from the postman api collection url
COLLECTION_UID_FROM_URL_EXTRACTION_PATTERN =
/https?:\/\/api\.(?:get)?postman.*\.com\/(?:collections)\/([A-Za-z0-9-]+)/,

// Regex pattern to extract the environment id from the postman api environment url
ENVIRONMENT_UID_FROM_URL_EXTRACTION_PATTERN =
/https?:\/\/api\.(?:get)?postman.*\.com\/(?:environments)\/([A-Za-z0-9-]+)/,

// Regex pattern to extract the api key from the postman api collection url
API_KEY_FROM_URL_EXTRACTION_PATTERN =
/https?:\/\/api.(?:get)?postman.com\/[a-z]+s\/[a-z0-9-]+\?apikey=([a-z0-9A-Z-]+)/;

util = {

Expand Down Expand Up @@ -284,6 +296,76 @@ util = {
*/
isFloat: function (value) {
return (value - parseFloat(value) + 1) >= 0;
},

/**
* Extracts the collection id
*
* @private
* @param {String} resourceUrl - should be of the form `https://api.getpostman.com/collections/:collection-id
* @returns {String}
*/
extractCollectionId: function (resourceUrl) {
if (!_.isString(resourceUrl)) {
return '';
}

const result = COLLECTION_UID_FROM_URL_EXTRACTION_PATTERN.exec(resourceUrl);

if (result) {
// The returned array has the matched text as the first item and then
// one item for each parenthetical capture group of the matched text.
return _.nth(result, 1);
}

return '';
},

/**
* Extracts the environment id
*
* @private
* @param {String} resourceUrl - should be of the form `https://api.getpostman.com/environments/:environment-id
* @returns {String}
*/
extractEnvironmentId: function (resourceUrl) {
if (!_.isString(resourceUrl)) {
return '';
}

const result = ENVIRONMENT_UID_FROM_URL_EXTRACTION_PATTERN.exec(resourceUrl);

if (result) {
// The returned array has the matched text as the first item and then
// one item for each parenthetical capture group of the matched text.
return _.nth(result, 1);
}

return '';
},

/**
* Extracts the api key from collection postman public api link
*
* @private
* @param {String} resourceUrl -
* should be of the form `https://api.getpostman.com/collections/:collection-id?apikey=:apikey
* @returns {String}
*/
extractPostmanApiKey: function (resourceUrl) {
if (!_.isString(resourceUrl)) {
return '';
}

const result = API_KEY_FROM_URL_EXTRACTION_PATTERN.exec(resourceUrl);

if (result) {
// The returned array has the matched text as the first item and then
// one item for each parenthetical capture group of the matched text.
return _.nth(result, 1);
}

return '';
}
};

Expand Down
35 changes: 1 addition & 34 deletions test/unit/postman-reporter/postman-reporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const sinon = require('sinon'),

print = require('../../../lib/print'),
upload = require('../../../lib/reporters/postman/helpers/upload-run'),
util = require('../../../lib/reporters/postman/helpers/util'),

COLLECTION = {
id: 'C1',
Expand Down Expand Up @@ -47,21 +46,13 @@ describe('Postman reporter', function () {

it('should print informational message if api key is not found', function (done) {
const collectionUID = '1234-588025f9-2497-46f7-b849-47f58b865807',
apiKey = '12345678',
apiKey = '',
collectionPostmanURL = `https://api.getpostman.com/collections/${collectionUID}?apikey=${apiKey}`;

nock('https://api.getpostman.com')
.get(/^\/collections/)
.reply(200, COLLECTION);

sinon.stub(util, 'parseCLIArguments').callsFake(() => {
return { collection: collectionUID };
});

sinon.stub(util, 'getAPIKeyFromCLIArguments').callsFake(() => {
return '';
});

sinon.spy(print, 'lf');

newman.run({
Expand Down Expand Up @@ -91,14 +82,6 @@ describe('Postman reporter', function () {
return callback(new Error('Error message'));
});

sinon.stub(util, 'parseCLIArguments').callsFake(() => {
return { collection: collectionUID };
});

sinon.stub(util, 'getAPIKeyFromCLIArguments').callsFake(() => {
return apiKey;
});

sinon.spy(print, 'lf');

newman.run({
Expand Down Expand Up @@ -137,14 +120,6 @@ describe('Postman reporter', function () {
return callback(null, uploadRunResponse);
});

sinon.stub(util, 'parseCLIArguments').callsFake(() => {
return { collection: collectionUID, environment: environmentUID };
});

sinon.stub(util, 'getAPIKeyFromCLIArguments').callsFake(() => {
return apiKey;
});

sinon.spy(print, 'lf');

newman.run({
Expand Down Expand Up @@ -181,14 +156,6 @@ describe('Postman reporter', function () {
return callback(null, uploadRunResponse);
});

sinon.stub(util, 'parseCLIArguments').callsFake(() => {
return { collection: collectionUID };
});

sinon.stub(util, 'getAPIKeyFromCLIArguments').callsFake(() => {
return apiKey;
});

sinon.spy(print, 'lf');

newman.run({
Expand Down

0 comments on commit 18e8fe3

Please sign in to comment.