Skip to content

Commit

Permalink
Merge pull request #24 from Finect/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
odelvalle committed Jan 21, 2021
2 parents f0a68ad + ceb9f92 commit a658566
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "driven-swagger-test",
"version": "1.1.8",
"version": "1.1.12",
"description": "Test swagger quality definition and create Postman collection with test",
"preferGlobal": true,
"bin": "driven-swagger-test.js",
Expand Down Expand Up @@ -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"
}
}
9 changes: 6 additions & 3 deletions src/endpoint.js
@@ -1,3 +1,4 @@
// @ts-nocheck
'use strict';

const Item = require('postman-collection').Item;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down
11 changes: 6 additions & 5 deletions src/endpoints.js
Expand Up @@ -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];

Expand All @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/definitions/test-security.js
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions src/swagger.js
Expand Up @@ -15,11 +15,28 @@ const Endpoints = require('./endpoints');
* @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<any>} [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<any>} 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 || [];
Expand Down Expand Up @@ -69,7 +86,12 @@ 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,
reporters: 'cli'
};
Expand Down
12 changes: 12 additions & 0 deletions test/postman-spec.js
Expand Up @@ -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();
Expand Down

0 comments on commit a658566

Please sign in to comment.