Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rename swagger command to openapi and keep swagger as alias #333

Merged
merged 10 commits into from
Jul 6, 2021
112 changes: 112 additions & 0 deletions __tests__/__fixtures__/invalid-oas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore",
"description": "A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "Swagger API Team",
"email": "[email protected]",
"url": "http://swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"servers": [
{
"url": "http://petstore.swagger.io/api"
}
],
"paths": {
"/pets": {
"get": {
"description": "Returns all pets from the system that the user has access to\nNam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.\n\nSed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.\n",
"operationId": "findPets",
"parameters": [
{
"name": "tags",
"in": "query",
"description": "tags to filter by",
"required": false,
"style": "form",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
},
{
"name": "limit",
"in": "query",
"description": "maximum number of results to return",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
}
],
"responses": {
"200": {
"description": "pet response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Pet"
}
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Pet": {
"allOf": [
{
"$ref": "#/components/schemas/NewPet"
},
{
"required": ["id"],
"properties": {
"id": {
"type": "integer",
"format": "int64"
}
}
}
]
},
"NewPet": {
"required": ["name"],
"properties": {
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
}
}
}
}
}
40 changes: 30 additions & 10 deletions __tests__/cmds/swagger.test.js → __tests__/cmds/openapi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const config = require('config');
const fs = require('fs');
const promptHandler = require('../../src/lib/prompts');
const swagger = require('../../src/cmds/swagger');
const openapi = require('../../src/cmds/openapi');

const key = 'Xmw4bGctRVIQz7R7dQXqH9nQe5d0SPQs';
const version = '1.0.0';
Expand All @@ -13,7 +14,7 @@ const getCommandOutput = () => {
return [console.warn.mock.calls.join('\n\n'), console.log.mock.calls.join('\n\n')].filter(Boolean).join('\n\n');
};

describe('rdme swagger', () => {
describe('rdme openapi', () => {
const exampleRefLocation = `${config.host}/project/example-project/1.0.1/refs/ex`;

beforeAll(() => nock.disableNetConnect());
Expand Down Expand Up @@ -53,7 +54,7 @@ describe('rdme swagger', () => {
// to break.
fs.copyFileSync('./__tests__/__fixtures__/swagger.json', './swagger.json');

return swagger.run({ key }).then(() => {
return openapi.run({ key }).then(() => {
expect(console.log).toHaveBeenCalledTimes(2);

const output = getCommandOutput();
Expand Down Expand Up @@ -86,7 +87,7 @@ describe('rdme swagger', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

return expect(swagger.run({ spec: './__tests__/__fixtures__/swagger.json', key, version }))
return expect(openapi.run({ spec: './__tests__/__fixtures__/swagger.json', key, version }))
.rejects.toThrow('The version you specified')
.then(() => mock.done());
});
Expand All @@ -103,7 +104,7 @@ describe('rdme swagger', () => {
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });

return swagger.run({ spec: './__tests__/__fixtures__/swagger.json', key, version }).then(() => {
return openapi.run({ spec: './__tests__/__fixtures__/swagger.json', key, version }).then(() => {
expect(console.log).toHaveBeenCalledTimes(1);

const output = getCommandOutput();
Expand Down Expand Up @@ -134,7 +135,7 @@ describe('rdme swagger', () => {
help: 'If you need help, email [email protected] and mention log "fake-metrics-uuid".',
});

return expect(swagger.run({ spec: './__tests__/__fixtures__/invalid-swagger.json', key, version }))
return expect(openapi.run({ spec: './__tests__/__fixtures__/invalid-swagger.json', key, version }))
.rejects.toThrow('README VALIDATION ERROR "x-samples-languages" must be of type "Array"')
.then(() => mock.done());
});
Expand All @@ -161,7 +162,7 @@ describe('rdme swagger', () => {
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });

return swagger.run({ spec: './__tests__/__fixtures__/swagger.json', key }).then(() => {
return openapi.run({ spec: './__tests__/__fixtures__/swagger.json', key }).then(() => {
mock.done();
});
});
Expand All @@ -174,7 +175,7 @@ describe('rdme swagger', () => {
.basicAuth({ user: key })
.reply(201, { body: '{ id: 1 }' });

return swagger.run({ spec: './__tests__/__fixtures__/swagger.json', key, id, version }).then(() => {
return openapi.run({ spec: './__tests__/__fixtures__/swagger.json', key, id, version }).then(() => {
mock.done();
});
});
Expand All @@ -187,7 +188,7 @@ describe('rdme swagger', () => {
.basicAuth({ user: key })
.reply(201, { id: 1 }, { location: exampleRefLocation });

return swagger.run({ spec: './__tests__/__fixtures__/swagger.json', token: `${key}-${id}`, version }).then(() => {
return openapi.run({ spec: './__tests__/__fixtures__/swagger.json', token: `${key}-${id}`, version }).then(() => {
expect(console.warn).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledTimes(1);

Expand All @@ -200,7 +201,7 @@ describe('rdme swagger', () => {
});

it('should error if no api key provided', async () => {
await expect(swagger.run({ spec: './__tests__/__fixtures__/swagger.json' })).rejects.toThrow(
await expect(openapi.run({ spec: './__tests__/__fixtures__/swagger.json' })).rejects.toThrow(
'No project API key provided. Please use `--key`.'
);
});
Expand All @@ -211,8 +212,27 @@ describe('rdme swagger', () => {
.basicAuth({ user: key })
.reply(200, { version: '1.0.0' });

await expect(swagger.run({ key, version })).rejects.toThrow(/We couldn't find a Swagger or OpenAPI file./);
await expect(openapi.run({ key, version })).rejects.toThrow(/We couldn't find a Swagger or OpenAPI file./);

mock.done();
});

it('should throw an error if file is invalid', async () => {
const id = '5aa0409b7cf527a93bfb44df';

await expect(openapi.run({ spec: './__tests__/__fixtures__/invalid-oas.json', key, id, version })).rejects.toThrow(
'Token "Error" does not exist.'
);
});
});

describe('rdme swagger', () => {
it('should run `rdme openapi`', async () => {
const id = '5aa0409b7cf527a93bfb44df';

await expect(swagger.run({ spec: '', key, id, version })).rejects.toThrow(
"We couldn't find a Swagger or OpenAPI file.\n\n" +
'Run `rdme openapi ./path/to/file` to upload an existing file or `rdme oas init` to create a fresh one!'
);
});
});
6 changes: 1 addition & 5 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ describe('cli', () => {
});
});

it('should not show related commands on commands that have none', () => {
return cli(['swagger', '--help']).then(output => {
expect(output).not.toContain('Related commands');
});
});
it.todo('should not show related commands on commands that have none');
});

describe('subcommands', () => {
Expand Down
Loading