Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 0810142

Browse files
authored
Merge pull request #172 from netlify/chore/improve-test-id
Small improvement of the test logic
2 parents 496193e + 9bcfcce commit 0810142

File tree

3 files changed

+63
-52
lines changed

3 files changed

+63
-52
lines changed

package-lock.json

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"npm-run-all": "^4.1.3",
101101
"nyc": "^15.0.1",
102102
"prettier": "^1.19.1",
103+
"uuid": "^8.3.1",
103104
"webpack": "^4.42.1",
104105
"webpack-cli": "^3.3.11"
105106
},

src/index.test.js

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const test = require('ava')
44
const fromString = require('from2-string')
55
const { TextHTTPError, JSONHTTPError } = require('micro-api-client')
66
const nock = require('nock')
7+
const { v4: uuidv4 } = require('uuid')
78

89
const NetlifyAPI = require('./index')
910

@@ -111,7 +112,7 @@ test('Can specify access token as an option', async t => {
111112
})
112113

113114
test('Can use underscored parameters in path variables', async t => {
114-
const account_id = '1'
115+
const account_id = uuidv4()
115116
const scope = nock(origin)
116117
.get(`${pathPrefix}/accounts/${account_id}`)
117118
.reply(200)
@@ -123,7 +124,7 @@ test('Can use underscored parameters in path variables', async t => {
123124
})
124125

125126
test('Can use camelcase parameters in path variables', async t => {
126-
const account_id = '2'
127+
const account_id = uuidv4()
127128
const scope = nock(origin)
128129
.get(`${pathPrefix}/accounts/${account_id}`)
129130
.reply(200)
@@ -135,7 +136,7 @@ test('Can use camelcase parameters in path variables', async t => {
135136
})
136137

137138
test('Can use global parameters in path variables', async t => {
138-
const account_id = '3'
139+
const account_id = uuidv4()
139140
const scope = nock(origin)
140141
.get(`${pathPrefix}/accounts/${account_id}`)
141142
.reply(200)
@@ -147,7 +148,7 @@ test('Can use global parameters in path variables', async t => {
147148
})
148149

149150
test('Can use underscored parameters in query variables', async t => {
150-
const client_id = '1'
151+
const client_id = uuidv4()
151152
const scope = nock(origin)
152153
.post(`${pathPrefix}/oauth/tickets`)
153154
.query({ client_id })
@@ -160,7 +161,7 @@ test('Can use underscored parameters in query variables', async t => {
160161
})
161162

162163
test('Can use camelcase parameters in query variables', async t => {
163-
const client_id = '2'
164+
const client_id = uuidv4()
164165
const scope = nock(origin)
165166
.post(`${pathPrefix}/oauth/tickets`)
166167
.query({ client_id })
@@ -173,7 +174,7 @@ test('Can use camelcase parameters in query variables', async t => {
173174
})
174175

175176
test('Can use global parameters in query variables', async t => {
176-
const client_id = '3'
177+
const client_id = uuidv4()
177178
const scope = nock(origin)
178179
.post(`${pathPrefix}/oauth/tickets`)
179180
.query({ client_id })
@@ -210,7 +211,7 @@ test('Can specify JSON request body as a function', async t => {
210211
})
211212

212213
test('Can specify binary request body as a stream', async t => {
213-
const deploy_id = '1'
214+
const deploy_id = uuidv4()
214215
const path = 'testPath'
215216
const body = 'test'
216217
const expectedResponse = { test: 'test' }
@@ -226,7 +227,7 @@ test('Can specify binary request body as a stream', async t => {
226227
})
227228

228229
test('Can specify binary request body as a function', async t => {
229-
const deploy_id = '2'
230+
const deploy_id = uuidv4()
230231
const path = 'testPath'
231232
const body = 'test'
232233
const expectedResponse = { test: 'test' }
@@ -254,7 +255,7 @@ test('Can use global parameters in request body', async t => {
254255
})
255256

256257
test('Validates required path parameters', async t => {
257-
const account_id = '4'
258+
const account_id = uuidv4()
258259
const scope = nock(origin)
259260
.put(`${pathPrefix}/accounts/${account_id}`)
260261
.reply(200)
@@ -266,7 +267,7 @@ test('Validates required path parameters', async t => {
266267
})
267268

268269
test('Validates required query parameters', async t => {
269-
const account_slug = '1'
270+
const account_slug = uuidv4()
270271
const scope = nock(origin)
271272
.post(`${pathPrefix}/${account_slug}/members`)
272273
.reply(200)
@@ -280,7 +281,7 @@ test('Validates required query parameters', async t => {
280281
test('Can set request headers', async t => {
281282
const headerName = 'test'
282283
const headerValue = 'test'
283-
const account_id = '5'
284+
const account_id = uuidv4()
284285
const scope = nock(origin)
285286
.get(`${pathPrefix}/accounts/${account_id}`)
286287
.matchHeader(headerName, headerValue)
@@ -293,7 +294,7 @@ test('Can set request headers', async t => {
293294
})
294295

295296
test('Can parse JSON responses', async t => {
296-
const account_id = '6'
297+
const account_id = uuidv4()
297298
const expectedResponse = { test: 'test' }
298299
const scope = nock(origin)
299300
.get(`${pathPrefix}/accounts/${account_id}`)
@@ -307,7 +308,7 @@ test('Can parse JSON responses', async t => {
307308
})
308309

309310
test('Can parse text responses', async t => {
310-
const account_id = '7'
311+
const account_id = uuidv4()
311312
const expectedResponse = 'test'
312313
const scope = nock(origin)
313314
.get(`${pathPrefix}/accounts/${account_id}`)
@@ -321,7 +322,7 @@ test('Can parse text responses', async t => {
321322
})
322323

323324
test('Handle error empty responses', async t => {
324-
const account_id = '8'
325+
const account_id = uuidv4()
325326
const status = 404
326327
const scope = nock(origin)
327328
.get(`${pathPrefix}/accounts/${account_id}`)
@@ -339,7 +340,7 @@ test('Handle error empty responses', async t => {
339340
})
340341

341342
test('Handle error text responses', async t => {
342-
const account_id = '9'
343+
const account_id = uuidv4()
343344
const status = 404
344345
const expectedResponse = 'test'
345346
const scope = nock(origin)
@@ -358,7 +359,7 @@ test('Handle error text responses', async t => {
358359
})
359360

360361
test('Handle error text responses on JSON endpoints', async t => {
361-
const account_id = '9'
362+
const account_id = uuidv4()
362363
const status = 404
363364
const expectedResponse = 'test'
364365
const scope = nock(origin)
@@ -377,7 +378,7 @@ test('Handle error text responses on JSON endpoints', async t => {
377378
})
378379

379380
test('Handle error JSON responses', async t => {
380-
const account_id = '8'
381+
const account_id = uuidv4()
381382
const status = 404
382383
const errorJson = { error: true }
383384
const scope = nock(origin)
@@ -396,7 +397,7 @@ test('Handle error JSON responses', async t => {
396397
})
397398

398399
test('Handle network errors', async t => {
399-
const account_id = '10'
400+
const account_id = uuidv4()
400401
const expectedResponse = 'test'
401402
const url = `${pathPrefix}/accounts/${account_id}`
402403
const scope = nock(origin)
@@ -415,75 +416,78 @@ test('Handle network errors', async t => {
415416
})
416417

417418
test('Can get an access token from a ticket', async t => {
418-
const id = '1'
419+
const ticket_id = uuidv4()
419420
const access_token = 'test'
420421
const scope = nock(origin)
421-
.get(`${pathPrefix}/oauth/tickets/${id}`)
422-
.reply(200, { authorized: true, id })
423-
.post(`${pathPrefix}/oauth/tickets/${id}/exchange`)
422+
.get(`${pathPrefix}/oauth/tickets/${ticket_id}`)
423+
.reply(200, { authorized: true, id: ticket_id })
424+
.post(`${pathPrefix}/oauth/tickets/${ticket_id}/exchange`)
424425
.reply(200, { access_token })
425426

426427
const client = getClient()
427-
const response = await client.getAccessToken({ id, poll: 1, timeout: 5e3 })
428+
const response = await client.getAccessToken({ id: ticket_id, poll: 1, timeout: 5e3 })
428429

429430
t.is(response, access_token)
430431
t.is(client.accessToken, access_token)
431432
t.true(scope.isDone())
432433
})
433434

434435
test('Can poll for access token', async t => {
435-
const id = '2'
436+
const ticket_id = uuidv4()
436437
const access_token = 'test'
437438
const scope = nock(origin)
438-
.get(`${pathPrefix}/oauth/tickets/${id}`)
439+
.get(`${pathPrefix}/oauth/tickets/${ticket_id}`)
439440
.reply(200, {})
440-
.get(`${pathPrefix}/oauth/tickets/${id}`)
441-
.reply(200, { authorized: true, id })
442-
.post(`${pathPrefix}/oauth/tickets/${id}/exchange`)
441+
.get(`${pathPrefix}/oauth/tickets/${ticket_id}`)
442+
.reply(200, { authorized: true, id: ticket_id })
443+
.post(`${pathPrefix}/oauth/tickets/${ticket_id}/exchange`)
443444
.reply(200, { access_token })
444445

445446
const client = getClient()
446-
await client.getAccessToken({ id })
447+
await client.getAccessToken({ id: ticket_id })
447448

448449
t.true(scope.isDone())
449450
})
450451

451452
test('Can change access token polling', async t => {
452-
const id = '3'
453+
const ticket_id = uuidv4()
453454
const access_token = 'test'
454455
const scope = nock(origin)
455-
.get(`${pathPrefix}/oauth/tickets/${id}`)
456+
.get(`${pathPrefix}/oauth/tickets/${ticket_id}`)
456457
.reply(200, {})
457-
.get(`${pathPrefix}/oauth/tickets/${id}`)
458-
.reply(200, { authorized: true, id })
459-
.post(`${pathPrefix}/oauth/tickets/${id}/exchange`)
458+
.get(`${pathPrefix}/oauth/tickets/${ticket_id}`)
459+
.reply(200, { authorized: true, id: ticket_id })
460+
.post(`${pathPrefix}/oauth/tickets/${ticket_id}/exchange`)
460461
.reply(200, { access_token })
461462

462463
const client = getClient()
463-
await client.getAccessToken({ id }, { poll: 1 })
464+
await client.getAccessToken({ id: ticket_id }, { poll: 1 })
464465

465466
t.true(scope.isDone())
466467
})
467468

468469
test('Can timeout access token polling', async t => {
469-
const id = '4'
470+
const ticket_id = uuidv4()
470471
const access_token = 'test'
471472
const scope = nock(origin)
472-
.get(`${pathPrefix}/oauth/tickets/${id}`)
473+
.get(`${pathPrefix}/oauth/tickets/${ticket_id}`)
473474
.reply(200, {})
474-
.get(`${pathPrefix}/oauth/tickets/${id}`)
475-
.reply(200, { authorized: true, id })
476-
.post(`${pathPrefix}/oauth/tickets/${id}/exchange`)
475+
.get(`${pathPrefix}/oauth/tickets/${ticket_id}`)
476+
.reply(200, { authorized: true, id: ticket_id })
477+
.post(`${pathPrefix}/oauth/tickets/${ticket_id}/exchange`)
477478
.reply(200, { access_token })
478479

479480
const client = getClient()
480-
await t.throwsAsync(client.getAccessToken({ id }, { poll: 1, timeout: 1 }), 'Promise timed out after 1 milliseconds')
481+
await t.throwsAsync(
482+
client.getAccessToken({ id: ticket_id }, { poll: 1, timeout: 1 }),
483+
'Promise timed out after 1 milliseconds'
484+
)
481485

482486
t.false(scope.isDone())
483487
})
484488

485489
test('Handles API rate limiting', async t => {
486-
const account_id = '11'
490+
const account_id = uuidv4()
487491
const retryAtMs = Date.now() + TEST_RATE_LIMIT_DELAY
488492
const retryAt = Math.ceil(retryAtMs / SECS_TO_MSECS)
489493
const expectedResponse = { test: 'test' }
@@ -502,7 +506,7 @@ test('Handles API rate limiting', async t => {
502506
})
503507

504508
test('Handles API rate limiting when date is in the past', async t => {
505-
const account_id = '12'
509+
const account_id = uuidv4()
506510
const expectedResponse = { test: 'test' }
507511
const scope = nock(origin)
508512
.get(`${pathPrefix}/accounts/${account_id}`)
@@ -517,7 +521,7 @@ test('Handles API rate limiting when date is in the past', async t => {
517521
})
518522

519523
test('Handles API rate limiting when X-RateLimit-Reset is missing', async t => {
520-
const account_id = '13'
524+
const account_id = uuidv4()
521525
const expectedResponse = { test: 'test' }
522526
const retryAt = 'invalid'
523527
const scope = nock(origin)
@@ -533,7 +537,7 @@ test('Handles API rate limiting when X-RateLimit-Reset is missing', async t => {
533537
})
534538

535539
test('Gives up retrying on API rate limiting after a timeout', async t => {
536-
const account_id = '14'
540+
const account_id = uuidv4()
537541
const retryAt = Math.ceil(Date.now() / SECS_TO_MSECS)
538542
const expectedResponse = { test: 'test' }
539543
const scope = nock(origin)
@@ -554,7 +558,7 @@ test('Gives up retrying on API rate limiting after a timeout', async t => {
554558
})
555559

556560
test('Retries on ETIMEDOUT connection errors', async t => {
557-
const account_id = '17'
561+
const account_id = uuidv4()
558562
const retryAtMs = Date.now() + TEST_RATE_LIMIT_DELAY
559563
const expectedResponse = { test: 'test' }
560564
const scope = nock(origin)
@@ -572,7 +576,7 @@ test('Retries on ETIMEDOUT connection errors', async t => {
572576
})
573577

574578
test('Recreates a function body when handling API rate limiting', async t => {
575-
const deploy_id = '3'
579+
const deploy_id = uuidv4()
576580
const path = 'testPath'
577581
const body = 'test'
578582
const retryAtMs = Date.now() + TEST_RATE_LIMIT_DELAY
@@ -597,7 +601,7 @@ test('Can set (proxy) agent', async t => {
597601
})
598602

599603
test('(Proxy) agent is passed as request option', async t => {
600-
const account_id = '15'
604+
const account_id = uuidv4()
601605
const scope = nock(origin)
602606
.get(`${pathPrefix}/accounts/${account_id}`)
603607
.reply(200)
@@ -608,7 +612,7 @@ test('(Proxy) agent is passed as request option', async t => {
608612
})
609613

610614
test('(Proxy) agent is not passed as request option if not set', async t => {
611-
const account_id = '16'
615+
const account_id = uuidv4()
612616
const scope = nock(origin)
613617
.get(`${pathPrefix}/accounts/${account_id}`)
614618
.reply(200)

0 commit comments

Comments
 (0)