From 2f32cb7e63da9838eb1547d7c27719a5d06a8271 Mon Sep 17 00:00:00 2001 From: Omar del Valle Rodriguez Date: Wed, 2 Dec 2020 15:36:14 +0100 Subject: [PATCH 1/6] upgrade version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 908b783..fb0d936 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "driven-swagger-test", - "version": "1.1.8", + "version": "1.1.9", "description": "Test swagger quality definition and create Postman collection with test", "preferGlobal": true, "bin": "driven-swagger-test.js", From 8bdc6dc7e9b34f2fe9465760378206542d1144ba Mon Sep 17 00:00:00 2001 From: Omar del Valle Rodriguez Date: Wed, 2 Dec 2020 18:18:25 +0100 Subject: [PATCH 2/6] Ignore redirect to test 301 & 302 --- package-lock.json | 2 +- package.json | 2 +- src/swagger.js | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2cb17c6..7257ab8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "driven-swagger-test", - "version": "1.1.7", + "version": "1.1.9", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index fb0d936..5696934 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "driven-swagger-test", - "version": "1.1.9", + "version": "1.1.10", "description": "Test swagger quality definition and create Postman collection with test", "preferGlobal": true, "bin": "driven-swagger-test.js", diff --git a/src/swagger.js b/src/swagger.js index 0b49474..11cccce 100644 --- a/src/swagger.js +++ b/src/swagger.js @@ -70,6 +70,7 @@ module.exports = async (swagger, options = {}) => { process.stdout.write('\nOption --run detected. Using newman to run collection.\n'); const newmanOptions = { + ignoreRedirects: true, collection: result.collection, reporters: 'cli' }; From 88e6426d3ea835e5a08f7b25715ae371b99adebd Mon Sep 17 00:00:00 2001 From: Omar del Valle Rodriguez Date: Fri, 4 Dec 2020 09:19:46 +0100 Subject: [PATCH 3/6] fixes securities errors && redirects --- package-lock.json | 2 +- src/endpoint.js | 9 ++++++--- src/endpoints.js | 11 ++++++----- src/plugins/definitions/test-security.js | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7257ab8..dd4d9cc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "driven-swagger-test", - "version": "1.1.9", + "version": "1.1.10", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/endpoint.js b/src/endpoint.js index a47e01d..4e61288 100644 --- a/src/endpoint.js +++ b/src/endpoint.js @@ -1,3 +1,4 @@ +// @ts-nocheck 'use strict'; const Item = require('postman-collection').Item; @@ -30,7 +31,9 @@ class Endpoint { tests: (globalTests && globalTests['tests']) || [] }; - this.security = security; + this.security = security && + this.def.security && + this.def.security.length === 0 ? undefined : security; buildPostmanItems(this); return this.postmanItems; @@ -106,7 +109,7 @@ function getGlobalsTest (endpoint, status) { function buildDefaultsTest (endpoint, status) { const tests = [{ - description: `['${status}'] on ${endpoint.url}`, + description: `['${status}'] ${endpoint.def.responses[status].description}`, }]; if (!endpoint.def.parameters) return tests; @@ -136,7 +139,7 @@ function buildPostmanItem (endpoint, test, content, accept, status) { pathParameters.forEach(param => { urlVariables.push({ key: param.name, value: param.value.toString() }); - url = url.replace(`{${param.name}}`, `:${param.name}`); + url = url.replace(`{${param.name}}`, param.variable ? `{{${param.name}}}` : `:${param.name}`); }); const item = new Item({ diff --git a/src/endpoints.js b/src/endpoints.js index 8a42014..d49f62b 100644 --- a/src/endpoints.js +++ b/src/endpoints.js @@ -89,7 +89,8 @@ class Endpoints { let results = []; this.endpoints.forEach(endpoint => { let security; - if (this.swagger.securityDefinitions && endpoint.def.security) { + const securityDefinitions = endpoint.def.security || this.swagger.securityDefinitions || []; + if (securityDefinitions && securityDefinitions.length > 0) { const key = Object.keys(endpoint.def.security[0])[0]; security = securities[key]; @@ -103,18 +104,18 @@ class Endpoints { let folder; if (endpoint.def.tags && endpoint.def.tags.length > 0) { folder = result.collection.items.find(member => member.name === endpoint.def.tags[0], null); - // @ts-ignore if (folder) folder.item = folder.item.concat(members); else members = { name: endpoint.def.tags[0], item: members }; } - // @ts-ignore if (!folder) result.collection.items.members = result.collection.items.members.concat(members); onExportEndpoint(endpoint.method + ': ' + endpoint.url); // Test definition results - results = results.concat(deinitionTests - .definitionTestsPassed(endpoint, this.swagger.security || endpoint.def.security)); + results = results.concat(deinitionTests.definitionTestsPassed( + endpoint, + endpoint.def.buildPostmanItems || this.swagger.security + )); }); if (results.length > 0 && results.some(result => result.code >= 5000)) { diff --git a/src/plugins/definitions/test-security.js b/src/plugins/definitions/test-security.js index abdcc88..5296bfc 100644 --- a/src/plugins/definitions/test-security.js +++ b/src/plugins/definitions/test-security.js @@ -14,7 +14,7 @@ class TestSecurity extends TestResponse { */ security (definition, security) { const result = []; - if (security === undefined) return result; + if (security === undefined || (definition.security && definition.security.length === 0)) return result; const error = this.accept(definition.responses, ['401'], 'Security definition, should be contain (401) response.', 6001); From a8e59b451e7579d8a8f11fe9472d1fa7b3912416 Mon Sep 17 00:00:00 2001 From: Omar del Valle Rodriguez Date: Fri, 4 Dec 2020 09:20:55 +0100 Subject: [PATCH 4/6] update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5696934..8a54ca9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "driven-swagger-test", - "version": "1.1.10", + "version": "1.1.11", "description": "Test swagger quality definition and create Postman collection with test", "preferGlobal": true, "bin": "driven-swagger-test.js", From 1b2195ff640d5f5ef91b220fa34a31e9c8c89c2b Mon Sep 17 00:00:00 2001 From: Omar del Valle Rodriguez Date: Thu, 21 Jan 2021 08:05:01 +0100 Subject: [PATCH 5/6] - production option New option to allow run only GETs methods if production is true --- package-lock.json | 2 +- package.json | 6 +- src/swagger.js | 22 +++++ test/postman-spec.js | 12 +++ test/swaggers/petstore-swagger-postman.json | 94 ++++++++++----------- 5 files changed, 85 insertions(+), 51 deletions(-) diff --git a/package-lock.json b/package-lock.json index dd4d9cc..6f939d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "driven-swagger-test", - "version": "1.1.10", + "version": "1.1.11", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8a54ca9..4299c40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "driven-swagger-test", - "version": "1.1.11", + "version": "1.1.12", "description": "Test swagger quality definition and create Postman collection with test", "preferGlobal": true, "bin": "driven-swagger-test.js", @@ -50,7 +50,7 @@ "typescript": "^3.4.5" }, "scripts": { - "test": "nyc --reporter=lcov mocha --timeout 0 --exit test/*-spec.js", - "coverage": "nyc --reporter=text-lcov mocha --timeout 0 --exit test/*-spec.js | coveralls" + "test": "nyc --reporter=lcov node_modules/mocha/bin/_mocha --timeout 0 --exit test/*-spec.js", + "coverage": "nyc --reporter=text-lcov node_modules/mocha/bin/_mocha --timeout 0 --exit test/*-spec.js | coveralls" } } diff --git a/src/swagger.js b/src/swagger.js index 11cccce..80da82f 100644 --- a/src/swagger.js +++ b/src/swagger.js @@ -9,17 +9,35 @@ const colors = require('colors'); const CollectionError = require('./errors').CollectionError; const Endpoints = require('./endpoints'); +const { Collection } = require('postman-collection'); /** * @param {string} swagger Path to swagger file * @param {object} [options] Options parameters * @param {boolean | string} [options.run] Run postman collection using, optionally, data file * @param {boolean} [options.save] Save postman collection + * @param {boolean} [options.production] If production is true, only GET test are executed * @param {Array} [options.global] Globals options * @param {string} [options.tokenUrl] Url to resolve OAuth token (only support grant type: password) * */ module.exports = async (swagger, options = {}) => { + /** + * Disable Tests in production env + * + * @param {Array} members + */ + function productionTests (members, items = []) { + members.forEach(member => { + if (member.item) { + return productionTests(member.item, items); + } + if (member.request.method === 'GET') items.push(member); + }); + + return items; + }; + const absSwaggerFile = path.resolve(swagger); const absDataFile = options.run && typeof options.run === 'string' ? path.resolve(options.run) : null; options.global = options.global || []; @@ -69,6 +87,10 @@ module.exports = async (swagger, options = {}) => { if (!options.run) return result; process.stdout.write('\nOption --run detected. Using newman to run collection.\n'); + if (options.production) { + // @ts-ignore + result.collection.items.members = productionTests(result.collection.items.members); + } const newmanOptions = { ignoreRedirects: true, collection: result.collection, diff --git a/test/postman-spec.js b/test/postman-spec.js index a4d5e3e..6d65958 100644 --- a/test/postman-spec.js +++ b/test/postman-spec.js @@ -44,6 +44,18 @@ describe('Swagger definition to Postman test', () => { } catch (error) { throw error; } }); + it('Pet Store run all GETS in production', async () => { + try { + const results = await swaggerTests(`${__dirname}/swaggers/petstore-swagger.yaml`, { + run: `${__dirname}/data.json`, + save: true, + production: true + }); + + console.assert(!results.tests.definition.some(result => result.code >= 5000), 'Errors in test.'); + } catch (error) { throw error; } + }); + after(done => { server.stop(() => { done(); diff --git a/test/swaggers/petstore-swagger-postman.json b/test/swaggers/petstore-swagger-postman.json index a05d861..f795676 100644 --- a/test/swaggers/petstore-swagger-postman.json +++ b/test/swaggers/petstore-swagger-postman.json @@ -4,7 +4,7 @@ "name": "pet", "item": [ { - "id": "2e234662-4af4-4002-8e0a-2ad316eca3b4", + "id": "09b4e270-5f87-4fd4-a86a-2401ffb6fb70", "name": "[201] on /pet", "request": { "url": { @@ -42,7 +42,7 @@ { "listen": "test", "script": { - "id": "80fe4905-422c-49fe-85ba-ec64b1035543", + "id": "dfac8628-9143-425e-9d50-5ad20f9927c1", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -66,8 +66,8 @@ ] }, { - "id": "a8bcb021-2c4b-483b-b941-9fd2c51f081f", - "name": "['400'] on /pet", + "id": "3b973e99-7168-4e61-9180-aa37ea7e915e", + "name": "['400'] Invalid input", "request": { "url": { "path": [ @@ -103,7 +103,7 @@ { "listen": "test", "script": { - "id": "8517c9ea-3234-48a0-81cb-62acd0798b7f", + "id": "645aaafc-13c9-49fe-8159-f142ea9ec3f4", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -118,8 +118,8 @@ ] }, { - "id": "28e109ee-d056-4588-acc2-8c0396bdfddc", - "name": "['401'] on /pet", + "id": "bc1e9366-68b4-4ab0-a923-b415c2c960b7", + "name": "['401'] Unauthorized", "request": { "url": { "path": [ @@ -151,7 +151,7 @@ { "listen": "test", "script": { - "id": "ffff91ac-786a-4f4c-a077-7521eb9cd030", + "id": "12e56796-5cef-4317-9ad7-35a1eb152be5", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -166,7 +166,7 @@ ] }, { - "id": "111bb462-6454-4895-8cb9-74ad820c5c8d", + "id": "b99ce371-9135-47dd-88dd-0abe803163f5", "name": "[200] on /pet/findByStatus", "request": { "url": { @@ -210,7 +210,7 @@ { "listen": "test", "script": { - "id": "282a6740-beca-4e5f-9660-54c9c8e00d6d", + "id": "4f441eeb-8199-4587-b183-e4eafca1e390", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -305,7 +305,7 @@ ] }, { - "id": "71c40462-ce88-44ee-b582-d1b9a4639c7a", + "id": "1df20fc4-d411-4152-bb33-9a3f9a992b44", "name": "[400] on /pet/findByStatus", "request": { "url": { @@ -349,7 +349,7 @@ { "listen": "test", "script": { - "id": "f1a024dd-63f9-41b8-bc16-d5483f1afd21", + "id": "074473a2-e31f-4c5c-8e3f-4d11e7e6468d", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -364,8 +364,8 @@ ] }, { - "id": "a5e71575-9a99-4310-a846-f6c6a5bb4232", - "name": "['401'] on /pet/findByStatus", + "id": "e35945e8-a273-4ab1-991b-337a5a8e5111", + "name": "['401'] Unauthorized", "request": { "url": { "path": [ @@ -405,7 +405,7 @@ { "listen": "test", "script": { - "id": "4497d6aa-6cd3-4edd-b32b-54c2afd8f915", + "id": "814a61b7-4a87-4adf-b375-1ec88ad60f90", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -420,7 +420,7 @@ ] }, { - "id": "b1d4db2a-3eaf-4ca9-8dca-3b7b04d5f40a", + "id": "80bd28dc-3d30-4960-abe8-802cb8aa049b", "name": "[404] on /pet/findByStatus", "request": { "url": { @@ -464,7 +464,7 @@ { "listen": "test", "script": { - "id": "5ac47f26-389c-48fa-829f-3c63cf507fd9", + "id": "0eccd2b2-561e-41ba-880a-b23b093c49a4", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -479,7 +479,7 @@ ] }, { - "id": "270559d8-4c61-48a0-a6a3-e4261a7b752b", + "id": "6edc5da7-b1ce-44ce-acf7-2f7cb010adb9", "name": "[200] on /pet/:petId", "request": { "url": { @@ -523,7 +523,7 @@ { "listen": "test", "script": { - "id": "6400eaa5-7131-4717-b945-73f99d06308b", + "id": "a366ae8a-3c15-4949-9047-dacb32225ec4", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -538,7 +538,7 @@ ] }, { - "id": "905085a7-8c43-4a0f-9a98-7d354e28a490", + "id": "d75687fa-b27a-47b1-88cb-d29e28eaf51b", "name": "[400] on /pet/:petId", "request": { "url": { @@ -582,7 +582,7 @@ { "listen": "test", "script": { - "id": "850ad5eb-0b70-447c-9916-12b00b7c5ab7", + "id": "33c127e1-5a85-4d49-9bc5-85dc25249d09", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -597,8 +597,8 @@ ] }, { - "id": "8eacc4ff-562f-4e04-9415-16299fd31d5f", - "name": "['401'] on /pet/{petId}", + "id": "a151b73f-695a-42e8-847b-5fd552f061cd", + "name": "['401'] Unauthorized", "request": { "url": { "path": [ @@ -637,7 +637,7 @@ { "listen": "test", "script": { - "id": "8a62a929-dd0c-4b06-81fa-deb8a6cc943c", + "id": "b8a507d7-9a64-4a98-a6a6-976927243263", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -652,8 +652,8 @@ ] }, { - "id": "7a80d581-dc7a-4e63-b62e-34ad0fd463d8", - "name": "['404'] on /pet/{petId}", + "id": "b0be6ded-3666-4267-87e2-aca34c426ae9", + "name": "['404'] Pet not found", "request": { "url": { "path": [ @@ -696,7 +696,7 @@ { "listen": "test", "script": { - "id": "bc41d582-4b5d-40cd-b579-d179b6fcd67d", + "id": "9a96a506-5836-4b85-8697-34a34be8c5fa", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -711,7 +711,7 @@ ] }, { - "id": "4548443e-0bc5-43b1-84e6-c366f9aaf07a", + "id": "22f59e85-a669-4804-a2c9-b36c9ebaa0ab", "name": "[200] on /pet/:petId", "request": { "url": { @@ -755,7 +755,7 @@ { "listen": "test", "script": { - "id": "78d1af5e-4e5b-41e4-8a64-2d9368c8a4b4", + "id": "6b93370b-f774-482c-baf6-b1a21f383205", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -770,7 +770,7 @@ ] }, { - "id": "a4cd5ed7-b57e-46ee-9106-b452a33a9937", + "id": "0993f916-0118-48a9-a1ac-a1971fac25d2", "name": "[400] on /pet/:petId", "request": { "url": { @@ -814,7 +814,7 @@ { "listen": "test", "script": { - "id": "59b677c6-9220-4f4f-9ca9-c8476725abfa", + "id": "6c5c5ba5-524a-4fe9-9681-810cef85391d", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -829,8 +829,8 @@ ] }, { - "id": "fd4cd8be-c791-49c4-ab93-1b1372d40707", - "name": "['401'] on /pet/{petId}", + "id": "c994752b-f7b0-403b-aed2-cf8e1542b7d3", + "name": "['401'] Unauthorized", "request": { "url": { "path": [ @@ -868,7 +868,7 @@ { "listen": "test", "script": { - "id": "2042f22a-cd4a-4a7b-8a5c-2394168a2044", + "id": "6cac3d90-df1f-4732-b1d6-b49999fda5f4", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -883,7 +883,7 @@ ] }, { - "id": "18269292-aeda-4028-bba4-198adf2dadec", + "id": "8dba468c-7292-4a6e-8f47-ecdd8bc4f1a7", "name": "[404] on /pet/:petId", "request": { "url": { @@ -927,7 +927,7 @@ { "listen": "test", "script": { - "id": "44d9b17b-b41b-400c-adc4-c38ff6726c21", + "id": "39e3f1f6-140b-4d17-9d70-dac74a137c65", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -942,7 +942,7 @@ ] }, { - "id": "f6396764-3b7a-4739-84d5-c73a1bf2d189", + "id": "0880e179-4f96-4300-820a-66b7ccfe47f0", "name": "[204] on /pet/:petId", "request": { "url": { @@ -991,7 +991,7 @@ { "listen": "test", "script": { - "id": "97dc9521-1b15-4c96-9164-2bc3db99cbfb", + "id": "fac37ca8-0aeb-4c06-8647-1b37eddb9ce5", "type": "text/javascript", "exec": [ "pm.test(\"Body is empty\", function() {", @@ -1007,7 +1007,7 @@ ] }, { - "id": "52ba5dd0-7489-46ba-9e03-a815bd25cf62", + "id": "e4f08e56-7b8d-409f-b6d5-3b3358152f21", "name": "[400] on /pet/:petId", "request": { "url": { @@ -1056,7 +1056,7 @@ { "listen": "test", "script": { - "id": "93a863e3-d9a3-4ad6-99b9-d490bda92a7e", + "id": "fae7d8f5-403c-428e-93f4-fdf7215d6483", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -1071,8 +1071,8 @@ ] }, { - "id": "5ccc5e55-f24c-4abd-9acc-ea80e7a136ab", - "name": "['401'] on /pet/{petId}", + "id": "fa384f3c-80d5-4442-b5da-5dfedae25f94", + "name": "['401'] Unauthorized", "request": { "url": { "path": [ @@ -1116,7 +1116,7 @@ { "listen": "test", "script": { - "id": "b917ed12-399d-49e6-9b0e-594663e9e11a", + "id": "5090c01b-40cc-4097-91b5-c2e57fa5ebb7", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -1131,8 +1131,8 @@ ] }, { - "id": "95b53646-c490-4cd5-8380-8dfa3bb27cb5", - "name": "['404'] on /pet/{petId}", + "id": "d05ddcc6-e8bf-42cc-ae71-fa8d35c96fa1", + "name": "['404'] Pet not found", "request": { "url": { "path": [ @@ -1180,7 +1180,7 @@ { "listen": "test", "script": { - "id": "16f9f446-5334-4393-ad27-efa85f63e7fd", + "id": "08fa68fb-fbc3-4523-b644-4e4a7c37d6a7", "type": "text/javascript", "exec": [ "pm.test(\"Content-Type is present\", function() {", @@ -1219,7 +1219,7 @@ } ], "info": { - "_postman_id": "f52cdd6e-b18f-4032-a5f4-06b0d47458a4", + "_postman_id": "1b7c8613-66b2-44a0-9ef6-99481210fa12", "name": "Swagger Petstore", "version": { "raw": "1.0.0", From ceb9f92ef63924acc58cc66811cbc06a090aa11c Mon Sep 17 00:00:00 2001 From: Omar del Valle Rodriguez Date: Thu, 21 Jan 2021 08:06:47 +0100 Subject: [PATCH 6/6] remove require --- src/swagger.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/swagger.js b/src/swagger.js index 80da82f..f70dcdf 100644 --- a/src/swagger.js +++ b/src/swagger.js @@ -9,7 +9,6 @@ const colors = require('colors'); const CollectionError = require('./errors').CollectionError; const Endpoints = require('./endpoints'); -const { Collection } = require('postman-collection'); /** * @param {string} swagger Path to swagger file