@prisme.ai/sdk 0.0.2 → 1.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.
package/lib/api.test.ts CHANGED
@@ -1,62 +1,89 @@
1
1
  import api, { Api } from './api';
2
+ import ApiError from './ApiError';
3
+ import UsersEndpoint from './endpoints/users';
4
+ import WorkspacesEndpoint from './endpoints/workspaces';
5
+ import WorkspacesVersionsEndpoint from './endpoints/workspacesVersions';
6
+
7
+ jest.mock('./events', () => {
8
+ class Events {
9
+ static mockedConstructor = jest.fn();
10
+ static mockedClose = jest.fn();
11
+ constructor({ api, ...args }: any) {
12
+ // @ts-ignore
13
+ this.api = api;
14
+ Events.mockedConstructor({ api, ...args });
15
+ }
16
+
17
+ once = jest.fn((event: string, cb: Function) => {
18
+ // @ts-ignore
19
+ const shouldFail = this.api._shouldFail;
20
+ if (!shouldFail && event === 'connect') cb();
21
+ if (shouldFail && event !== 'connect') cb();
22
+ return () => null;
23
+ });
24
+ close = Events.mockedClose;
25
+ }
26
+ return { Events };
27
+ });
2
28
 
3
29
  it('should export an instance', () => {
4
30
  expect(api).toBeInstanceOf(Api);
5
31
  });
6
32
 
7
- it('should call /me', () => {
8
- const api = new Api('/fake/');
9
- api.get = jest.fn();
10
- api.me();
33
+ it('should call /me', async () => {
34
+ const api = new Api({ host: '/fake/' });
35
+ api.get = jest.fn(
36
+ async () =>
37
+ ({
38
+ sessionId: 'session',
39
+ } as any)
40
+ );
41
+ const me = await api.me();
11
42
  expect(api.get).toHaveBeenCalledWith('/me');
43
+ expect(api.user).toBe(me);
12
44
  });
13
45
 
14
- it('should call /signin', () => {
15
- const api = new Api('/fake/');
46
+ it('should call /login/anonymous', async () => {
47
+ const api = new Api({ host: '/fake/' });
16
48
  api.post = jest.fn();
17
- api.signin('user@fake.com', 'password');
18
- expect(api.post).toHaveBeenCalledWith('/login', {
19
- email: 'user@fake.com',
20
- password: 'password',
21
- });
49
+ const user = await api.createAnonymousSession();
50
+ expect(api.post).toHaveBeenCalledWith(
51
+ '/login/anonymous',
52
+ {},
53
+ { credentials: 'omit' }
54
+ );
55
+ expect(api.user).toBe(user);
22
56
  });
23
57
 
24
58
  it('should call /signup', () => {
25
- const api = new Api('/fake/');
59
+ const api = new Api({ host: '/fake/' });
26
60
  api.post = jest.fn();
27
- api.signup('user@fake.com', 'password', 'firstname', 'lastname');
61
+ api.signup('user@fake.com', 'password', 'firstname', 'lastname', 'fr');
28
62
  expect(api.post).toHaveBeenCalledWith('/signup', {
29
63
  email: 'user@fake.com',
30
64
  password: 'password',
31
65
  firstName: 'firstname',
32
66
  lastName: 'lastname',
67
+ language: 'fr',
33
68
  });
34
69
  });
35
70
 
36
- it('should call /signout', () => {
37
- const api = new Api('/fake/');
38
- api.post = jest.fn();
39
- api.signout();
40
- expect(api.post).toHaveBeenCalledWith('/logout');
41
- expect(api.token).toBeNull();
42
- });
43
-
44
71
  it('should call get /workspaces', () => {
45
- const api = new Api('/fake/');
72
+ const api = new Api({ host: '/fake/' });
46
73
  api.get = jest.fn();
47
74
  api.getWorkspaces();
48
- expect(api.get).toHaveBeenCalledWith('/workspaces?limit=300');
75
+ expect(api.get).toHaveBeenCalledWith('/workspaces?limit=600');
49
76
  });
50
77
 
51
78
  it('should call get /workspaces/42', () => {
52
- const api = new Api('/fake/');
79
+ const api = new Api({ host: '/fake/' });
53
80
  api.get = jest.fn();
54
81
  api.getWorkspace('42');
55
82
  expect(api.get).toHaveBeenCalledWith('/workspaces/42');
56
83
  });
57
84
 
58
85
  it('should call post /workspaces', () => {
59
- const api = new Api('/fake/');
86
+ const api = new Api({ host: '/fake/' });
60
87
  api.post = jest.fn();
61
88
  api.createWorkspace('foo');
62
89
  expect(api.post).toHaveBeenCalledWith('/workspaces', {
@@ -65,7 +92,7 @@ it('should call post /workspaces', () => {
65
92
  });
66
93
 
67
94
  it('should call patch /workspaces/42', async () => {
68
- const api = new Api('/fake/');
95
+ const api = new Api({ host: '/fake/' });
69
96
  api.patch = jest.fn();
70
97
  await api.updateWorkspace({
71
98
  id: '42',
@@ -83,22 +110,20 @@ it('should call patch /workspaces/42', async () => {
83
110
  });
84
111
  });
85
112
 
113
+ it('should call delete /workspaces/42', async () => {
114
+ const api = new Api({ host: '/fake/' });
115
+ api.delete = jest.fn();
116
+ await api.deleteWorkspace('42');
117
+ expect(api.delete).toHaveBeenCalledWith('/workspaces/42');
118
+ });
119
+
86
120
  it('should call post /workspaces/42/automations', () => {
87
- const api = new Api('/fake/');
121
+ const api = new Api({ host: '/fake/' });
88
122
  api.post = jest.fn();
89
- api.createAutomation(
90
- {
91
- id: '42',
92
- name: 'foo',
93
- automations: {},
94
- createdAt: '',
95
- updatedAt: '',
96
- },
97
- {
98
- name: 'foo',
99
- do: [],
100
- }
101
- );
123
+ api.createAutomation('42', {
124
+ name: 'foo',
125
+ do: [],
126
+ });
102
127
  expect(api.post).toHaveBeenCalledWith('/workspaces/42/automations', {
103
128
  name: 'foo',
104
129
  do: [],
@@ -106,22 +131,12 @@ it('should call post /workspaces/42/automations', () => {
106
131
  });
107
132
 
108
133
  it('should call patch /workspaces/42/automations', async () => {
109
- const api = new Api('/fake/');
134
+ const api = new Api({ host: '/fake/' });
110
135
  api.patch = jest.fn();
111
- await api.updateAutomation(
112
- {
113
- id: '42',
114
- name: 'foo',
115
- automations: {},
116
- createdAt: '',
117
- updatedAt: '',
118
- },
119
- '42-1',
120
- {
121
- name: 'foo',
122
- do: [],
123
- }
124
- );
136
+ await api.updateAutomation('42', '42-1', {
137
+ name: 'foo',
138
+ do: [],
139
+ });
125
140
  expect(api.patch).toHaveBeenCalledWith('/workspaces/42/automations/42-1', {
126
141
  name: 'foo',
127
142
  do: [],
@@ -129,23 +144,14 @@ it('should call patch /workspaces/42/automations', async () => {
129
144
  });
130
145
 
131
146
  it('should call delete /workspaces/42/automations/42-1', () => {
132
- const api = new Api('/fake/');
147
+ const api = new Api({ host: '/fake/' });
133
148
  api.delete = jest.fn();
134
- api.deleteAutomation(
135
- {
136
- id: '42',
137
- name: 'foo',
138
- automations: {},
139
- createdAt: '',
140
- updatedAt: '',
141
- },
142
- '42-1'
143
- );
149
+ api.deleteAutomation('42', '42-1');
144
150
  expect(api.delete).toHaveBeenCalledWith('/workspaces/42/automations/42-1');
145
151
  });
146
152
 
147
153
  it('should call get /workspaces/42/events', async () => {
148
- const api = new Api('/fake/');
154
+ const api = new Api({ host: '/fake/' });
149
155
  api.get = jest.fn(
150
156
  async (): Promise<any> => ({
151
157
  result: {
@@ -167,7 +173,7 @@ it('should call get /workspaces/42/events', async () => {
167
173
  });
168
174
 
169
175
  it('should replace all images data', async () => {
170
- const api = new Api('/fake/');
176
+ const api = new Api({ host: '/fake/' });
171
177
  api.uploadFiles = jest.fn(async () => [
172
178
  { url: 'http://image1.jpg' } as any,
173
179
  { url: 'http://image2.jpg' } as any,
@@ -211,7 +217,7 @@ it('should replace all images data', async () => {
211
217
  });
212
218
 
213
219
  it('should upload file', async () => {
214
- const api = new Api('/fake/');
220
+ const api = new Api({ host: '/fake/' });
215
221
  // @ts-ignore
216
222
  api._fetch = jest.fn(() => [{}]);
217
223
  await api.uploadFiles(
@@ -229,3 +235,538 @@ it('should upload file', async () => {
229
235
  expect(body.getAll('file').length).toBe(1);
230
236
  expect((body.getAll('file')[0] as File).name).toBe('foo.jpg');
231
237
  });
238
+
239
+ it('should generate api key', async () => {
240
+ const api = new Api({ host: '/fake/' });
241
+ api.post = jest.fn((): any => ({
242
+ apiKey: 'api-key',
243
+ }));
244
+ const apiKey = await api.generateApiKey('42', ['event1', 'event2']);
245
+ expect(apiKey).toBe('api-key');
246
+ expect(api.post).toHaveBeenCalledWith('/workspaces/42/apiKeys', {
247
+ rules: {
248
+ events: {
249
+ types: ['event1', 'event2'],
250
+ filters: {
251
+ 'source.sessionId': '${user.sessionId}',
252
+ },
253
+ },
254
+ uploads: undefined,
255
+ },
256
+ });
257
+ (api.post as jest.Mock).mockClear();
258
+
259
+ await api.generateApiKey('42', ['event1', 'event2'], ['images/*']);
260
+ expect(api.post).toHaveBeenCalledWith('/workspaces/42/apiKeys', {
261
+ rules: {
262
+ events: {
263
+ types: ['event1', 'event2'],
264
+ filters: {
265
+ 'source.sessionId': '${user.sessionId}',
266
+ },
267
+ },
268
+ uploads: {
269
+ mimetypes: ['images/*'],
270
+ },
271
+ },
272
+ });
273
+ });
274
+
275
+ it('should update api key', async () => {
276
+ const api = new Api({ host: '/fake/' });
277
+ api.put = jest.fn((): any => ({
278
+ apiKey: 'api-key',
279
+ }));
280
+ const apiKey = await api.updateApiKey('42', 'api-key', [
281
+ 'event1',
282
+ 'event2',
283
+ 'event3',
284
+ ]);
285
+ expect(apiKey).toBe('api-key');
286
+ expect(api.put).toHaveBeenCalledWith('/workspaces/42/apiKeys/api-key', {
287
+ rules: {
288
+ events: {
289
+ types: ['event1', 'event2', 'event3'],
290
+ filters: {
291
+ 'source.sessionId': '${user.sessionId}',
292
+ },
293
+ uploads: undefined,
294
+ },
295
+ },
296
+ });
297
+
298
+ (api.put as jest.Mock).mockClear();
299
+
300
+ await api.updateApiKey('42', 'api-key', ['event1', 'event2'], ['images/*']);
301
+ expect(api.put).toHaveBeenCalledWith('/workspaces/42/apiKeys/api-key', {
302
+ rules: {
303
+ events: {
304
+ types: ['event1', 'event2'],
305
+ filters: {
306
+ 'source.sessionId': '${user.sessionId}',
307
+ },
308
+ },
309
+ uploads: {
310
+ mimetypes: ['images/*'],
311
+ },
312
+ },
313
+ });
314
+ });
315
+
316
+ it('should send validation mail', () => {
317
+ const api = new Api({ host: '/fake/' });
318
+ api.post = jest.fn();
319
+ api.sendValidationMail('email', 'fr');
320
+ expect(api.post).toHaveBeenCalledWith('/user/validate', {
321
+ email: 'email',
322
+ language: 'fr',
323
+ });
324
+ });
325
+
326
+ it('should validate mail', () => {
327
+ const api = new Api({ host: '/fake/' });
328
+ api.post = jest.fn();
329
+ api.validateMail('token');
330
+ expect(api.post).toHaveBeenCalledWith('/user/validate', {
331
+ token: 'token',
332
+ });
333
+ });
334
+
335
+ it('should send password reset mail', () => {
336
+ const api = new Api({ host: '/fake/' });
337
+ api.post = jest.fn();
338
+ api.sendPasswordResetMail('email', 'fr');
339
+ expect(api.post).toHaveBeenCalledWith('/user/password', {
340
+ email: 'email',
341
+ language: 'fr',
342
+ });
343
+ });
344
+
345
+ it('should reset password', () => {
346
+ const api = new Api({ host: '/fake/' });
347
+ api.post = jest.fn();
348
+ api.passwordReset('token', 'azerty');
349
+ expect(api.post).toHaveBeenCalledWith('/user/password', {
350
+ token: 'token',
351
+ password: 'azerty',
352
+ });
353
+ });
354
+
355
+ it('should get pages', async () => {
356
+ const api = new Api({ host: '/fake/' });
357
+ api.get = jest.fn((): any => [
358
+ {
359
+ id: '123',
360
+ createdAt: 'must be removed',
361
+ createdBy: 'must be removed',
362
+ updatedAt: 'must be removed',
363
+ updatedBy: 'must be removed',
364
+ },
365
+ ]);
366
+ const pages = await api.getPages('42');
367
+ expect(api.get).toHaveBeenCalledWith('/workspaces/42/pages');
368
+ expect(pages).toEqual([{ id: '123' }]);
369
+
370
+ api.get = jest.fn((): any => {
371
+ throw new Error();
372
+ });
373
+ const empty = await api.getPages('42');
374
+ expect(empty).toEqual([]);
375
+ });
376
+
377
+ it('should get page', () => {
378
+ const api = new Api({ host: '/fake/' });
379
+ api.get = jest.fn((): any => ({}));
380
+ api.getPage('42', '123');
381
+ expect(api.get).toHaveBeenCalledWith('/workspaces/42/pages/123');
382
+ });
383
+
384
+ it('should get page by slug', () => {
385
+ const api = new Api({ host: '/fake/' });
386
+ api.get = jest.fn((): any => ({}));
387
+ api.getPageBySlug('42', '123');
388
+ expect(api.get).toHaveBeenCalledWith('/pages/42/123');
389
+ });
390
+
391
+ it('should create page', () => {
392
+ const api = new Api({ host: '/fake/' });
393
+ api.post = jest.fn((): any => ({}));
394
+ api.createPage('42', {} as Prismeai.Page);
395
+ expect(api.post).toHaveBeenCalledWith(`/workspaces/42/pages`, {});
396
+ });
397
+
398
+ it('should update page', async () => {
399
+ const api = new Api({ host: '/fake/' });
400
+ api.patch = jest.fn((): any => ({ slug: 'my-page' }));
401
+ await api.updatePage('42', { id: '123', slug: 'my-page' } as Prismeai.Page);
402
+ expect(api.patch).toHaveBeenCalledWith(`/workspaces/42/pages/my-page`, {
403
+ id: '123',
404
+ slug: 'my-page',
405
+ });
406
+ });
407
+
408
+ it('should delete page', () => {
409
+ const api = new Api({ host: '/fake/' });
410
+ api.delete = jest.fn((): any => ({ id: '123' }));
411
+ api.deletePage('42', '123');
412
+ expect(api.delete).toHaveBeenCalledWith('/workspaces/42/pages/123');
413
+ });
414
+
415
+ it('should stream events', async () => {
416
+ const api = new Api({ host: '/fake/' });
417
+
418
+ const events = await api.streamEvents('42');
419
+ expect((events.constructor as any).mockedConstructor).toHaveBeenCalledWith({
420
+ workspaceId: '42',
421
+ token: '',
422
+ apiKey: undefined,
423
+ apiHost: '/fake/',
424
+ filters: undefined,
425
+ api,
426
+ legacyToken: '',
427
+ });
428
+ expect(events.once).toHaveBeenCalled();
429
+ (events.constructor as any).mockedConstructor.mockClear();
430
+
431
+ const events2 = await api.streamEvents('42', { 'source.sessionId': true });
432
+ expect((events2.constructor as any).mockedConstructor).toHaveBeenCalledWith({
433
+ workspaceId: '42',
434
+ token: '',
435
+ apiKey: undefined,
436
+ apiHost: '/fake/',
437
+ filters: {},
438
+ api,
439
+ legacyToken: '',
440
+ });
441
+ expect(events2.once).toHaveBeenCalled();
442
+
443
+ // @ts-ignore
444
+ api.sessionId = 'session id';
445
+ // @ts-ignore
446
+ api._apiKey = 'api key';
447
+ const events3 = await api.streamEvents('42', { 'source.sessionId': true });
448
+ expect((events2.constructor as any).mockedConstructor).toHaveBeenCalledWith({
449
+ workspaceId: '42',
450
+ token: '',
451
+ apiKey: 'api key',
452
+ apiHost: '/fake/',
453
+ filters: {
454
+ 'source.sessionId': 'session id',
455
+ },
456
+ api,
457
+ legacyToken: '',
458
+ });
459
+ expect(events3.once).toHaveBeenCalled();
460
+
461
+ // @ts-ignore
462
+ api._shouldFail = true;
463
+ try {
464
+ await api.streamEvents('42');
465
+ } catch (e) {}
466
+ expect((events3.constructor as any).mockedConstructor).toHaveBeenCalledWith({
467
+ workspaceId: '42',
468
+ token: '',
469
+ apiKey: 'api key',
470
+ apiHost: '/fake/',
471
+ filters: undefined,
472
+ api,
473
+ legacyToken: '',
474
+ });
475
+ expect(events3.close).toHaveBeenCalled();
476
+ });
477
+
478
+ it('should get events', async () => {
479
+ const api = new Api({ host: '/fake/' });
480
+ api.get = jest.fn((): any => ({
481
+ result: { events: [{ createdAt: '2022-01-01', id: '123' }] },
482
+ }));
483
+ api.getEvents('42');
484
+ expect(api.get).toHaveBeenCalledWith('/workspaces/42/events');
485
+
486
+ const events = await api.getEvents('42', { foo: 'bar' });
487
+ expect(api.get).toHaveBeenCalledWith('/workspaces/42/events?foo=bar');
488
+ expect(events).toEqual([{ id: '123', createdAt: new Date('2022-01-01') }]);
489
+
490
+ api.get = jest.fn((): any => {
491
+ throw new Error();
492
+ });
493
+ const empty = await api.getEvents('42', { foo: 'bar' });
494
+ expect(empty).toEqual([]);
495
+ });
496
+
497
+ it('should post events', async () => {
498
+ const api = new Api({ host: '/fake/' });
499
+ api.post = jest.fn((): any => {});
500
+ const res = await api.postEvents('42', [
501
+ {
502
+ type: 'foo',
503
+ payload: {},
504
+ },
505
+ ]);
506
+ expect(api.post).toHaveBeenCalledWith('/workspaces/42/events', {
507
+ events: [
508
+ {
509
+ type: 'foo',
510
+ payload: {},
511
+ },
512
+ ],
513
+ });
514
+ expect(res).toBe(true);
515
+
516
+ api.post = jest.fn((): any => {
517
+ throw new Error();
518
+ });
519
+ const res2 = await api.postEvents('42', [
520
+ {
521
+ type: 'foo',
522
+ payload: {},
523
+ },
524
+ ]);
525
+ expect(res2).toBe(false);
526
+ });
527
+
528
+ describe('permissions', () => {
529
+ const api = new Api({ host: '/fake/' });
530
+ api.get = jest.fn((path: string): any => {
531
+ if (path === '/pages/id/permissions') {
532
+ return {
533
+ result: [],
534
+ };
535
+ }
536
+ });
537
+ api.post = jest.fn((path: string, body: any): any => {
538
+ if (path === '/pages/id/permissions') {
539
+ return body;
540
+ }
541
+ });
542
+ api.findContacts = jest.fn(({ email }): any => {
543
+ if (email === 'foo@bar.com')
544
+ return {
545
+ contacts: [
546
+ {
547
+ id: '1234',
548
+ },
549
+ ],
550
+ };
551
+ return { contacts: [] };
552
+ });
553
+ api.getPermissions('pages', 'id');
554
+
555
+ it('should get permissions', async () => {
556
+ expect(api.get).toHaveBeenCalledWith('/pages/id/permissions');
557
+ });
558
+
559
+ it('should add permissions', async () => {
560
+ await api.addPermissions('pages', 'id', {
561
+ permissions: {},
562
+ target: { email: 'foo@bar.com' },
563
+ });
564
+ expect(api.post).toHaveBeenCalledWith('/pages/id/permissions', {
565
+ permissions: {},
566
+ target: {
567
+ id: '1234',
568
+ },
569
+ });
570
+ });
571
+
572
+ it('should fail to add permissions', async () => {
573
+ let expected: ApiError = {} as ApiError;
574
+ try {
575
+ await api.addPermissions('pages', 'id', {
576
+ permissions: {},
577
+ target: { email: 'foofoo@bar.com' },
578
+ });
579
+ } catch (e) {
580
+ console.log(e);
581
+ expected = e as ApiError;
582
+ }
583
+ expect(expected.message).toBe('This user does not exist');
584
+ });
585
+
586
+ it('should delete permissions', async () => {
587
+ api.delete = jest.fn((): any => {});
588
+ api.deletePermissions('pages', 'id', 'foo');
589
+ expect(api.delete).toHaveBeenCalledWith('/pages/id/permissions/foo');
590
+ });
591
+ });
592
+
593
+ it('should get apps', async () => {
594
+ const api = new Api({ host: '/fake/' });
595
+ const get: jest.Mock = (api.get = jest.fn((): any => {}));
596
+ api.getApps({});
597
+ expect(api.get).toHaveBeenCalledWith('/apps?');
598
+ get.mockClear();
599
+
600
+ api.getApps({ query: 'foo' });
601
+ expect(get).toHaveBeenCalledWith('/apps?text=foo');
602
+ get.mockClear();
603
+
604
+ api.getApps({ query: 'foo', page: 1, limit: 10, workspaceId: '42' });
605
+ expect(get).toHaveBeenCalledWith(
606
+ '/apps?text=foo&page=1&limit=10&workspaceId=42'
607
+ );
608
+ });
609
+
610
+ it('should install app', async () => {
611
+ const api = new Api({ host: '/fake/' });
612
+ api.post = jest.fn((): any => {});
613
+ api.installApp('42', { appSlug: 'app' });
614
+ expect(api.post).toHaveBeenCalledWith('/workspaces/42/apps', {
615
+ appSlug: 'app',
616
+ });
617
+ });
618
+
619
+ it('should update app', async () => {
620
+ const api = new Api({ host: '/fake/' });
621
+ api.patch = jest.fn((): any => {});
622
+ api.updateApp('42', 'app', { appSlug: 'app' });
623
+ expect(api.patch).toHaveBeenCalledWith('/workspaces/42/apps/app', {
624
+ appSlug: 'app',
625
+ });
626
+ });
627
+
628
+ it('should uninstall app', async () => {
629
+ const api = new Api({ host: '/fake/' });
630
+ api.delete = jest.fn((): any => {});
631
+ api.uninstallApp('42', 'app');
632
+ expect(api.delete).toHaveBeenCalledWith('/workspaces/42/apps/app');
633
+ });
634
+
635
+ it('should publish app', async () => {
636
+ const api = new Api({ host: '/fake/' });
637
+ api.post = jest.fn((): any => {});
638
+ api.publishApp({ workspaceId: '42' });
639
+ expect(api.post).toHaveBeenCalledWith('/apps', { workspaceId: '42' });
640
+ });
641
+
642
+ it('should list app instances', async () => {
643
+ const api = new Api({ host: '/fake/' });
644
+ api.get = jest.fn((): any => {});
645
+ api.listAppInstances('42');
646
+ expect(api.get).toHaveBeenCalledWith('/workspaces/42/apps');
647
+ });
648
+
649
+ it('should get app config', async () => {
650
+ const api = new Api({ host: '/fake/' });
651
+ api.get = jest.fn((): any => {});
652
+ api.getAppConfig('42', 'app');
653
+ expect(api.get).toHaveBeenCalledWith('/workspaces/42/apps/app/config');
654
+ });
655
+
656
+ it('should update app config', async () => {
657
+ const api = new Api({ host: '/fake/' });
658
+ api.patch = jest.fn((): any => {});
659
+ api.updateAppConfig('42', 'app', {});
660
+ expect(api.patch).toHaveBeenCalledWith('/workspaces/42/apps/app/config', {});
661
+ });
662
+
663
+ it('should save app instance', async () => {
664
+ const api = new Api({ host: '/fake/' });
665
+ api.patch = jest.fn((): any => {});
666
+ api.saveAppInstance('42', 'app', {});
667
+ expect(api.patch).toHaveBeenCalledWith('/workspaces/42/apps/app', {});
668
+ });
669
+
670
+ it('should upload files', async () => {
671
+ const api = new Api({ host: '/fake/' });
672
+ // @ts-ignore
673
+ let fetch: jest.Mock = (api._fetch = jest.fn((): any => {}));
674
+ api.uploadFiles('data:text/plain;base64,SGVsbG8gV29ybGQ', '42');
675
+ // @ts-ignore
676
+ expect(fetch).toHaveBeenCalledWith('/workspaces/42/files', {
677
+ method: 'POST',
678
+ body: expect.any(FormData),
679
+ });
680
+
681
+ expect(fetch.mock.calls[0][1].body.get('file')).toBeInstanceOf(Blob);
682
+ fetch.mockClear();
683
+
684
+ api.uploadFiles(['data:text/plain;text,Hello World'], '42');
685
+ // @ts-ignore
686
+ expect(fetch).toHaveBeenCalledWith('/workspaces/42/files', {
687
+ method: 'POST',
688
+ body: expect.any(FormData),
689
+ });
690
+
691
+ // @ts-ignore
692
+ fetch = api._fetch = jest.fn((): any => {
693
+ throw new Error();
694
+ });
695
+ const empty = await api.uploadFiles('data:text/plain;text,Hello World', '42');
696
+ expect(empty).toEqual([]);
697
+ });
698
+
699
+ it('should call automation', () => {
700
+ const api = new Api({ host: '/fake/' });
701
+ api.post = jest.fn((): any => {});
702
+ api.callAutomation('42', 'automation');
703
+ expect(api.post).toHaveBeenCalledWith(
704
+ '/workspaces/42/webhooks/automation',
705
+ undefined
706
+ );
707
+ });
708
+
709
+ it('should get workspace usage', () => {
710
+ const api = new Api({ host: '/fake/' });
711
+ const get: jest.Mock = (api.get = jest.fn((): any => {}));
712
+ api.getWorkspaceUsage('42');
713
+ expect(api.get).toHaveBeenCalledWith('/workspaces/42/usage?');
714
+ get.mockClear();
715
+
716
+ api.getWorkspaceUsage('42', {
717
+ afterDate: '2022',
718
+ beforeDate: undefined,
719
+ });
720
+ expect(api.get).toHaveBeenCalledWith('/workspaces/42/usage?afterDate=2022');
721
+ });
722
+
723
+ describe('Workspaces', () => {
724
+ it('should access workspaces methods', () => {
725
+ expect(api.workspaces('42')).toBeInstanceOf(WorkspacesEndpoint);
726
+ });
727
+ describe('Versions', () => {
728
+ it('should access versions methods', () => {
729
+ expect(api.workspaces('42').versions).toBeInstanceOf(
730
+ WorkspacesVersionsEndpoint
731
+ );
732
+ });
733
+ it('create', () => {
734
+ api.post = jest.fn();
735
+ api.workspaces('42').versions.create({ description: 'foo' });
736
+ expect(api.post).toHaveBeenCalledWith('/workspaces/42/versions', {
737
+ description: 'foo',
738
+ });
739
+ });
740
+ it('rollback', () => {
741
+ api.post = jest.fn();
742
+ api.workspaces('42').versions.rollback('v1.0.0');
743
+ expect(api.post).toHaveBeenCalledWith(
744
+ '/workspaces/42/versions/v1.0.0/rollback'
745
+ );
746
+ });
747
+ });
748
+ });
749
+
750
+ describe('User', () => {
751
+ it('should access user methods', () => {
752
+ expect(api.users('42')).toBeInstanceOf(UsersEndpoint);
753
+ });
754
+ it('should access user methods with current user', () => {
755
+ // @ts-ignore
756
+ api._user = { id: '42' } as any;
757
+ expect(api.users()).toBeInstanceOf(UsersEndpoint);
758
+ });
759
+
760
+ it('should set meta data', () => {
761
+ api.post = jest.fn();
762
+ api.users().setMeta('foo', 'bar');
763
+ expect(api.post).toHaveBeenCalledWith('/user/meta', {
764
+ foo: 'bar',
765
+ });
766
+ });
767
+ it('should delete meta data', () => {
768
+ api.delete = jest.fn();
769
+ api.users().deleteMeta('foo');
770
+ expect(api.delete).toHaveBeenCalledWith('/user/meta/foo');
771
+ });
772
+ });