From bbc7c5e02f98ba81fae15869e69683cf4839090d Mon Sep 17 00:00:00 2001 From: Abhijit Kane Date: Wed, 18 Dec 2019 14:59:13 +0530 Subject: [PATCH] Lint fixes + getting older wrappers using new API correctly --- index.js | 4 +-- lib/schemaUtils.js | 70 ++++++++++--------------------------- lib/schemapack.js | 2 +- test/unit/util.test.js | 5 ++- test/unit/validator.temp.js | 3 +- 5 files changed, 25 insertions(+), 59 deletions(-) diff --git a/index.js b/index.js index e581378..c9c26eb 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,10 @@ const SchemaPack = require('./lib/schemapack.js').SchemaPack; module.exports = { // Old API wrapping the new API convert: function(input, options, cb) { - var schema = new SchemaPack(input); + var schema = new SchemaPack(input, options); if (schema.validated) { - return schema.convert(options, cb); + return schema.convert(cb); } return cb(null, schema.validationResult); diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index e063bd2..ded6558 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -1158,7 +1158,8 @@ module.exports = { if (contentObj[URLENCODED].hasOwnProperty('schema') && contentObj[URLENCODED].schema.hasOwnProperty('$ref')) { contentObj[URLENCODED].schema = this.getRefObject(contentObj[URLENCODED].schema.$ref, components, options); } - bodyData = this.convertToPmBodyData(contentObj[URLENCODED], requestType, URLENCODED, PARAMETER_SOURCE.REQUEST, '', components, options); + bodyData = this.convertToPmBodyData(contentObj[URLENCODED], requestType, URLENCODED, + PARAMETER_SOURCE.REQUEST, '', components, options); encoding = contentObj[URLENCODED].encoding ? contentObj[URLENCODED].encoding : {}; // create query parameters and add it to the request body object _.forOwn(bodyData, (value, key) => { @@ -1209,7 +1210,8 @@ module.exports = { } else if (contentObj.hasOwnProperty(FORM_DATA)) { rDataMode = 'formdata'; - bodyData = this.convertToPmBodyData(contentObj[FORM_DATA], requestType, FORM_DATA, PARAMETER_SOURCE.REQUEST, '', components, options); + bodyData = this.convertToPmBodyData(contentObj[FORM_DATA], requestType, FORM_DATA, + PARAMETER_SOURCE.REQUEST, '', components, options); encoding = contentObj[FORM_DATA].encoding ? contentObj[FORM_DATA].encoding : {}; // create the form parameters and add it to the request body object _.forOwn(bodyData, (value, key) => { @@ -1528,7 +1530,8 @@ module.exports = { }; } else if (authHelper.properties.in === 'query') { - this.convertToPmQueryParameters(authHelper.properties, REQUEST_TYPE.ROOT, components, options).forEach((pmParam) => { + this.convertToPmQueryParameters(authHelper.properties, REQUEST_TYPE.ROOT, + components, options).forEach((pmParam) => { item.request.url.addQueryParams(pmParam); }); item.request.auth = { @@ -1551,13 +1554,15 @@ module.exports = { query.value = (typeof query.value === 'object') ? JSON.stringify(query.value) : query.value; }); item.request.url.variables.clear(); - item.request.url.variables.assimilate(this.convertPathVariables('param', pathVarArray, reqParams.path, components, options)); + item.request.url.variables.assimilate(this.convertPathVariables('param', pathVarArray, reqParams.path, + components, options)); // TODO: There is a bug in Postman that causes these request descriptions // to be converted as "object Object" // adding headers to request from reqParam _.forEach(reqParams.header, (header) => { - item.request.addHeader(this.convertToPmHeader(header, REQUEST_TYPE.ROOT, PARAMETER_SOURCE.REQUEST, components, options)); + item.request.addHeader(this.convertToPmHeader(header, REQUEST_TYPE.ROOT, PARAMETER_SOURCE.REQUEST, + components, options)); }); // adding Request Body and Content-Type header @@ -1591,15 +1596,18 @@ module.exports = { thisOriginalRequest.url = displayUrl; // setting query params thisOriginalRequest.url += '?'; - thisOriginalRequest.url += this.convertToPmQueryArray(reqParams, REQUEST_TYPE.EXAMPLE, components, options).join('&'); + thisOriginalRequest.url += this.convertToPmQueryArray(reqParams, REQUEST_TYPE.EXAMPLE, components, + options).join('&'); // setting headers _.forEach(reqParams.header, (header) => { - originalRequestHeaders.push(this.convertToPmHeader(header, REQUEST_TYPE.EXAMPLE, PARAMETER_SOURCE.REQUEST, components, options)); + originalRequestHeaders.push(this.convertToPmHeader(header, REQUEST_TYPE.EXAMPLE, + PARAMETER_SOURCE.REQUEST, components, options)); }); thisOriginalRequest.header = originalRequestHeaders; // setting request body try { - exampleRequestBody = this.convertToPmBody(operationItem.properties.requestBody, REQUEST_TYPE.EXAMPLE, components, options); + exampleRequestBody = this.convertToPmBody(operationItem.properties.requestBody, + REQUEST_TYPE.EXAMPLE, components, options); thisOriginalRequest.body = exampleRequestBody.body ? exampleRequestBody.body.toJSON() : {}; } catch (e) { @@ -2014,11 +2022,8 @@ module.exports = { checkResponseBody: function (schemaResponse, body, transactionPathPrefix, schemaPathPrefix, components, options, callback) { - let schemaHeaders, - schemaHeader, - schemaContent = _.get(schemaResponse, ['content', 'application/json', 'schema']), - mismatchProperty = 'RESPONSE_BODY', - mismatches = []; + let schemaContent = _.get(schemaResponse, ['content', 'application/json', 'schema']), + mismatchProperty = 'RESPONSE_BODY'; if (!schemaContent) { // no specific or default response with application/json @@ -2041,45 +2046,6 @@ module.exports = { options, callback ); - - - return async.map(headers, (pHeader, cb) => { - schemaHeader = schemaHeaders[pHeader.key]; - - if (!schemaHeader) { - // no schema header found - mismatches.push({ - property: mismatchProperty, - transactionJsonPath: transactionPathPrefix + '/' + pHeader.key, - schemaJsonPath: schemaPathPrefix + '/body', - reasonCode: 'MISSING_IN_SCHEMA', - reason: `The header ${pHeader.key} was not found in the schema` - }); - return cb(null, []); - } - - // header found in spec. check header's schema - return this.checkValueAgainstSchema(mismatchProperty, - transactionPathPrefix + '/' + pHeader.key, - pHeader.value, - schemaPathPrefix + responsePathPrefix + '.headers[' + pHeader.key + ']', - schemaHeader.schema, - cb - ); - }, (err, res) => { - _.each(_.filter(schemaHeaders, (h) => { return h.required; }), (header) => { - if (!_.find(headers, (param) => { return param.key === header.name; })) { - mismatches.push({ - property: mismatchProperty, - transactionJsonPath: null, - schemaJsonPath: header.pathPrefix + header.name, - reasonCode: 'MISSING_IN_REQUEST', - reason: `The requried header ${header.name} was not found in the transaction` - }); - } - }); - callback(null, _.concat(_.flatten(res), mismatches)); - }); }, checkResponses: function (responses, transactionPathPrefix, schemaPathPrefix, schemaPath, components, options, cb) { diff --git a/lib/schemapack.js b/lib/schemapack.js index afc68ac..b740aa5 100644 --- a/lib/schemapack.js +++ b/lib/schemapack.js @@ -315,7 +315,7 @@ class SchemaPack { return true; } - getOptions() { + static getOptions() { return getOptions(); } } diff --git a/test/unit/util.test.js b/test/unit/util.test.js index f0e9ebc..c60512f 100644 --- a/test/unit/util.test.js +++ b/test/unit/util.test.js @@ -1340,7 +1340,8 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { occupation: 'student' }; - expect(SchemaUtils.getQueryStringWithStyle(param, '%20')).to.equal('name%20tuhin%20age%2022%20occupation%20student'); + expect(SchemaUtils.getQueryStringWithStyle(param, '%20')).to.equal( + 'name%20tuhin%20age%2022%20occupation%20student'); expect(SchemaUtils.getQueryStringWithStyle(param, '|')).to.equal('name|tuhin|age|22|occupation|student'); expect(SchemaUtils.getQueryStringWithStyle(param, ',')).to.equal('name,tuhin,age,22,occupation,student'); done(); @@ -1360,8 +1361,6 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () { }); }); - - describe('convertToPmBody function', function() { describe('should convert requestbody of media type', function() { it(' application/json', function(done) { diff --git a/test/unit/validator.temp.js b/test/unit/validator.temp.js index 9fdd490..eb2a071 100644 --- a/test/unit/validator.temp.js +++ b/test/unit/validator.temp.js @@ -17,7 +17,8 @@ describe('The converter must validate a history request against the schema', fun it('correctly', function(done) { let schemaPack = new Converter.SchemaPack({ type: 'json', data: openapi }, {}); schemaPack.validateTransaction(historyRequest, (err, result) => { - console.log('Final result: ', JSON.stringify(result, null, 2)); + expect(err).to.be.null; + expect(result).to.be.an.object; done(); }); });