1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
12 describe('collection-service', () => {
13 let collectionService: CollectionService;
14 let serverApi: AxiosInstance;
15 let axiosMock: MockAdapter;
16 let webdavClient: any;
21 serverApi = axios.create();
22 axiosMock = new MockAdapter(serverApi);
28 authService = {} as AuthService;
30 progressFn: jest.fn(),
33 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
34 collectionService.update = jest.fn();
37 describe('get', () => {
38 it('should make a request with default selected fields', async () => {
39 serverApi.get = jest.fn(() => Promise.resolve(
40 { data: { items: [{}] } }
42 const uuid = 'zzzzz-4zz18-0123456789abcde'
43 await collectionService.get(uuid);
44 expect(serverApi.get).toHaveBeenCalledWith(
45 `/collections/${uuid}`, {
47 select: JSON.stringify(defaultCollectionSelectedFields.map(snakeCase)),
53 it('should be able to request specific fields', async () => {
54 serverApi.get = jest.fn(() => Promise.resolve(
55 { data: { items: [{}] } }
57 const uuid = 'zzzzz-4zz18-0123456789abcde'
58 await collectionService.get(uuid, undefined, ['manifestText']);
59 expect(serverApi.get).toHaveBeenCalledWith(
60 `/collections/${uuid}`, {
62 select: `["manifest_text"]`
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> = {
78 preserve_version: true,
80 select: ['uuid', 'name', 'version', 'modified_at'],
82 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
83 await collectionService.update('uuid', data);
84 expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
88 describe('uploadFiles', () => {
89 it('should skip if no files to upload files', async () => {
91 const files: File[] = [];
92 const collectionUUID = '';
95 await collectionService.uploadFiles(collectionUUID, files);
98 expect(webdavClient.upload).not.toHaveBeenCalled();
101 it('should upload files', async () => {
103 const files: File[] = [{name: 'test-file1'} as File];
104 const collectionUUID = 'zzzzz-4zz18-0123456789abcde';
107 await collectionService.uploadFiles(collectionUUID, files);
110 expect(webdavClient.upload).toHaveBeenCalledTimes(1);
111 expect(webdavClient.upload.mock.calls[0][0]).toEqual("c=zzzzz-4zz18-0123456789abcde/test-file1");
114 it('should upload files with custom uplaod target', async () => {
116 const files: File[] = [{name: 'test-file1'} as File];
117 const collectionUUID = 'zzzzz-4zz18-0123456789abcde';
118 const customTarget = 'zzzzz-4zz18-0123456789adddd/test-path/'
121 await collectionService.uploadFiles(collectionUUID, files, undefined, customTarget);
124 expect(webdavClient.upload).toHaveBeenCalledTimes(1);
125 expect(webdavClient.upload.mock.calls[0][0]).toEqual("c=zzzzz-4zz18-0123456789adddd/test-path/test-file1");
129 describe('deleteFiles', () => {
130 it('should remove no files', async () => {
132 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
133 const filePaths: string[] = [];
134 const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
137 await collectionService.deleteFiles(collectionUUID, filePaths);
140 expect(serverApi.put).toHaveBeenCalledTimes(1);
141 expect(serverApi.put).toHaveBeenCalledWith(
142 `/collections/${collectionUUID}`, {
144 preserve_version: true
151 it('should remove only root files', async () => {
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';
158 await collectionService.deleteFiles(collectionUUID, filePaths);
161 expect(serverApi.put).toHaveBeenCalledTimes(1);
162 expect(serverApi.put).toHaveBeenCalledWith(
163 `/collections/${collectionUUID}`, {
165 preserve_version: true
168 '/root/3/300/test.txt': '',
176 it('should remove files with uuid prefix', async () => {
178 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
179 const filePaths: string[] = ['/root/1'];
180 const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
183 await collectionService.deleteFiles(collectionUUID, filePaths);
186 expect(serverApi.put).toHaveBeenCalledTimes(1);
187 expect(serverApi.put).toHaveBeenCalledWith(
188 `/collections/${collectionUUID}`, {
190 preserve_version: true
199 it('should batch remove files', async () => {
200 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
202 const filePaths: string[] = ['/root/1', '/secondFile', 'barefile.txt'];
203 const collectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
206 await collectionService.deleteFiles(collectionUUID, filePaths);
209 expect(serverApi.put).toHaveBeenCalledTimes(1);
210 expect(serverApi.put).toHaveBeenCalledWith(
211 `/collections/${collectionUUID}`, {
213 preserve_version: true
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';
233 await collectionService.renameFile(collectionUuid, collectionPdh, oldPath, newPath);
235 expect(serverApi.put).toHaveBeenCalledTimes(1);
236 expect(serverApi.put).toHaveBeenCalledWith(
237 `/collections/${collectionUuid}`, {
239 preserve_version: true
242 [newPath]: `${collectionPdh}${oldPath}`,
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';
255 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
256 const destinationPath = '/destinationPath';
259 await collectionService.copyFiles(sourcePdh, filePaths, destinationUuid, destinationPath);
262 expect(serverApi.put).toHaveBeenCalledTimes(1);
263 expect(serverApi.put).toHaveBeenCalledWith(
264 `/collections/${destinationUuid}`, {
266 preserve_version: true
269 [`${destinationPath}/1`]: `${sourcePdh}/root/1`,
270 [`${destinationPath}/secondFile`]: `${sourcePdh}/secondFile`,
271 [`${destinationPath}/barefile.txt`]: `${sourcePdh}/barefile.txt`,
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';
283 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
284 const destinationPath = '/destinationPath';
286 await collectionService.copyFiles(sourcePdh, filePaths, destinationUuid, destinationPath);
288 expect(serverApi.put).toHaveBeenCalledTimes(1);
289 expect(serverApi.put).toHaveBeenCalledWith(
290 `/collections/${destinationUuid}`, {
292 preserve_version: true
295 [`${destinationPath}`]: `${sourcePdh}/`,
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';
307 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
308 const destinationPath = '/';
310 await collectionService.copyFiles(sourcePdh, filePaths, destinationUuid, destinationPath);
312 expect(serverApi.put).toHaveBeenCalledTimes(1);
313 expect(serverApi.put).toHaveBeenCalledWith(
314 `/collections/${destinationUuid}`, {
316 preserve_version: true
319 "/": `${sourcePdh}/`,
326 describe('moveFiles', () => {
327 it('should batch move files', async () => {
328 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
330 const filePaths: string[] = ['/rootFile', '/secondFile', '/subpath/subfile', 'barefile.txt'];
331 const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
332 const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
334 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
335 const destinationPath = '/destinationPath';
338 await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, destinationUuid, destinationPath);
341 expect(serverApi.put).toHaveBeenCalledTimes(2);
343 expect(serverApi.put).toHaveBeenCalledWith(
344 `/collections/${destinationUuid}`, {
346 preserve_version: true
349 [`${destinationPath}/rootFile`]: `${srcCollectionPdh}/rootFile`,
350 [`${destinationPath}/secondFile`]: `${srcCollectionPdh}/secondFile`,
351 [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
352 [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
357 expect(serverApi.put).toHaveBeenCalledWith(
358 `/collections/${srcCollectionUUID}`, {
360 preserve_version: true
365 "/subpath/subfile": "",
372 it('should batch move files within collection', async () => {
373 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
375 const filePaths: string[] = ['/one', '/two', '/subpath/subfile', 'barefile.txt'];
376 const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
377 const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
379 const destinationPath = '/destinationPath';
382 await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, srcCollectionUUID, destinationPath);
385 expect(serverApi.put).toHaveBeenCalledTimes(1);
387 expect(serverApi.put).toHaveBeenCalledWith(
388 `/collections/${srcCollectionUUID}`, {
390 preserve_version: true
393 [`${destinationPath}/one`]: `${srcCollectionPdh}/one`,
395 [`${destinationPath}/two`]: `${srcCollectionPdh}/two`,
397 [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
398 ['/subpath/subfile']: '',
399 [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
400 ['/barefile.txt']: '',
406 it('should abort batch move when copy fails', async () => {
407 // Simulate failure to copy
408 serverApi.put = jest.fn(() => Promise.reject({
411 "errors": ["error getting snapshot of \"rootFile\" from \"8cd9ce1dfa21c635b620b1bfee7aaa08+180\": file does not exist"]
415 const filePaths: string[] = ['/rootFile', '/secondFile', '/subpath/subfile', 'barefile.txt'];
416 const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
417 const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
419 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
420 const destinationPath = '/destinationPath';
424 await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, destinationUuid, destinationPath);
428 expect(serverApi.put).toHaveBeenCalledTimes(1);
430 expect(serverApi.put).toHaveBeenCalledWith(
431 `/collections/${destinationUuid}`, {
433 preserve_version: true
436 [`${destinationPath}/rootFile`]: `${srcCollectionPdh}/rootFile`,
437 [`${destinationPath}/secondFile`]: `${srcCollectionPdh}/secondFile`,
438 [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
439 [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
446 describe('createDirectory', () => {
447 it('creates empty directory', async () => {
449 const directoryNames = [
450 {in: 'newDir', out: 'newDir'},
451 {in: '/fooDir', out: 'fooDir'},
452 {in: '/anotherPath/', out: 'anotherPath'},
453 {in: 'trailingSlash/', out: 'trailingSlash'},
455 const collectionUuid = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
457 for (var i = 0; i < directoryNames.length; i++) {
458 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
460 await collectionService.createDirectory(collectionUuid, directoryNames[i].in);
462 expect(serverApi.put).toHaveBeenCalledTimes(1);
463 expect(serverApi.put).toHaveBeenCalledWith(
464 `/collections/${collectionUuid}`, {
466 preserve_version: true
469 ["/" + directoryNames[i].out]: emptyCollectionUuid,