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 } 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);
27 authService = {} as AuthService;
29 progressFn: jest.fn(),
31 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
32 collectionService.update = jest.fn();
35 describe('get', () => {
36 it('should make a request with default selected fields', async () => {
37 serverApi.get = jest.fn(() => Promise.resolve(
38 { data: { items: [{}] } }
40 const uuid = 'zzzzz-4zz18-0123456789abcde'
41 await collectionService.get(uuid);
42 expect(serverApi.get).toHaveBeenCalledWith(
43 `/collections/${uuid}`, {
45 select: JSON.stringify(defaultCollectionSelectedFields.map(snakeCase)),
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(
58 `/collections/${uuid}`, {
60 select: `["manifest_text"]`
67 describe('update', () => {
68 it('should call put selecting updated fields + others', async () => {
69 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
70 const data: Partial<CollectionResource> = {
76 preserve_version: true,
78 select: ['uuid', 'name', 'version', 'modified_at'],
80 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
81 await collectionService.update('uuid', data);
82 expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
86 describe('uploadFiles', () => {
87 it('should skip if no files to upload files', async () => {
89 const files: File[] = [];
90 const collectionUUID = '';
93 await collectionService.uploadFiles(collectionUUID, files);
96 expect(webdavClient.upload).not.toHaveBeenCalled();
99 it('should upload files', async () => {
101 const files: File[] = [{name: 'test-file1'} as File];
102 const collectionUUID = 'zzzzz-4zz18-0123456789abcde';
105 await collectionService.uploadFiles(collectionUUID, files);
108 expect(webdavClient.upload).toHaveBeenCalledTimes(1);
109 expect(webdavClient.upload.mock.calls[0][0]).toEqual("c=zzzzz-4zz18-0123456789abcde/test-file1");
112 it('should upload files with custom uplaod target', async () => {
114 const files: File[] = [{name: 'test-file1'} as File];
115 const collectionUUID = 'zzzzz-4zz18-0123456789abcde';
116 const customTarget = 'zzzzz-4zz18-0123456789adddd/test-path/'
119 await collectionService.uploadFiles(collectionUUID, files, undefined, customTarget);
122 expect(webdavClient.upload).toHaveBeenCalledTimes(1);
123 expect(webdavClient.upload.mock.calls[0][0]).toEqual("c=zzzzz-4zz18-0123456789adddd/test-path/test-file1");
127 describe('deleteFiles', () => {
128 it('should remove no files', async () => {
130 const filePaths: string[] = [];
131 const collectionUUID = '';
134 await collectionService.deleteFiles(collectionUUID, filePaths);
137 expect(webdavClient.delete).not.toHaveBeenCalled();
140 it('should remove only root files', async () => {
142 const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
143 const collectionUUID = '';
146 await collectionService.deleteFiles(collectionUUID, filePaths);
149 expect(webdavClient.delete).toHaveBeenCalledTimes(3);
150 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
151 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
152 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
155 it('should remove files with uuid prefix', async () => {
157 const filePaths: string[] = ['/root/1'];
158 const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
161 await collectionService.deleteFiles(collectionUUID, filePaths);
164 expect(webdavClient.delete).toHaveBeenCalledTimes(1);
165 expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");