1
1
const nock = require ( 'nock' ) ;
2
+ const chalk = require ( 'chalk' ) ;
2
3
const config = require ( 'config' ) ;
3
4
const fs = require ( 'fs' ) ;
4
5
const promptHandler = require ( '../../src/lib/prompts' ) ;
@@ -9,25 +10,41 @@ const APIError = require('../../src/lib/apiError');
9
10
const key = 'API_KEY' ;
10
11
const id = '5aa0409b7cf527a93bfb44df' ;
11
12
const version = '1.0.0' ;
13
+ const exampleRefLocation = `${ config . host } /project/example-project/1.0.1/refs/ex` ;
14
+ const successfulMessageBase = [
15
+ '' ,
16
+ `\t${ chalk . green ( exampleRefLocation ) } ` ,
17
+ '' ,
18
+ 'To update your OpenAPI or Swagger definition, run the following:' ,
19
+ '' ,
20
+ `\t${ chalk . green ( `rdme openapi FILE --key=${ key } --id=1` ) } ` ,
21
+ ] ;
22
+ const successfulUpload = [
23
+ "You've successfully uploaded a new OpenAPI file to your ReadMe project!" ,
24
+ ...successfulMessageBase ,
25
+ ] . join ( '\n' ) ;
26
+
27
+ const successfulUpdate = [
28
+ "You've successfully updated an OpenAPI file on your ReadMe project!" ,
29
+ ...successfulMessageBase ,
30
+ ] . join ( '\n' ) ;
12
31
13
32
jest . mock ( '../../src/lib/prompts' ) ;
14
33
15
34
const getCommandOutput = ( ) => {
16
- return [ console . warn . mock . calls . join ( '\n\n' ) , console . log . mock . calls . join ( '\n\n' ) ] . filter ( Boolean ) . join ( '\n\n' ) ;
35
+ return [ console . warn . mock . calls . join ( '\n\n' ) , console . info . mock . calls . join ( '\n\n' ) ] . filter ( Boolean ) . join ( '\n\n' ) ;
17
36
} ;
18
37
19
38
describe ( 'rdme openapi' , ( ) => {
20
- const exampleRefLocation = `${ config . host } /project/example-project/1.0.1/refs/ex` ;
21
-
22
39
beforeAll ( ( ) => nock . disableNetConnect ( ) ) ;
23
40
24
41
beforeEach ( ( ) => {
25
- console . log = jest . fn ( ) ;
42
+ console . info = jest . fn ( ) ;
26
43
console . warn = jest . fn ( ) ;
27
44
} ) ;
28
45
29
46
afterEach ( ( ) => {
30
- console . log . mockRestore ( ) ;
47
+ console . info . mockRestore ( ) ;
31
48
console . warn . mockRestore ( ) ;
32
49
33
50
nock . cleanAll ( ) ;
@@ -41,7 +58,7 @@ describe('rdme openapi', () => {
41
58
[ 'OpenAPI 3.0' , 'yaml' , '3.0' ] ,
42
59
[ 'OpenAPI 3.1' , 'json' , '3.1' ] ,
43
60
[ 'OpenAPI 3.1' , 'yaml' , '3.1' ] ,
44
- ] ) ( 'should support uploading a %s definition (format: %s)' , ( _ , format , specVersion ) => {
61
+ ] ) ( 'should support uploading a %s definition (format: %s)' , async ( _ , format , specVersion ) => {
45
62
const mock = nock ( config . host )
46
63
. get ( '/api/v1/api-specification' )
47
64
. basicAuth ( { user : key } )
@@ -53,26 +70,20 @@ describe('rdme openapi', () => {
53
70
. basicAuth ( { user : key } )
54
71
. reply ( 201 , { _id : 1 } , { location : exampleRefLocation } ) ;
55
72
56
- return openapi
57
- . run ( {
73
+ await expect (
74
+ openapi . run ( {
58
75
spec : require . resolve ( `@readme/oas-examples/${ specVersion } /${ format } /petstore.${ format } ` ) ,
59
76
key,
60
77
version,
61
78
} )
62
- . then ( ( ) => {
63
- expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
79
+ ) . resolves . toBe ( successfulUpload ) ;
64
80
65
- const output = getCommandOutput ( ) ;
66
- expect ( output ) . not . toMatch ( / w e f o u n d s w a g g e r .j s o n / i) ;
67
- expect ( output ) . toMatch ( / s u c c e s s f u l l y u p l o a d e d / ) ;
68
- expect ( output ) . toMatch ( exampleRefLocation ) ;
69
- expect ( output ) . toMatch ( / t o u p d a t e y o u r o p e n a p i o r s w a g g e r d e f i n i t i o n / i) ;
81
+ expect ( console . info ) . toHaveBeenCalledTimes ( 0 ) ;
70
82
71
- mock . done ( ) ;
72
- } ) ;
83
+ return mock . done ( ) ;
73
84
} ) ;
74
85
75
- it ( 'should discover and upload an API definition if none is provided' , ( ) => {
86
+ it ( 'should discover and upload an API definition if none is provided' , async ( ) => {
76
87
promptHandler . createOasPrompt . mockResolvedValue ( { option : 'create' } ) ;
77
88
78
89
const mock = nock ( config . host )
@@ -95,18 +106,15 @@ describe('rdme openapi', () => {
95
106
// to break.
96
107
fs . copyFileSync ( require . resolve ( '@readme/oas-examples/2.0/json/petstore.json' ) , './swagger.json' ) ;
97
108
98
- return openapi . run ( { key } ) . then ( ( ) => {
99
- expect ( console . log ) . toHaveBeenCalledTimes ( 2 ) ;
109
+ await expect ( openapi . run ( { key } ) ) . resolves . toBe ( successfulUpload ) ;
100
110
101
- const output = getCommandOutput ( ) ;
102
- expect ( output ) . toMatch ( / w e f o u n d s w a g g e r .j s o n / i) ;
103
- expect ( output ) . toMatch ( / s u c c e s s f u l l y u p l o a d e d / ) ;
104
- expect ( output ) . toMatch ( exampleRefLocation ) ;
105
- expect ( output ) . toMatch ( / t o u p d a t e y o u r o p e n a p i o r s w a g g e r d e f i n i t i o n / i) ;
111
+ expect ( console . info ) . toHaveBeenCalledTimes ( 1 ) ;
106
112
107
- fs . unlinkSync ( './swagger.json' ) ;
108
- return mock . done ( ) ;
109
- } ) ;
113
+ const output = getCommandOutput ( ) ;
114
+ expect ( output ) . toBe ( chalk . yellow ( 'We found swagger.json and are attempting to upload it.' ) ) ;
115
+
116
+ fs . unlinkSync ( './swagger.json' ) ;
117
+ return mock . done ( ) ;
110
118
} ) ;
111
119
} ) ;
112
120
@@ -118,69 +126,73 @@ describe('rdme openapi', () => {
118
126
[ 'OpenAPI 3.0' , 'yaml' , '3.0' ] ,
119
127
[ 'OpenAPI 3.1' , 'json' , '3.1' ] ,
120
128
[ 'OpenAPI 3.1' , 'yaml' , '3.1' ] ,
121
- ] ) ( 'should support updating a %s definition (format: %s)' , ( _ , format , specVersion ) => {
129
+ ] ) ( 'should support updating a %s definition (format: %s)' , async ( _ , format , specVersion ) => {
122
130
const mock = nock ( config . host )
123
131
. put ( `/api/v1/api-specification/${ id } ` , body => body . match ( 'form-data; name="spec"' ) )
124
132
. basicAuth ( { user : key } )
125
- . reply ( 201 , { body : '{ id: 1 }' } ) ;
133
+ . reply ( 201 , { _id : 1 } , { location : exampleRefLocation } ) ;
126
134
127
- return openapi
128
- . run ( {
135
+ await expect (
136
+ openapi . run ( {
129
137
spec : require . resolve ( `@readme/oas-examples/${ specVersion } /${ format } /petstore.${ format } ` ) ,
130
138
key,
131
139
id,
132
140
version,
133
141
} )
134
- . then ( ( ) => {
135
- mock . done ( ) ;
136
- } ) ;
142
+ ) . resolves . toBe ( successfulUpdate ) ;
143
+
144
+ return mock . done ( ) ;
137
145
} ) ;
138
146
139
- it ( 'should still support `token`' , ( ) => {
147
+ it ( 'should still support `token`' , async ( ) => {
140
148
const mock = nock ( config . host )
141
149
. put ( `/api/v1/api-specification/${ id } ` , body => body . match ( 'form-data; name="spec"' ) )
142
150
. basicAuth ( { user : key } )
143
- . reply ( 201 , { id : 1 } , { location : exampleRefLocation } ) ;
151
+ . reply ( 201 , { _id : 1 } , { location : exampleRefLocation } ) ;
144
152
145
- return openapi
146
- . run ( { spec : require . resolve ( '@readme/oas-examples/3.1/json/petstore.json' ) , token : `${ key } -${ id } ` , version } )
147
- . then ( ( ) => {
148
- expect ( console . warn ) . toHaveBeenCalledTimes ( 2 ) ;
149
- expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
153
+ await expect (
154
+ openapi . run ( {
155
+ spec : require . resolve ( '@readme/oas-examples/3.1/json/petstore.json' ) ,
156
+ token : `${ key } -${ id } ` ,
157
+ version,
158
+ } )
159
+ ) . resolves . toBe ( successfulUpdate ) ;
150
160
151
- const output = getCommandOutput ( ) ;
161
+ expect ( console . warn ) . toHaveBeenCalledTimes ( 2 ) ;
162
+ expect ( console . info ) . toHaveBeenCalledTimes ( 0 ) ;
152
163
153
- expect ( output ) . toMatch ( / T h e ` - - t o k e n ` o p t i o n h a s b e e n d e p r e c a t e d / i) ;
164
+ const output = getCommandOutput ( ) ;
165
+
166
+ expect ( output ) . toMatch ( / T h e ` - - t o k e n ` o p t i o n h a s b e e n d e p r e c a t e d / i) ;
154
167
155
- mock . done ( ) ;
156
- } ) ;
168
+ return mock . done ( ) ;
157
169
} ) ;
158
170
159
- it ( 'should return warning if providing `id` and `version`' , ( ) => {
171
+ it ( 'should return warning if providing `id` and `version`' , async ( ) => {
160
172
const mock = nock ( config . host )
161
173
. put ( `/api/v1/api-specification/${ id } ` , body => body . match ( 'form-data; name="spec"' ) )
162
174
. basicAuth ( { user : key } )
163
- . reply ( 201 , { id : 1 } , { location : exampleRefLocation } ) ;
175
+ . reply ( 201 , { _id : 1 } , { location : exampleRefLocation } ) ;
164
176
165
- return openapi
166
- . run ( { spec : require . resolve ( '@readme/oas-examples/3.1/json/petstore.json' ) , key, id, version } )
167
- . then ( ( ) => {
168
- expect ( console . warn ) . toHaveBeenCalledTimes ( 1 ) ;
169
- expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
177
+ await expect (
178
+ openapi . run ( { spec : require . resolve ( '@readme/oas-examples/3.1/json/petstore.json' ) , key, id, version } )
179
+ ) . resolves . toBe ( successfulUpdate ) ;
170
180
171
- const output = getCommandOutput ( ) ;
181
+ expect ( console . warn ) . toHaveBeenCalledTimes ( 1 ) ;
182
+ expect ( console . info ) . toHaveBeenCalledTimes ( 0 ) ;
172
183
173
- expect ( output ) . toMatch ( / t h e ` - - v e r s i o n ` o p t i o n w i l l b e i g n o r e d / i) ;
184
+ const output = getCommandOutput ( ) ;
185
+
186
+ expect ( output ) . toMatch ( / t h e ` - - v e r s i o n ` o p t i o n w i l l b e i g n o r e d / i) ;
174
187
175
- mock . done ( ) ;
176
- } ) ;
188
+ return mock . done ( ) ;
177
189
} ) ;
178
190
} ) ;
179
191
180
192
describe ( 'versioning' , ( ) => {
181
193
it . todo ( 'should return a 404 if version flag not found' ) ;
182
194
183
- it ( 'should request a version list if version is not found' , ( ) => {
195
+ it ( 'should request a version list if version is not found' , async ( ) => {
184
196
promptHandler . generatePrompts . mockResolvedValue ( {
185
197
option : 'create' ,
186
198
newVersion : '1.0.1' ,
@@ -200,18 +212,15 @@ describe('rdme openapi', () => {
200
212
. basicAuth ( { user : key } )
201
213
. reply ( 201 , { _id : 1 } , { location : exampleRefLocation } ) ;
202
214
203
- return openapi . run ( { spec : require . resolve ( '@readme/oas-examples/2.0/json/petstore.json' ) , key } ) . then ( ( ) => {
204
- const output = getCommandOutput ( ) ;
205
- expect ( output ) . toMatch ( / s u c c e s s f u l l y u p l o a d e d / ) ;
206
- expect ( output ) . toMatch ( exampleRefLocation ) ;
207
- expect ( output ) . toMatch ( / t o u p d a t e y o u r o p e n a p i o r s w a g g e r d e f i n i t i o n / i) ;
215
+ await expect (
216
+ openapi . run ( { spec : require . resolve ( '@readme/oas-examples/2.0/json/petstore.json' ) , key } )
217
+ ) . resolves . toBe ( successfulUpload ) ;
208
218
209
- mock . done ( ) ;
210
- } ) ;
219
+ return mock . done ( ) ;
211
220
} ) ;
212
221
} ) ;
213
222
214
- it ( 'should bundle and upload the expected content' , ( ) => {
223
+ it ( 'should bundle and upload the expected content' , async ( ) => {
215
224
let requestBody = null ;
216
225
const mock = nock ( config . host )
217
226
. get ( '/api/v1/api-specification' )
@@ -229,23 +238,20 @@ describe('rdme openapi', () => {
229
238
. basicAuth ( { user : key } )
230
239
. reply ( 201 , { _id : 1 } , { location : exampleRefLocation } ) ;
231
240
232
- return openapi . run ( { spec : './__tests__/__fixtures__/ref-oas/petstore.json' , key, version } ) . then ( ( ) => {
233
- expect ( console . log ) . toHaveBeenCalledTimes ( 1 ) ;
241
+ await expect ( openapi . run ( { spec : './__tests__/__fixtures__/ref-oas/petstore.json' , key, version } ) ) . resolves . toBe (
242
+ successfulUpload
243
+ ) ;
234
244
235
- expect ( requestBody ) . toMatchSnapshot ( ) ;
245
+ expect ( console . info ) . toHaveBeenCalledTimes ( 0 ) ;
236
246
237
- const output = getCommandOutput ( ) ;
238
- expect ( output ) . toMatch ( / s u c c e s s f u l l y u p l o a d e d / ) ;
239
- expect ( output ) . toMatch ( exampleRefLocation ) ;
240
- expect ( output ) . toMatch ( / t o u p d a t e y o u r o p e n a p i o r s w a g g e r d e f i n i t i o n / i) ;
247
+ expect ( requestBody ) . toMatchSnapshot ( ) ;
241
248
242
- mock . done ( ) ;
243
- } ) ;
249
+ return mock . done ( ) ;
244
250
} ) ;
245
251
246
252
describe ( 'error handling' , ( ) => {
247
- it ( 'should error if no api key provided' , async ( ) => {
248
- await expect (
253
+ it ( 'should error if no api key provided' , ( ) => {
254
+ return expect (
249
255
openapi . run ( { spec : require . resolve ( '@readme/oas-examples/3.0/json/petstore.json' ) } )
250
256
) . rejects . toThrow ( 'No project API key provided. Please use `--key`.' ) ;
251
257
} ) ;
@@ -258,17 +264,17 @@ describe('rdme openapi', () => {
258
264
259
265
await expect ( openapi . run ( { key, version } ) ) . rejects . toThrow ( / W e c o u l d n ' t f i n d a n O p e n A P I o r S w a g g e r d e f i n i t i o n ./ ) ;
260
266
261
- mock . done ( ) ;
267
+ return mock . done ( ) ;
262
268
} ) ;
263
269
264
- it ( 'should throw an error if an invalid OpenAPI 3.0 definition is supplied' , async ( ) => {
265
- await expect (
270
+ it ( 'should throw an error if an invalid OpenAPI 3.0 definition is supplied' , ( ) => {
271
+ return expect (
266
272
openapi . run ( { spec : './__tests__/__fixtures__/invalid-oas.json' , key, id, version } )
267
273
) . rejects . toThrow ( 'Token "Error" does not exist.' ) ;
268
274
} ) ;
269
275
270
- it ( 'should throw an error if an invalid OpenAPI 3.1 definition is supplied' , async ( ) => {
271
- await expect (
276
+ it ( 'should throw an error if an invalid OpenAPI 3.1 definition is supplied' , ( ) => {
277
+ return expect (
272
278
openapi . run ( { spec : './__tests__/__fixtures__/invalid-oas-3.1.json' , key, id, version } )
273
279
) . rejects . toMatchSnapshot ( ) ;
274
280
} ) ;
@@ -301,7 +307,7 @@ describe('rdme openapi', () => {
301
307
} )
302
308
) . rejects . toStrictEqual ( new APIError ( errorObject ) ) ;
303
309
304
- mock . done ( ) ;
310
+ return mock . done ( ) ;
305
311
} ) ;
306
312
307
313
it ( 'should error if API errors' , async ( ) => {
@@ -329,7 +335,7 @@ describe('rdme openapi', () => {
329
335
openapi . run ( { spec : require . resolve ( '@readme/oas-examples/2.0/json/petstore.json' ) , key, version } )
330
336
) . rejects . toStrictEqual ( new APIError ( errorObject ) ) ;
331
337
332
- mock . done ( ) ;
338
+ return mock . done ( ) ;
333
339
} ) ;
334
340
335
341
it ( 'should error if API errors (generic upload error)' , async ( ) => {
@@ -349,7 +355,7 @@ describe('rdme openapi', () => {
349
355
openapi . run ( { spec : require . resolve ( '@readme/oas-examples/2.0/json/petstore.json' ) , key, version } )
350
356
) . rejects . toStrictEqual ( new Error ( 'There was an error uploading!' ) ) ;
351
357
352
- mock . done ( ) ;
358
+ return mock . done ( ) ;
353
359
} ) ;
354
360
355
361
it ( 'should error if API errors (request timeout)' , async ( ) => {
@@ -373,14 +379,14 @@ describe('rdme openapi', () => {
373
379
)
374
380
) ;
375
381
376
- mock . done ( ) ;
382
+ return mock . done ( ) ;
377
383
} ) ;
378
384
} ) ;
379
385
} ) ;
380
386
381
387
describe ( 'rdme swagger' , ( ) => {
382
- it ( 'should run `rdme openapi`' , async ( ) => {
383
- await expect ( swagger . run ( { spec : '' , key, id, version } ) ) . rejects . toThrow (
388
+ it ( 'should run `rdme openapi`' , ( ) => {
389
+ return expect ( swagger . run ( { spec : '' , key, id, version } ) ) . rejects . toThrow (
384
390
"We couldn't find an OpenAPI or Swagger definition.\n\n" +
385
391
'Run `rdme openapi ./path/to/api/definition` to upload an existing definition or `rdme oas init` to create a fresh one!'
386
392
) ;
0 commit comments