Skip to content

Commit

Permalink
Merge 2ecb438 into 9f78973
Browse files Browse the repository at this point in the history
  • Loading branch information
odelvalle committed May 31, 2019
2 parents 9f78973 + 2ecb438 commit b64037c
Show file tree
Hide file tree
Showing 12 changed files with 987 additions and 120 deletions.
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -117,6 +117,30 @@ Additionally, you can inclide more tests using plugins.
item: 'data'
```

## Global test
You can define global test in swagger for all paths and all responses, except to... :-)

```
x-pm-test:
tests:
- raw: |
var body = JSON.parse(responseBody);
tests['Internal code response is ok'] = body.code === pm.response.code;
except:
responses:
- 201
- 401
methods:
- delete
```

### Why `x-pm-test-ignore404`
You decide. If do you have a GET that return an array object, what do you prefer, return empty array or 404 response.

- empty array?: use `x-pm-test-ignore404` in GET definition
- 404?: don't use `x-pm-test-ignore404`.


## Testing
```
$ npm install -g driven-swagger-test
Expand Down
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.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "driven-swagger-test",
"version": "1.1.1-beta.1",
"version": "1.1.1-beta.2",
"description": "Test swagger quality definition and create Postman collection with test",
"preferGlobal": true,
"bin": "driven-swagger-test.js",
Expand Down
4 changes: 2 additions & 2 deletions src/endpoint.js
Expand Up @@ -68,7 +68,7 @@ function buildPostmanItems (endpoint) {
return acc;
}, test.params);

//add all parameters in defenition if not exist in test
//add all parameters in definition if not exist in test
test.params = (endpoint.def.parameters || []).filter(dp => dp.in !== 'body').reduce((acc, p) => {
if (!acc.find(pl => p.name === pl.name && p.in === pl.in)) {
acc.push({
Expand Down Expand Up @@ -143,7 +143,7 @@ function buildPostmanItem (endpoint, test, content, accept, status) {
name: (test.description || `[${status}] on ${url}`).replace('[url]', url),
request: {
method: endpoint.method,
url: `{{base-url}}${url.substring(1)}`,
url: `{{base-url}}${url}`,
// @ts-ignore
header: [
{ key: 'accept', value: accept },
Expand Down
76 changes: 38 additions & 38 deletions src/endpoints.js
Expand Up @@ -59,7 +59,7 @@ class Endpoints {
let security;
const securityDefinitions = { securities: [] };
Object.keys(this.swagger.securityDefinitions).forEach(sec => {
securityDefinitions.securities.push(Object.assign({ name: sec }, this.swagger.securityDefinitions[sec]));
securityDefinitions.securities.push(Object.assign({ key: sec }, this.swagger.securityDefinitions[sec]));
});

if (this.swagger.securityDefinitions) {
Expand Down Expand Up @@ -126,7 +126,7 @@ class Endpoints {
result.collection.variables.add(new Variable({
key: 'base-url',
id: 'base-url',
value: variable.value + this.swagger.basePath,
value: variable.value + (this.swagger.basePath || ''),
type: 'string'
}));

Expand All @@ -145,7 +145,7 @@ class Endpoints {
result.collection.variables.add(new Variable({
key: 'base-url',
id: 'base-url',
value: this.swagger.schemes[0] + '://' + this.swagger.host + this.swagger.basePath,
value: this.swagger.schemes[0] + '://' + this.swagger.host + (this.swagger.basePath || ''),
type: 'string'
}));
}
Expand All @@ -158,8 +158,8 @@ async function getSecurities (securityDef, tokenUrl) {
const security = {};

// only support password flow
const oauth2 = securityDef.securities.find(security => security.type === 'oauth2');
if (oauth2) {
const oauth = securityDef.securities.filter(security => security.type === 'oauth2');
for (const oauth2 of oauth) {
switch (oauth2.flow) {
case 'password':
const b = Buffer.from(securityDef.value.client_id + ':' + securityDef.value.client_secret);
Expand All @@ -181,14 +181,14 @@ async function getSecurities (securityDef, tokenUrl) {
try {
const response = await request.post(authRequest);
const result = JSON.parse(response);
security[oauth2.name] = {
security[oauth2.key] = {
param: {
in: 'header',
name: 'Authorization',
value: `{{${oauth2.name}}}`
value: `{{authorization}}`
},
variable: {
name: oauth2.name,
name: 'authorization',
value: result.token_type + ' ' + result.access_token
}
};
Expand All @@ -198,47 +198,47 @@ async function getSecurities (securityDef, tokenUrl) {

break;
default:
security[oauth2.name] = {
security[oauth2.key] = {
param: {
in: 'header',
name: 'Authorization',
value: `{{${oauth2.name}}}`
value: `{{authorization}}`
},
variable: {
name: oauth2.name,
name: 'authorization',
value: 'unsupported'
}
};
}
}
};

const basic = securityDef.securities.find(security => security.type === 'basic');
if (basic) {
const b = Buffer.from(securityDef.value.client_id + ':' + securityDef.value.client_secret);

security[basic.name] = {
param: {
in: 'header',
name: 'Authorization',
value: `{{${basic.name}}}`
},
variable: {
name: basic.name,
value: 'Basic ' + b.toString('base64')
}
};
}
securityDef.securities.filter(security => security.type === 'basic')
.forEach(basic => {
const b = Buffer.from(securityDef.value.client_id + ':' + securityDef.value.client_secret);

const apiKey = securityDef.securities.find(security => security.type === 'apiKey');
if (apiKey) {
security[apiKey.name] = {
param: {
in: apiKey.in,
name: apiKey.name,
value: `{{${apiKey.name}}}`
}
};
}
security[basic.key] = {
param: {
in: 'header',
name: 'Authorization',
value: `{{${basic.name}}}`
},
variable: {
name: basic.name,
value: 'Basic ' + b.toString('base64')
}
};
});

securityDef.securities.filter(security => security.type === 'apiKey')
.forEach(apiKey => {
security[apiKey.key] = {
param: {
in: apiKey.in,
name: apiKey.name,
value: `{{${apiKey.name}}}`
}
};
});

return security;
}
Expand Down
4 changes: 4 additions & 0 deletions src/events.js
Expand Up @@ -26,6 +26,10 @@ class Events {
result = result.concat(plugins.definitionTests(test.plugins));
}

if (test.raw) {
result = result.concat(test.raw);
}

return {
listen: 'test',
script: {
Expand Down
11 changes: 11 additions & 0 deletions test/postman-spec.js
Expand Up @@ -14,6 +14,17 @@ describe('Swagger definition to Postman test', () => {
it('Pet Store run all', async () => {
try {
const results = await swaggerTests(`${__dirname}/swaggers/petstore-swagger.yaml`, {
run: `${__dirname}/data.json`,
save: false // don't save postman collection to disk
});

console.assert(!results.tests.definition.some(result => result.code >= 5000), 'Errors in test.');
} catch (error) { throw error; }
});

it('Global test run ok', async () => {
try {
const results = await swaggerTests(`${__dirname}/swaggers/GET-global-postman.test-swagger.yaml`, {
run: `${__dirname}/data.json`,
save: true
});
Expand Down
10 changes: 5 additions & 5 deletions test/server.js
Expand Up @@ -22,17 +22,17 @@ module.exports.run = callback => {
});

app.use('/pet/findByStatus', (req, res, next) => {
if (req.query.status === 'aaaaaa') return res.status(400).json();
if (req.query.status === 'sold') return res.status(404).json();
if (req.query.status === 'aaaaaa') return res.status(400).json({ code: 400 });
if (req.query.status === 'sold') return res.status(404).json({ code: 404 });

return res.status(200).json({ code: 200, data: [] });
});

app.use('/pet/:id', (req, res, next) => {
if (Number(req.params.id) === 0) return res.status(404).json();
if (isNaN(Number(req.params.id))) return res.status(400).json();
if (Number(req.params.id) === 0) return res.status(404).json({ code: 404 });
if (isNaN(Number(req.params.id))) return res.status(400).json({ code: 400 });

if (req.method === 'GET') return res.status(200).json({});
if (req.method === 'GET') return res.status(200).json({ code: 200 });
if (req.method === 'PUT') return res.status(200).json({});
if (req.method === 'DELETE') return res.status(204).json();

Expand Down

0 comments on commit b64037c

Please sign in to comment.