Skip to content

Commit

Permalink
fix(openapi): stricter tests, create flag edge case (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta authored Aug 30, 2022
1 parent 5a368ab commit 05e8d78
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
85 changes: 69 additions & 16 deletions __tests__/cmds/openapi/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ describe('rdme openapi', () => {
.reply(200, [])
.get(`/api/v1/version/${version}`)
.basicAuth({ user: key })
.reply(200, { version: '1.0.0' })
.reply(200, { version: '1.0.0' });

const postMock = getAPIMockWithVersionHeader(version)
.post('/api/v1/api-specification', { registryUUID })
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });
Expand All @@ -100,6 +102,7 @@ describe('rdme openapi', () => {

expect(console.info).toHaveBeenCalledTimes(0);

postMock.done();
return mock.done();
});

Expand Down Expand Up @@ -207,11 +210,17 @@ describe('rdme openapi', () => {
});

it('should create a new spec via `--create` flag and ignore `--id`', async () => {
prompts.inject(['update', version]);
const registryUUID = getRandomRegistryId();

const mock = getAPIMock()
.get('/api/v1/version')
.basicAuth({ user: key })
.reply(200, [{ version }])
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const postMock = getAPIMockWithVersionHeader(version)
.post('/api/v1/api-specification', { registryUUID })
.delayConnection(1000)
.basicAuth({ user: key })
Expand All @@ -235,6 +244,7 @@ describe('rdme openapi', () => {

expect(output).toMatch(/the `--id` parameter will be ignored/i);

postMock.done();
return mock.done();
});

Expand All @@ -254,7 +264,9 @@ describe('rdme openapi', () => {
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, [])
.reply(200, []);

const postMock = getAPIMockWithVersionHeader(version)
.post('/api/v1/api-specification', { registryUUID })
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });
Expand All @@ -267,6 +279,7 @@ describe('rdme openapi', () => {

expect(requestBody).toMatchSnapshot();

postMock.done();
return mock.done();
});

Expand All @@ -283,7 +296,9 @@ describe('rdme openapi', () => {

return body.match('form-data; name="spec"');
})
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const mockWithHeader = getAPIMockWithVersionHeader(version)
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, [])
Expand All @@ -306,6 +321,7 @@ describe('rdme openapi', () => {

expect(requestBody).toMatchSnapshot();

mockWithHeader.done();
return mock.done();
});

Expand Down Expand Up @@ -342,7 +358,9 @@ describe('rdme openapi', () => {

const mock = getAPIMock()
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: specVersion } })
.reply(201, { registryUUID, spec: { openapi: specVersion } });

const putMock = getAPIMockWithVersionHeader(version)
.put(`/api/v1/api-specification/${id}`, { registryUUID })
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });
Expand All @@ -358,15 +376,19 @@ describe('rdme openapi', () => {
})
).resolves.toBe(successfulUpdate(spec, type));

putMock.done();
return mock.done();
});

it('should return warning if providing `id` and `version`', async () => {
expect.assertions(4);
const registryUUID = getRandomRegistryId();

const mock = getAPIMock()
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const putMock = getAPIMockWithVersionHeader(version)
.put(`/api/v1/api-specification/${id}`, { registryUUID })
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });
Expand All @@ -382,6 +404,7 @@ describe('rdme openapi', () => {

expect(output).toMatch(/the `--version` option will be ignored/i);

putMock.done();
return mock.done();
});

Expand Down Expand Up @@ -491,6 +514,7 @@ describe('rdme openapi', () => {
});

it('should warn if providing both `update` and `id`', async () => {
expect.assertions(5);
const registryUUID = getRandomRegistryId();

const mock = getAPIMock()
Expand All @@ -499,7 +523,11 @@ describe('rdme openapi', () => {
.put('/api/v1/api-specification/spec1', { registryUUID })
.delayConnection(1000)
.basicAuth({ user: key })
.reply(201, { _id: 1 }, { location: exampleRefLocation });
.reply(function (uri, rBody, cb) {
expect(this.req.headers['x-readme-version']).toBeUndefined();
return cb(null, [201, { _id: 1 }, { location: exampleRefLocation }]);
});

const spec = './__tests__/__fixtures__/ref-oas/petstore.json';

await expect(
Expand Down Expand Up @@ -538,7 +566,9 @@ describe('rdme openapi', () => {

return body.match('form-data; name="spec"');
})
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const mockWithHeader = getAPIMockWithVersionHeader(version)
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, [])
Expand All @@ -553,6 +583,7 @@ describe('rdme openapi', () => {

await expect(openapi.run({ spec, key, version })).resolves.toBe(successfulUpload(spec));

mockWithHeader.done();
return mock.done();
});

Expand All @@ -571,7 +602,9 @@ describe('rdme openapi', () => {

return body.match('form-data; name="spec"');
})
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const mockWithHeader = getAPIMockWithVersionHeader(specVersion)
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, [])
Expand All @@ -586,6 +619,7 @@ describe('rdme openapi', () => {

await expect(openapi.run({ spec, key, version, useSpecVersion: true })).resolves.toBe(successfulUpload(spec));

mockWithHeader.done();
return mock.done();
});

Expand Down Expand Up @@ -660,7 +694,8 @@ describe('rdme openapi', () => {
});

it('should request a version list if version is not found', async () => {
prompts.inject(['create', '1.0.1']);
const selectedVersion = '1.0.1';
prompts.inject(['create', selectedVersion]);

const registryUUID = getRandomRegistryId();

Expand All @@ -672,7 +707,9 @@ describe('rdme openapi', () => {
.basicAuth({ user: key })
.reply(200, { from: '1.0.0', version: '1.0.1' })
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const mockWithHeader = getAPIMockWithVersionHeader(selectedVersion)
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, [])
Expand All @@ -684,6 +721,7 @@ describe('rdme openapi', () => {

await expect(openapi.run({ spec, key })).resolves.toBe(successfulUpload(spec, 'Swagger'));

mockWithHeader.done();
return mock.done();
});
});
Expand Down Expand Up @@ -765,7 +803,9 @@ describe('rdme openapi', () => {
.basicAuth({ user: key })
.reply(200, { version: '1.0.0' })
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const mockWithHeader = getAPIMockWithVersionHeader(version)
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, [])
Expand All @@ -782,6 +822,7 @@ describe('rdme openapi', () => {
})
).rejects.toStrictEqual(new APIError(errorObject));

mockWithHeader.done();
return mock.done();
});

Expand All @@ -797,7 +838,9 @@ describe('rdme openapi', () => {

const mock = getAPIMock()
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const putMock = getAPIMockWithVersionHeader(version)
.put(`/api/v1/api-specification/${id}`, { registryUUID })
.delayConnection(1000)
.basicAuth({ user: key })
Expand All @@ -812,6 +855,7 @@ describe('rdme openapi', () => {
})
).rejects.toStrictEqual(new APIError(errorObject));

putMock.done();
return mock.done();
});

Expand Down Expand Up @@ -857,7 +901,9 @@ describe('rdme openapi', () => {
.basicAuth({ user: key })
.reply(200, { version: '1.0.0' })
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const mockWithHeader = getAPIMockWithVersionHeader(version)
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, [])
Expand All @@ -870,6 +916,7 @@ describe('rdme openapi', () => {
openapi.run({ spec: require.resolve('@readme/oas-examples/2.0/json/petstore.json'), key, version })
).rejects.toStrictEqual(new APIError(errorObject));

mockWithHeader.done();
return mock.done();
});

Expand All @@ -881,7 +928,9 @@ describe('rdme openapi', () => {
.basicAuth({ user: key })
.reply(200, { version: '1.0.0' })
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const mockWithHeader = getAPIMockWithVersionHeader(version)
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, [])
Expand All @@ -898,6 +947,7 @@ describe('rdme openapi', () => {
)
);

mockWithHeader.done();
return mock.done();
});

Expand All @@ -909,7 +959,9 @@ describe('rdme openapi', () => {
.basicAuth({ user: key })
.reply(200, { version: '1.0.0' })
.post('/api/v1/api-registry', body => body.match('form-data; name="spec"'))
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } })
.reply(201, { registryUUID, spec: { openapi: '3.0.0' } });

const mockWithHeader = getAPIMockWithVersionHeader(version)
.get('/api/v1/api-specification')
.basicAuth({ user: key })
.reply(200, [])
Expand All @@ -926,6 +978,7 @@ describe('rdme openapi', () => {
)
);

mockWithHeader.done();
return mock.done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/helpers/get-api-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function getAPIMock(reqHeaders = {}) {
});
}

export function getAPIMockWithVersionHeader(v) {
export function getAPIMockWithVersionHeader(v: string) {
return getAPIMock({
'x-readme-version': v,
});
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default class OpenAPICommand extends Command {
selectedVersion = specVersion;
}

if (!id) {
if (create || !id) {
selectedVersion = await getProjectVersion(selectedVersion, key, true);
}

Expand Down

0 comments on commit 05e8d78

Please sign in to comment.