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 { CollectionResource } from 'models/collection';
8 import { AuthService } from '../auth-service/auth-service';
9 import { CollectionService } from './collection-service';
11 describe('collection-service', () => {
12 let collectionService: CollectionService;
13 let serverApi: AxiosInstance;
14 let axiosMock: MockAdapter;
15 let webdavClient: any;
20 serverApi = axios.create();
21 axiosMock = new MockAdapter(serverApi);
26 authService = {} as AuthService;
28 progressFn: jest.fn(),
30 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
31 collectionService.update = jest.fn();
34 describe('get', () => {
35 it('should make a list request with uuid filtering', async () => {
36 serverApi.get = jest.fn(() => Promise.resolve(
37 { data: { items: [{}] } }
39 const uuid = 'zzzzz-4zz18-0123456789abcde'
40 await collectionService.get(uuid);
41 expect(serverApi.get).toHaveBeenCalledWith(
44 filters: `[["uuid","=","zzzzz-4zz18-0123456789abcde"]]`,
45 include_old_versions: true,
51 it('should be able to request specific fields', async () => {
52 serverApi.get = jest.fn(() => Promise.resolve(
53 { data: { items: [{}] } }
55 const uuid = 'zzzzz-4zz18-0123456789abcde'
56 await collectionService.get(uuid, undefined, ['manifestText']);
57 expect(serverApi.get).toHaveBeenCalledWith(
60 filters: `[["uuid","=","zzzzz-4zz18-0123456789abcde"]]`,
61 include_old_versions: true,
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.only('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 const filePaths: string[] = [];
133 const collectionUUID = '';
136 await collectionService.deleteFiles(collectionUUID, filePaths);
139 expect(webdavClient.delete).not.toHaveBeenCalled();
142 it('should remove only root files', async () => {
144 const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
145 const collectionUUID = '';
148 await collectionService.deleteFiles(collectionUUID, filePaths);
151 expect(webdavClient.delete).toHaveBeenCalledTimes(3);
152 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
153 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
154 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
157 it('should remove files with uuid prefix', async () => {
159 const filePaths: string[] = ['/root/1'];
160 const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
163 await collectionService.deleteFiles(collectionUUID, filePaths);
166 expect(webdavClient.delete).toHaveBeenCalledTimes(1);
167 expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");