Skip to content

Commit

Permalink
Merge pull request #53 from apiaddicts/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
marcosanz-apiquality authored Dec 30, 2024
2 parents 7423bdb + 88bf82a commit 1bfb143
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 130 deletions.
118 changes: 72 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,24 @@ try {
is_inline: false,
schema_is_inline: false,
schema_pretty_print: true,
generate_oneOf_anyOf:false,
minimal_endpoints:false,
environments:[
{
name : "DEV",
postman_collection_name: "%api_name%_DEV",
postman_environment_name: "%api_name%_DEV",
target_folder: "./out",
has_scopes: false,
microcks_headers:true,
application_token: false,
number_of_scopes: 0
number_of_scopes: 0,
application_token: false,
has_scopes: false,
read_only: false,
validate_schema:false,
host_server_pattern:"%dev%",

}
]
};
Expand Down Expand Up @@ -86,49 +95,54 @@ let endpointsPostman = [];
const endpoints = require('./src/generator/endpoints.js')(endpointsParsed);

_.forEach(endpoints, function (endpoint, i) {
endpoint = require('./src/generator/testStatus.js')(endpoint);
endpoint = require('./src/generator/testBody.js')(endpoint, configurationFile);
endpoint = require('./src/generator/contentType.js')(endpoint);
endpoint = require("./src/generator/microcks.js")(endpoint);
endpoint = require('./src/generator/authorization.js')(endpoint, endpoint.aux.status)
global.currentId = endpoint.request.method + endpoint.request.url.path[0]
global.currentId = global.currentId.replace(/{{/g,'{').replace(/}}/g,'}').split('?')[0]
if (endpoint.aux.status === 404 && endpoint.aux.pathParameter) {
endpoint.request.url.raw = _.replace(endpoint.request.url.raw, '{{' + endpoint.aux.pathParameter + '}}', '{{' +endpoint.aux.pathParameter + '_not_found}}')
endpoint.request.url.path[0] = _.replace(endpoint.request.url.path[0], '{{' +endpoint.aux.pathParameter + '}}', '{{' +endpoint.aux.pathParameter + '_not_found}}')
endpoint = require('./src/generator/body.js')(endpoint)
endpoint = require('./src/generator/queryParamsRequired.js')(endpoint)
endpointsPostman.push(endpoint)
} else if (endpoint.aux.status === 400) {
global.queryParamsRequiredAdded = []
let endpointPostman
do{
endpointPostman = require('./src/generator/queryParamsRequired.js')(endpoint,true)
if (endpointPostman) {
endpointPostman = require('./src/generator/body.js')(endpointPostman)
if (endpointPostman.aux.hasOwnProperty('suffix') && endpointPostman.aux.suffix.includes('wrong')) {
endpointPostman.name += '.with.' + endpointPostman.aux.suffix;
endpointPostman = require('./src/generator/queryParamsRequired.js')(endpointPostman);
endpointPostman.request.url.path[0] = _.replace(endpointPostman.request.url.path[0], '{{' +endpointPostman.aux.suffix.split(' ')[1]+ '}}', '{{' +endpointPostman.aux.suffix.split(' ')[1]+ '_wrong}}')
} else {
endpointPostman.name += '.without.' + _.last(global.queryParamsRequiredAdded);
endpointPostman.aux.suffix = '.without.' +_.last(global.queryParamsRequiredAdded);
for (let index = 0; index < endpoint.count; index++) {
let endpointCopy = _.cloneDeep(endpoint);
endpointCopy = require('./src/generator/testStatus.js')(endpoint);
endpointCopy = require('./src/generator/testBody.js')(endpoint, configurationFile);
endpointCopy = require('./src/generator/contentType.js')(endpoint);
endpointCopy = require("./src/generator/microcks.js")(endpoint);
endpointCopy = require('./src/generator/authorization.js')(endpoint, endpoint.aux.status)
global.currentId = endpointCopy.request.method + endpointCopy.request.url.path[0]
global.currentId = global.currentId.replace(/{{/g,'{').replace(/}}/g,'}').split('?')[0]
if (endpointCopy.aux.status === 404 && endpointCopy.aux.pathParameter) {
endpointCopy.request.url.raw = _.replace(endpointCopy.request.url.raw, '{{' + endpointCopy.aux.pathParameter + '}}', '{{' +endpointCopy.aux.pathParameter + '_not_found}}')
endpointCopy.request.url.path[0] = _.replace(endpointCopy.request.url.path[0], '{{' +endpointCopy.aux.pathParameter + '}}', '{{' +endpointCopy.aux.pathParameter + '_not_found}}')
endpointCopy = require('./src/generator/body.js')(endpointCopy,false,false,index)
endpointCopy = require('./src/generator/queryParamsRequired.js')(endpointCopy)
endpointsPostman.push(endpointCopy)
} else if (endpointCopy.aux.status === 400) {
global.queryParamsRequiredAdded = []
let endpointPostman
do{
endpointPostman = require('./src/generator/queryParamsRequired.js')(endpointCopy,true)
if (endpointPostman) {
endpointPostman = require('./src/generator/body.js')(endpointPostman,false, false, index)
if (endpointPostman.aux.hasOwnProperty('suffix') && endpointPostman.aux.suffix.includes('wrong')) {
endpointPostman.name += '.with.' + endpointPostman.aux.suffix;
endpointPostman = require('./src/generator/queryParamsRequired.js')(endpointPostman);
endpointPostman.request.url.path[0] = _.replace(endpointPostman.request.url.path[0], '{{' +endpointPostman.aux.suffix.split(' ')[1]+ '}}', '{{' +endpointPostman.aux.suffix.split(' ')[1]+ '_wrong}}')
} else {
endpointPostman.name += '.without.' + _.last(global.queryParamsRequiredAdded);
endpointPostman.aux.suffix = '.without.' +_.last(global.queryParamsRequiredAdded);
}
endpointsPostman.push(endpointPostman);
}
endpointsPostman.push(endpointPostman);
} while(endpointPostman)

const endpointWithoutQueryParamsRequired = require('./src/generator/queryParamsNotRequired.js')(endpointCopy,index);
if (endpointWithoutQueryParamsRequired && endpointCopy) {
endpointsPostman.push(endpointWithoutQueryParamsRequired);
}
} while(endpointPostman)

const endpointWithoutQueryParamsRequired = require('./src/generator/queryParamsNotRequired.js')(endpoint);
if (endpointWithoutQueryParamsRequired && endpoint) {
endpointsPostman.push(endpointWithoutQueryParamsRequired);

let hasWrongParams = global.configurationFile.minimal_endpoints ? false : true;

addBadRequestEndpoints(endpointsPostman, endpointCopy, 'requiredParams', '', true, false,index);
addBadRequestEndpoints(endpointsPostman, endpointCopy, 'wrongParams', '.wrong', false, hasWrongParams,index);
} else if ((endpointCopy.aux.status >= 200 && endpointCopy.aux.status < 300) || ((endpointCopy.aux.status === 401 || endpointCopy.aux.status === 403) && endpointCopy.aux.authorization)) {
endpointCopy = require('./src/generator/body.js')(endpointCopy,false,false,index);
endpointCopy = require('./src/generator/queryParamsRequired.js')(endpointCopy);
endpointsPostman.push(endpointCopy);
}

addBadRequestEndpoints(endpointsPostman, endpoint, 'requiredParams', '', true, false);
addBadRequestEndpoints(endpointsPostman, endpoint, 'wrongParams', '.wrong', false, true);
} else if ((endpoint.aux.status >= 200 && endpoint.aux.status < 300) || ((endpoint.aux.status === 401 || endpoint.aux.status === 403) && endpoint.aux.authorization)) {
endpoint = require('./src/generator/body.js')(endpoint);
endpoint = require('./src/generator/queryParamsRequired.js')(endpoint);
endpointsPostman.push(endpoint);
}
})

Expand All @@ -150,6 +164,7 @@ let environments = configurationFile.environments;
_.forEach(environments, function (element) {
const endpointsStage = _.cloneDeep(endpointsPostman)
let exclude = {}
let urlPath;
if ( element.read_only ) {
exclude.write = true
}
Expand Down Expand Up @@ -212,12 +227,23 @@ _.forEach(environments, function (element) {
}
}
}
if ( element.custom_authorizations_file ) {
require('./src/parser/authorizationRequests.js')(endpointsStage,element.custom_authorizations_file)
if(global.definition.components.securitySchemes){
console.log('securitySchemes');
let securityDefinition = require('./src/parser/openapiAuthorizationDefinition.js')(global.definition.components.securitySchemes)
if(securityDefinition){
require('./src/parser/authorizationRequests.js')(endpointsStage,securityDefinition)
} else {
exclude.auth = true
}
} else {
// Elimina la cabecera Authorization de las peticiones en Postman
exclude.auth = true
}

if(element.host_server_pattern){
urlPath = require('./src/generator/serverPath.js')(global.definition.servers,element.host_server_pattern)
}

let endpointsPostmanWithFolders = require('./src/generator/folders.js')(endpointsStage, exclude)
// Crea el listado de variables de entorno
let environmentVariables = require('./src/generator/environmentVariablesNames.js')(endpointsPostmanWithFolders)
Expand All @@ -232,14 +258,14 @@ _.forEach(environments, function (element) {
element.postman_environment_name = _.replace(element.postman_environment_name, '%api_name%', apiName)
}
require('./src/generator/collection.js')(element.target_folder, element.postman_collection_name, endpointsPostmanWithFolders)
require('./src/generator/environment.js')(element.target_folder, element.postman_environment_name, element.host, element.port, schemaHostBasePath,environmentVariables)
require('./src/generator/environment.js')(element.target_folder, element.postman_environment_name, element.host, element.port, schemaHostBasePath,environmentVariables,urlPath)
})
function addBadRequestEndpoints(endpointsPostman, endpointBase, memoryAlreadyAdded, suffix, withoutRequired, withWrongParam) {
function addBadRequestEndpoints(endpointsPostman, endpointBase, memoryAlreadyAdded, suffix, withoutRequired, withWrongParam,index) {
global[memoryAlreadyAdded] = [];
do {
var initialCount = global[memoryAlreadyAdded].length;
let endpointPostman = require('./src/generator/queryParamsRequired.js')(endpointBase);
endpointPostman = require('./src/generator/body.js')(endpointPostman, withoutRequired, withWrongParam);
endpointPostman = require('./src/generator/body.js')(endpointPostman, withoutRequired, withWrongParam,index);
if (global[memoryAlreadyAdded].length > initialCount) {
endpointPostman.name += '-' + _.last(global[memoryAlreadyAdded]) + suffix;
endpointPostman.aux.suffix = _.last(global[memoryAlreadyAdded]) + suffix;
Expand Down
102 changes: 54 additions & 48 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 🛠️ Openapi2Postman ![Release](https://img.shields.io/badge/release-2.0.x-purple) ![OpenApi](https://img.shields.io/badge/-openapi-%23Clojure?style=flat&logo=openapiinitiative&logoColor=white) ![Postman](https://img.shields.io/badge/postman-FF6C37?style=flat&logo=postman&logoColor=white) [![NPM](https://img.shields.io/badge/npm-%23CB3837.svg?style=flat&logo=npm&logoColor=white)](https://www.npmjs.com/package/openapi2postman) [![License: LGPL v3](https://img.shields.io/badge/license-LGPL_v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0)
# 🛠️ Openapi2Postman ![Release](https://img.shields.io/badge/release-2.0.x-purple) ![OpenApi](https://img.shields.io/badge/-openapi-%23Clojure?style=flat&logo=openapiinitiative&logoColor=white) ![Postman](https://img.shields.io/badge/postman-FF6C37?style=flat&logo=postman&logoColor=white) [![NPM](https://img.shields.io/badge/npm-%23CB3837.svg?style=flat&logo=npm&logoColor=white)](https://www.npmjs.com/package/openapi2postman) [![License: LGPL v3](https://img.shields.io/badge/license-LGPL_v3-blue.svg)](https://www.gnu.org/licenses/lgpl-3.0)

**Openapi2Postman** creates automatic tests from Openapi 3.0 using postman format. Also, it creates environments files, depending of configuration.
Those collections can be importend in postan application. The tests includes tests for 2xx, 4xx... and tests to validate output formats.
Those collections can be importend in postan application. The tests includes tests for 2xx, 4xx... and tests to validate output formats.

<p align="center">
<a href="https://apiaddicts.org/">
Expand All @@ -11,65 +11,70 @@ Those collections can be importend in postan application. The tests includes te

### This repository is intended for :octocat: **community** use, it can be modified and adapted without commercial use. If you need a version, support or help for your **enterprise** or project, please contact us 📧 [email protected]

[![Twitter](https://img.shields.io/badge/Twitter-%23000000.svg?style=for-the-badge&logo=x&logoColor=white)](https://twitter.com/APIAddicts)
[![Twitter](https://img.shields.io/badge/Twitter-%23000000.svg?style=for-the-badge&logo=x&logoColor=white)](https://twitter.com/APIAddicts)
[![Discord](https://img.shields.io/badge/Discord-%235865F2.svg?style=for-the-badge&logo=discord&logoColor=white)](https://discord.gg/ZdbGqMBYy8)
[![LinkedIn](https://img.shields.io/badge/linkedin-%230077B5.svg?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/company/apiaddicts/)
[![Facebook](https://img.shields.io/badge/Facebook-%231877F2.svg?style=for-the-badge&logo=Facebook&logoColor=white)](https://www.facebook.com/apiaddicts)
[![YouTube](https://img.shields.io/badge/YouTube-%23FF0000.svg?style=for-the-badge&logo=YouTube&logoColor=white)](https://www.youtube.com/@APIAddictslmaoo)

# 🙌 Join the **Openapi2postman** Adopters list
# 🙌 Join the **Openapi2postman** Adopters list

📢 If Openapi2postman is part of your organization's toolkit, we kindly encourage you to include your company's name in our Adopters list. 🙏 This not only significantly boosts the project's visibility and reputation but also represents a small yet impactful way to give back to the project.

| Organization | Description of Use / Referenc |
|---|---|
| [CloudAppi](https://cloudappi.net/) | Apification and generation of microservices |
| [Madrid Digital](https://www.comunidad.madrid/servicios/sede-electronica/madrid-digital/) | Generation of microservices |
| Organization | Description of Use / Referenc |
| ----------------------------------------------------------------------------------------- | ------------------------------------------- |
| [CloudAppi](https://cloudappi.net/) | Apification and generation of microservices |
| [Madrid Digital](https://www.comunidad.madrid/servicios/sede-electronica/madrid-digital/) | Generation of microservices |
| [APIquality](https://apiquality.io/) | Common contributor |

# 👩🏽‍💻 Contribute to ApiAddicts
# 👩🏽‍💻 Contribute to ApiAddicts

We're an inclusive and open community, welcoming you to join our effort to enhance ApiAddicts, and we're excited to prioritize tasks based on community input, inviting you to review and collaborate through our GitHub issue tracker.

Feel free to drop by and greet us on our GitHub discussion or Discord chat. You can also show your support by giving us some GitHub stars ⭐️, or by following us on Twitter, LinkedIn, and subscribing to our YouTube channel! 🚀

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/apiaddicts)


# 📑 Getting started
# 📑 Getting started

[![NPM](https://img.shields.io/badge/openapi2postman-%23CB3837.svg?style=for-the-badge&logo=npm&logoColor=white)](https://www.npmjs.com/package/openapi2postman)

### Install package

```
npm i openapi2postman -g
```

### First steps

#### `Arguments`
| argv | a |description |
|---|---|---|
| --file | -f | path openapi file |
| --configuration | -c | path config file |

#### `Normal` usage
| argv | a | description |
| --------------- | --- | ----------------- |
| --file | -f | path openapi file |
| --configuration | -c | path config file |

#### `Normal` usage

```
o2p -c .\example\o2p_config_file.json -f .\example\petstore.yaml
```

#### `Default` config
#### `Default` config

```
o2p -f .\example\petstore.yaml
```

If you do not use the -c argument, the following default configuration will be used. The output path `target_folder` is `./out`.

```
api_name: filename,
is_inline: false,
schema_is_inline: false,
schema_pretty_print: true,
generate_oneOf_anyOf:false,
minimal_endpoints:false,
environments:[
{
name : "DEV",
Expand All @@ -80,59 +85,60 @@ If you do not use the -c argument, the following default configuration will be u
application_token: false,
number_of_scopes: 0,
microcks_headers: false
read_only: false,
validate_schema:false,
host_server_pattern:"%dev%",
}
]
```


## Table of content

* [Structure and submodules](#structure-and-submodules)
* [Build and run](#build-and-run)
* [Prerequisites](#prerequisites)
* [Get the source code for the first time](#get-the-source-code-for-the-first-time)
* [Run](#run)
* [Get the latest changes](#get-the-latest-changes)
* [Contribute](#contribute)
* [IDE support](#ide-support)
* [IntelliJ IDEA](#intellij-idea)
* [Eclipse](#eclipse)
* [Documentation](#documentation)
* [Advanced Functionality](#advanced-functionality)
* [Additional resources](#additional-resources)

- [Structure and submodules](#structure-and-submodules)
- [Build and run](#build-and-run)
- [Prerequisites](#prerequisites)
- [Get the source code for the first time](#get-the-source-code-for-the-first-time)
- [Run](#run)
- [Get the latest changes](#get-the-latest-changes)
- [Contribute](#contribute)
- [IDE support](#ide-support)
- [IntelliJ IDEA](#intellij-idea)
- [Eclipse](#eclipse)
- [Documentation](#documentation)
- [Advanced Functionality](#advanced-functionality)
- [Additional resources](#additional-resources)

## Structure and submodules

* *[docs](docs)* - This module contents the guides to configurate and run the product. Documents Pdfs by CloudAppi.
* *[example](example)* - Integration and system tests for SoapUI.
* *[src](src)* – Source code
* *[test](soapui-maven-plugin-tester)* - Testing folder
- _[docs](docs)_ - This module contents the guides to configurate and run the product. Documents Pdfs by CloudAppi.
- _[example](example)_ - Integration and system tests for SoapUI.
- _[src](src)_ – Source code
- _[test](soapui-maven-plugin-tester)_ - Testing folder

## Build and run

### Prerequisites
You need node v10 or later.

You need node v10 or later.

### run

Please review the o2p_config_file.json before to execute the command
Execute
Execute
node index.js --configuration o2p_config_file.json --file example/swagger_provincias.yml

The output files are the following (in example folder):
* SWAGGER_API_TestSuite_DEV.postman_collection.json
* SWAGGER_API_TestSuiteEnv_DEV.postman_environment.json
* SWAGGER_API_TestSuite_PROD.postman_collection.json
* SWAGGER_API_TestSuiteEnv_PROD.postman_environment.json
The output files are the following (in example folder):

- SWAGGER_API_TestSuite_DEV.postman_collection.json
- SWAGGER_API_TestSuiteEnv_DEV.postman_environment.json
- SWAGGER_API_TestSuite_PROD.postman_collection.json
- SWAGGER_API_TestSuiteEnv_PROD.postman_environment.json

In postman:

* import generated postman collection file in postman
* import generated postman environment file in postman
* update variables in the envirnoment to test all cases

- import generated postman collection file in postman
- import generated postman environment file in postman
- update variables in the envirnoment to test all cases

### Configuration file

Expand All @@ -147,11 +153,11 @@ Node.js and npm package manager are required to run the tool. Its adviced to use
[https://nodejs.org/es/download/](https://nodejs.org/es/download/)

## 💛 Sponsors

<p align="center">
<a href="https://apiaddicts.org/">
<img src="https://apiaddicts.cloudappi.net/web/image/4248/LOGOCloudappi2020Versiones-01.png" alt="cloudappi" width="150"/>
<img src="https://www.comunidad.madrid/sites/default/files/styles/block_teaser_image/public/img/logos-simbolos/logo_centrado_md.png?itok=4rTUhmcj" alt="md" width="150"/>
<img src="https://apiaddicts-web.s3.eu-west-1.amazonaws.com/wp-content/uploads/2022/03/17155736/cropped-APIAddicts-logotipo_rojo.png" height = "75">
</a>
</p>

Loading

0 comments on commit 1bfb143

Please sign in to comment.