Added test for 31x

Added test for 31x
This commit is contained in:
Luis Tejeda
2021-11-25 12:11:32 -06:00
committed by Erik Mendoza
parent 0be5fb9a75
commit d6fa8832eb
11 changed files with 276 additions and 17 deletions

View File

@@ -4,8 +4,14 @@ const inputValidation31X = require('./inputValidation31X'),
module.exports = {
/**
* Parses an OAS 3.1.X string/object as a YAML or JSON
* @param {YAML/JSON} openApiSpec - The OAS 3.1.x specification specified in either YAML or JSON
* @returns {Object} - Contains the parsed JSON-version of the OAS spec, or an error
* @no-unit-test
*/
parseSpec: function (openApiSpec) {
return schemaUtilsCommon.parseSpec(openApiSpec, inputValidation31X);
}
},
inputValidation31X
};

View File

@@ -128,10 +128,11 @@ module.exports = {
/** Given an array of files returns the root OAS file if present
*
* @param {Array} input input object that contains files array
* @param {Object} inputValidation Validator according to version
* @param {Object} files Files map
* @return {String} rootFile
*/
getRootFiles: function (input, files = {}) {
getRootFiles: function (input, inputValidation, files = {}) {
let rootFilesArray = [],
filesPathArray = input.data,
origin = input.origin || '';
@@ -159,7 +160,7 @@ module.exports = {
else {
throw new Error(obj.reason);
}
if (this.validateSpec(oasObject).result) {
if (inputValidation.validateSpec(oasObject).result) {
rootFilesArray.push(filePath.fileName);
}
}

View File

@@ -4618,5 +4618,6 @@ module.exports = {
});
});
return endpoints;
}
},
inputValidation
};

View File

@@ -21,12 +21,29 @@ const COLLECTION_NAME = 'Imported from OpenAPI 3.0',
// This provides the base class for
// errors with the input OpenAPI spec
OpenApiErr = require('./error.js');
OpenApiErr = require('./error.js'),
DEFAULT_SPEC_VERSION = '3.0';
let path = require('path'),
schemaUtils,
pathBrowserify = require('path-browserify');
/**
*
* @param {string} specVersion - the OAS specification version
* @returns {NodeRequire} the schema utils according to version
*/
function getConcreteSchemaUtils(specVersion) {
let schemaUtils = {};
if (specVersion === DEFAULT_SPEC_VERSION) {
schemaUtils = require('./schemaUtils');
}
else {
schemaUtils = require('./schemaUtils31X');
}
return schemaUtils;
}
class SchemaPack {
constructor (input, options = {}) {
this.input = input;
@@ -49,14 +66,7 @@ class SchemaPack {
let indentCharacter = this.computedOptions.indentCharacter,
specVersion = '3.0';// Get real version
this.computedOptions.indentCharacter = indentCharacter === 'tab' ? '\t' : ' ';
if (specVersion === '3.0') {
schemaUtils = require('./schemaUtils');
}
else {
schemaUtils = require('./schemaUtils31X');
}
schemaUtils = getConcreteSchemaUtils(specVersion);
this.validate();
}
@@ -194,7 +204,7 @@ class SchemaPack {
}
try {
rootFiles = parse.getRootFiles(input, files);
rootFiles = parse.getRootFiles(input, schemaUtils.inputValidation, files);
}
catch (e) {
return cb(null, {

View File

@@ -40,4 +40,4 @@ fi
node --max-old-space-size=2048 ./node_modules/.bin/nyc ${COVERAGE_REPORT} --report-dir ./.coverage \
-x **/assets/** --print both ./node_modules/.bin/_mocha \
--reporter ${MOCHA_REPORTER} --reporter-options output=${XUNIT_FILE} \
test/unit/*.test.js --recursive --prof --grep "$1";
"test/unit/**/**.test.js" --recursive --prof --grep "$1";

View File

@@ -0,0 +1,46 @@
{
"openapi": "3.1.0",
"webhooks": {
"newPet": {
"post": {
"requestBody": {
"description": "Information about a new pet in the system",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"responses": {
"200": {
"description": "Return a 200 status to indicate that the data was received successfully"
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
}
}

View File

@@ -0,0 +1,50 @@
{
"openapi": "3.1.0",
"info": {
"title": "Webhook Example",
"version": "1.0.0"
},
"webhooks": {
"newPet": {
"post": {
"requestBody": {
"description": "Information about a new pet in the system",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"responses": {
"200": {
"description": "Return a 200 status to indicate that the data was received successfully"
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
}
}

View File

@@ -0,0 +1,112 @@
const { expect } = require('chai'),
{
validateSpec
} = require('../../../lib/31Xsupport/inputValidation31X'),
correctMockedEntryWH = {
openapi: '3.1.0',
info: {
title: 'Webhook Example',
version: '1.0.0'
},
webhooks: {
}
},
correctMockedEntryPath = {
openapi: '3.1.0',
info: {
title: 'Webhook Example',
version: '1.0.0'
},
paths: {
}
},
correctMockedEntryComponent = {
openapi: '3.1.0',
info: {
title: 'Webhook Example',
version: '1.0.0'
},
components: {
}
},
correctMockedEntry = {
openapi: '3.1.0',
info: {
title: 'Webhook Example',
version: '1.0.0'
},
paths: {
},
components: {
},
webhooks: {
}
},
incorrectMockedEntryNoOpenapi = {
info: {
title: 'Webhook Example',
version: '1.0.0'
},
components: {
}
},
incorrectMockedEntryNoInfo = {
openapi: '3.1.0',
paths: {
},
components: {
},
webhooks: {
}
},
incorrectMockedEntryNOPathsComponentsWebhooks = {
openapi: '3.1.0',
info: {
title: 'Webhook Example',
version: '1.0.0'
}
};
describe('validateSpec method', function () {
it('should return true with a valid simple spec with webhooks', function () {
const validationResult = validateSpec(correctMockedEntryWH);
expect(validationResult.result).to.be.true;
});
it('should return true with a valid simple spec with paths', function () {
const validationResult = validateSpec(correctMockedEntryPath);
expect(validationResult.result).to.be.true;
});
it('should return true with a valid simple spec with components', function () {
const validationResult = validateSpec(correctMockedEntryComponent);
expect(validationResult.result).to.be.true;
});
it('should return true with a valid simple spec with components webhooks y paths', function () {
const validationResult = validateSpec(correctMockedEntry);
expect(validationResult.result).to.be.true;
});
it('should return false with an invalid input without openapi field', function () {
const validationResult = validateSpec(incorrectMockedEntryNoOpenapi);
expect(validationResult.result).to.be.false;
expect(validationResult.reason)
.to.equal('Specification must contain a semantic version number of the OAS specification');
});
it('should return false with an invalid input without info field', function () {
const validationResult = validateSpec(incorrectMockedEntryNoInfo);
expect(validationResult.result).to.be.false;
expect(validationResult.reason)
.to.equal('Specification must contain an Info Object for the meta-data of the API');
});
it('should return false with an invalid input without path components and webhooks', function () {
const validationResult = validateSpec(incorrectMockedEntryNOPathsComponentsWebhooks);
expect(validationResult.result).to.be.false;
expect(validationResult.reason)
.to.equal('Specification must contain either Paths, Webhooks or Components sections');
});
});

View File

@@ -0,0 +1,32 @@
const { expect } = require('chai'),
{
parseSpec
} = require('../../../lib/31Xsupport/schemaUtils31X'),
fs = require('fs'),
valid31xFolder = './test/data/valid_openapi31X',
invalid31xFolder = './test/data/invalid_openapi31X';
describe('parseSpec method', function () {
it('should return true and a parsed specification', function () {
let fileContent = fs.readFileSync(valid31xFolder + '/webhooks.json', 'utf8');
const parsedSpec = parseSpec(fileContent);
expect(parsedSpec.result).to.be.true;
expect(parsedSpec.openapi.openapi).to.equal('3.1.0');
expect(parsedSpec.openapi.webhooks).to.not.be.undefined;
});
it('should return false and invalid format message when input content is sent', function () {
let fileContent = fs.readFileSync(invalid31xFolder + '/empty-spec.yaml', 'utf8');
const parsedSpec = parseSpec(fileContent);
expect(parsedSpec.result).to.be.false;
expect(parsedSpec.reason).to.equal('Invalid format. Input must be in YAML or JSON format.');
});
it('should return false and Spec must contain info object', function () {
let fileContent = fs.readFileSync(invalid31xFolder + '/invalid-no-info.json', 'utf8');
const parsedSpec = parseSpec(fileContent);
expect(parsedSpec.result).to.be.false;
expect(parsedSpec.reason).to.equal('Specification must contain an Info Object for the meta-data of the API');
});
});

View File

@@ -1,6 +1,7 @@
const expect = require('chai').expect,
path = require('path'),
fs = require('fs'),
inputValidation = require('../../lib/inputValidation'),
parse = require('../../lib/parse.js');
describe('PARSE FUNCTION TESTS', function() {
@@ -15,7 +16,7 @@ describe('PARSE FUNCTION TESTS', function() {
{ fileName: folderPath + '/paths/foo.yaml' },
{ fileName: folderPath + '/paths/bar.yaml' }
],
result = parse.getRootFiles({ data: array, type: 'folder' });
result = parse.getRootFiles({ data: array, type: 'folder' }, inputValidation);
expect(result.length).to.equal(1);
expect(result[0]).to.equal(folderPath + '/index.yaml');
});