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);
25 authService = {} as AuthService;
27 progressFn: jest.fn(),
29 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
30 collectionService.update = jest.fn();
33 describe('get', () => {
34 it('should make a list request with uuid filtering', async () => {
35 serverApi.get = jest.fn(() => Promise.resolve(
36 { data: { items: [{}] } }
38 const uuid = 'zzzzz-4zz18-0123456789abcde'
39 await collectionService.get(uuid);
40 expect(serverApi.get).toHaveBeenCalledWith(
43 filters: `[["uuid","=","zzzzz-4zz18-0123456789abcde"]]`,
51 describe('update', () => {
52 it('should call put selecting updated fields + others', async () => {
53 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
54 const data: Partial<CollectionResource> = {
60 preserve_version: true,
62 select: ['uuid', 'name', 'version', 'modified_at'],
64 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
65 await collectionService.update('uuid', data);
66 expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
70 describe('deleteFiles', () => {
71 it('should remove no files', async () => {
73 const filePaths: string[] = [];
74 const collectionUUID = '';
77 await collectionService.deleteFiles(collectionUUID, filePaths);
80 expect(webdavClient.delete).not.toHaveBeenCalled();
83 it('should remove only root files', async () => {
85 const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
86 const collectionUUID = '';
89 await collectionService.deleteFiles(collectionUUID, filePaths);
92 expect(webdavClient.delete).toHaveBeenCalledTimes(3);
93 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
94 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
95 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
98 it('should remove files with uuid prefix', async () => {
100 const filePaths: string[] = ['/root/1'];
101 const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
104 await collectionService.deleteFiles(collectionUUID, filePaths);
107 expect(webdavClient.delete).toHaveBeenCalledTimes(1);
108 expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");