20029: Refactor replaceFiles operations
[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, emptyCollectionUuid } from './collection-service';
11
12 describe('collection-service', () => {
13     let collectionService: CollectionService;
14     let serverApi: AxiosInstance;
15     let axiosMock: MockAdapter;
16     let webdavClient: any;
17     let authService;
18     let actions;
19
20     beforeEach(() => {
21         serverApi = axios.create();
22         axiosMock = new MockAdapter(serverApi);
23         webdavClient = {
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, webdavClient, 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, webdavClient, 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(webdavClient.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(webdavClient.upload).toHaveBeenCalledTimes(1);
111             expect(webdavClient.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(webdavClient.upload).toHaveBeenCalledTimes(1);
125             expect(webdavClient.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 remove files with uuid prefix', async () => {
177             // given
178             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
179             const filePaths: string[] = ['/root/1'];
180             const collectionUUID = 'zzzzz-tpzed-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                     },
195                 }
196             );
197         });
198
199         it('should batch remove files', async () => {
200             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
201             // given
202             const filePaths: string[] = ['/root/1', '/secondFile', 'barefile.txt'];
203             const collectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
204
205             // when
206             await collectionService.deleteFiles(collectionUUID, filePaths);
207
208             // then
209             expect(serverApi.put).toHaveBeenCalledTimes(1);
210             expect(serverApi.put).toHaveBeenCalledWith(
211                 `/collections/${collectionUUID}`, {
212                     collection: {
213                         preserve_version: true
214                     },
215                     replace_files: {
216                         '/root/1': '',
217                         '/secondFile': '',
218                         '/barefile.txt': '',
219                     },
220                 }
221             );
222         });
223     });
224
225     describe('renameFile', () => {
226         it('should rename file', async () => {
227             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
228             const collectionUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
229             const collectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
230             const oldPath = '/old/path';
231             const newPath = '/new/filename';
232
233             await collectionService.renameFile(collectionUuid, collectionPdh, oldPath, newPath);
234
235             expect(serverApi.put).toHaveBeenCalledTimes(1);
236             expect(serverApi.put).toHaveBeenCalledWith(
237                 `/collections/${collectionUuid}`, {
238                     collection: {
239                         preserve_version: true
240                     },
241                     replace_files: {
242                         [newPath]: `${collectionPdh}${oldPath}`,
243                     },
244                 }
245             );
246         });
247     });
248
249     describe('copyFiles', () => {
250         it('should batch copy files', async () => {
251             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
252             const filePaths: string[] = ['/root/1', '/secondFile', 'barefile.txt'];
253             const sourcePdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
254
255             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
256             const destinationPath = '/destinationPath';
257
258             // when
259             await collectionService.copyFiles(sourcePdh, filePaths, destinationUuid, destinationPath);
260
261             // then
262             expect(serverApi.put).toHaveBeenCalledTimes(1);
263             expect(serverApi.put).toHaveBeenCalledWith(
264                 `/collections/${destinationUuid}`, {
265                     collection: {
266                         preserve_version: true
267                     },
268                     replace_files: {
269                         [`${destinationPath}/1`]: `${sourcePdh}/root/1`,
270                         [`${destinationPath}/secondFile`]: `${sourcePdh}/secondFile`,
271                         [`${destinationPath}/barefile.txt`]: `${sourcePdh}/barefile.txt`,
272                     },
273                 }
274             );
275         });
276
277         it('should copy files from rooth', async () => {
278             // Test copying from root paths
279             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
280             const filePaths: string[] = ['/'];
281             const sourcePdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
282
283             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
284             const destinationPath = '/destinationPath';
285
286             await collectionService.copyFiles(sourcePdh, filePaths, destinationUuid, destinationPath);
287
288             expect(serverApi.put).toHaveBeenCalledTimes(1);
289             expect(serverApi.put).toHaveBeenCalledWith(
290                 `/collections/${destinationUuid}`, {
291                     collection: {
292                         preserve_version: true
293                     },
294                     replace_files: {
295                         [`${destinationPath}`]: `${sourcePdh}/`,
296                     },
297                 }
298             );
299         });
300
301         it('should copy files to root path', async () => {
302             // Test copying to root paths
303             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
304             const filePaths: string[] = ['/'];
305             const sourcePdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
306
307             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
308             const destinationPath = '/';
309
310             await collectionService.copyFiles(sourcePdh, filePaths, destinationUuid, destinationPath);
311
312             expect(serverApi.put).toHaveBeenCalledTimes(1);
313             expect(serverApi.put).toHaveBeenCalledWith(
314                 `/collections/${destinationUuid}`, {
315                     collection: {
316                         preserve_version: true
317                     },
318                     replace_files: {
319                         "/": `${sourcePdh}/`,
320                     },
321                 }
322             );
323         });
324     });
325
326     describe('moveFiles', () => {
327         it('should batch move files', async () => {
328             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
329             // given
330             const filePaths: string[] = ['/rootFile', '/secondFile', '/subpath/subfile', 'barefile.txt'];
331             const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
332             const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
333
334             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
335             const destinationPath = '/destinationPath';
336
337             // when
338             await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, destinationUuid, destinationPath);
339
340             // then
341             expect(serverApi.put).toHaveBeenCalledTimes(2);
342             // Verify copy
343             expect(serverApi.put).toHaveBeenCalledWith(
344                 `/collections/${destinationUuid}`, {
345                     collection: {
346                         preserve_version: true
347                     },
348                     replace_files: {
349                         [`${destinationPath}/rootFile`]: `${srcCollectionPdh}/rootFile`,
350                         [`${destinationPath}/secondFile`]: `${srcCollectionPdh}/secondFile`,
351                         [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
352                         [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
353                     },
354                 }
355             );
356             // Verify delete
357             expect(serverApi.put).toHaveBeenCalledWith(
358                 `/collections/${srcCollectionUUID}`, {
359                     collection: {
360                         preserve_version: true
361                     },
362                     replace_files: {
363                         "/rootFile": "",
364                         "/secondFile": "",
365                         "/subpath/subfile": "",
366                         "/barefile.txt": "",
367                     },
368                 }
369             );
370         });
371
372         it('should batch move files within collection', async () => {
373             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
374             // given
375             const filePaths: string[] = ['/one', '/two', '/subpath/subfile', 'barefile.txt'];
376             const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
377             const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
378
379             const destinationPath = '/destinationPath';
380
381             // when
382             await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, srcCollectionUUID, destinationPath);
383
384             // then
385             expect(serverApi.put).toHaveBeenCalledTimes(1);
386             // Verify copy
387             expect(serverApi.put).toHaveBeenCalledWith(
388                 `/collections/${srcCollectionUUID}`, {
389                     collection: {
390                         preserve_version: true
391                     },
392                     replace_files: {
393                         [`${destinationPath}/one`]: `${srcCollectionPdh}/one`,
394                         ['/one']: '',
395                         [`${destinationPath}/two`]: `${srcCollectionPdh}/two`,
396                         ['/two']: '',
397                         [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
398                         ['/subpath/subfile']: '',
399                         [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
400                         ['/barefile.txt']: '',
401                     },
402                 }
403             );
404         });
405
406         it('should abort batch move when copy fails', async () => {
407             // Simulate failure to copy
408             serverApi.put = jest.fn(() => Promise.reject({
409                 data: {},
410                 response: {
411                     "errors": ["error getting snapshot of \"rootFile\" from \"8cd9ce1dfa21c635b620b1bfee7aaa08+180\": file does not exist"]
412                 }
413             }));
414             // given
415             const filePaths: string[] = ['/rootFile', '/secondFile', '/subpath/subfile', 'barefile.txt'];
416             const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
417             const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
418
419             const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
420             const destinationPath = '/destinationPath';
421
422             // when
423             try {
424                 await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, destinationUuid, destinationPath);
425             } catch {}
426
427             // then
428             expect(serverApi.put).toHaveBeenCalledTimes(1);
429             // Verify copy
430             expect(serverApi.put).toHaveBeenCalledWith(
431                 `/collections/${destinationUuid}`, {
432                     collection: {
433                         preserve_version: true
434                     },
435                     replace_files: {
436                         [`${destinationPath}/rootFile`]: `${srcCollectionPdh}/rootFile`,
437                         [`${destinationPath}/secondFile`]: `${srcCollectionPdh}/secondFile`,
438                         [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
439                         [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
440                     },
441                 }
442             );
443         });
444     });
445
446     describe('createDirectory', () => {
447         it('creates empty directory', async () => {
448             // given
449             const directoryNames = [
450                 {in: 'newDir', out: 'newDir'},
451                 {in: '/fooDir', out: 'fooDir'},
452                 {in: '/anotherPath/', out: 'anotherPath'},
453                 {in: 'trailingSlash/', out: 'trailingSlash'},
454             ];
455             const collectionUuid = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
456
457             for (var i = 0; i < directoryNames.length; i++) {
458                 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
459                 // when
460                 await collectionService.createDirectory(collectionUuid, directoryNames[i].in);
461                 // then
462                 expect(serverApi.put).toHaveBeenCalledTimes(1);
463                 expect(serverApi.put).toHaveBeenCalledWith(
464                     `/collections/${collectionUuid}`, {
465                         collection: {
466                             preserve_version: true
467                         },
468                         replace_files: {
469                             ["/" + directoryNames[i].out]: emptyCollectionUuid,
470                         },
471                     }
472                 );
473             }
474         });
475     });
476
477 });