From 0911d77d750ee0909622b8bc9d70ef278ff36d82 Mon Sep 17 00:00:00 2001 From: Samith Bharadwaj Date: Fri, 12 May 2023 22:00:16 +0530 Subject: [PATCH 1/2] [CUE-3798] Adding flag to capture collection and request fields that can be reset --- lib/converters/v2.0.0/converter-v2-to-v1.js | 19 ++++++++++++++----- lib/converters/v2.1.0/converter-v21-to-v1.js | 5 ++++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/converters/v2.0.0/converter-v2-to-v1.js b/lib/converters/v2.0.0/converter-v2-to-v1.js index 1e120f42..b05087e8 100644 --- a/lib/converters/v2.0.0/converter-v2-to-v1.js +++ b/lib/converters/v2.0.0/converter-v2-to-v1.js @@ -15,7 +15,7 @@ _.assign(Builders.prototype, { * * @param {Object} entityV2 - The v1 auth manifest to be transformed into v1. * @param {?Object} options - The set of options for the current auth cleansing operation. - * @param {?Boolean} [options.includeNoauth=false] - When set to true, noauth is set to null. + * @param {?Boolean} [options.excludeNoauth=false] - When set to true, noauth is set to null. * * @returns {Object} The transformed auth object. */ @@ -463,6 +463,8 @@ _.assign(Builders.prototype, { variables = self.variables(item, { retainIds: self.options.retainIds }), url = req && req.url, retainEmpty = self.options.retainEmptyValues, + allowAuthReset = _.get(self.options, 'allowFieldResets', []).includes('auth'), + allowDescriptionReset = _.get(self.options, 'allowFieldResets', []).includes('description'), handlePartial = self.options.handlePartial, urlObj = _.isString(url) ? util.urlparse(url) : url, headers = req && (req.headers || req.header), @@ -514,9 +516,11 @@ _.assign(Builders.prototype, { // Prevent empty request descriptions from showing up in the converted result, keeps collections clean. if (description) { request.description = description; } - else if (retainEmpty) { request.description = null; } + else if (retainEmpty || allowDescriptionReset && item.request.description === null) { request.description = null; } - (auth || (auth === null)) && (request.auth = auth); + if(allowAuthReset && auth === null){ + request.auth = null; + } else (auth || (auth === null)) && (request.auth = auth); events && events.length && (request.events = events); variables && variables.length && (request.variables = variables); @@ -691,11 +695,16 @@ _.assign(Builders.prototype, { description: function (descriptionV2) { var description, retainEmpty = this.options.retainEmptyValues; + allowReset = _.get(this.options, 'allowFieldResets', []).includes('description') description = _.isObject(descriptionV2) ? descriptionV2.content : descriptionV2; - if (description) { return description; } - else if (retainEmpty) { return null; } + if (description) { + return description; + } else if (retainEmpty || (allowReset && descriptionV2 === null)) { + return null; + } + } }); diff --git a/lib/converters/v2.1.0/converter-v21-to-v1.js b/lib/converters/v2.1.0/converter-v21-to-v1.js index a822f893..bc37a721 100644 --- a/lib/converters/v2.1.0/converter-v21-to-v1.js +++ b/lib/converters/v2.1.0/converter-v21-to-v1.js @@ -98,6 +98,7 @@ module.exports = { varOpts = options && { fallback: options.env, retainIds: options.retainIds }, id = _.get(collection, 'info._postman_id') || _.get(collection, 'info.id'), info = collection && collection.info, + allowAuthReset = _.get(options, 'allowFieldResets', []).includes('auth'), newCollection = { id: id && options && options.retainIds ? id : util.uid(), name: info && info.name @@ -108,7 +109,9 @@ module.exports = { try { // eslint-disable-next-line max-len newCollection.description = builders.description(info && info.description); - (auth = builders.auth(collection, authOptions)) && (newCollection.auth = auth); + if(allowAuthReset && builders.auth(collection, authOptions) === null){ + newCollection.auth = builders.auth(collection, authOptions) + } else (auth = builders.auth(collection, authOptions)) && (newCollection.auth = auth); (events = builders.events(collection)) && (newCollection.events = events); (variables = builders.variables(collection, varOpts)) && (newCollection.variables = variables); util.addProtocolProfileBehavior(collection, newCollection); From c9312215eab3aece58b1d822e26e74a820fcf8f2 Mon Sep 17 00:00:00 2001 From: Samith Bharadwaj Date: Thu, 18 May 2023 19:47:05 +0530 Subject: [PATCH 2/2] [CUE-3798] Adding tests for allowFieldResets --- test/unit/v2.1.0/converter-v21-to-v1.test.js | 129 +++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/test/unit/v2.1.0/converter-v21-to-v1.test.js b/test/unit/v2.1.0/converter-v21-to-v1.test.js index 1632f3fd..b42029bb 100644 --- a/test/unit/v2.1.0/converter-v21-to-v1.test.js +++ b/test/unit/v2.1.0/converter-v21-to-v1.test.js @@ -345,6 +345,46 @@ describe('v2.1.0 to v1.0.0', function () { done(); }); }); + + it('should retain empty description when allowFieldResets contains description', function (done) { + const source = { + info: { + _postman_id: '969e90b1-0742-41b5-8602-e137d25274ac', + description: null + } + }; + + options.allowFieldResets = ['description']; + + transformer.convert(source, options, function (err, converted) { + expect(err).to.not.be.ok; + + // remove `undefined` properties for testing + converted = JSON.parse(JSON.stringify(converted)); + + expect(_.get(converted, 'description')).to.eql(null); + done(); + }); + delete options.allowFieldResets; + }); + + it('should not retain description when allowFieldResets doesn\'t contains description', function (done) { + const source = { + info: { + _postman_id: '969e90b1-0742-41b5-8602-e137d25274ac', + description: null + } + }; + + transformer.convert(source, options, function (err, converted) { + expect(err).to.not.be.ok; + + // remove `undefined` properties for testing + converted = JSON.parse(JSON.stringify(converted)); + expect(_.get(converted, 'description')).to.eql(undefined); + done(); + }); + }); }); describe('request file body', function () { @@ -1708,6 +1748,45 @@ describe('v2.1.0 to v1.0.0', function () { done(); }); }); + + it('should retain noauth from null auth object when allowFieldResets contains auth', function (done) { + const source = { + info: { + _postman_id: '969e90b1-0742-41b5-8602-e137d25274ac' + }, + auth: null, + item: [{ + _postman_id: 'a9832f4d-657c-4cd2-a5a4-7ddd6bc4948e', + auth: null, + item: [] + }] + }; + + options.allowFieldResets = ['auth']; + + transformer.convert(source, options, function (err, converted) { + expect(err).to.not.be.ok; + + converted = JSON.parse(JSON.stringify(converted)); + + expect(_.get(converted, 'folders.0.auth')).to.eql(null); + done(); + }); + delete options.allowFieldResets; + }); + + it('should not retain noauth when allowFieldResets doesn\'t contains auth', function (done) { + var fixture = require('../fixtures/sample-description'); + + transformer.convert(fixture.v21WithEmptyDescription, options, function (err, converted) { + expect(err).to.not.be.ok; + + // remove `undefined` properties for testing + converted = JSON.parse(JSON.stringify(converted)); + expect(_.get(converted, 'folders.0.auth')).to.eql(undefined); + done(); + }); + }); }); describe('with collections', function () { @@ -1780,6 +1859,56 @@ describe('v2.1.0 to v1.0.0', function () { done(); }); }); + + it('should retain noauth from null auth object when allowFieldResets contains auth', function (done) { + const source = { + info: { + _postman_id: '969e90b1-0742-41b5-8602-e137d25274ac' + }, + auth: null, + item: [{ + _postman_id: 'a9832f4d-657c-4cd2-a5a4-7ddd6bc4948e', + auth: null, + item: [] + }] + }; + + options.allowFieldResets = ['auth']; + + transformer.convert(source, options, function (err, converted) { + expect(err).to.not.be.ok; + + // remove `undefined` properties for testing + converted = JSON.parse(JSON.stringify(converted)); + + expect(_.get(converted, 'auth')).to.eql(null); + done(); + }); + delete options.allowFieldResets; + }); + + it('should not retain noauth when allowFieldResets doesn\'t contains auth', function (done) { + const source = { + info: { + _postman_id: '969e90b1-0742-41b5-8602-e137d25274ac' + }, + auth: null, + item: [{ + _postman_id: 'a9832f4d-657c-4cd2-a5a4-7ddd6bc4948e', + auth: null, + item: [] + }] + }; + + transformer.convert(source, options, function (err, converted) { + expect(err).to.not.be.ok; + + // remove `undefined` properties for testing + converted = JSON.parse(JSON.stringify(converted)); + expect(_.get(converted, 'auth')).to.eql(undefined); + done(); + }); + }); }); });