particle-api-js 11.1.7 → 12.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/README.md +90 -10
  2. package/dist/particle.min.js +1 -1
  3. package/dist/particle.min.js.map +1 -1
  4. package/fs.d.ts +2 -0
  5. package/lib/fs.d.ts +2 -0
  6. package/lib/fs.js +3 -0
  7. package/lib/package.json +110 -0
  8. package/lib/src/Agent.d.ts +40 -0
  9. package/lib/src/Agent.d.ts.map +1 -0
  10. package/lib/src/Agent.js +233 -0
  11. package/lib/src/Agent.js.map +1 -0
  12. package/lib/src/Client.d.ts +80 -0
  13. package/lib/src/Client.d.ts.map +1 -0
  14. package/lib/src/Client.js +104 -0
  15. package/lib/src/Client.js.map +1 -0
  16. package/lib/src/Defaults.d.ts +6 -0
  17. package/lib/src/Defaults.d.ts.map +1 -0
  18. package/lib/src/Defaults.js +12 -0
  19. package/lib/src/Defaults.js.map +1 -0
  20. package/lib/src/EventStream.d.ts +31 -0
  21. package/lib/src/EventStream.d.ts.map +1 -0
  22. package/lib/src/EventStream.js +275 -0
  23. package/lib/src/EventStream.js.map +1 -0
  24. package/lib/src/Library.d.ts +33 -0
  25. package/lib/src/Library.d.ts.map +1 -0
  26. package/lib/src/Library.js +19 -0
  27. package/lib/src/Library.js.map +1 -0
  28. package/{src/Particle.js → lib/src/Particle.d.ts} +623 -1779
  29. package/lib/src/Particle.d.ts.map +1 -0
  30. package/lib/src/Particle.js +2578 -0
  31. package/lib/src/Particle.js.map +1 -0
  32. package/lib/src/types/common.d.ts +73 -0
  33. package/lib/src/types/common.d.ts.map +1 -0
  34. package/lib/src/types/common.js +3 -0
  35. package/lib/src/types/common.js.map +1 -0
  36. package/lib/src/types/index.d.ts +4 -0
  37. package/lib/src/types/index.d.ts.map +1 -0
  38. package/lib/src/types/index.js +20 -0
  39. package/lib/src/types/index.js.map +1 -0
  40. package/lib/src/types/requests.d.ts +568 -0
  41. package/lib/src/types/requests.d.ts.map +1 -0
  42. package/lib/src/types/requests.js +3 -0
  43. package/lib/src/types/requests.js.map +1 -0
  44. package/lib/src/types/responses.d.ts +449 -0
  45. package/lib/src/types/responses.d.ts.map +1 -0
  46. package/lib/src/types/responses.js +3 -0
  47. package/lib/src/types/responses.js.map +1 -0
  48. package/package.json +35 -14
  49. package/scripts/postprocess-docs.js +306 -0
  50. package/typedoc.json +20 -0
  51. package/.circleci/config.yml +0 -104
  52. package/.nvmrc +0 -1
  53. package/CHANGELOG.md +0 -404
  54. package/EventStream-e2e-browser.html +0 -39
  55. package/EventStream-e2e-node.js +0 -34
  56. package/RELEASE.md +0 -12
  57. package/bower.json +0 -30
  58. package/docs/api.md +0 -2594
  59. package/eslint.config.mjs +0 -7
  60. package/examples/login/login.html +0 -17
  61. package/karma.conf.js +0 -80
  62. package/src/Agent.js +0 -397
  63. package/src/Client.js +0 -171
  64. package/src/Defaults.js +0 -8
  65. package/src/EventStream.js +0 -269
  66. package/src/Library.js +0 -33
  67. package/test/Agent.integration.js +0 -23
  68. package/test/Agent.spec.js +0 -488
  69. package/test/Client.spec.js +0 -216
  70. package/test/Defaults.spec.js +0 -30
  71. package/test/EventStream.feature +0 -65
  72. package/test/EventStream.spec.js +0 -263
  73. package/test/FakeAgent.js +0 -27
  74. package/test/Library.spec.js +0 -40
  75. package/test/Particle.integration.js +0 -38
  76. package/test/Particle.spec.js +0 -3198
  77. package/test/fixtures/index.js +0 -15
  78. package/test/fixtures/libraries.json +0 -33
  79. package/test/fixtures/library.json +0 -31
  80. package/test/fixtures/libraryVersions.json +0 -211
  81. package/test/out.tmp +0 -0
  82. package/test/support/FixtureHttpServer.js +0 -28
  83. package/test/test-setup.js +0 -17
  84. package/tsconfig.json +0 -17
  85. package/webpack.config.js +0 -46
@@ -1,3198 +0,0 @@
1
- 'use strict';
2
- const should = require('should'); // monkeypatch the world~!1
3
- const Particle = require('../src/Particle');
4
- const Defaults = require('../src/Defaults');
5
- const Client = require('../src/Client');
6
- const EventStream = require('../src/EventStream');
7
- const FakeAgent = require('./FakeAgent');
8
- const { sinon, expect } = require('./test-setup');
9
-
10
- let api;
11
-
12
- const props = {
13
- url: 'http://www.zombo.com/',
14
- password: 'test-password',
15
- currentPassword: 'test-password',
16
- data: { sentient: true },
17
- isPrivate: true,
18
- username: 'test-user',
19
- argument: 'noThanks',
20
- shouldUpdate: 'duh',
21
- name: 'specialName',
22
- productId: '9001',
23
- deviceId: '1337',
24
- device: 'my-device',
25
- key: 'c1a55e5',
26
- event: 'main',
27
- token: 'Y',
28
- signal: '1',
29
- auth: 'X',
30
- accountInfo : { first_name: 'John', last_name: 'Scully', business_account: true, company_name: 'Apple Inc.' },
31
- files: {
32
- 'app.ino': new Buffer('void(){}\nsetup(){}\n')
33
- },
34
- binaryId: '123456',
35
- targetVersion: '0.4.7',
36
- headers: {
37
- test: 'header'
38
- },
39
- rejectUnauthorized: true,
40
- noDefaults: true,
41
- hook: {
42
- method: 'PUT',
43
- auth: {
44
- username: 'u',
45
- password: 'p'
46
- },
47
- headers: {
48
- one: '1',
49
- two: '2'
50
- },
51
- query: {
52
- q: 'p'
53
- },
54
- json: {
55
- j: 'd'
56
- },
57
- form: {
58
- f: 'd'
59
- },
60
- body: '{{data}}',
61
- responseTemplate: 'template',
62
- responseEvent: 'res-event',
63
- errorResponseEvent: 'res-err-event'
64
- },
65
- hookId: 'hook-1234567890',
66
- integrationId: 'integration-1234567890',
67
- clientId: 'client-123',
68
- type: 'web',
69
- redirect_uri: 'https://example.com',
70
- scope: 'Owner',
71
- deviceName: 'test-device',
72
- page: 5,
73
- perPage: 50,
74
- sortAttr: 'deviceId',
75
- sortDir: 'asc',
76
- deny: false,
77
- deviceIds: ['abc', 'xyz'],
78
- development: false,
79
- notes: 'A fancy Photon',
80
- desiredFirmwareVersion: 42,
81
- flash: false,
82
- version: '42',
83
- title: 'prod',
84
- description: 'ready for production',
85
- file: new Buffer('ELF...'),
86
- country: 'RO',
87
- iccid: '1234567890',
88
- iccids: ['1234567890', '9876543210'],
89
- serialNumber: 'PH-123456',
90
- settings: {
91
- url: 'http://example.com'
92
- },
93
- otp: '123456',
94
- mfaToken: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
95
- networkId: '65535',
96
- groups: ['foo', 'bar'],
97
- dateRange: '2020-05-15T18:29:45.000Z,2020-05-19T18:29:45.000Z',
98
- rectBl: '56.185412,-4.049868',
99
- rectTr: '56.571537,-5.385920',
100
- logicFunctionId: 'ea838324-a4a7-4fa6-b278-95bdaed7114b',
101
- logicRunId: 'd0580956-71dc-4f11-89e2-987dcf82a86f',
102
- logicFunction: {
103
- enabled: true,
104
- name: 'function-1',
105
- description: 'hello world',
106
- source: {
107
- type: 'JavaScript',
108
- code: 'console.log("hello from function-1");'
109
- },
110
- logic_triggers: [
111
- {
112
- type: 'Event',
113
- enabled: true,
114
- product_id: 9001,
115
- event_name: 'main',
116
- },
117
- {
118
- type: 'Scheduled',
119
- enabled: true,
120
- cron: '0 0 1 * *',
121
- start_at: '2021-05-15T18:29:45.000Z',
122
- end_at: '2021-05-19T18:29:45.000Z',
123
- }
124
- ]
125
- },
126
- logic: {
127
- source: {
128
- type: 'JavaScript',
129
- code: 'export default function main() { console.log("hi"); }'
130
- },
131
- event: {
132
- event_name: 'main',
133
- device_id: '1234',
134
- event_data: JSON.stringify({ data: true }),
135
- product_id: 9001,
136
- user_id: '5678'
137
- }
138
- },
139
- ledgerName: 'myledger',
140
- ledger: {
141
- scope: 'Owner',
142
- name: 'myledger',
143
- description: 'my ledger',
144
- direction: 'CloudOnly'
145
- },
146
- scopeValue: 'abc1234',
147
- instance: {
148
- property: 'yes'
149
- },
150
- setMode: 'Replace'
151
- };
152
-
153
- const product = 'ze-product-v1';
154
- const propsWithProduct = Object.assign({ product }, props);
155
-
156
- const org = 'test-org';
157
- const propsWithOrg = Object.assign({ org }, props);
158
-
159
- class Common {
160
- static expectCredentials({ form }){
161
- form.username.should.equal(props.username);
162
- form.password.should.equal(props.password);
163
- }
164
- static expectDeviceUrlAndToken(results){
165
- results.uri.should.containEql(props.deviceId);
166
- results.auth.should.equal(props.auth);
167
- }
168
- }
169
-
170
- describe('ParticleAPI', () => {
171
- beforeEach(() => {
172
- api = new Particle();
173
- });
174
-
175
-
176
- describe('operations', () => {
177
- beforeEach(() => {
178
- api.agent = new FakeAgent();
179
- });
180
-
181
- describe('constructor', () => {
182
- it('sets maps defaults to instance properties', () => {
183
- Object.keys(Defaults).forEach((setting) => {
184
- expect(api[setting]).to.eql(Defaults[setting]);
185
- });
186
- });
187
-
188
- describe('without defaultAuth', () => {
189
- it('does NOT call .setDefaultAuth(defaultAuth) unless provided value is truthy', () => {
190
- sinon.stub(api, 'setDefaultAuth');
191
- expect(api.setDefaultAuth).to.have.property('callCount', 0);
192
- });
193
- });
194
-
195
- describe('with defaultAuth', () => {
196
- it('calls .setDefaultAuth(defaultAuth) when provided defaultAuth value is truthy', () => {
197
- const fakeAuthToken = 'foo';
198
- sinon.stub(Particle.prototype, 'setDefaultAuth');
199
- api = new Particle({ auth: fakeAuthToken });
200
- expect(api.setDefaultAuth).to.have.property('callCount', 1);
201
- expect(api.setDefaultAuth.firstCall.args).to.have.lengthOf(1);
202
- expect(api.setDefaultAuth.firstCall.args[0]).to.eql(fakeAuthToken);
203
- });
204
- });
205
- });
206
-
207
- describe('trackingIdentity', () => {
208
- const context = { tool: { name: 'cli', version:'1.2.3' } };
209
-
210
- it('full', () => {
211
- return api.trackingIdentity({ auth: 'X', full: true, context })
212
- .then((req) => {
213
- expect(req).to.have.property('uri', '/v1/user/identify');
214
- expect(req).to.have.property('method', 'get');
215
- expect(req).to.have.property('query', undefined);
216
- expect(req).to.have.property('context').that.is.eql(context);
217
- expect(req).to.have.property('auth', 'X');
218
- });
219
- });
220
-
221
- it('id only', () => {
222
- return api.trackingIdentity().then(req => {
223
- expect(req).to.have.property('uri').eql('/v1/user/identify');
224
- expect(req).to.have.property('method').eql('get');
225
- expect(req).to.have.property('context').eql({});
226
- expect(req).to.have.property('query').eql({ tracking: 1 });
227
- });
228
- });
229
- });
230
-
231
- describe('.login', () => {
232
- it('sends credentials', () => {
233
- return api.login(props).then(Common.expectCredentials);
234
- });
235
- });
236
-
237
- describe('.loginAsClientOwner', () => {
238
- it('sends client ID and secret', () => {
239
- const clientApi = new Particle({
240
- clientId: 'foo',
241
- clientSecret: 'bar'
242
- });
243
- clientApi.agent = new FakeAgent();
244
- return clientApi.loginAsClientOwner({}).then(req => {
245
- expect(req.form).to.have.property('client_id').eql('foo');
246
- expect(req.form).to.have.property('client_secret').eql('bar');
247
- expect(req.form).to.have.property('grant_type').eql('client_credentials');
248
- });
249
- });
250
- });
251
-
252
- describe('.sendOtp', () => {
253
- it('sends request to oauth token endpoint to verify the user login', () => {
254
- return api.sendOtp(props).then((results) => {
255
- expect(results).to.have.property('method').eql('post');
256
- expect(results).to.have.property('uri').eql('/oauth/token');
257
- expect(results.form).to.have.property('otp').eql(props.otp);
258
- expect(results.form).to.have.property('mfa_token').eql(props.mfaToken);
259
- });
260
- });
261
- });
262
-
263
- describe('.enableMfa', () => {
264
- it('sends request to begin mfa enrollment', () => {
265
- return api.enableMfa(props).then((results) => {
266
- results.should.eql({
267
- uri: '/v1/user/mfa-enable',
268
- method: 'get',
269
- auth: props.auth,
270
- headers: props.headers,
271
- query: undefined,
272
- context: {}
273
- });
274
- });
275
- });
276
- });
277
-
278
- describe('.confirmMfa', () => {
279
- it('sends request to confirm mfa enrollment', () => {
280
- return api.confirmMfa(props).then((results) => {
281
- results.should.eql({
282
- uri: '/v1/user/mfa-enable',
283
- method: 'post',
284
- auth: props.auth,
285
- headers: props.headers,
286
- data: {
287
- otp: props.otp,
288
- mfa_token: props.mfaToken
289
- },
290
- context: {}
291
- });
292
- });
293
- });
294
- it('allows invalidating tokens', () => {
295
- return api.confirmMfa(Object.assign({ invalidateTokens: true }, props)).then((results) => {
296
- results.should.eql({
297
- uri: '/v1/user/mfa-enable',
298
- method: 'post',
299
- auth: props.auth,
300
- headers: props.headers,
301
- data: {
302
- otp: props.otp,
303
- mfa_token: props.mfaToken,
304
- invalidate_tokens: true
305
- },
306
- context: {}
307
- });
308
- });
309
- });
310
- });
311
-
312
- describe('.disableMfa', () => {
313
- it('sends request to disable mfa', () => {
314
- return api.disableMfa(props).then((results) => {
315
- results.should.eql({
316
- uri: '/v1/user/mfa-disable',
317
- method: 'put',
318
- auth: props.auth,
319
- headers: props.headers,
320
- query: undefined,
321
- data: {
322
- current_password: props.password,
323
- },
324
- context: {},
325
- });
326
- });
327
- });
328
- });
329
-
330
- describe('.createCustomer', () => {
331
- it('sends client ID and secret', () => {
332
- const clientApi = new Particle({
333
- clientId: 'foo',
334
- clientSecret: 'bar'
335
- });
336
- clientApi.agent = new FakeAgent();
337
- return clientApi.loginAsClientOwner({}).then(req => {
338
- expect(req.form).to.have.property('client_id').eql('foo');
339
- expect(req.form).to.have.property('client_secret').eql('bar');
340
- expect(req.form).to.have.property('grant_type').eql('client_credentials');
341
- });
342
- });
343
- });
344
-
345
- describe('.createUser', () => {
346
- it('sends credentials', () => {
347
- return api.createUser(props).then((results) => {
348
- results.should.eql({
349
- uri: '/v1/users',
350
- method: 'post',
351
- auth: undefined,
352
- headers: props.headers,
353
- data: {
354
- username: props.username,
355
- password: props.password,
356
- account_info: props.accountInfo,
357
- utm: undefined
358
- },
359
- context: {}
360
- });
361
- });
362
- });
363
- it('allows sending utm parameters', () => {
364
- props.utm = { utm_source: 'web' };
365
- return api.createUser(props).then((results) => {
366
- results.should.eql({
367
- uri: '/v1/users',
368
- method: 'post',
369
- auth: undefined,
370
- headers: props.headers,
371
- data: {
372
- username: props.username,
373
- password: props.password,
374
- account_info: props.accountInfo,
375
- utm: {
376
- utm_source: 'web'
377
- }
378
- },
379
- context: {}
380
- });
381
- });
382
- });
383
- });
384
-
385
- describe('.resetPassword', () => {
386
- it('sends request', () => {
387
- return api.resetPassword(props).then(({ data }) => {
388
- data.username.should.equal(props.username);
389
- });
390
- });
391
- });
392
-
393
- describe('.deleteAccessToken', () => {
394
- it('sends request', () => {
395
- return api.deleteAccessToken(props).then((results) => {
396
- results.should.match({
397
- method: 'delete',
398
- uri: `/v1/access_tokens/${props.token}`,
399
- });
400
- });
401
- });
402
- });
403
-
404
- describe('.deleteCurrentAccessToken', () => {
405
- it('sends request', () => {
406
- return api.deleteCurrentAccessToken(props).then((results) => {
407
- results.should.match({
408
- method: 'delete',
409
- uri: '/v1/access_tokens/current',
410
- auth: props.auth,
411
- });
412
- });
413
- });
414
- });
415
-
416
- describe('.deleteActiveAccessTokens', () => {
417
- it('sends request', () => {
418
- return api.deleteActiveAccessTokens(props).then((results) => {
419
- results.should.match({
420
- method: 'delete',
421
- uri: '/v1/access_tokens',
422
- auth: props.auth,
423
- });
424
- });
425
- });
426
- });
427
-
428
- describe('.listDevices', () => {
429
- describe('user scope', () => {
430
- it('generates request', () => {
431
- return api.listDevices(props).then((results) => {
432
- results.should.match({
433
- method: 'get',
434
- uri: '/v1/devices',
435
- auth: props.auth,
436
- query: undefined
437
- });
438
- });
439
- });
440
- });
441
-
442
- describe('product scope', () => {
443
- it('generates request', () => {
444
- return api.listDevices(propsWithProduct).then((results) => {
445
- results.should.match({
446
- method: 'get',
447
- uri: `/v1/products/${product}/devices`,
448
- auth: props.auth,
449
- query: {
450
- deviceName: props.deviceName,
451
- page: props.page,
452
- per_page: props.perPage,
453
- groups: props.groups.join(','),
454
- sortAttr: props.sortAttr,
455
- sortDir: props.sortDir
456
- }
457
- });
458
- });
459
- });
460
- });
461
- });
462
-
463
- describe('.getDevice', () => {
464
- describe('user scope', () => {
465
- it('generates request', () => {
466
- return api.getDevice(props).then((results) => {
467
- results.should.match({
468
- method: 'get',
469
- uri: `/v1/devices/${props.deviceId}`,
470
- auth: props.auth
471
- });
472
- });
473
- });
474
- });
475
-
476
- describe('product scope', () => {
477
- it('generates request', () => {
478
- return api.getDevice(propsWithProduct).then((results) => {
479
- results.should.match({
480
- method: 'get',
481
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
482
- auth: props.auth
483
- });
484
- });
485
- });
486
- });
487
- });
488
-
489
- describe('.claimDevice', () => {
490
- it('sends credentials', () => {
491
- return api.claimDevice(props).then((results) => {
492
- results.data.should.be.instanceOf(Object);
493
- results.data.id.should.equal(props.deviceId);
494
- results.auth.should.equal(props.auth);
495
- });
496
- });
497
- });
498
-
499
- describe('.addDeviceToProduct', () => {
500
- it('sends request to add a single device by id', () => {
501
- const prodProps = Object.assign({}, propsWithProduct);
502
- delete prodProps.file;
503
- return api.addDeviceToProduct(prodProps).then((results) => {
504
- results.should.match({
505
- method: 'post',
506
- uri: `/v1/products/${product}/devices`,
507
- auth: props.auth,
508
- data: {
509
- id: props.deviceId
510
- }
511
- });
512
- });
513
- });
514
-
515
- it('sends request to add multiple devices using a file', () => {
516
- return api.addDeviceToProduct(propsWithProduct).then((results) => {
517
- results.should.match({
518
- method: 'post',
519
- uri: `/v1/products/${product}/devices`,
520
- auth: props.auth,
521
- files: {
522
- file: props.file,
523
- }
524
- });
525
- });
526
- });
527
- });
528
-
529
- describe('.removeDevice', () => {
530
- describe('user scope', () => {
531
- it('generates request', () => {
532
- return api.removeDevice(props).then((results) => {
533
- results.should.match({
534
- method: 'delete',
535
- uri: `/v1/devices/${props.deviceId}`,
536
- auth: props.auth
537
- });
538
- });
539
- });
540
- });
541
-
542
- describe('product scope', () => {
543
- it('generates request', () => {
544
- return api.removeDevice(propsWithProduct).then((results) => {
545
- results.should.match({
546
- method: 'delete',
547
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
548
- auth: props.auth,
549
- data: {
550
- deny: props.deny
551
- }
552
- });
553
- });
554
- });
555
- });
556
- });
557
-
558
- describe('.removeDeviceOwner', () => {
559
- it('generates request', () => {
560
- return api.removeDeviceOwner(propsWithProduct).then((results) => {
561
- results.should.match({
562
- method: 'delete',
563
- uri: `/v1/products/${product}/devices/${props.deviceId}/owner`,
564
- auth: props.auth
565
- });
566
- });
567
- });
568
- });
569
-
570
- describe('.renameDevice', () => {
571
- describe('user scope', () => {
572
- it('generates request', () => {
573
- return api.renameDevice(props).then((results) => {
574
- results.should.match({
575
- method: 'put',
576
- uri: `/v1/devices/${props.deviceId}`,
577
- auth: props.auth,
578
- data: {
579
- name: props.name
580
- }
581
- });
582
- });
583
- });
584
- });
585
-
586
- describe('product scope', () => {
587
- it('generates request', () => {
588
- return api.renameDevice(propsWithProduct).then((results) => {
589
- results.should.match({
590
- method: 'put',
591
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
592
- auth: props.auth,
593
- data: {
594
- name: props.name
595
- }
596
- });
597
- });
598
- });
599
- });
600
- });
601
-
602
- describe('.setDeviceNotes', () => {
603
- describe('user scope', () => {
604
- it('generates request', () => {
605
- return api.setDeviceNotes(props).then((results) => {
606
- results.should.match({
607
- method: 'put',
608
- uri: `/v1/devices/${props.deviceId}`,
609
- auth: props.auth,
610
- data: {
611
- notes: props.notes
612
- }
613
- });
614
- });
615
- });
616
- });
617
-
618
- describe('product scope', () => {
619
- it('generates request', () => {
620
- return api.setDeviceNotes(propsWithProduct).then((results) => {
621
- results.should.match({
622
- method: 'put',
623
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
624
- auth: props.auth,
625
- data: {
626
- notes: props.notes
627
- }
628
- });
629
- });
630
- });
631
- });
632
- });
633
-
634
- describe('.markAsDevelopmentDevice', () => {
635
- describe('without development parameter', () => {
636
- it('generates request', () => {
637
- const params = Object.assign({}, propsWithProduct, { development: undefined });
638
- return api.markAsDevelopmentDevice(params).then((results) => {
639
- results.should.match({
640
- method: 'put',
641
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
642
- auth: props.auth,
643
- data: {
644
- development: true
645
- }
646
- });
647
- });
648
- });
649
- });
650
-
651
- describe('with development parameter', () => {
652
- it('generates request', () => {
653
- return api.markAsDevelopmentDevice(propsWithProduct).then((results) => {
654
- results.should.match({
655
- method: 'put',
656
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
657
- auth: props.auth,
658
- data: {
659
- development: false
660
- }
661
- });
662
- });
663
- });
664
- });
665
- });
666
-
667
- describe('.lockDeviceProductFirmware', () => {
668
- it('generates request', () => {
669
- return api.lockDeviceProductFirmware(propsWithProduct).then((results) => {
670
- results.should.match({
671
- method: 'put',
672
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
673
- auth: props.auth,
674
- data: {
675
- desired_firmware_version: props.desiredFirmwareVersion,
676
- flash: props.flash
677
- }
678
- });
679
- });
680
- });
681
- });
682
-
683
- describe('.unlockDeviceProductFirmware', () => {
684
- it('generates request', () => {
685
- return api.unlockDeviceProductFirmware(propsWithProduct).then((results) => {
686
- results.should.match({
687
- method: 'put',
688
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
689
- auth: props.auth,
690
- data: {
691
- desired_firmware_version: null
692
- }
693
- });
694
- });
695
- });
696
- });
697
-
698
- describe('.updateDevice', () => {
699
- describe('user scope', () => {
700
- it('generates request', () => {
701
- return api.updateDevice(props).then((results) => {
702
- results.should.match({
703
- method: 'put',
704
- uri: `/v1/devices/${props.deviceId}`,
705
- auth: props.auth,
706
- data: {
707
- name: props.name,
708
- notes: props.notes
709
- }
710
- });
711
- });
712
- });
713
- });
714
-
715
- describe('product scope', () => {
716
- it('generates request', () => {
717
- return api.updateDevice(propsWithProduct).then((results) => {
718
- results.should.match({
719
- method: 'put',
720
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
721
- auth: props.auth,
722
- data: {
723
- name: props.name,
724
- notes: props.notes,
725
- development: props.development,
726
- desired_firmware_version: props.desiredFirmwareVersion,
727
- flash: props.flash
728
- }
729
- });
730
- });
731
- });
732
- });
733
- });
734
-
735
- describe('.provisionDevice', () => {
736
- it('generates request', () => {
737
- return api.provisionDevice(props).then((results) => {
738
- results.should.match({
739
- method: 'post',
740
- uri: '/v1/devices',
741
- auth: props.auth,
742
- data: {
743
- product_id: props.productId
744
- }
745
- });
746
- });
747
- });
748
- });
749
-
750
- describe('.getClaimCode', () => {
751
- describe('user scope', () => {
752
- it('generates request', () => {
753
- return api.getClaimCode(props).then((results) => {
754
- results.should.match({
755
- method: 'post',
756
- uri: '/v1/device_claims',
757
- auth: props.auth,
758
- data: {
759
- iccid: props.iccid
760
- }
761
- });
762
- });
763
- });
764
- });
765
-
766
- describe('product scope', () => {
767
- it('generates request', () => {
768
- return api.getClaimCode(propsWithProduct).then((results) => {
769
- results.should.match({
770
- method: 'post',
771
- uri: `/v1/products/${product}/device_claims`,
772
- auth: props.auth,
773
- data: {
774
- iccid: props.iccid
775
- }
776
- });
777
- });
778
- });
779
- });
780
- });
781
-
782
- describe('.getVariable', () => {
783
- describe('user scope', () => {
784
- it('generates request', () => {
785
- return api.getVariable(props).then((results) => {
786
- results.should.match({
787
- method: 'get',
788
- uri: `/v1/devices/${props.deviceId}/${props.name}`,
789
- auth: props.auth
790
- });
791
- });
792
- });
793
- });
794
-
795
- describe('product scope', () => {
796
- it('generates request', () => {
797
- return api.getVariable(propsWithProduct).then((results) => {
798
- results.should.match({
799
- method: 'get',
800
- uri: `/v1/products/${product}/devices/${props.deviceId}/${props.name}`,
801
- auth: props.auth
802
- });
803
- });
804
- });
805
- });
806
- });
807
-
808
- describe('.signalDevice', () => {
809
- it('generates request', () => {
810
- return api.signalDevice(props).then(Common.expectDeviceUrlAndToken);
811
- });
812
-
813
- it('sends proper data', () => {
814
- return api.signalDevice(props).then(({ data }) => {
815
- data.should.be.instanceOf(Object);
816
- data.signal.should.equal(props.signal);
817
- });
818
- });
819
- });
820
-
821
- describe('.flashTinker', () => {
822
- it('generates request', () => {
823
- return api.flashTinker(props).then(Common.expectDeviceUrlAndToken);
824
- });
825
-
826
- it('sends proper data', () => {
827
- return api.flashTinker(props).then(({ data }) => {
828
- data.should.be.instanceOf(Object);
829
- data.app.should.equal('tinker');
830
- });
831
- });
832
- });
833
-
834
- describe('.flashDevice', () => {
835
- describe('user scope', () => {
836
- it('generates request', () => {
837
- return api.flashDevice(props).then(Common.expectDeviceUrlAndToken);
838
- });
839
-
840
- it('sends proper data', () => {
841
- return api.flashDevice(props).then(({ files, form }) => {
842
- form.should.be.instanceOf(Object);
843
- files.should.be.instanceOf(Object);
844
- files.should.have.property('app.ino').and.be.ok;
845
- form.build_target_version.should.equal(props.targetVersion);
846
- });
847
- });
848
- });
849
-
850
- describe('product scope', () => {
851
- it('generates request', () => {
852
- return api.flashDevice(propsWithProduct).then((results) => {
853
- results.should.match({
854
- method: 'put',
855
- uri: `/v1/products/${product}/devices/${props.deviceId}`,
856
- auth: props.auth
857
- });
858
- });
859
- });
860
- });
861
- });
862
-
863
- describe('.compileCode', () => {
864
- it('generates request', () => {
865
- return api.compileCode(props).then((results) => {
866
- results.auth.should.equal(props.auth);
867
- });
868
- });
869
-
870
- it('sends proper data', () => {
871
- return api.compileCode(props).then(({ files, form }) => {
872
- form.should.be.instanceOf(Object);
873
- files.should.be.instanceOf(Object);
874
- files.should.have.property('app.ino').and.be.ok;
875
- form.build_target_version.should.equal(props.targetVersion);
876
- });
877
- });
878
- });
879
-
880
- describe('.downloadFirmwareBinary', () => {
881
- it('generates request', () => {
882
- sinon.stub(api, 'request').callsFake(x => Promise.resolve(x));
883
- const req = api.downloadFirmwareBinary(propsWithProduct);
884
- api.request.callCount.should.equal(1);
885
- return req.then((results) => {
886
- results.should.match({
887
- uri: `/v1/binaries/${props.binaryId}`,
888
- method: 'get',
889
- auth: props.auth,
890
- isBuffer: true
891
- });
892
- });
893
- });
894
- });
895
-
896
- describe('.sendPublicKey', () => {
897
- it('generates request', () => {
898
- return api.sendPublicKey(props).then(Common.expectDeviceUrlAndToken);
899
- });
900
-
901
- it('sends proper data', () => {
902
- return api.sendPublicKey(props).then(({ data }) => {
903
- data.should.be.instanceOf(Object);
904
- data.deviceID.should.equal(props.deviceId);
905
- data.publicKey.should.equal(props.key);
906
- data.filename.should.equal('particle-api');
907
- data.order.should.containEql('manual');
908
- data.algorithm.should.equal('rsa');
909
- });
910
- });
911
-
912
- it('works with buffer', () => {
913
- return api.sendPublicKey({
914
- deviceId: '1337',
915
- key: new Buffer(props.key),
916
- }).then(({ data }) => {
917
- data.publicKey.should.equal(props.key);
918
- });
919
- });
920
- });
921
-
922
- describe('.callFunction', () => {
923
- describe('user scope', () => {
924
- it('generates request', () => {
925
- return api.callFunction(props).then((results) => {
926
- results.should.match({
927
- method: 'post',
928
- uri: `/v1/devices/${props.deviceId}/${props.name}`,
929
- auth: props.auth,
930
- data: {
931
- args: props.argument,
932
- }
933
- });
934
- });
935
- });
936
- });
937
-
938
- describe('product scope', () => {
939
- it('generates request', () => {
940
- return api.callFunction(propsWithProduct).then((results) => {
941
- results.should.match({
942
- method: 'post',
943
- uri: `/v1/products/${product}/devices/${props.deviceId}/${props.name}`,
944
- auth: props.auth,
945
- data: {
946
- args: props.argument,
947
- }
948
- });
949
- });
950
- });
951
- });
952
- });
953
-
954
- describe('.getEventStream', () => {
955
- before(() => {
956
- sinon.stub(EventStream.prototype, 'connect').callsFake(function connect(){
957
- return Promise.resolve({ uri: this.uri });
958
- });
959
- });
960
-
961
- after(() => {
962
- sinon.restore();
963
- });
964
-
965
- it('requests public events', () => {
966
- return api.getEventStream({ }).then(({ uri }) => {
967
- uri.should.endWith('events');
968
- });
969
- });
970
-
971
- it('requests all user events', () => {
972
- return api.getEventStream({ deviceId: 'mine' }).then(({ uri }) => {
973
- uri.should.endWith('devices/events');
974
- });
975
- });
976
-
977
- it('requests all named events', () => {
978
- return api.getEventStream({ name: 'test' }).then(({ uri }) => {
979
- uri.should.endWith('v1/events/test');
980
- });
981
- });
982
-
983
- it('requests all device events', () => {
984
- return api.getEventStream({ deviceId: '1337' }).then(({ uri }) => {
985
- uri.should.endWith('1337/events');
986
- });
987
- });
988
-
989
- it('requests user\'s named events', () => {
990
- return api.getEventStream({
991
- deviceId: 'mine',
992
- name: 'test',
993
- }).then(({ uri }) => {
994
- uri.should.endWith('devices/events/test');
995
- });
996
- });
997
-
998
- it('requests device\'s named events', () => {
999
- return api.getEventStream(props).then(({ uri }) => {
1000
- uri.should.endWith(`${ props.deviceId }/events/${ props.name }`);
1001
- });
1002
- });
1003
-
1004
- it('requests org\'s events', () => {
1005
- return api.getEventStream({ org: 'test-org' }).then(({ uri }) => {
1006
- uri.should.endWith('v1/orgs/test-org/events');
1007
- });
1008
- });
1009
-
1010
- it('requests org\'s device events', () => {
1011
- return api.getEventStream({ org: 'test-org', deviceId: props.deviceId }).then(({ uri }) => {
1012
- uri.should.endWith(`v1/orgs/test-org/devices/${props.deviceId}/events`);
1013
- });
1014
- });
1015
-
1016
- it('requests org\'s device named events', () => {
1017
- return api.getEventStream({ org: 'test-org', deviceId: props.deviceId, name: 'test' }).then(({ uri }) => {
1018
- uri.should.endWith(`v1/orgs/test-org/devices/${props.deviceId}/events/test`);
1019
- });
1020
- });
1021
-
1022
- it('requests product\'s events', () => {
1023
- return api.getEventStream({ org: 'test-org', product: 'test-product' }).then(({ uri }) => {
1024
- uri.should.endWith('v1/orgs/test-org/products/test-product/events');
1025
- });
1026
- });
1027
-
1028
- it('requests product\'s named events', () => {
1029
- return api.getEventStream({ org: 'test-org', product: 'test-product', name: 'test' }).then(({ uri }) => {
1030
- uri.should.endWith('v1/orgs/test-org/products/test-product/events/test');
1031
- });
1032
- });
1033
-
1034
- it('requests a product\'s event without an org provided', () => {
1035
- return api.getEventStream({ product: 'test-product' })
1036
- .then(({ uri }) => {
1037
- uri.should.endWith('v1/products/test-product/events');
1038
- });
1039
- });
1040
-
1041
- it('requests a product\'s named event without an org provided', () => {
1042
- return api.getEventStream({ product: 'test-product', name: 'foo' })
1043
- .then(({ uri }) => {
1044
- uri.should.endWith('v1/products/test-product/events/foo');
1045
- });
1046
- });
1047
-
1048
- it('requests product\'s device events', () => {
1049
- return api.getEventStream({ product: 'test-product', deviceId: props.deviceId }).then(({ uri }) => {
1050
- uri.should.endWith(`v1/products/test-product/devices/${props.deviceId}/events`);
1051
- });
1052
- });
1053
-
1054
- it('requests product\'s device named events', () => {
1055
- return api.getEventStream({ product: 'test-product', deviceId: props.deviceId, name: 'foo' }).then(({ uri }) => {
1056
- uri.should.endWith(`v1/products/test-product/devices/${props.deviceId}/events/foo`);
1057
- });
1058
- });
1059
-
1060
- it('calls _getActiveAuthToken(auth)', () => {
1061
- const fakeToken = 'abc123';
1062
- sinon.stub(api, '_getActiveAuthToken').returns(fakeToken);
1063
- api.getEventStream({});
1064
- expect(api._getActiveAuthToken).to.have.property('callCount', 1);
1065
- });
1066
- });
1067
-
1068
- describe('.publishEvent', () => {
1069
- describe('user scope', () => {
1070
- it('generates request', () => {
1071
- return api.publishEvent(props).then((results) => {
1072
- results.should.match({
1073
- method: 'post',
1074
- uri: '/v1/devices/events',
1075
- auth: props.auth,
1076
- data: {
1077
- name: props.name,
1078
- data: props.data,
1079
- private: props.isPrivate
1080
- }
1081
- });
1082
- });
1083
- });
1084
- });
1085
-
1086
- describe('product scope', () => {
1087
- it('generates request', () => {
1088
- return api.publishEvent(propsWithProduct).then((results) => {
1089
- results.should.match({
1090
- method: 'post',
1091
- uri: `/v1/products/${product}/events`,
1092
- auth: props.auth,
1093
- data: {
1094
- name: props.name,
1095
- data: props.data,
1096
- private: props.isPrivate
1097
- }
1098
- });
1099
- });
1100
- });
1101
- });
1102
- });
1103
-
1104
- describe('.createWebhook', () => {
1105
- describe('user scope', () => {
1106
- it('creates for a single device', () => {
1107
- return api.createWebhook(props).then((results) => {
1108
- results.should.match({
1109
- uri: '/v1/webhooks',
1110
- method: 'post',
1111
- auth: props.auth,
1112
- headers: props.headers,
1113
- data: {
1114
- event: props.event,
1115
- url: props.url,
1116
- deviceId: props.device,
1117
- rejectUnauthorized: props.rejectUnauthorized,
1118
- noDefaults: props.noDefaults,
1119
- requestType: props.hook.method,
1120
- auth: props.hook.auth,
1121
- headers: props.hook.headers,
1122
- query: props.hook.query,
1123
- json: props.hook.json,
1124
- form: props.hook.form,
1125
- body: props.hook.body,
1126
- responseTemplate: props.hook.responseTemplate,
1127
- responseTopic: props.hook.responseEvent,
1128
- errorResponseTopic: props.hook.errorResponseEvent,
1129
- },
1130
- context: {}
1131
- });
1132
- });
1133
- });
1134
-
1135
- it('creates for user\'s devices', () => {
1136
- const params = Object.assign({}, props);
1137
- delete params.device;
1138
- return api.createWebhook(params).then((results) => {
1139
- results.should.match({
1140
- uri: '/v1/webhooks',
1141
- method: 'post',
1142
- auth: props.auth,
1143
- headers: props.headers,
1144
- data: {
1145
- event: props.event,
1146
- url: props.url,
1147
- deviceId: undefined,
1148
- rejectUnauthorized: props.rejectUnauthorized,
1149
- noDefaults: props.noDefaults,
1150
- requestType: props.hook.method,
1151
- auth: props.hook.auth,
1152
- headers: props.hook.headers,
1153
- query: props.hook.query,
1154
- json: props.hook.json,
1155
- form: props.hook.form,
1156
- body: props.hook.body,
1157
- responseTemplate: props.hook.responseTemplate,
1158
- responseTopic: props.hook.responseEvent,
1159
- errorResponseTopic: props.hook.errorResponseEvent,
1160
- },
1161
- context: {}
1162
- });
1163
- });
1164
- });
1165
-
1166
- it('creates using defaults', () => {
1167
- const params = Object.assign({}, props);
1168
- delete params.device;
1169
- delete params.rejectUnauthorized;
1170
- delete params.noDefaults;
1171
- delete params.hook;
1172
- delete params.headers;
1173
- delete params.context;
1174
- return api.createWebhook(params).then((results) => {
1175
- results.should.match({
1176
- uri: '/v1/webhooks',
1177
- method: 'post',
1178
- auth: props.auth,
1179
- headers: undefined,
1180
- data: {
1181
- event: props.event,
1182
- url: props.url,
1183
- deviceId: undefined,
1184
- rejectUnauthorized: undefined,
1185
- noDefaults: undefined,
1186
- requestType: 'POST'
1187
- },
1188
- context: {}
1189
- });
1190
- });
1191
- });
1192
- });
1193
-
1194
- describe('product scope', () => {
1195
- it('generates request', () => {
1196
- return api.createWebhook(propsWithProduct).then((results) => {
1197
- results.should.match({
1198
- uri: `/v1/products/${product}/webhooks`,
1199
- method: 'post',
1200
- auth: props.auth,
1201
- headers: props.headers,
1202
- data: {
1203
- event: props.event,
1204
- url: props.url,
1205
- deviceId: props.device,
1206
- rejectUnauthorized: props.rejectUnauthorized,
1207
- noDefaults: props.noDefaults,
1208
- requestType: props.hook.method,
1209
- auth: props.hook.auth,
1210
- headers: props.hook.headers,
1211
- query: props.hook.query,
1212
- json: props.hook.json,
1213
- form: props.hook.form,
1214
- body: props.hook.body,
1215
- responseTemplate: props.hook.responseTemplate,
1216
- responseTopic: props.hook.responseEvent,
1217
- errorResponseTopic: props.hook.errorResponseEvent,
1218
- },
1219
- context: {}
1220
- });
1221
- });
1222
- });
1223
- });
1224
- });
1225
-
1226
- describe('.deleteWebhook', () => {
1227
- describe('user scope', () => {
1228
- it('generates request', () => {
1229
- return api.deleteWebhook(props).then((results) => {
1230
- results.should.match({
1231
- method: 'delete',
1232
- uri: `/v1/webhooks/${props.hookId}`,
1233
- auth: props.auth,
1234
- });
1235
- });
1236
- });
1237
- });
1238
-
1239
- describe('product scope', () => {
1240
- it('generates request', () => {
1241
- return api.deleteWebhook(propsWithProduct).then((results) => {
1242
- results.should.match({
1243
- method: 'delete',
1244
- uri: `/v1/products/${product}/webhooks/${props.hookId}`,
1245
- auth: props.auth,
1246
- });
1247
- });
1248
- });
1249
- });
1250
- });
1251
-
1252
- describe('.listWebhooks', () => {
1253
- describe('user scope', () => {
1254
- it('generates request', () => {
1255
- return api.listWebhooks(props).then((results) => {
1256
- results.should.match({
1257
- method: 'get',
1258
- uri: '/v1/webhooks',
1259
- auth: props.auth,
1260
- });
1261
- });
1262
- });
1263
- });
1264
-
1265
- describe('product scope', () => {
1266
- it('generates request', () => {
1267
- return api.listWebhooks(propsWithProduct).then((results) => {
1268
- results.should.match({
1269
- method: 'get',
1270
- uri: `/v1/products/${product}/webhooks`,
1271
- auth: props.auth,
1272
- });
1273
- });
1274
- });
1275
- });
1276
- });
1277
-
1278
- describe('.createIntegration', () => {
1279
- describe('user scope', () => {
1280
- it('generates request', () => {
1281
- return api.createIntegration(props).then((results) => {
1282
- results.should.match({
1283
- method: 'post',
1284
- uri: '/v1/integrations',
1285
- auth: props.auth,
1286
- data: {
1287
- event: props.event,
1288
- deviceid: props.deviceId,
1289
- url: props.settings.url,
1290
- }
1291
- });
1292
- });
1293
- });
1294
- });
1295
-
1296
- describe('product scope', () => {
1297
- it('generates request', () => {
1298
- return api.createIntegration(propsWithProduct).then((results) => {
1299
- results.should.match({
1300
- method: 'post',
1301
- uri: `/v1/products/${product}/integrations`,
1302
- auth: props.auth,
1303
- data: {
1304
- event: props.event,
1305
- deviceid: props.deviceId,
1306
- url: props.settings.url,
1307
- }
1308
- });
1309
- });
1310
- });
1311
- });
1312
- });
1313
-
1314
- describe('.editIntegration', () => {
1315
- describe('user scope', () => {
1316
- it('generates request', () => {
1317
- return api.editIntegration(props).then((results) => {
1318
- results.should.match({
1319
- method: 'put',
1320
- uri: `/v1/integrations/${props.integrationId}`,
1321
- auth: props.auth,
1322
- data: {
1323
- event: props.event,
1324
- deviceid: props.deviceId,
1325
- url: props.settings.url,
1326
- }
1327
- });
1328
- });
1329
- });
1330
- });
1331
-
1332
- describe('product scope', () => {
1333
- it('generates request', () => {
1334
- return api.editIntegration(propsWithProduct).then((results) => {
1335
- results.should.match({
1336
- method: 'put',
1337
- uri: `/v1/products/${product}/integrations/${props.integrationId}`,
1338
- auth: props.auth,
1339
- data: {
1340
- event: props.event,
1341
- deviceid: props.deviceId,
1342
- url: props.settings.url,
1343
- }
1344
- });
1345
- });
1346
- });
1347
- });
1348
- });
1349
-
1350
- describe('.deleteIntegration', () => {
1351
- describe('user scope', () => {
1352
- it('generates request', () => {
1353
- return api.deleteIntegration(props).then((results) => {
1354
- results.should.match({
1355
- method: 'delete',
1356
- uri: `/v1/integrations/${props.integrationId}`,
1357
- auth: props.auth,
1358
- });
1359
- });
1360
- });
1361
- });
1362
-
1363
- describe('product scope', () => {
1364
- it('generates request', () => {
1365
- return api.deleteIntegration(propsWithProduct).then((results) => {
1366
- results.should.match({
1367
- method: 'delete',
1368
- uri: `/v1/products/${product}/integrations/${props.integrationId}`,
1369
- auth: props.auth,
1370
- });
1371
- });
1372
- });
1373
- });
1374
- });
1375
-
1376
- describe('.listIntegrations', () => {
1377
- describe('user scope', () => {
1378
- it('generates request', () => {
1379
- return api.listIntegrations(props).then((results) => {
1380
- results.should.match({
1381
- method: 'get',
1382
- uri: '/v1/integrations',
1383
- auth: props.auth,
1384
- });
1385
- });
1386
- });
1387
- });
1388
-
1389
- describe('product scope', () => {
1390
- it('generates request', () => {
1391
- return api.listIntegrations(propsWithProduct).then((results) => {
1392
- results.should.match({
1393
- method: 'get',
1394
- uri: `/v1/products/${product}/integrations`,
1395
- auth: props.auth,
1396
- });
1397
- });
1398
- });
1399
- });
1400
- });
1401
-
1402
- describe('.listBuildTargets', () => {
1403
- it('generates request', () => {
1404
- return api.listBuildTargets(props).then(({ auth, query }) => {
1405
- auth.should.equal(props.auth);
1406
- should.not.exist(query);
1407
- });
1408
- });
1409
-
1410
- it('passes featured flag', () => {
1411
- const params = { auth: props.auth, onlyFeatured: true };
1412
- return api.listBuildTargets(params).then(({ auth, query }) => {
1413
- auth.should.equal(props.auth);
1414
- query.should.eql({ featured: true });
1415
- });
1416
- });
1417
- });
1418
-
1419
- describe('.validatePromoCode', () => {
1420
- it('generates request', () => {
1421
- return api.validatePromoCode({ auth: 'X', promoCode: '123ABCD' }).then((results) => {
1422
- results.uri.should.be.instanceOf(String);
1423
- results.auth.should.equal('X');
1424
- });
1425
- });
1426
- });
1427
-
1428
- describe('.setUserInfo', () => {
1429
- it('generates request', () => {
1430
- return api.setUserInfo({ auth: 'X', accountInfo: { first_name: 'John', last_name: 'Scully' } })
1431
- .then((results) => {
1432
- results.should.eql({
1433
- uri: '/v1/user',
1434
- method: 'put',
1435
- auth: 'X',
1436
- headers: undefined,
1437
- query: undefined,
1438
- data: {
1439
- account_info: { first_name: 'John', last_name: 'Scully' }
1440
- },
1441
- context: {}
1442
- });
1443
- });
1444
- });
1445
- });
1446
-
1447
- describe('.changeUsername', () => {
1448
- it('generates request', () => {
1449
- return api.changeUsername({ auth: 'X', currentPassword: 'blabla', username: 'john@skul.ly' })
1450
- .then((results) => {
1451
- results.should.eql({
1452
- uri: '/v1/user',
1453
- method: 'put',
1454
- auth: 'X',
1455
- headers: undefined,
1456
- query: undefined,
1457
- data: {
1458
- current_password: 'blabla',
1459
- username: 'john@skul.ly'
1460
- },
1461
- context: {}
1462
- });
1463
- });
1464
- });
1465
- it('allows invalidating tokens', () => {
1466
- return api.changeUsername({ auth: 'X', currentPassword: 'blabla', username: 'john@skul.ly', invalidateTokens: true })
1467
- .then((results) => {
1468
- results.should.eql({
1469
- uri: '/v1/user',
1470
- method: 'put',
1471
- auth: 'X',
1472
- headers: undefined,
1473
- query: undefined,
1474
- data: {
1475
- current_password: 'blabla',
1476
- username: 'john@skul.ly',
1477
- invalidate_tokens: true
1478
- },
1479
- context: {}
1480
- });
1481
- });
1482
- });
1483
- });
1484
-
1485
- describe('.changeUserPassword', () => {
1486
- it('generates request', () => {
1487
- return api.changeUserPassword({ auth: 'X', currentPassword: 'blabla', password: 'blabla2' })
1488
- .then((results) => {
1489
- results.should.eql({
1490
- uri: '/v1/user',
1491
- method: 'put',
1492
- auth: 'X',
1493
- headers: undefined,
1494
- query: undefined,
1495
- data: {
1496
- current_password: 'blabla',
1497
- password: 'blabla2'
1498
- },
1499
- context: {}
1500
- });
1501
- });
1502
- });
1503
-
1504
- it('allows invalidating tokens', () => {
1505
- return api.changeUserPassword({ auth: 'X', currentPassword: 'blabla', password: 'blabla2', invalidateTokens: true })
1506
- .then((results) => {
1507
- results.should.eql({
1508
- method: 'put',
1509
- uri: '/v1/user',
1510
- auth: 'X',
1511
- headers: undefined,
1512
- query: undefined,
1513
- data: {
1514
- current_password: 'blabla',
1515
- password: 'blabla2',
1516
- invalidate_tokens: true
1517
- },
1518
- context: {}
1519
- });
1520
- });
1521
- });
1522
- });
1523
-
1524
- describe('.listSIMs', () => {
1525
- describe('user scope', () => {
1526
- it('generates request', () => {
1527
- return api.listSIMs(props).then((results) => {
1528
- results.should.match({
1529
- uri: '/v1/sims',
1530
- method: 'get',
1531
- auth: props.auth,
1532
- headers: props.headers,
1533
- query: undefined,
1534
- context: {}
1535
- });
1536
- });
1537
- });
1538
- });
1539
-
1540
- describe('product scope', () => {
1541
- it('generates request', () => {
1542
- return api.listSIMs(propsWithProduct).then((results) => {
1543
- results.should.match({
1544
- uri: `/v1/products/${product}/sims`,
1545
- method: 'get',
1546
- auth: props.auth,
1547
- headers: props.headers,
1548
- query: {
1549
- iccid: props.iccid,
1550
- deviceId: props.deviceId,
1551
- deviceName: props.deviceName,
1552
- page: props.page,
1553
- per_page: props.perPage
1554
- },
1555
- context: {}
1556
- });
1557
- });
1558
- });
1559
- });
1560
- });
1561
-
1562
- describe('.activateSIM', () => {
1563
- describe('user scope', () => {
1564
- it('generates request', () => {
1565
- return api.activateSIM(props).then((results) => {
1566
- results.should.match({
1567
- uri: `/v1/sims/${props.iccid}`,
1568
- method: 'put',
1569
- auth: props.auth,
1570
- headers: props.headers,
1571
- data: {
1572
- country: props.country,
1573
- action: 'activate'
1574
- },
1575
- context: {}
1576
- });
1577
- });
1578
- });
1579
- });
1580
-
1581
- describe('product scope', () => {
1582
- describe('single SIM', () => {
1583
- it('generates request', () => {
1584
- const propsSingleSIM = Object.assign({}, propsWithProduct);
1585
- delete propsSingleSIM.iccids;
1586
- return api.activateSIM(propsSingleSIM).then((results) => {
1587
- results.should.match({
1588
- uri: `/v1/products/${product}/sims`,
1589
- method: 'post',
1590
- auth: props.auth,
1591
- headers: props.headers,
1592
- data: {
1593
- sims: [props.iccid],
1594
- country: props.country,
1595
- },
1596
- context: {}
1597
- });
1598
- });
1599
- });
1600
- });
1601
-
1602
- describe('multiple SIMs', () => {
1603
- it('generates request', () => {
1604
- return api.activateSIM(propsWithProduct).then((results) => {
1605
- results.should.match({
1606
- uri: `/v1/products/${product}/sims`,
1607
- method: 'post',
1608
- auth: props.auth,
1609
- headers: props.headers,
1610
- data: {
1611
- sims: props.iccids,
1612
- country: props.country,
1613
- },
1614
- context: {}
1615
- });
1616
- });
1617
- });
1618
- });
1619
- });
1620
- });
1621
-
1622
- describe('.deactivateSIM', () => {
1623
- describe('user scope', () => {
1624
- it('generates request', () => {
1625
- return api.deactivateSIM(props).then((results) => {
1626
- results.should.match({
1627
- uri: `/v1/sims/${props.iccid}`,
1628
- method: 'put',
1629
- auth: props.auth,
1630
- headers: props.headers,
1631
- data: {
1632
- action: 'deactivate'
1633
- },
1634
- context: {}
1635
- });
1636
- });
1637
- });
1638
- });
1639
-
1640
- describe('product scope', () => {
1641
- it('generates request', () => {
1642
- return api.deactivateSIM(propsWithProduct).then((results) => {
1643
- results.should.match({
1644
- uri: `/v1/products/${product}/sims/${props.iccid}`,
1645
- method: 'put',
1646
- auth: props.auth,
1647
- headers: props.headers,
1648
- data: {
1649
- action: 'deactivate'
1650
- },
1651
- context: {}
1652
- });
1653
- });
1654
- });
1655
- });
1656
- });
1657
-
1658
- describe('.reactivateSIM', () => {
1659
- describe('user scope', () => {
1660
- it('generates request', () => {
1661
- return api.reactivateSIM(props).then((results) => {
1662
- results.should.match({
1663
- uri: `/v1/sims/${props.iccid}`,
1664
- method: 'put',
1665
- auth: props.auth,
1666
- headers: props.headers,
1667
- data: {
1668
- mb_limit: props.mbLimit,
1669
- action: 'reactivate'
1670
- },
1671
- context: {}
1672
- });
1673
- });
1674
- });
1675
- });
1676
-
1677
- describe('product scope', () => {
1678
- it('generates request', () => {
1679
- return api.reactivateSIM(propsWithProduct).then((results) => {
1680
- results.should.match({
1681
- uri: `/v1/products/${product}/sims/${props.iccid}`,
1682
- method: 'put',
1683
- auth: props.auth,
1684
- headers: props.headers,
1685
- data: {
1686
- mb_limit: props.mbLimit,
1687
- action: 'reactivate'
1688
- },
1689
- context: {}
1690
- });
1691
- });
1692
- });
1693
- });
1694
- });
1695
-
1696
- describe('.removeSIM', () => {
1697
- describe('user scope', () => {
1698
- it('generates request', () => {
1699
- return api.removeSIM(props).then((results) => {
1700
- results.should.match({
1701
- uri: `/v1/sims/${props.iccid}`,
1702
- method: 'delete',
1703
- auth: props.auth,
1704
- headers: props.headers,
1705
- data: undefined,
1706
- context: {}
1707
- });
1708
- });
1709
- });
1710
- });
1711
-
1712
- describe('product scope', () => {
1713
- it('generates request', () => {
1714
- return api.removeSIM(propsWithProduct).then((results) => {
1715
- results.should.match({
1716
- uri: `/v1/products/${product}/sims/${props.iccid}`,
1717
- method: 'delete',
1718
- auth: props.auth,
1719
- headers: props.headers,
1720
- data: undefined,
1721
- context: {}
1722
- });
1723
- });
1724
- });
1725
- });
1726
- });
1727
-
1728
- describe('.updateSIM', () => {
1729
- describe('user scope', () => {
1730
- it('generates request', () => {
1731
- return api.updateSIM(props).then((results) => {
1732
- results.should.match({
1733
- uri: `/v1/sims/${props.iccid}`,
1734
- method: 'put',
1735
- auth: props.auth,
1736
- headers: props.headers,
1737
- data: {
1738
- mb_limit: props.mbLimit
1739
- },
1740
- context: {}
1741
- });
1742
- });
1743
- });
1744
- });
1745
-
1746
- describe('product scope', () => {
1747
- it('generates request', () => {
1748
- return api.updateSIM(propsWithProduct).then((results) => {
1749
- results.should.match({
1750
- uri: `/v1/products/${product}/sims/${props.iccid}`,
1751
- method: 'put',
1752
- auth: props.auth,
1753
- headers: props.headers,
1754
- data: {
1755
- mb_limit: props.mbLimit
1756
- },
1757
- context: {}
1758
- });
1759
- });
1760
- });
1761
- });
1762
- });
1763
-
1764
- describe('.getSIMDataUsage', () => {
1765
- describe('user scope', () => {
1766
- it('generates request', () => {
1767
- return api.getSIMDataUsage(props).then((results) => {
1768
- results.should.match({
1769
- uri: `/v1/sims/${props.iccid}/data_usage`,
1770
- method: 'get',
1771
- auth: props.auth,
1772
- headers: props.headers,
1773
- query: undefined,
1774
- context: {}
1775
- });
1776
- });
1777
- });
1778
- });
1779
-
1780
- describe('product scope', () => {
1781
- it('generates request', () => {
1782
- return api.getSIMDataUsage(propsWithProduct).then((results) => {
1783
- results.should.match({
1784
- uri: `/v1/products/${product}/sims/${props.iccid}/data_usage`,
1785
- method: 'get',
1786
- auth: props.auth,
1787
- headers: props.headers,
1788
- query: undefined,
1789
- context: {}
1790
- });
1791
- });
1792
- });
1793
- });
1794
- });
1795
-
1796
- describe('.getFleetDataUsage', () => {
1797
- it('generates request', () => {
1798
- return api.getFleetDataUsage(propsWithProduct).then((results) => {
1799
- results.should.match({
1800
- uri: `/v1/products/${product}/sims/data_usage`,
1801
- method: 'get',
1802
- auth: props.auth,
1803
- headers: props.headers,
1804
- query: undefined,
1805
- context: {}
1806
- });
1807
- });
1808
- });
1809
- });
1810
-
1811
- describe('.checkSIM', () => {
1812
- it('generates request', () => {
1813
- return api.checkSIM({ auth: 'X', iccid: '1234567890123456789' }).then((results) => {
1814
- results.should.match({
1815
- uri: '/v1/sims/1234567890123456789',
1816
- method: 'head',
1817
- auth: 'X',
1818
- headers: undefined,
1819
- query: undefined,
1820
- context: {}
1821
- });
1822
- });
1823
- });
1824
- });
1825
-
1826
- describe('.listLibraries', () => {
1827
- it('generates request', () => {
1828
- return api.listLibraries(props).then((results) => {
1829
- results.should.match({
1830
- uri: '/v1/libraries',
1831
- method: 'get',
1832
- auth: props.auth,
1833
- headers: props.headers,
1834
- context: {}
1835
- });
1836
- });
1837
- });
1838
-
1839
- it('forwards query parameters', () => {
1840
- return api.listLibraries({
1841
- auth: 'X',
1842
- page: 3,
1843
- filter: 'abc',
1844
- sort: 'name',
1845
- architectures: ['spark-core', 'particle-photon'],
1846
- category: 'Other'
1847
- }).then((results) => {
1848
- results.query.should.eql({
1849
- page: 3,
1850
- limit: undefined,
1851
- filter: 'abc',
1852
- excludeScopes: undefined,
1853
- sort: 'name',
1854
- scope: undefined,
1855
- architectures: 'spark-core,particle-photon',
1856
- category: 'Other'
1857
- });
1858
- });
1859
- });
1860
- });
1861
-
1862
- describe('.getLibrary', () => {
1863
- it('generates request', () => {
1864
- return api.getLibrary({ name: 'mylib', auth: 'X' }).then((results) => {
1865
- results.should.match({
1866
- uri: '/v1/libraries/mylib',
1867
- auth: 'X'
1868
- });
1869
- });
1870
- });
1871
-
1872
- it('forwards query parameters', () => {
1873
- return api.getLibrary({ name: 'mylib', auth: 'X', version: '1.2.0' }).then((results) => {
1874
- results.query.should.eql({
1875
- version: '1.2.0'
1876
- });
1877
- });
1878
- });
1879
- });
1880
-
1881
- describe('.getLibraryVersions', () => {
1882
- it('generates request', () => {
1883
- return api.getLibraryVersions({ name: 'mylib', auth: 'X' }).then((results) => {
1884
- results.should.match({
1885
- uri: '/v1/libraries/mylib/versions',
1886
- auth: 'X'
1887
- });
1888
- });
1889
- });
1890
-
1891
- it('forwards query parameters', () => {
1892
- return api.getLibraryVersions({ name: 'mylib', auth: 'X', page: 3 }).then((results) => {
1893
- results.query.should.eql({
1894
- page: 3,
1895
- limit: undefined
1896
- });
1897
- });
1898
- });
1899
- });
1900
-
1901
- describe('.contributeLibrary', () => {
1902
- it('generates request', () => {
1903
- const archive = new Buffer('tarball');
1904
- return api.contributeLibrary({ archive: archive, auth: 'X' }).then((results) => {
1905
- results.should.match({
1906
- uri: '/v1/libraries',
1907
- method: 'post',
1908
- auth: 'X'
1909
- });
1910
- });
1911
- });
1912
- });
1913
-
1914
- describe('.publishLibrary', () => {
1915
- it('generates request', () => {
1916
- const name = 'noname';
1917
- return api.publishLibrary({ name, auth: 'X' }).then((results) => {
1918
- results.should.match({
1919
- uri: '/v1/libraries/noname',
1920
- method: 'patch',
1921
- auth: 'X',
1922
- data: {
1923
- visibility: 'public'
1924
- }
1925
- });
1926
- });
1927
- });
1928
- });
1929
-
1930
- describe('.deleteLibrary', () => {
1931
- it('generates request', () => {
1932
- return api.deleteLibrary({ name: 'mylib', auth: 'X', force: 'xyz' }).then((results) => {
1933
- results.should.match({
1934
- uri: '/v1/libraries/mylib',
1935
- method: 'delete',
1936
- auth: 'X',
1937
- headers:undefined,
1938
- data: {
1939
- force: 'xyz'
1940
- },
1941
- context: {}
1942
- });
1943
- });
1944
- });
1945
- });
1946
-
1947
- describe('.downloadFile', () => {
1948
- it('generates request', () => {
1949
- sinon.stub(api, 'request').callsFake(x => Promise.resolve(x));
1950
- const uri = 'http://example.com/path/to/file.png';
1951
- const req = api.downloadFile({ uri });
1952
- api.request.callCount.should.equal(1);
1953
- return req.then((results) => {
1954
- results.should.match({ uri, method: 'get', isBuffer: true });
1955
- });
1956
- });
1957
- });
1958
-
1959
- describe('.listOAuthClients', () => {
1960
- describe('user scope', () => {
1961
- it('generates request', () => {
1962
- return api.listOAuthClients(props).then((results) => {
1963
- results.should.match({
1964
- uri: '/v1/clients',
1965
- method: 'get',
1966
- auth: props.auth,
1967
- headers: props.headers,
1968
- query: undefined,
1969
- context: {}
1970
- });
1971
- });
1972
- });
1973
- });
1974
-
1975
- describe('product scope', () => {
1976
- it('generates request', () => {
1977
- return api.listOAuthClients(propsWithProduct).then((results) => {
1978
- results.should.match({
1979
- uri: `/v1/products/${product}/clients`,
1980
- method: 'get',
1981
- auth: props.auth,
1982
- headers: props.headers,
1983
- query: undefined,
1984
- context: {}
1985
- });
1986
- });
1987
- });
1988
- });
1989
- });
1990
-
1991
- describe('.createOAuthClient', () => {
1992
- describe('user scope', () => {
1993
- it('generates request', () => {
1994
- return api.createOAuthClient(props).then((results) => {
1995
- results.should.match({
1996
- uri: '/v1/clients',
1997
- method: 'post',
1998
- auth: props.auth,
1999
- headers: props.headers,
2000
- data: {
2001
- name: props.name,
2002
- type: props.type,
2003
- redirect_uri: props.redirect_uri,
2004
- scope: props.scope
2005
- },
2006
- context: {}
2007
- });
2008
- });
2009
- });
2010
- });
2011
-
2012
- describe('product scope', () => {
2013
- it('generates request', () => {
2014
- return api.createOAuthClient(propsWithProduct).then((results) => {
2015
- results.should.match({
2016
- uri: `/v1/products/${product}/clients`,
2017
- method: 'post',
2018
- auth: props.auth,
2019
- headers: props.headers,
2020
- data: {
2021
- name: props.name,
2022
- type: props.type,
2023
- redirect_uri: props.redirect_uri,
2024
- scope: props.scope
2025
- },
2026
- context: {}
2027
- });
2028
- });
2029
- });
2030
- });
2031
- });
2032
-
2033
- describe('.updateOAuthClient', () => {
2034
- describe('user scope', () => {
2035
- it('generates request', () => {
2036
- return api.updateOAuthClient(props).then((results) => {
2037
- results.should.match({
2038
- uri: `/v1/clients/${props.clientId}`,
2039
- method: 'put',
2040
- auth: props.auth,
2041
- headers: props.headers,
2042
- data: {
2043
- name: props.name,
2044
- scope: props.scope
2045
- },
2046
- context: {}
2047
- });
2048
- });
2049
- });
2050
- });
2051
- describe('product scope', () => {
2052
- it('generates request', () => {
2053
- return api.updateOAuthClient(propsWithProduct).then((results) => {
2054
- results.should.match({
2055
- uri: `/v1/products/${product}/clients/${props.clientId}`,
2056
- method: 'put',
2057
- auth: props.auth,
2058
- headers: props.headers,
2059
- data: {
2060
- name: props.name,
2061
- scope: props.scope
2062
- },
2063
- context: {}
2064
- });
2065
- });
2066
- });
2067
- });
2068
- });
2069
-
2070
- describe('.deleteOAuthClient', () => {
2071
- describe('user scope', () => {
2072
- it('generates request', () => {
2073
- return api.deleteOAuthClient(props).then((results) => {
2074
- results.should.match({
2075
- uri: `/v1/clients/${props.clientId}`,
2076
- method: 'delete',
2077
- auth: props.auth,
2078
- headers: props.headers,
2079
- data: undefined,
2080
- context: {}
2081
- });
2082
- });
2083
- });
2084
- });
2085
- describe('product scope', () => {
2086
- it('generates request', () => {
2087
- return api.deleteOAuthClient(propsWithProduct).then((results) => {
2088
- results.should.match({
2089
- uri: `/v1/products/${product}/clients/${props.clientId}`,
2090
- method: 'delete',
2091
- auth: props.auth,
2092
- headers: props.headers,
2093
- data: undefined,
2094
- context: {}
2095
- });
2096
- });
2097
- });
2098
- });
2099
- });
2100
-
2101
- describe('.listProducts', () => {
2102
- it('generates request', () => {
2103
- return api.listProducts(props).then((results) => {
2104
- results.should.match({
2105
- uri: '/v1/products',
2106
- method: 'get',
2107
- auth: props.auth,
2108
- headers: props.headers,
2109
- query: undefined,
2110
- context: {}
2111
- });
2112
- });
2113
- });
2114
- });
2115
-
2116
- describe('.getProduct', () => {
2117
- it('generates request', () => {
2118
- return api.getProduct(propsWithProduct).then((results) => {
2119
- results.should.match({
2120
- uri: `/v1/products/${product}`,
2121
- method: 'get',
2122
- auth: props.auth,
2123
- headers: props.headers,
2124
- query: undefined,
2125
- context: {}
2126
- });
2127
- });
2128
- });
2129
- });
2130
-
2131
- describe('.listProductFirmware', () => {
2132
- it('generates request', () => {
2133
- return api.listProductFirmware(propsWithProduct).then((results) => {
2134
- results.should.match({
2135
- uri: `/v1/products/${product}/firmware`,
2136
- method: 'get',
2137
- auth: props.auth,
2138
- headers: props.headers,
2139
- query: undefined,
2140
- context: {}
2141
- });
2142
- });
2143
- });
2144
- });
2145
-
2146
- describe('.uploadProductFirmware', () => {
2147
- it('generates request', () => {
2148
- return api.uploadProductFirmware(propsWithProduct).then((results) => {
2149
- results.should.match({
2150
- uri: `/v1/products/${product}/firmware`,
2151
- method: 'post',
2152
- auth: props.auth,
2153
- headers: props.headers,
2154
- files: {
2155
- 'firmware.bin': props.file,
2156
- },
2157
- form: {
2158
- version: props.version,
2159
- title: props.title,
2160
- description: props.description
2161
- },
2162
- context: {}
2163
- });
2164
- });
2165
- });
2166
- });
2167
-
2168
- describe('.downloadProductFirmware', () => {
2169
- it('generates request', () => {
2170
- sinon.stub(api, 'request').callsFake(x => Promise.resolve(x));
2171
- const req = api.downloadProductFirmware(propsWithProduct);
2172
- api.request.callCount.should.equal(1);
2173
- return req.then((results) => {
2174
- results.should.match({
2175
- uri: `/v1/products/${product}/firmware/${props.version}/binary`,
2176
- method: 'get',
2177
- auth: props.auth,
2178
- headers: props.headers,
2179
- context: undefined,
2180
- isBuffer: true
2181
- });
2182
- });
2183
- });
2184
- });
2185
-
2186
- describe('.getProductFirmware', () => {
2187
- it('generates request', () => {
2188
- return api.getProductFirmware(propsWithProduct).then((results) => {
2189
- results.should.match({
2190
- uri: `/v1/products/${product}/firmware/${props.version}`,
2191
- method: 'get',
2192
- auth: props.auth,
2193
- headers: props.headers,
2194
- query: undefined,
2195
- context: {}
2196
- });
2197
- });
2198
- });
2199
- });
2200
-
2201
- describe('.updateProductFirmware', () => {
2202
- it('generates request', () => {
2203
- return api.updateProductFirmware(propsWithProduct).then((results) => {
2204
- results.should.match({
2205
- uri: `/v1/products/${product}/firmware/${props.version}`,
2206
- method: 'put',
2207
- auth: props.auth,
2208
- headers: props.headers,
2209
- data: {
2210
- title: props.title,
2211
- description: props.description
2212
- },
2213
- context: {}
2214
- });
2215
- });
2216
- });
2217
- });
2218
-
2219
- describe('.releaseProductFirmware', () => {
2220
- it('generates request', () => {
2221
- return api.releaseProductFirmware(propsWithProduct).then((results) => {
2222
- results.should.match({
2223
- uri: `/v1/products/${product}/firmware/release`,
2224
- method: 'put',
2225
- auth: props.auth,
2226
- headers: props.headers,
2227
- data: {
2228
- version: props.version,
2229
- },
2230
- context: {}
2231
- });
2232
- });
2233
- });
2234
- });
2235
-
2236
- describe('.listTeamMembers', () => {
2237
- it('generates request', () => {
2238
- return api.listTeamMembers(propsWithProduct).then((results) => {
2239
- results.should.match({
2240
- uri: `/v1/products/${product}/team`,
2241
- method: 'get',
2242
- auth: props.auth,
2243
- headers: props.headers,
2244
- query: undefined,
2245
- context: {}
2246
- });
2247
- });
2248
- });
2249
- });
2250
-
2251
- describe('.inviteTeamMember', () => {
2252
- it('generates request', () => {
2253
- return api.inviteTeamMember(propsWithProduct).then((results) => {
2254
- results.should.match({
2255
- uri: `/v1/products/${product}/team`,
2256
- method: 'post',
2257
- auth: props.auth,
2258
- headers: props.headers,
2259
- data: {
2260
- username: props.username
2261
- },
2262
- context: {}
2263
- });
2264
- });
2265
- });
2266
- });
2267
-
2268
- describe('.removeTeamMember', () => {
2269
- it('generates request', () => {
2270
- return api.removeTeamMember(propsWithProduct).then((results) => {
2271
- results.should.match({
2272
- uri: `/v1/products/${product}/team/${props.username}`,
2273
- method: 'delete',
2274
- auth: props.auth,
2275
- headers: props.headers,
2276
- data: undefined,
2277
- context: {}
2278
- });
2279
- });
2280
- });
2281
- });
2282
-
2283
- describe('.lookupSerialNumber', () => {
2284
- it('generates request', () => {
2285
- return api.lookupSerialNumber(props).then((results) => {
2286
- results.should.match({
2287
- uri: `/v1/serial_numbers/${props.serialNumber}`,
2288
- method: 'get',
2289
- auth: props.auth,
2290
- headers: props.headers,
2291
- query: undefined,
2292
- context: {}
2293
- });
2294
- });
2295
- });
2296
- });
2297
-
2298
- describe('.createMeshNetwork', () => {
2299
- it('generates request', () => {
2300
- return api.createMeshNetwork(props).then((results) => {
2301
- results.should.match({
2302
- uri: '/v1/networks',
2303
- method: 'post',
2304
- auth: props.auth,
2305
- headers: props.headers,
2306
- data: {
2307
- name: props.name,
2308
- device_id: props.deviceId,
2309
- iccid: props.iccid
2310
- },
2311
- context: {}
2312
- });
2313
- });
2314
- });
2315
- });
2316
-
2317
- describe('.removeMeshNetwork', () => {
2318
- it('generates request', () => {
2319
- return api.removeMeshNetwork(props).then((results) => {
2320
- results.should.match({
2321
- uri: `/v1/networks/${props.networkId}`,
2322
- method: 'delete',
2323
- auth: props.auth,
2324
- headers: props.headers,
2325
- data: undefined,
2326
- context: {}
2327
- });
2328
- });
2329
- });
2330
- });
2331
-
2332
- describe('.listMeshNetworks', () => {
2333
- it('generates request', () => {
2334
- return api.listMeshNetworks(props).then((results) => {
2335
- results.should.match({
2336
- method: 'get',
2337
- uri: '/v1/networks',
2338
- auth: props.auth,
2339
- headers: props.headers,
2340
- query: {
2341
- page: props.page,
2342
- per_page: props.perPage
2343
- },
2344
- context: {}
2345
- });
2346
- });
2347
- });
2348
- });
2349
-
2350
- describe('.getMeshNetwork', () => {
2351
- it('generates request', () => {
2352
- return api.getMeshNetwork(props).then((results) => {
2353
- results.should.match({
2354
- uri: `/v1/networks/${props.networkId}`,
2355
- method: 'get',
2356
- auth: props.auth,
2357
- headers: props.headers,
2358
- query: undefined,
2359
- context: {}
2360
- });
2361
- });
2362
- });
2363
- });
2364
-
2365
- describe('.updateMeshNetwork', () => {
2366
- it('generates request', () => {
2367
- const p = Object.assign(props, { action: 'test' });
2368
- return api.updateMeshNetwork(p).then((results) => {
2369
- results.should.match({
2370
- uri: `/v1/networks/${p.networkId}`,
2371
- method: 'put',
2372
- auth: p.auth,
2373
- headers: p.headers,
2374
- data: {
2375
- action: p.action,
2376
- device_id: p.deviceId
2377
- },
2378
- context: {}
2379
- });
2380
- });
2381
- });
2382
- });
2383
-
2384
- describe('.addMeshNetworkDevice', () => {
2385
- it('generates request', () => {
2386
- return api.addMeshNetworkDevice(props).then((results) => {
2387
- results.should.match({
2388
- method: 'put',
2389
- uri: `/v1/networks/${props.networkId}`,
2390
- auth: props.auth,
2391
- data: {
2392
- action: 'add-device',
2393
- device_id: props.deviceId
2394
- }
2395
- });
2396
- });
2397
- });
2398
- });
2399
-
2400
- describe('.removeMeshNetworkDevice', () => {
2401
- it('generates request', () => {
2402
- return api.removeMeshNetworkDevice(props).then((results) => {
2403
- results.should.match({
2404
- method: 'put',
2405
- uri: `/v1/networks/${props.networkId}`,
2406
- auth: props.auth,
2407
- data: {
2408
- action: 'remove-device',
2409
- device_id: props.deviceId
2410
- }
2411
- });
2412
- });
2413
- });
2414
- it('does not require the network ID argument', () => {
2415
- const p = Object.assign({}, props);
2416
- delete p.networkId;
2417
- return api.removeMeshNetworkDevice(p).then((results) => {
2418
- results.should.match({
2419
- method: 'delete',
2420
- uri: `/v1/devices/${props.deviceId}/network`,
2421
- auth: props.auth
2422
- });
2423
- });
2424
- });
2425
- });
2426
-
2427
- describe('.listMeshNetworkDevices', () => {
2428
- it('generates request', () => {
2429
- const p = Object.assign({ role: 'node' }, props);
2430
- return api.listMeshNetworkDevices(p).then((results) => {
2431
- results.should.match({
2432
- method: 'get',
2433
- uri: `/v1/networks/${props.networkId}/devices`,
2434
- auth: p.auth,
2435
- query: {
2436
- role: p.role,
2437
- page: p.page,
2438
- per_page: p.perPage
2439
- }
2440
- });
2441
- });
2442
- });
2443
- });
2444
-
2445
- describe('.getProductConfiguration', () => {
2446
- it('generates request', () => {
2447
- return api.getProductConfiguration(propsWithProduct).then((results) => {
2448
- results.should.match({
2449
- method: 'get',
2450
- uri: `/v1/products/${product}/config`,
2451
- auth: props.auth
2452
- });
2453
- });
2454
- });
2455
- });
2456
-
2457
- describe('.getProductConfigurationSchema', () => {
2458
- it('generates request', () => {
2459
- return api.getProductConfigurationSchema(propsWithProduct).then((results) => {
2460
- results.should.match({
2461
- method: 'get',
2462
- uri: `/v1/products/${product}/config`,
2463
- auth: props.auth,
2464
- headers: { 'accept': 'application/schema+json' }
2465
- });
2466
- });
2467
- });
2468
- });
2469
-
2470
- describe('.getProductDeviceConfiguration', () => {
2471
- it('generates request', () => {
2472
- return api.getProductDeviceConfiguration(propsWithProduct).then((results) => {
2473
- results.should.match({
2474
- method: 'get',
2475
- uri: `/v1/products/${product}/config/${props.deviceId}`,
2476
- auth: props.auth
2477
- });
2478
- });
2479
- });
2480
- });
2481
-
2482
- describe('.getProductDeviceConfigurationSchema', () => {
2483
- it('generates request', () => {
2484
- return api.getProductDeviceConfigurationSchema(propsWithProduct).then((results) => {
2485
- results.should.match({
2486
- method: 'get',
2487
- uri: `/v1/products/${product}/config/${props.deviceId}`,
2488
- auth: props.auth,
2489
- headers: { 'accept': 'application/schema+json' }
2490
- });
2491
- });
2492
- });
2493
- });
2494
-
2495
- describe('.setProductConfiguration', () => {
2496
- it('generates request', () => {
2497
- const p = Object.assign({ config: {
2498
- foo: 'bar'
2499
- } }, propsWithProduct);
2500
- return api.setProductConfiguration(p).then((results) => {
2501
- results.should.match({
2502
- method: 'put',
2503
- uri: `/v1/products/${product}/config`,
2504
- auth: props.auth,
2505
- data: {
2506
- foo: 'bar'
2507
- }
2508
- });
2509
- });
2510
- });
2511
- });
2512
-
2513
- describe('.setProductDeviceConfiguration', () => {
2514
- it('generates request', () => {
2515
- const p = Object.assign({ config: {
2516
- foo: 'bar'
2517
- } }, propsWithProduct);
2518
- return api.setProductDeviceConfiguration(p).then((results) => {
2519
- results.should.match({
2520
- method: 'put',
2521
- uri: `/v1/products/${product}/config/${props.deviceId}`,
2522
- auth: props.auth,
2523
- data: {
2524
- foo: 'bar'
2525
- }
2526
- });
2527
- });
2528
- });
2529
- });
2530
-
2531
- describe('.getProductLocations', () => {
2532
- it('generates request', () => {
2533
- return api.getProductLocations(propsWithProduct).then((results) => {
2534
- results.should.match({
2535
- method: 'get',
2536
- uri: `/v1/products/${product}/locations`,
2537
- auth: props.auth,
2538
- query: {
2539
- date_range: props.dateRange,
2540
- rect_bl: props.rectBl,
2541
- rect_tr: props.rectTr,
2542
- device_id: props.deviceId,
2543
- device_name: props.deviceName,
2544
- groups: props.groups,
2545
- page: props.page,
2546
- per_page: props.perPage
2547
- }
2548
- });
2549
- });
2550
- });
2551
- });
2552
-
2553
- describe('.getProductDeviceLocations', () => {
2554
- it('generates request', () => {
2555
- return api.getProductDeviceLocations(propsWithProduct).then((results) => {
2556
- results.should.match({
2557
- method: 'get',
2558
- uri: `/v1/products/${product}/locations/${props.deviceId}`,
2559
- auth: props.auth,
2560
- query: {
2561
- date_range: props.dateRange,
2562
- rect_bl: props.rectBl,
2563
- rect_tr: props.rectTr
2564
- }
2565
- });
2566
- });
2567
- });
2568
- });
2569
-
2570
- describe('.executeLogic', () => {
2571
- it('generates org request', () => {
2572
- return api.executeLogic(propsWithOrg).then((results) => {
2573
- results.should.match({
2574
- method: 'post',
2575
- uri: `/v1/orgs/${org}/logic/execute`,
2576
- auth: props.auth,
2577
- data: props.logic
2578
- });
2579
- });
2580
- });
2581
-
2582
- it('generates user request', () => {
2583
- return api.executeLogic(props).then((results) => {
2584
- results.should.match({
2585
- method: 'post',
2586
- uri: '/v1/logic/execute',
2587
- auth: props.auth,
2588
- data: props.logic
2589
- });
2590
- });
2591
- });
2592
- });
2593
-
2594
- describe('.createLogicFunction', () => {
2595
- it('generates request', () => {
2596
- return api.createLogicFunction(propsWithOrg).then((results) => {
2597
- results.should.match({
2598
- method: 'post',
2599
- uri: `/v1/orgs/${org}/logic/functions`,
2600
- auth: props.auth,
2601
- data: {
2602
- logic_function: props.logicFunction
2603
- }
2604
- });
2605
- });
2606
- });
2607
- });
2608
-
2609
- describe('.getLogicFunction', () => {
2610
- it('generates request', () => {
2611
- return api.getLogicFunction(propsWithOrg).then((results) => {
2612
- results.should.match({
2613
- method: 'get',
2614
- uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}`,
2615
- auth: props.auth
2616
- });
2617
- });
2618
- });
2619
- });
2620
-
2621
- describe('.updateLogicFunction', () => {
2622
- it('generates request', () => {
2623
- return api.updateLogicFunction(propsWithOrg).then((results) => {
2624
- results.should.match({
2625
- method: 'put',
2626
- uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}`,
2627
- auth: props.auth,
2628
- data: {
2629
- logic_function: props.logicFunction
2630
- }
2631
- });
2632
- });
2633
- });
2634
- });
2635
-
2636
- describe('.deleteLogicFunction', () => {
2637
- it('generates request', () => {
2638
- return api.deleteLogicFunction(propsWithOrg).then((results) => {
2639
- results.should.match({
2640
- method: 'delete',
2641
- uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}`,
2642
- auth: props.auth
2643
- });
2644
- });
2645
- });
2646
- });
2647
-
2648
- describe('.listLogicFunctions', () => {
2649
- it('generates request', () => {
2650
- return api.listLogicFunctions(propsWithOrg).then((results) => {
2651
- results.should.match({
2652
- method: 'get',
2653
- uri: `/v1/orgs/${org}/logic/functions`,
2654
- auth: props.auth
2655
- });
2656
- });
2657
- });
2658
- });
2659
-
2660
- describe('.listLogicRuns', () => {
2661
- it('generates request', () => {
2662
- return api.listLogicRuns(propsWithOrg).then((results) => {
2663
- results.should.match({
2664
- method: 'get',
2665
- uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}/runs`,
2666
- auth: props.auth,
2667
- });
2668
- });
2669
- });
2670
- });
2671
-
2672
- describe('.getLogicRun', () => {
2673
- it('generates request', () => {
2674
- return api.getLogicRun(propsWithOrg).then((results) => {
2675
- results.should.match({
2676
- method: 'get',
2677
- uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}/runs/${props.logicRunId}`,
2678
- auth: props.auth,
2679
- });
2680
- });
2681
- });
2682
- });
2683
-
2684
- describe('.getLogicRunLogs', () => {
2685
- it('generates request', () => {
2686
- return api.getLogicRunLogs(propsWithOrg).then((results) => {
2687
- results.should.match({
2688
- method: 'get',
2689
- uri: `/v1/orgs/${org}/logic/functions/${props.logicFunctionId}/runs/${props.logicRunId}/logs`,
2690
- auth: props.auth,
2691
- });
2692
- });
2693
- });
2694
- });
2695
-
2696
- describe('.deleteUser', () => {
2697
- it('sends request to delete the current user', () => {
2698
- return api.deleteUser(props).then(result => {
2699
- result.should.match({
2700
- context: {},
2701
- data: { password: props.password },
2702
- method: 'delete',
2703
- uri: '/v1/user',
2704
- auth: props.auth
2705
- });
2706
- });
2707
- });
2708
- });
2709
-
2710
- describe('.createLedger', () => {
2711
- it('generates request', () => {
2712
- return api.createLedger(propsWithOrg).then((results) => {
2713
- results.should.match({
2714
- method: 'post',
2715
- uri: `/v1/orgs/${org}/ledgers`,
2716
- auth: props.auth,
2717
- data: {
2718
- ledger: props.ledger
2719
- }
2720
- });
2721
- });
2722
- });
2723
- });
2724
-
2725
- describe('.getLedger', () => {
2726
- it('generates request', () => {
2727
- return api.getLedger(propsWithOrg).then((results) => {
2728
- results.should.match({
2729
- method: 'get',
2730
- uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}`,
2731
- auth: props.auth
2732
- });
2733
- });
2734
- });
2735
- });
2736
-
2737
- describe('.updateLedger', () => {
2738
- it('generates request', () => {
2739
- return api.updateLedger(propsWithOrg).then((results) => {
2740
- results.should.match({
2741
- method: 'put',
2742
- uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}`,
2743
- auth: props.auth,
2744
- data: {
2745
- ledger: props.ledger
2746
- }
2747
- });
2748
- });
2749
- });
2750
- });
2751
-
2752
- describe('.archiveLedger', () => {
2753
- it('generates request', () => {
2754
- return api.archiveLedger(propsWithOrg).then((results) => {
2755
- results.should.match({
2756
- method: 'delete',
2757
- uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}`,
2758
- auth: props.auth
2759
- });
2760
- });
2761
- });
2762
- });
2763
-
2764
- describe('.listLedgers', () => {
2765
- it('generates request', () => {
2766
- return api.listLedgers(propsWithOrg).then((results) => {
2767
- results.should.match({
2768
- method: 'get',
2769
- uri: `/v1/orgs/${org}/ledgers`,
2770
- auth: props.auth
2771
- });
2772
- });
2773
- });
2774
- });
2775
-
2776
- describe('.getLedgerInstance', () => {
2777
- it('generates request', () => {
2778
- return api.getLedgerInstance(propsWithOrg).then((results) => {
2779
- results.should.match({
2780
- method: 'get',
2781
- uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}/instances/${props.scopeValue}`,
2782
- auth: props.auth
2783
- });
2784
- });
2785
- });
2786
- });
2787
-
2788
- describe('.setLedgerInstance', () => {
2789
- it('generates request', () => {
2790
- return api.setLedgerInstance(propsWithOrg).then((results) => {
2791
- results.should.match({
2792
- method: 'put',
2793
- uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}/instances/${props.scopeValue}`,
2794
- query: {
2795
- set_mode: props.setMode
2796
- },
2797
- auth: props.auth,
2798
- data: {
2799
- instance: props.instance
2800
- }
2801
- });
2802
- });
2803
- });
2804
- });
2805
-
2806
- describe('.deleteLedgerInstance', () => {
2807
- it('generates request', () => {
2808
- return api.deleteLedgerInstance(propsWithOrg).then((results) => {
2809
- results.should.match({
2810
- method: 'delete',
2811
- uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}/instances/${props.scopeValue}`,
2812
- auth: props.auth
2813
- });
2814
- });
2815
- });
2816
- });
2817
-
2818
- describe('.listLedgerInstances', () => {
2819
- it('generates request', () => {
2820
- return api.listLedgerInstances(propsWithOrg).then((results) => {
2821
- results.should.match({
2822
- method: 'get',
2823
- uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}/instances`,
2824
- auth: props.auth
2825
- });
2826
- });
2827
- });
2828
- });
2829
-
2830
- describe('listLedgerInstanceVersions', () => {
2831
- it('generates request', () => {
2832
- return api.listLedgerInstanceVersions(propsWithOrg).then((results) => {
2833
- results.should.match({
2834
- method: 'get',
2835
- uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}/instances/${props.scopeValue}/versions`,
2836
- auth: props.auth
2837
- });
2838
- });
2839
- });
2840
- });
2841
-
2842
- describe('.getLedgerInstanceVersion', () => {
2843
- it('generates request', () => {
2844
- return api.getLedgerInstanceVersion(propsWithOrg).then((results) => {
2845
- results.should.match({
2846
- method: 'get',
2847
- uri: `/v1/orgs/${org}/ledgers/${props.ledgerName}/instances/${props.scopeValue}/versions/${props.version}`,
2848
- auth: props.auth
2849
- });
2850
- });
2851
- });
2852
- });
2853
-
2854
- describe('.listDeviceOsVersions', () => {
2855
- it('generates request without optional parameters', () => {
2856
- return api.listDeviceOsVersions({ auth: props.auth }).then((results) => {
2857
- results.should.match({
2858
- method: 'get',
2859
- uri: '/v1/device-os/versions',
2860
- auth: props.auth,
2861
- query: {}
2862
- });
2863
- });
2864
- });
2865
-
2866
- it('generates request with all optional parameters', () => {
2867
- const options = {
2868
- auth: props.auth,
2869
- platformId: 6,
2870
- internalVersion: '1.2.3',
2871
- page: 2,
2872
- perPage: 25
2873
- };
2874
- return api.listDeviceOsVersions(options).then((results) => {
2875
- results.should.match({
2876
- method: 'get',
2877
- uri: '/v1/device-os/versions',
2878
- auth: props.auth,
2879
- query: {
2880
- platform_id: 6,
2881
- internal_version: '1.2.3',
2882
- page: 2,
2883
- per_page: 25
2884
- }
2885
- });
2886
- });
2887
- });
2888
- });
2889
-
2890
- describe('.getDeviceOsVersion', () => {
2891
- it('generates request without optional parameters', () => {
2892
- const options = {
2893
- auth: props.auth,
2894
- version: '1.2.3'
2895
- };
2896
- return api.getDeviceOsVersion(options).then((results) => {
2897
- results.should.match({
2898
- method: 'get',
2899
- uri: '/v1/device-os/versions/1.2.3',
2900
- auth: props.auth,
2901
- query: {}
2902
- });
2903
- });
2904
- });
2905
-
2906
- it('generates request with platformId parameter', () => {
2907
- const options = {
2908
- auth: props.auth,
2909
- version: '1.2.3',
2910
- platformId: 6
2911
- };
2912
- return api.getDeviceOsVersion(options).then((results) => {
2913
- results.should.match({
2914
- method: 'get',
2915
- uri: '/v1/device-os/versions/1.2.3',
2916
- auth: props.auth,
2917
- query: {
2918
- platform_id: 6
2919
- }
2920
- });
2921
- });
2922
- });
2923
- });
2924
-
2925
- describe('.unprotectDevice', () => {
2926
- it('generates request', () => {
2927
- return api.unprotectDevice(Object.assign({}, propsWithProduct, {
2928
- action: 'action',
2929
- deviceNonce: 'device-nonce',
2930
- serverNonce: 'server-nonce',
2931
- deviceSignature: 'device-signature',
2932
- devicePublicKeyFingerprint: 'device-public-key-fingerprint'
2933
- })).then((results) => {
2934
- results.should.match({
2935
- method: 'put',
2936
- uri: `/v1/products/${product}/devices/${props.deviceId}/unprotect`,
2937
- auth: props.auth,
2938
- headers: props.headers,
2939
- data: {
2940
- action: 'action',
2941
- device_nonce: 'device-nonce',
2942
- server_nonce: 'server-nonce',
2943
- device_signature: 'device-signature',
2944
- device_public_key_fingerprint: 'device-public-key-fingerprint'
2945
- }
2946
- });
2947
- });
2948
- });
2949
- });
2950
- });
2951
-
2952
- describe('.deviceUri', () => {
2953
- describe('user scope', () => {
2954
- it('gets the user device uri', () => {
2955
- const uri = api.deviceUri({ deviceId: 'abc' });
2956
- uri.should.equal('/v1/devices/abc');
2957
- });
2958
- });
2959
- describe('product scope', () => {
2960
- it('gets the product device uri', () => {
2961
- const uri = api.deviceUri({ deviceId: 'abc', product: 'xyz' });
2962
- uri.should.equal('/v1/products/xyz/devices/abc');
2963
- });
2964
- });
2965
- describe('org scope', () => {
2966
- it('gets the org device uri', () => {
2967
- const uri = api.deviceUri({ deviceId: 'abc', org: 'xyz' });
2968
- uri.should.equal('/v1/orgs/xyz/devices/abc');
2969
- });
2970
- });
2971
- });
2972
-
2973
-
2974
- describe('.client', () => {
2975
- it('creates a client', (done) => {
2976
- api.client().should.be.instanceOf(Client);
2977
- done();
2978
- });
2979
- it('passes the api', (done) => {
2980
- api.client().api.should.equal(api);
2981
- done();
2982
- });
2983
- });
2984
-
2985
- describe('context', () => {
2986
- describe('_isValidContext', () => {
2987
- it('does not have context items set after default construction', () => {
2988
- const api = new Particle();
2989
- api.should.have.property('context').eql({});
2990
- });
2991
-
2992
- it('is valid for known types and non-empty object', () => {
2993
- api._isValidContext('tool', { abc:'123' }).should.be.ok;
2994
- api._isValidContext('project', { abc:'123' }).should.be.ok;
2995
- });
2996
-
2997
- it('is not valid for unknown types and non-empty object', () => {
2998
- api._isValidContext('tool1', { abc:'123' }).should.not.be.ok;
2999
- api._isValidContext('project1', { abc:'123' }).should.not.be.ok;
3000
- });
3001
-
3002
- it('is not valid for known types and falsey object', () => {
3003
- api._isValidContext('tool', {}).should.not.be.ok;
3004
- api._isValidContext('tool', 0).should.not.be.ok;
3005
- api._isValidContext('tool', null).should.not.be.ok;
3006
- api._isValidContext('tool').should.not.be.ok;
3007
- });
3008
-
3009
- it('sets a valid context', () => {
3010
- api.setContext('tool', { name:'spanner' });
3011
- api.context.should.have.property('tool').property('name').equal('spanner');
3012
- });
3013
- });
3014
-
3015
- describe('_buildContext', () => {
3016
- it('uses the api context when no context provided', () => {
3017
- const tool = { name:'spanner' };
3018
- api.setContext('tool', tool);
3019
- api._buildContext().should.eql({ tool });
3020
- });
3021
-
3022
- it('overrides the api context completely for a given context item', () => {
3023
- const tool = { name:'spanner', version:'1.2.3' };
3024
- api.setContext('tool', tool);
3025
- const newTool = { name:'pliers' };
3026
- api._buildContext({ tool:newTool }).should.eql({ tool:newTool });
3027
- });
3028
- });
3029
-
3030
- describe('agent forwarders', () => {
3031
- let uri, auth, headers, query, data, context, contextResult, result;
3032
-
3033
- beforeEach(() => {
3034
- uri = 'http://example.com/v1';
3035
- auth = 'fake-token';
3036
- headers = { 'X-FOO': 'foo', 'X-BAR': 'bar' };
3037
- query = 'foo=1&bar=2';
3038
- data = { foo: true, bar: false };
3039
- context = { abc: 123 };
3040
- contextResult = { def: 456 };
3041
- result = 'fake-result';
3042
- api._buildContext = sinon.stub().returns(contextResult);
3043
- api._getActiveAuthToken = sinon.stub().returns(auth);
3044
- });
3045
-
3046
- afterEach(() => {
3047
- expect(api._buildContext).to.have.been.calledWith(context);
3048
- expect(api._getActiveAuthToken).to.have.been.calledWith(auth);
3049
- });
3050
-
3051
- it('calls _buildContext and _getActiveAuthToken from get', () => {
3052
- api.agent.get = sinon.stub().returns(result);
3053
- const options = { uri, auth, headers, query, context };
3054
- const res = api.get(options);
3055
- expect(res).to.equal(result);
3056
- expect(api.agent.get).to.have.been.calledWith({
3057
- uri,
3058
- auth,
3059
- headers,
3060
- query,
3061
- context: contextResult
3062
- });
3063
- });
3064
-
3065
- it('calls _buildContext and _getActiveAuthToken from head', () => {
3066
- api.agent.head = sinon.stub().returns(result);
3067
- const options = { uri, auth, headers, query, context };
3068
- const res = api.head(options);
3069
- expect(res).to.equal(result);
3070
- expect(api.agent.head).to.have.been.calledWith({
3071
- uri,
3072
- auth,
3073
- headers,
3074
- query,
3075
- context: contextResult
3076
- });
3077
- });
3078
-
3079
- it('calls _buildContext and _getActiveAuthToken from post', () => {
3080
- api.agent.post = sinon.stub().returns(result);
3081
- const options = { uri, auth, headers, data, context };
3082
- const res = api.post(options);
3083
- expect(res).to.equal(result);
3084
- expect(api.agent.post).to.have.been.calledWith({
3085
- uri,
3086
- auth,
3087
- headers,
3088
- data,
3089
- context: contextResult
3090
- });
3091
- });
3092
-
3093
- it('calls _buildContext and _getActiveAuthToken from put', () => {
3094
- api.agent.put = sinon.stub().returns(result);
3095
- const options = { uri, auth, headers, data, context, query };
3096
- const res = api.put(options);
3097
- expect(res).to.equal(result);
3098
- expect(api.agent.put).to.have.been.calledWith({
3099
- uri,
3100
- auth,
3101
- headers,
3102
- data,
3103
- query,
3104
- context: contextResult
3105
- });
3106
- });
3107
-
3108
- it('calls _buildContext and _getActiveAuthToken from delete', () => {
3109
- api.agent.delete = sinon.stub().returns(result);
3110
- const options = { uri, auth, headers, data, context };
3111
- const res = api.delete(options);
3112
- expect(res).to.equal(result);
3113
- expect(api.agent.delete).to.have.been.calledWith({
3114
- uri,
3115
- auth,
3116
- headers,
3117
- data,
3118
- context: contextResult
3119
- });
3120
- });
3121
-
3122
- it('calls _buildContext and _getActiveAuthToken from request', () => {
3123
- api.agent.request = sinon.stub().returns(result);
3124
- api.request({ context, auth }).should.eql(result);
3125
- expect(api.agent.request).to.have.been.calledWith({ context:contextResult, auth });
3126
- });
3127
- });
3128
- });
3129
-
3130
- describe('setBaseUrl(baseUrl)', () => {
3131
- afterEach(() => {
3132
- sinon.restore();
3133
- });
3134
-
3135
- it('sets baseUrl instance property', () => {
3136
- const baseUrl = 'foo';
3137
- api.setBaseUrl(baseUrl);
3138
- expect(api.baseUrl).to.eql(baseUrl);
3139
- });
3140
-
3141
- it('calls agent.setBaseUrl', () => {
3142
- const baseUrl = 'foo';
3143
- sinon.stub(api.agent, 'setBaseUrl');
3144
- api.setBaseUrl(baseUrl);
3145
- expect(api.agent.setBaseUrl).to.have.property('callCount', 1);
3146
- expect(api.agent.setBaseUrl.firstCall.args).to.have.lengthOf(1);
3147
- expect(api.agent.setBaseUrl.firstCall.args[0]).to.eql(baseUrl);
3148
- });
3149
- });
3150
-
3151
- describe('setDefaultAuth(auth)', () => {
3152
- afterEach(() => {
3153
- sinon.restore();
3154
- });
3155
-
3156
- it('sets ._defaultAuth', () => {
3157
- const auth = 'foo';
3158
- api.setDefaultAuth(auth);
3159
- expect(api._defaultAuth).to.eql(auth);
3160
- });
3161
-
3162
- it('throws error unless given a non-empty string', () => {
3163
- let error;
3164
- try {
3165
- api.setDefaultAuth(undefined);
3166
- } catch (e) {
3167
- error = e;
3168
- }
3169
- expect(error).to.be.an.instanceOf(Error);
3170
- expect(error.message).to.eql('Must pass a non-empty string representing an auth token!');
3171
- });
3172
- });
3173
-
3174
- describe('_getActiveAuthToken(auth)', () => {
3175
- afterEach(() => {
3176
- sinon.restore();
3177
- });
3178
-
3179
- it('returns provided value when provided value is truthy', () => {
3180
- const expectedReturnValue = 'pass through';
3181
- expect(api._getActiveAuthToken(expectedReturnValue)).to.eql(expectedReturnValue);
3182
- });
3183
-
3184
- it('returns value of _defaultAuth when provided value is NOT truthy', () => {
3185
- const providedValue = undefined;
3186
- const expectedReturnValue = 'default auth value';
3187
- api.setDefaultAuth(expectedReturnValue);
3188
- expect(api._getActiveAuthToken(providedValue)).to.eql(expectedReturnValue);
3189
- });
3190
-
3191
- it('returns undefined when both provided value and _defaultAuth are NOT truthy', () => {
3192
- const providedValue = undefined;
3193
- const expectedReturnValue = undefined;
3194
- api._defaultAuth = undefined;
3195
- expect(api._getActiveAuthToken(providedValue)).to.eql(expectedReturnValue);
3196
- });
3197
- });
3198
- });