webhooks scenarios

This commit is contained in:
Luis Tejeda
2022-01-27 12:33:42 -06:00
parent 68bb3f7c4f
commit 384c23f01c
7 changed files with 7497 additions and 15 deletions

View File

@@ -212,17 +212,18 @@ function safeSchemaFaker(oldSchema, resolveTo, resolveFor, parameterSourceOption
* @returns {object} the concatenation of paths and webhooks
*/
function concatPathsAndWebhooks(schema) {
let schemaPathItems = {};
_.forOwn(schema.paths, (pathItemObject) => {
let schemaPathItems = {},
deepCopy = _.cloneDeep(schema);
_.forOwn(deepCopy.paths, (pathItemObject) => {
pathItemObject.kind = 'paths';
});
schemaPathItems = schema.paths;
schemaPathItems = deepCopy.paths;
if (schema.webhooks) {
_.forOwn(schema.webhooks, (webhookItemObject) => {
if (deepCopy.webhooks) {
_.forOwn(deepCopy.webhooks, (webhookItemObject) => {
webhookItemObject.kind = 'webhooks';
});
schemaPathItems = Object.assign(schemaPathItems, schema.webhooks);
schemaPathItems = Object.assign(schemaPathItems, deepCopy.webhooks);
}
return schemaPathItems;
}
@@ -4566,9 +4567,10 @@ module.exports = {
*/
getMissingSchemaEndpoints: function (schema, matchedEndpoints, components, options, schemaCache) {
let endpoints = [],
schemaPaths = schema.paths,
// schemaPaths = schema.paths,
rootCollectionVariables,
schemaJsonPath;
schemaJsonPath,
schemaPathItems;
// collection variables generated for resolving for baseUrl and variables
rootCollectionVariables = this.convertToPmCollectionVariables(
@@ -4576,10 +4578,10 @@ module.exports = {
'baseUrl',
schema.baseUrl
);
_.forEach(schemaPaths, (schemaPathObj, schemaPath) => {
schemaPathItems = concatPathsAndWebhooks(schema);
_.forEach(schemaPathItems, (schemaPathObj, schemaPath) => {
_.forEach(_.keys(schemaPathObj), (pathKey) => {
schemaJsonPath = `$.paths[${schemaPath}].${_.toLower(pathKey)}`;
schemaJsonPath = `$.${schemaPathObj.kind}[${schemaPath}].${_.toLower(pathKey)}`;
if (METHODS.includes(pathKey) && !matchedEndpoints.includes(schemaJsonPath)) {
let mismatchObj = {
property: 'ENDPOINT',

View File

@@ -1,5 +1,542 @@
{
"item": [
{
"id": "b3106bf6-1436-4ee5-8a14-08e11c535686",
"name": "pets",
"item": [
{
"id": "015fd91c-7b3a-4150-95aa-dab832687c4a",
"name": "List all pets",
"request": {
"name": "List all pets",
"description": {
"content": "",
"type": "text/plain"
},
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [
{
"disabled": false,
"key": "variable",
"value": "Duis laboris deserunt incididunt",
"description": "random variable"
},
{
"disabled": false,
"key": "variable",
"value": "minim incididunt magna commodo sed",
"description": "random variable"
},
{
"disabled": false,
"key": "variable2",
"value": "3290821%20-52415762",
"description": "another random variable"
}
],
"variable": [
{
"disabled": false,
"type": "any",
"value": "ex Lorem",
"key": "petId",
"description": "(Required) The id of the pet to retrieve"
}
]
},
"header": [
{
"disabled": false,
"description": "How many items to return at one time (max 100)",
"key": "limit",
"value": "43314755"
}
],
"method": "GET",
"auth": null
},
"response": [
{
"id": "3546c79a-a8cd-4621-99cf-92e24c0e2746",
"name": "An paged array of pets",
"originalRequest": {
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [
{
"key": "variable",
"value": "labore commodo velit"
},
{
"key": "variable",
"value": "reprehenderit aliquip consequat"
},
{
"key": "variable2",
"value": "16666331%2041579325"
}
],
"variable": [
{
"disabled": false,
"type": "any",
"value": "ex Lorem",
"key": "petId",
"description": "(Required) The id of the pet to retrieve"
}
]
},
"header": [
{
"disabled": false,
"description": "How many items to return at one time (max 100)",
"key": "limit",
"value": "77845793"
}
],
"method": "GET",
"body": {}
},
"status": "OK",
"code": 200,
"header": [
{
"disabled": false,
"description": "A link to the next page of responses",
"key": "x-next",
"value": "qui amet et"
},
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": "[\n {\n \"id\": 33336926,\n \"name\": \"name\"\n },\n {\n \"id\": 21348645,\n \"name\": \"name\"\n }\n]",
"cookie": [],
"_postman_previewlanguage": "json"
},
{
"id": "4fadf536-558a-45ec-a8d7-3504dc644155",
"name": "unexpected error",
"originalRequest": {
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [
{
"key": "variable",
"value": "labore commodo velit"
},
{
"key": "variable",
"value": "reprehenderit aliquip consequat"
},
{
"key": "variable2",
"value": "16666331%2041579325"
}
],
"variable": [
{
"disabled": false,
"type": "any",
"value": "ex Lorem",
"key": "petId",
"description": "(Required) The id of the pet to retrieve"
}
]
},
"header": [
{
"disabled": false,
"description": "How many items to return at one time (max 100)",
"key": "limit",
"value": "77845793"
}
],
"method": "GET",
"body": {}
},
"status": "Internal Server Error",
"code": 500,
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": "{\n \"code\": -17,\n \"message\": \"sed laborum\"\n}",
"cookie": [],
"_postman_previewlanguage": "json"
}
],
"event": []
},
{
"id": "c39d4f2d-b968-4431-bc05-a4a804ad1616",
"name": "Create a pet",
"request": {
"name": "Create a pet",
"description": {},
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": [
{
"disabled": false,
"type": "any",
"value": "ex Lorem",
"key": "petId",
"description": "(Required) The id of the pet to retrieve"
}
]
},
"method": "POST",
"auth": null
},
"response": [
{
"id": "8303badc-f9fb-43b1-800b-64f5142676de",
"name": "Null response",
"originalRequest": {
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": [
{
"disabled": false,
"type": "any",
"value": "ex Lorem",
"key": "petId",
"description": "(Required) The id of the pet to retrieve"
}
]
},
"method": "POST",
"body": {}
},
"status": "Created",
"code": 201,
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": "[\n {\n \"id\": 33336926,\n \"name\": \"name\"\n },\n {\n \"id\": 21348645,\n \"name\": \"name\"\n }\n]",
"cookie": [],
"_postman_previewlanguage": "json"
},
{
"id": "ca5dd925-1b59-410b-9ccc-735534d33acc",
"name": "unexpected error",
"originalRequest": {
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": [
{
"disabled": false,
"type": "any",
"value": "ex Lorem",
"key": "petId",
"description": "(Required) The id of the pet to retrieve"
}
]
},
"method": "POST",
"body": {}
},
"status": "Internal Server Error",
"code": 500,
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": "{\n \"code\": -17,\n \"message\": \"sed laborum\"\n}",
"cookie": [],
"_postman_previewlanguage": "json"
}
],
"event": []
},
{
"id": "c4490316-b6f5-4325-b2ad-7783bdc33586",
"name": "{pet Id}",
"item": [
{
"id": "40ab37b1-3e4c-4675-8098-6687bd4de878",
"name": "Info for a specific pet",
"request": {
"name": "Info for a specific pet",
"description": {},
"url": {
"path": [
"pets",
":petId"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": [
{
"disabled": false,
"type": "any",
"value": "ex Lorem",
"key": "petId",
"description": "(Required) The id of the pet to retrieve"
}
]
},
"method": "GET",
"auth": null
},
"response": [
{
"id": "5393a0c2-fe86-484a-bbe0-bc025158800d",
"name": "Expected response to a valid request",
"originalRequest": {
"url": {
"path": [
"pets",
":petId"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": [
{
"disabled": false,
"type": "any",
"value": "ex Lorem",
"key": "petId",
"description": "(Required) The id of the pet to retrieve"
}
]
},
"method": "GET",
"body": {}
},
"status": "OK",
"code": 200,
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": "{\n \"id\": 17267059,\n \"name\": \"name\"\n}",
"cookie": [],
"_postman_previewlanguage": "json"
},
{
"id": "d55d2454-a355-453d-b424-049e7d4e8c00",
"name": "unexpected error",
"originalRequest": {
"url": {
"path": [
"pets",
":petId"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": [
{
"disabled": false,
"type": "any",
"value": "ex Lorem",
"key": "petId",
"description": "(Required) The id of the pet to retrieve"
}
]
},
"method": "GET",
"body": {}
},
"status": "Internal Server Error",
"code": 500,
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": "{\n \"code\": -195,\n \"message\": \"sed laborum\"\n}",
"cookie": [],
"_postman_previewlanguage": "json"
}
],
"event": []
},
{
"id": "2927089f-94fd-4273-b57a-f67369406c73",
"name": "Uploads an image",
"request": {
"name": "Uploads an image",
"description": {
"content": "",
"type": "text/plain"
},
"url": {
"path": [
"pets",
":petId",
"uploadImage"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": [
{
"disabled": false,
"type": "any",
"value": "-33595069",
"key": "petId",
"description": "(Required) ID of pet to update"
}
]
},
"header": [
{
"key": "Content-Type",
"value": "application/octet-stream"
}
],
"method": "POST",
"auth": null,
"body": {
"mode": "file"
}
},
"response": [
{
"id": "80ebe1a4-eef6-4188-95fb-e3f2b65cb7a5",
"name": "Null response",
"originalRequest": {
"url": {
"path": [
"pets",
":petId",
"uploadImage"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": [
{
"disabled": false,
"type": "any",
"value": "-33595069",
"key": "petId",
"description": "(Required) ID of pet to update"
}
]
},
"method": "POST",
"body": {
"mode": "file"
}
},
"status": "Created",
"code": 201,
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": "[\n {\n \"id\": 33336926,\n \"name\": \"name\"\n },\n {\n \"id\": 21348645,\n \"name\": \"name\"\n }\n]",
"cookie": [],
"_postman_previewlanguage": "json"
},
{
"id": "5ee6e5c5-de8a-4707-a464-1c782f17d6b1",
"name": "unexpected error",
"originalRequest": {
"url": {
"path": [
"pets",
":petId",
"uploadImage"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": [
{
"disabled": false,
"type": "any",
"value": "-33595069",
"key": "petId",
"description": "(Required) ID of pet to update"
}
]
},
"method": "POST",
"body": {
"mode": "file"
}
},
"status": "Internal Server Error",
"code": 500,
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": "{\n \"code\": -1,\n \"message\": \"sed laborum\"\n}",
"cookie": [],
"_postman_previewlanguage": "json"
}
],
"event": []
}
],
"event": []
}
],
"event": []
},
{
"id": "4187f5ea-5b58-4508-9da7-609a1e410905",
"name": "webhooks",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -64,6 +64,8 @@ components:
id:
type: integer
format: int64
name:
type: string
Pets:
type: array
items:
@@ -71,7 +73,7 @@ components:
Error:
required:
- code
- mesage
- message
properties:
code:
type: integer

View File

@@ -150,8 +150,9 @@ describe('Openapi 3.1 schema pack validateTransactions', function() {
});
});
it('Should not generate any mismatch with a correct file with webhooks', function() {
const collectionSource = path.join(__dirname, OPENAPI_31_COLLECTIONS + '/simpleCollectionsWithWebhooks.json'),
it('Should not generate any mismatch webhook return 4 missing endpoints from paths', function() {
const collectionSource = path.join(__dirname, OPENAPI_31_COLLECTIONS +
'/simpleCollectionsWithWebhooksNoPaths.json'),
collectionData = fs.readFileSync(collectionSource, 'utf8'),
schemaSource = path.join(__dirname, OPENAPI_31_COLLECTIONS + '/simpleCollectionsWithWebhooksSpec.yaml'),
schemaData = fs.readFileSync(schemaSource, 'utf8'),
@@ -165,13 +166,95 @@ describe('Openapi 3.1 schema pack validateTransactions', function() {
validator.validateTransaction(transactions, (err, result) => {
let requestIds = Object.keys(result.requests);
// expect(err).to.be.null;
expect(err).to.be.null;
requestIds.forEach((requestId) => {
if (result.requests[requestId].endpoints[0].matched === false) {
failRequests.push(result.requests[requestId]);
}
});
expect(failRequests).to.be.empty;
expect(result.missingEndpoints.length).to.equal(4);
});
});
it('Should generate mismatch with errors in paths valid webhooks', function() {
const collectionSource = path.join(__dirname, OPENAPI_31_COLLECTIONS +
'/simpleCollectionsWithWebhooksErrorsInPaths.json'),
collectionData = fs.readFileSync(collectionSource, 'utf8'),
schemaSource = path.join(__dirname, OPENAPI_31_COLLECTIONS + '/simpleCollectionsWithWebhooksSpec.yaml'),
schemaData = fs.readFileSync(schemaSource, 'utf8'),
validator = new SchemaPack({
type: 'string',
data: schemaData
});
let transactions = [],
failRequests = [];
getAllTransactions(JSON.parse(collectionData), transactions);
validator.validateTransaction(transactions, (err, result) => {
let requestIds = Object.keys(result.requests);
expect(err).to.be.null;
requestIds.forEach((requestId) => {
if (result.requests[requestId].endpoints[0].matched === false) {
failRequests.push(result.requests[requestId]);
}
});
expect(failRequests.length).to.equal(4);
expect(result.missingEndpoints.length).to.equal(0);
});
});
it('Should not generate mismatch with valid webhooks and paths', function() {
const collectionSource = path.join(__dirname, OPENAPI_31_COLLECTIONS +
'/simpleCollectionsWithWebhooks.json'),
collectionData = fs.readFileSync(collectionSource, 'utf8'),
schemaSource = path.join(__dirname, OPENAPI_31_COLLECTIONS + '/simpleCollectionsWithWebhooksSpec.yaml'),
schemaData = fs.readFileSync(schemaSource, 'utf8'),
validator = new SchemaPack({
type: 'string',
data: schemaData
}, { suggestAvailableFixes: true });
let transactions = [],
failRequests = [];
getAllTransactions(JSON.parse(collectionData), transactions);
validator.validateTransaction(transactions, (err, result) => {
let requestIds = Object.keys(result.requests);
expect(err).to.be.null;
requestIds.forEach((requestId) => {
if (result.requests[requestId].endpoints[0].matched === false) {
failRequests.push(result.requests[requestId]);
}
});
expect(failRequests.length).to.equal(0);
expect(result.missingEndpoints.length).to.equal(0);
});
});
it('Should generate mismatch with invalid webhook', function() {
const collectionSource = path.join(__dirname, OPENAPI_31_COLLECTIONS +
'/simpleCollectionsWithWebhooksErrorWebhook.json'),
collectionData = fs.readFileSync(collectionSource, 'utf8'),
schemaSource = path.join(__dirname, OPENAPI_31_COLLECTIONS + '/simpleCollectionsWithWebhooksSpec.yaml'),
schemaData = fs.readFileSync(schemaSource, 'utf8'),
validator = new SchemaPack({
type: 'string',
data: schemaData
}, { suggestAvailableFixes: true });
let transactions = [],
failRequests = [];
getAllTransactions(JSON.parse(collectionData), transactions);
validator.validateTransaction(transactions, (err, result) => {
let requestIds = Object.keys(result.requests);
expect(err).to.be.null;
requestIds.forEach((requestId) => {
if (result.requests[requestId].endpoints[0].matched === false) {
failRequests.push(result.requests[requestId]);
}
});
expect(failRequests.length).to.equal(1);
expect(result.missingEndpoints.length).to.equal(0);
});
});