Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / services / collection-service / collection-service.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import axios, { AxiosInstance } from 'axios';
6 import MockAdapter from 'axios-mock-adapter';
7 import { snakeCase } from 'lodash';
8 import { CollectionResource, defaultCollectionSelectedFields } from 'models/collection';
9 import { AuthService } from '../auth-service/auth-service';
10 import { CollectionService, emptyCollectionPdh } from './collection-service';
11
12 describe('collection-service', () => {
13     let collectionService: CollectionService;
14     let serverApi: AxiosInstance;
15     let axiosMock: MockAdapter;
16     let keepWebdavClient: any;
17     let authService;
18     let actions;
19
20     beforeEach(() => {
21         serverApi = axios.create();
22         axiosMock = new MockAdapter(serverApi);
23         keepWebdavClient = {
24             delete: jest.fn(),
25             upload: jest.fn(),
26             mkdir: jest.fn(),
27         } as any;
28         authService = {} as AuthService;
29         actions = {
30             progressFn: jest.fn(),
31             errorFn: jest.fn(),
32         } as any;
33         collectionService = new CollectionService(serverApi, keepWebdavClient, authService, actions);
34         collectionService.update = jest.fn();
35     });
36
37     describe('get', () => {
38         it('should make a request with default selected fields', async () => {
39             serverApi.get = jest.fn(() => Promise.resolve(
40                 { data: { items: [{}] } }
41             ));
42             const uuid = 'zzzzz-4zz18-0123456789abcde'
43             await collectionService.get(uuid);
44             expect(serverApi.get).toHaveBeenCalledWith(
45                 `/collections/${uuid}`, {
46                     params: {
47                         select: JSON.stringify(defaultCollectionSelectedFields.map(snakeCase)),
48                     },
49                 }
50             );
51         });
52
53         it('should be able to request specific fields', async () => {
54             serverApi.get = jest.fn(() => Promise.resolve(
55                 { data: { items: [{}] } }
56             ));
57             const uuid = 'zzzzz-4zz18-0123456789abcde'
58             await collectionService.get(uuid, undefined, ['manifestText']);
59             expect(serverApi.get).toHaveBeenCalledWith(
60                 `/collections/${uuid}`, {
61                     params: {
62                         select: `["manifest_text"]`
63                     },
64                 }
65             );
66         });
67     });
68
69     describe('update', () => {
70         it('should call put selecting updated fields + others', async () => {
71             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
72             const data: Partial<CollectionResource> = {
73                 name: 'foo',
74             };
75             const expected = {
76                 collection: {
77                     ...data,
78                     preserve_version: true,
79                 },
80                 select: ['uuid', 'name', 'version', 'modified_at'],
81             }
82             collectionService = new CollectionService(serverApi, keepWebdavClient, authService, actions);
83             await collectionService.update('uuid', data);
84             expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
85         });
86     });
87
88     describe('uploadFiles', () => {
89         it('should skip if no files to upload files', async () => {
90             // given
91             const files: File[] = [];
92             const collectionUUID = '';
93
94             // when
95             await collectionService.uploadFiles(collectionUUID, files);
96
97             // then
98             expect(keepWebdavClient.upload).not.toHaveBeenCalled();
99         });
100
101         it('should upload files', async () => {
102             // given
103             const files: File[] = [{name: 'test-file1'} as File];
104             const collectionUUID = 'zzzzz-4zz18-0123456789abcde';
105
106             // when
107             await collectionService.uploadFiles(collectionUUID, files);
108
109             // then
110             expect(keepWebdavClient.upload).toHaveBeenCalledTimes(1);
111             expect(keepWebdavClient.upload.mock.calls[0][0]).toEqual("c=zzzzz-4zz18-0123456789abcde/test-file1");
112         });
113
114         it('should upload files with custom uplaod target', async () => {
115             // given
116             const files: File[] = [{name: 'test-file1'} as File];
117             const collectionUUID = 'zzzzz-4zz18-0123456789abcde';
118             const customTarget = 'zzzzz-4zz18-0123456789adddd/test-path/'
119
120             // when
121             await collectionService.uploadFiles(collectionUUID, files, undefined, customTarget);
122
123             // then
124             expect(keepWebdavClient.upload).toHaveBeenCalledTimes(1);
125             expect(keepWebdavClient.upload.mock.calls[0][0]).toEqual("c=zzzzz-4zz18-0123456789adddd/test-path/test-file1");
126         });
127     });
128
129     describe('deleteFiles', () => {
130         it('should remove no files', async () => {
131             // given
132             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
133             const filePaths: string[] = [];
134             const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
135
136             // when
137             await collectionService.deleteFiles(collectionUUID, filePaths);
138
139             // then
140             expect(serverApi.put).toHaveBeenCalledTimes(1);
141             expect(serverApi.put).toHaveBeenCalledWith(
142                 `/collections/${collectionUUID}`, {
143                     collection: {
144                         preserve_version: true
145                     },
146                     replace_files: {},
147                 }
148             );
149         });
150
151         it('should remove only root files', async () => {
152             // given
153             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
154             const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
155             const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
156
157             // when
158             await collectionService.deleteFiles(collectionUUID, filePaths);
159
160             // then
161             expect(serverApi.put).toHaveBeenCalledTimes(1);
162             expect(serverApi.put).toHaveBeenCalledWith(
163                 `/collections/${collectionUUID}`, {
164                     collection: {
165                         preserve_version: true
166                     },
167                     replace_files: {
168                         '/root/3/300/test.txt': '',
169                         '/root/2': '',
170                         '/root/1': '',
171                     },
172                 }
173             );
174         });
175
176         it('should batch remove files', async () => {
177             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
178             // given
179             const filePaths: string[] = ['/root/1', '/secondFile', 'barefile.txt'];
180             const collectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
181
182             // when
183             await collectionService.deleteFiles(collectionUUID, filePaths);
184
185             // then
186             expect(serverApi.put).toHaveBeenCalledTimes(1);
187             expect(serverApi.put).toHaveBeenCalledWith(
188                 `/collections/${collectionUUID}`, {
189                     collection: {
190                         preserve_version: true
191                     },
192                     replace_files: {
193                         '/root/1': '',
194                         '/secondFile': '',
195                         '/barefile.txt': '',
196                     },
197                 }
198             );
199         });
200     });
201
202     describe('renameFile', () => {
203         it('should rename file', async () => {
204             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
205             const collectionUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
206             const collectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
207             const oldPath = '/old/path';
208             const newPath = '/new/filename';
209
210             await collectionService.renameFile(collectionUuid, collectionPdh, oldPath, newPath);
211
212             expect(serverApi.put).toHaveBeenCalledTimes(1);
213             expect(serverApi.put).toHaveBeenCalledWith(
214                 `/collections/${collectionUuid}`, {
215                     collection: {
216                         preserve_version: true
217                     },
218                     replace_files: {
219                         [newPath]: `${collectionPdh}${oldPath}`,
220                         [oldPath]: '',
221                     },
222                 }
223             );
224         });
225     });
226
227     describe('copyFiles', () => {
228         it('should batch copy files', async () => {
229             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
230             const filePaths: string[] = ['/root/1', '/secondFile', 'barefile.txt'];
231             const sourcePdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
232
233             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
234             const destinationPath = '/destinationPath';
235
236             // when
237             await collectionService.copyFiles(sourcePdh, filePaths, {uuid: destinationUuid}, destinationPath);
238
239             // then
240             expect(serverApi.put).toHaveBeenCalledTimes(1);
241             expect(serverApi.put).toHaveBeenCalledWith(
242                 `/collections/${destinationUuid}`, {
243                     collection: {
244                         preserve_version: true
245                     },
246                     replace_files: {
247                         [`${destinationPath}/1`]: `${sourcePdh}/root/1`,
248                         [`${destinationPath}/secondFile`]: `${sourcePdh}/secondFile`,
249                         [`${destinationPath}/barefile.txt`]: `${sourcePdh}/barefile.txt`,
250                     },
251                 }
252             );
253         });
254
255         it('should copy files from rooth', async () => {
256             // Test copying from root paths
257             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
258             const filePaths: string[] = ['/'];
259             const sourcePdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
260
261             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
262             const destinationPath = '/destinationPath';
263
264             await collectionService.copyFiles(sourcePdh, filePaths, {uuid: destinationUuid}, destinationPath);
265
266             expect(serverApi.put).toHaveBeenCalledTimes(1);
267             expect(serverApi.put).toHaveBeenCalledWith(
268                 `/collections/${destinationUuid}`, {
269                     collection: {
270                         preserve_version: true
271                     },
272                     replace_files: {
273                         [`${destinationPath}`]: `${sourcePdh}/`,
274                     },
275                 }
276             );
277         });
278
279         it('should copy files to root path', async () => {
280             // Test copying to root paths
281             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
282             const filePaths: string[] = ['/'];
283             const sourcePdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
284
285             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
286             const destinationPath = '/';
287
288             await collectionService.copyFiles(sourcePdh, filePaths, {uuid: destinationUuid}, destinationPath);
289
290             expect(serverApi.put).toHaveBeenCalledTimes(1);
291             expect(serverApi.put).toHaveBeenCalledWith(
292                 `/collections/${destinationUuid}`, {
293                     collection: {
294                         preserve_version: true
295                     },
296                     replace_files: {
297                         "/": `${sourcePdh}/`,
298                     },
299                 }
300             );
301         });
302     });
303
304     describe('moveFiles', () => {
305         it('should batch move files', async () => {
306             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
307             // given
308             const filePaths: string[] = ['/rootFile', '/secondFile', '/subpath/subfile', 'barefile.txt'];
309             const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
310             const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
311
312             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
313             const destinationPath = '/destinationPath';
314
315             // when
316             await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, {uuid: destinationUuid}, destinationPath);
317
318             // then
319             expect(serverApi.put).toHaveBeenCalledTimes(2);
320             // Verify copy
321             expect(serverApi.put).toHaveBeenCalledWith(
322                 `/collections/${destinationUuid}`, {
323                     collection: {
324                         preserve_version: true
325                     },
326                     replace_files: {
327                         [`${destinationPath}/rootFile`]: `${srcCollectionPdh}/rootFile`,
328                         [`${destinationPath}/secondFile`]: `${srcCollectionPdh}/secondFile`,
329                         [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
330                         [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
331                     },
332                 }
333             );
334             // Verify delete
335             expect(serverApi.put).toHaveBeenCalledWith(
336                 `/collections/${srcCollectionUUID}`, {
337                     collection: {
338                         preserve_version: true
339                     },
340                     replace_files: {
341                         "/rootFile": "",
342                         "/secondFile": "",
343                         "/subpath/subfile": "",
344                         "/barefile.txt": "",
345                     },
346                 }
347             );
348         });
349
350         it('should batch move files within collection', async () => {
351             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
352             // given
353             const filePaths: string[] = ['/one', '/two', '/subpath/subfile', 'barefile.txt'];
354             const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
355             const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
356
357             const destinationPath = '/destinationPath';
358
359             // when
360             await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, {uuid: srcCollectionUUID}, destinationPath);
361
362             // then
363             expect(serverApi.put).toHaveBeenCalledTimes(1);
364             // Verify copy
365             expect(serverApi.put).toHaveBeenCalledWith(
366                 `/collections/${srcCollectionUUID}`, {
367                     collection: {
368                         preserve_version: true
369                     },
370                     replace_files: {
371                         [`${destinationPath}/one`]: `${srcCollectionPdh}/one`,
372                         ['/one']: '',
373                         [`${destinationPath}/two`]: `${srcCollectionPdh}/two`,
374                         ['/two']: '',
375                         [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
376                         ['/subpath/subfile']: '',
377                         [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
378                         ['/barefile.txt']: '',
379                     },
380                 }
381             );
382         });
383
384         it('should abort batch move when copy fails', async () => {
385             // Simulate failure to copy
386             serverApi.put = jest.fn(() => Promise.reject({
387                 data: {},
388                 response: {
389                     "errors": ["error getting snapshot of \"rootFile\" from \"8cd9ce1dfa21c635b620b1bfee7aaa08+180\": file does not exist"]
390                 }
391             }));
392             // given
393             const filePaths: string[] = ['/rootFile', '/secondFile', '/subpath/subfile', 'barefile.txt'];
394             const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
395             const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
396
397             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
398             const destinationPath = '/destinationPath';
399
400             // when
401             try {
402                 await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, {uuid: destinationUuid}, destinationPath);
403             } catch {}
404
405             // then
406             expect(serverApi.put).toHaveBeenCalledTimes(1);
407             // Verify copy
408             expect(serverApi.put).toHaveBeenCalledWith(
409                 `/collections/${destinationUuid}`, {
410                     collection: {
411                         preserve_version: true
412                     },
413                     replace_files: {
414                         [`${destinationPath}/rootFile`]: `${srcCollectionPdh}/rootFile`,
415                         [`${destinationPath}/secondFile`]: `${srcCollectionPdh}/secondFile`,
416                         [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
417                         [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
418                     },
419                 }
420             );
421         });
422     });
423
424     describe('createDirectory', () => {
425         it('creates empty directory', async () => {
426             // given
427             const directoryNames = [
428                 {in: 'newDir', out: 'newDir'},
429                 {in: '/fooDir', out: 'fooDir'},
430                 {in: '/anotherPath/', out: 'anotherPath'},
431                 {in: 'trailingSlash/', out: 'trailingSlash'},
432             ];
433             const collectionUuid = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
434
435             for (var i = 0; i < directoryNames.length; i++) {
436                 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
437                 // when
438                 await collectionService.createDirectory(collectionUuid, directoryNames[i].in);
439                 // then
440                 expect(serverApi.put).toHaveBeenCalledTimes(1);
441                 expect(serverApi.put).toHaveBeenCalledWith(
442                     `/collections/${collectionUuid}`, {
443                         collection: {
444                             preserve_version: true
445                         },
446                         replace_files: {
447                             ["/" + directoryNames[i].out]: emptyCollectionPdh,
448                         },
449                     }
450                 );
451             }
452         });
453     });
454
455 });