Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CUE-3798] Adding transformation option for resettable fields #590

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/converters/v2.0.0/converter-v2-to-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

}
});

Expand Down
5 changes: 4 additions & 1 deletion lib/converters/v2.1.0/converter-v21-to-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
129 changes: 129 additions & 0 deletions test/unit/v2.1.0/converter-v21-to-v1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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();
});
});
});
});

Expand Down