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('update', () => {
34 it('should call put selecting updated fields + others', async () => {
35 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
36 const data: Partial<CollectionResource> = {
42 preserve_version: true,
44 select: ['uuid', 'name', 'version', 'modified_at'],
46 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
47 await collectionService.update('uuid', data);
48 expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
52 describe('deleteFiles', () => {
53 it('should remove no files', async () => {
55 const filePaths: string[] = [];
56 const collectionUUID = '';
59 await collectionService.deleteFiles(collectionUUID, filePaths);
62 expect(webdavClient.delete).not.toHaveBeenCalled();
65 it('should remove only root files', async () => {
67 const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
68 const collectionUUID = '';
71 await collectionService.deleteFiles(collectionUUID, filePaths);
74 expect(webdavClient.delete).toHaveBeenCalledTimes(3);
75 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
76 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
77 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
80 it('should remove files with uuid prefix', async () => {
82 const filePaths: string[] = ['/root/1'];
83 const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
86 await collectionService.deleteFiles(collectionUUID, filePaths);
89 expect(webdavClient.delete).toHaveBeenCalledTimes(1);
90 expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");