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, emptyCollectionPdh } from './collection-service';
12 describe('collection-service', () => {
13 let collectionService: CollectionService;
14 let serverApi: AxiosInstance;
15 let axiosMock: MockAdapter;
16 let keepWebdavClient: any;
21 serverApi = axios.create();
22 axiosMock = new MockAdapter(serverApi);
28 authService = {} as AuthService;
30 progressFn: jest.fn(),
33 collectionService = new CollectionService(serverApi, keepWebdavClient, 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, keepWebdavClient, 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(keepWebdavClient.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(keepWebdavClient.upload).toHaveBeenCalledTimes(1);
111 expect(keepWebdavClient.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(keepWebdavClient.upload).toHaveBeenCalledTimes(1);
125 expect(keepWebdavClient.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 batch remove files', async () => {
177 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
179 const filePaths: string[] = ['/root/1', '/secondFile', 'barefile.txt'];
180 const collectionUUID = 'zzzzz-4zz18-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
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';
210 await collectionService.renameFile(collectionUuid, collectionPdh, oldPath, newPath);
212 expect(serverApi.put).toHaveBeenCalledTimes(1);
213 expect(serverApi.put).toHaveBeenCalledWith(
214 `/collections/${collectionUuid}`, {
216 preserve_version: true
219 [newPath]: `${collectionPdh}${oldPath}`,
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';
233 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
234 const destinationPath = '/destinationPath';
237 await collectionService.copyFiles(sourcePdh, filePaths, {uuid: destinationUuid}, destinationPath);
240 expect(serverApi.put).toHaveBeenCalledTimes(1);
241 expect(serverApi.put).toHaveBeenCalledWith(
242 `/collections/${destinationUuid}`, {
244 preserve_version: true
247 [`${destinationPath}/1`]: `${sourcePdh}/root/1`,
248 [`${destinationPath}/secondFile`]: `${sourcePdh}/secondFile`,
249 [`${destinationPath}/barefile.txt`]: `${sourcePdh}/barefile.txt`,
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';
261 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
262 const destinationPath = '/destinationPath';
264 await collectionService.copyFiles(sourcePdh, filePaths, {uuid: destinationUuid}, destinationPath);
266 expect(serverApi.put).toHaveBeenCalledTimes(1);
267 expect(serverApi.put).toHaveBeenCalledWith(
268 `/collections/${destinationUuid}`, {
270 preserve_version: true
273 [`${destinationPath}`]: `${sourcePdh}/`,
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';
285 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
286 const destinationPath = '/';
288 await collectionService.copyFiles(sourcePdh, filePaths, {uuid: destinationUuid}, destinationPath);
290 expect(serverApi.put).toHaveBeenCalledTimes(1);
291 expect(serverApi.put).toHaveBeenCalledWith(
292 `/collections/${destinationUuid}`, {
294 preserve_version: true
297 "/": `${sourcePdh}/`,
304 describe('moveFiles', () => {
305 it('should batch move files', async () => {
306 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
308 const filePaths: string[] = ['/rootFile', '/secondFile', '/subpath/subfile', 'barefile.txt'];
309 const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
310 const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
312 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
313 const destinationPath = '/destinationPath';
316 await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, {uuid: destinationUuid}, destinationPath);
319 expect(serverApi.put).toHaveBeenCalledTimes(2);
321 expect(serverApi.put).toHaveBeenCalledWith(
322 `/collections/${destinationUuid}`, {
324 preserve_version: true
327 [`${destinationPath}/rootFile`]: `${srcCollectionPdh}/rootFile`,
328 [`${destinationPath}/secondFile`]: `${srcCollectionPdh}/secondFile`,
329 [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
330 [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
335 expect(serverApi.put).toHaveBeenCalledWith(
336 `/collections/${srcCollectionUUID}`, {
338 preserve_version: true
343 "/subpath/subfile": "",
350 it('should batch move files within collection', async () => {
351 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
353 const filePaths: string[] = ['/one', '/two', '/subpath/subfile', 'barefile.txt'];
354 const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
355 const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
357 const destinationPath = '/destinationPath';
360 await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, {uuid: srcCollectionUUID}, destinationPath);
363 expect(serverApi.put).toHaveBeenCalledTimes(1);
365 expect(serverApi.put).toHaveBeenCalledWith(
366 `/collections/${srcCollectionUUID}`, {
368 preserve_version: true
371 [`${destinationPath}/one`]: `${srcCollectionPdh}/one`,
373 [`${destinationPath}/two`]: `${srcCollectionPdh}/two`,
375 [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
376 ['/subpath/subfile']: '',
377 [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
378 ['/barefile.txt']: '',
384 it('should abort batch move when copy fails', async () => {
385 // Simulate failure to copy
386 serverApi.put = jest.fn(() => Promise.reject({
389 "errors": ["error getting snapshot of \"rootFile\" from \"8cd9ce1dfa21c635b620b1bfee7aaa08+180\": file does not exist"]
393 const filePaths: string[] = ['/rootFile', '/secondFile', '/subpath/subfile', 'barefile.txt'];
394 const srcCollectionUUID = 'zzzzz-4zz18-5o5tg0l9a57gxxx';
395 const srcCollectionPdh = '8cd9ce1dfa21c635b620b1bfee7aaa08+180';
397 const destinationUuid = 'zzzzz-4zz18-ywq0rvhwwhkjnfq';
398 const destinationPath = '/destinationPath';
402 await collectionService.moveFiles(srcCollectionUUID, srcCollectionPdh, filePaths, {uuid: destinationUuid}, destinationPath);
406 expect(serverApi.put).toHaveBeenCalledTimes(1);
408 expect(serverApi.put).toHaveBeenCalledWith(
409 `/collections/${destinationUuid}`, {
411 preserve_version: true
414 [`${destinationPath}/rootFile`]: `${srcCollectionPdh}/rootFile`,
415 [`${destinationPath}/secondFile`]: `${srcCollectionPdh}/secondFile`,
416 [`${destinationPath}/subfile`]: `${srcCollectionPdh}/subpath/subfile`,
417 [`${destinationPath}/barefile.txt`]: `${srcCollectionPdh}/barefile.txt`,
424 describe('createDirectory', () => {
425 it('creates empty directory', async () => {
427 const directoryNames = [
428 {in: 'newDir', out: 'newDir'},
429 {in: '/fooDir', out: 'fooDir'},
430 {in: '/anotherPath/', out: 'anotherPath'},
431 {in: 'trailingSlash/', out: 'trailingSlash'},
433 const collectionUuid = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
435 for (var i = 0; i < directoryNames.length; i++) {
436 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
438 await collectionService.createDirectory(collectionUuid, directoryNames[i].in);
440 expect(serverApi.put).toHaveBeenCalledTimes(1);
441 expect(serverApi.put).toHaveBeenCalledWith(
442 `/collections/${collectionUuid}`, {
444 preserve_version: true
447 ["/" + directoryNames[i].out]: emptyCollectionPdh,