|
| 1 | +/* eslint-disable one-var */ |
| 2 | +const expect = require('chai').expect, |
| 3 | + Converter = require('../../index.js'), |
| 4 | + fs = require('fs'), |
| 5 | + path = require('path'), |
| 6 | + VALID_OPENAPI_PATH = '../data/valid_openapi', |
| 7 | + Ajv = require('ajv'), |
| 8 | + testSpec = path.join(__dirname, VALID_OPENAPI_PATH + '/test.json'), |
| 9 | + testSpec1 = path.join(__dirname, VALID_OPENAPI_PATH + '/test1.json'), |
| 10 | + readOnlyNestedSpec = |
| 11 | + path.join(__dirname, VALID_OPENAPI_PATH, '/readOnlyNested.json'), |
| 12 | + ajv = new Ajv({ allErrors: true, strict: false }), |
| 13 | + transformSchema = (schema) => { |
| 14 | + const properties = schema.properties, |
| 15 | + rest = Object.keys(schema) |
| 16 | + .filter((key) => { return key !== 'properties'; }) |
| 17 | + .reduce((acc, key) => { |
| 18 | + acc[key] = schema[key]; |
| 19 | + return acc; |
| 20 | + }, {}), |
| 21 | + |
| 22 | + transformedProperties = Object.entries(properties).reduce( |
| 23 | + (acc, [key, value]) => { |
| 24 | + acc[key] = { |
| 25 | + type: value.type, |
| 26 | + enum: value.enum !== null ? value.enum : undefined, |
| 27 | + minLength: value.minLength !== null ? value.minLength : undefined, |
| 28 | + maxLength: value.maxLength !== null ? value.maxLength : undefined, |
| 29 | + minimum: value.minimum !== null ? value.minimum : undefined, |
| 30 | + maximum: value.maximum !== null ? value.maximum : undefined, |
| 31 | + pattern: value.pattern !== null ? value.pattern : undefined, |
| 32 | + format: value.format !== null ? value.format : undefined |
| 33 | + }; |
| 34 | + return acc; |
| 35 | + }, |
| 36 | + {} |
| 37 | + ), |
| 38 | + |
| 39 | + |
| 40 | + transformedObject = Object.assign({}, rest, { properties: transformedProperties }); |
| 41 | + |
| 42 | + return transformedObject; |
| 43 | + }; |
| 44 | + |
| 45 | + |
| 46 | +describe('convertV2WithTypes should generate collection confirming to collection schema', function() { |
| 47 | + |
| 48 | + it('Should generate collection conforming to schema for and fail if not valid ' + |
| 49 | + testSpec, function(done) { |
| 50 | + var openapi = fs.readFileSync(testSpec, 'utf8'); |
| 51 | + Converter.convertV2WithTypes({ type: 'string', data: openapi }, { schemaFaker: true }, (err, conversionResult) => { |
| 52 | + expect(err).to.be.null; |
| 53 | + expect(conversionResult.result).to.equal(true); |
| 54 | + expect(conversionResult.output.length).to.equal(1); |
| 55 | + expect(conversionResult.output[0].type).to.equal('collection'); |
| 56 | + expect(conversionResult.output[0].data).to.have.property('info'); |
| 57 | + expect(conversionResult.output[0].data).to.have.property('item'); |
| 58 | + done(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('Should generate collection conforming to schema for and fail if not valid ' + |
| 63 | + testSpec1, function(done) { |
| 64 | + Converter.convertV2WithTypes( |
| 65 | + { type: 'file', data: testSpec1 }, { requestNameSource: 'url' }, (err, conversionResult) => { |
| 66 | + expect(err).to.be.null; |
| 67 | + expect(conversionResult.result).to.equal(true); |
| 68 | + expect(conversionResult.output.length).to.equal(1); |
| 69 | + expect(conversionResult.output[0].type).to.equal('collection'); |
| 70 | + expect(conversionResult.output[0].data).to.have.property('info'); |
| 71 | + expect(conversionResult.output[0].data).to.have.property('item'); |
| 72 | + |
| 73 | + done(); |
| 74 | + }); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | + |
| 79 | +describe('convertV2WithTypes', function() { |
| 80 | + it('should contain extracted types' + testSpec1, function () { |
| 81 | + Converter.convertV2WithTypes( |
| 82 | + { type: 'file', data: testSpec1 }, { requestNameSource: 'url' }, (err, conversionResult) => { |
| 83 | + expect(err).to.be.null; |
| 84 | + expect(conversionResult.result).to.equal(true); |
| 85 | + expect(conversionResult.extractedTypes).to.not.be.undefined; |
| 86 | + expect(conversionResult.extractedTypes.length).to.not.equal(0); |
| 87 | + } |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should validate the schema' + testSpec1, function() { |
| 92 | + const example = { |
| 93 | + code: 200, |
| 94 | + message: 'Success' |
| 95 | + }; |
| 96 | + Converter.convertV2WithTypes( |
| 97 | + { type: 'file', data: testSpec1 }, { requestNameSource: 'url' }, (err, conversionResult) => { |
| 98 | + |
| 99 | + expect(err).to.be.null; |
| 100 | + expect(conversionResult.extractedTypes).to.be.an('array').that.is.not.empty; |
| 101 | + const element = conversionResult.extractedTypes[0]; |
| 102 | + |
| 103 | + expect(element).to.be.an('object').that.includes.keys('request'); |
| 104 | + expect(element).to.be.an('object').that.includes.keys('response'); |
| 105 | + const { response } = element; |
| 106 | + expect(response).to.be.an('object').that.is.not.empty; |
| 107 | + const [key, value] = Object.entries(response)[1]; |
| 108 | + expect(key).to.be.a('string'); |
| 109 | + const schema = JSON.parse(value.body), |
| 110 | + transformedSchema = transformSchema(schema), |
| 111 | + validate = ajv.compile(transformedSchema), |
| 112 | + valid = validate(example); |
| 113 | + |
| 114 | + expect(value).to.have.property('body').that.is.a('string'); |
| 115 | + |
| 116 | + |
| 117 | + expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true; |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should resolve nested array and object schema types correctly in extractedTypes', function(done) { |
| 122 | + const example = { |
| 123 | + name: 'Buddy', |
| 124 | + pet: { |
| 125 | + id: 123, |
| 126 | + name: 'Charlie', |
| 127 | + address: { |
| 128 | + addressCode: { |
| 129 | + code: 'A123' |
| 130 | + }, |
| 131 | + city: 'New York' |
| 132 | + } |
| 133 | + } |
| 134 | + }, |
| 135 | + openapi = fs.readFileSync(readOnlyNestedSpec, 'utf8'), |
| 136 | + options = { schemaFaker: true, exampleParametersResolution: 'schema' }; |
| 137 | + |
| 138 | + Converter.convertV2WithTypes({ type: 'string', data: openapi }, options, (err, conversionResult) => { |
| 139 | + expect(err).to.be.null; |
| 140 | + expect(conversionResult.extractedTypes).to.be.an('array').that.is.not.empty; |
| 141 | + |
| 142 | + // Validate the first extracted type |
| 143 | + const element = conversionResult.extractedTypes[0]; |
| 144 | + const { response } = element; |
| 145 | + |
| 146 | + // Get the schema from the response |
| 147 | + const [key, value] = Object.entries(response)[0]; |
| 148 | + expect(value).to.have.property('body').that.is.a('string'); |
| 149 | + |
| 150 | + const schema = JSON.parse(value.body), |
| 151 | + transformedSchema = transformSchema(schema), |
| 152 | + validate = ajv.compile(transformedSchema), |
| 153 | + valid = validate(example); |
| 154 | + expect(valid, `Validation failed for key: ${key} with errors: ${JSON.stringify(validate.errors)}`).to.be.true; |
| 155 | + done(); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments