Skip to content

Commit f667c4f

Browse files
Check if API exists in api:validate command (#332)
* Add check to `api:validate` command to ensure API exists * Fix tests
1 parent ed6e7f3 commit f667c4f

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

src/commands/api/validate/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ValidateCommand extends BaseValidateCommand {
1111
const { args, flags } = await this.parse(ValidateCommand)
1212
const apiPath = getApiIdentifierArg(args)
1313
const validPath = await this.ensureVersion(apiPath)
14+
await this.checkApiExists(validPath)
1415
// eslint-disable-next-line immutable/no-let
1516
let validationResult = await this.getValidationResult(validPath)
1617
// Required to support On-Prem 2.4.1
@@ -21,6 +22,13 @@ class ValidateCommand extends BaseValidateCommand {
2122
this.exitWithCode(flags, validationResult)
2223
}
2324

25+
checkApiExists(apiPath) {
26+
return this.executeHttp({
27+
execute: () => getApi([apiPath]),
28+
options: { resolveStatus: [200] }
29+
})
30+
}
31+
2432
getValidationResult(apiPath) {
2533
return this.executeHttp({
2634
execute: () => getApi([apiPath, 'standardization']),

test/commands/api/validate/index.test.js

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@ describe('invalid api:validate', () => {
88

99
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
1010
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
11-
.get(`/${apiPath}/standardization`)
11+
.get(`/${apiPath}`)
1212
.reply(404, {
1313
code: 404,
1414
message: `SPEC ${apiPath} not found.`
1515
})
1616
)
1717
.stdout()
1818
.command(['api:validate', apiPath])
19-
.exit(0)
19+
.exit(2)
2020
.it('not found returned when fetching validation result of a non existing API')
2121

2222
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
23+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
24+
.get(`/${apiPath}`)
25+
.reply(200, {
26+
code: 200
27+
})
28+
)
2329
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
2430
.get(`/${apiPath}/standardization`)
2531
.reply(404, {
@@ -33,6 +39,12 @@ describe('invalid api:validate', () => {
3339
.it('not enabled returned when fetching validation result an existing')
3440

3541
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
42+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
43+
.get(`/${apiPath}`)
44+
.reply(200, {
45+
code: 200
46+
})
47+
)
3648
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
3749
.get(`/${apiPath}/standardization`)
3850
.reply(403, {
@@ -54,6 +66,12 @@ describe('invalid api:validate', () => {
5466
.it('not found returned when fetching default version of API')
5567

5668
test.env({ SWAGGERHUB_URL: 'https://example.com/v1' })
69+
.nock('https://example.com/v1/apis', { reqheaders: { Accept: 'application/json' } }, api => api
70+
.get(`/${apiPath}`)
71+
.reply(200, {
72+
code: 200
73+
})
74+
)
5775
.nock('https://example.com/v1/apis', { reqheaders: { Accept: 'application/json' } }, api => api
5876
.get(`/${apiPath}/standardization`)
5977
.reply(404)
@@ -76,6 +94,12 @@ describe('valid api:validate for swaggerhub on-premise <= 2.4.1', () => {
7694
const severity = 'warning'
7795

7896
test.env({ SWAGGERHUB_URL: 'https://example.com/v1' })
97+
.nock('https://example.com/v1/apis', { reqheaders: { Accept: 'application/json' } }, api => api
98+
.get(`/${apiPath}`)
99+
.reply(200, {
100+
code: 200
101+
})
102+
)
79103
.nock('https://example.com/v1/apis', { reqheaders: { Accept: 'application/json' } }, api => api
80104
.get(`/${apiPath}/standardization`)
81105
.reply(200, {
@@ -107,6 +131,12 @@ describe('valid api:validate', () => {
107131

108132
describe('without -c flag', () => {
109133
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
134+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
135+
.get(`/${apiPath}`)
136+
.reply(200, {
137+
code: 200
138+
})
139+
)
110140
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
111141
.get(`/${apiPath}/standardization`)
112142
.reply(200, {
@@ -125,6 +155,12 @@ describe('valid api:validate', () => {
125155

126156
describe('with -c flag', () => {
127157
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
158+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
159+
.get(`/${apiPath}`)
160+
.reply(200, {
161+
code: 200
162+
})
163+
)
128164
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
129165
.get(`/${apiPath}/standardization`)
130166
.reply(200, {
@@ -143,6 +179,12 @@ describe('valid api:validate', () => {
143179

144180
describe('with --fail-on-critical flag', () => {
145181
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
182+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
183+
.get(`/${apiPath}`)
184+
.reply(200, {
185+
code: 200
186+
})
187+
)
146188
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
147189
.get(`/${apiPath}/standardization`)
148190
.reply(200, {
@@ -165,6 +207,12 @@ describe('valid api:validate', () => {
165207

166208
describe('without -c flag', () => {
167209
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
210+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
211+
.get(`/${apiPath}`)
212+
.reply(200, {
213+
code: 200
214+
})
215+
)
168216
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
169217
.get(`/${apiPath}/standardization`)
170218
.reply(200, {
@@ -182,7 +230,14 @@ describe('valid api:validate', () => {
182230
})
183231

184232
describe('with -c flag', () => {
233+
185234
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
235+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
236+
.get(`/${apiPath}`)
237+
.reply(200, {
238+
code: 200
239+
})
240+
)
186241
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
187242
.get(`/${apiPath}/standardization`)
188243
.reply(200, {
@@ -201,6 +256,12 @@ describe('valid api:validate', () => {
201256

202257
describe('with --fail-on-critical flag', () => {
203258
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
259+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
260+
.get(`/${apiPath}`)
261+
.reply(200, {
262+
code: 200
263+
})
264+
)
204265
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
205266
.get(`/${apiPath}/standardization`)
206267
.reply(200, {
@@ -220,6 +281,12 @@ describe('valid api:validate', () => {
220281

221282
describe('when no standardization errors present', () => {
222283
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
284+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
285+
.get(`/${apiPath}`)
286+
.reply(200, {
287+
code: 200
288+
})
289+
)
223290
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
224291
.get(`/${apiPath}/standardization`)
225292
.reply(200, {
@@ -234,6 +301,12 @@ describe('valid api:validate', () => {
234301
})
235302

236303
test.stub(config, 'getConfig', () => ({ SWAGGERHUB_URL: 'https://api.swaggerhub.com' }))
304+
.nock('https://api.swaggerhub.com/apis', { reqheaders: { Accept: 'application/json' } }, api => api
305+
.get(`/${apiPath}`)
306+
.reply(200, {
307+
code: 200
308+
})
309+
)
237310
.nock('https://api.swaggerhub.com/apis', api => api
238311
.get(`/${apiPath.substring(0, apiPath.lastIndexOf('/'))}/settings/default`)
239312
.reply(200, {

0 commit comments

Comments
 (0)