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"]]`,
44 include_old_versions: true,
50 it('should be able to request specific fields', async () => {
51 serverApi.get = jest.fn(() => Promise.resolve(
52 { data: { items: [{}] } }
54 const uuid = 'zzzzz-4zz18-0123456789abcde'
55 await collectionService.get(uuid, undefined, ['manifestText']);
56 expect(serverApi.get).toHaveBeenCalledWith(
59 filters: `[["uuid","=","zzzzz-4zz18-0123456789abcde"]]`,
60 include_old_versions: true,
61 select: `["manifest_text"]`
68 describe('update', () => {
69 it('should call put selecting updated fields + others', async () => {
70 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
71 const data: Partial<CollectionResource> = {
77 preserve_version: true,
79 select: ['uuid', 'name', 'version', 'modified_at'],
81 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
82 await collectionService.update('uuid', data);
83 expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
87 describe('deleteFiles', () => {
88 it('should remove no files', async () => {
90 const filePaths: string[] = [];
91 const collectionUUID = '';
94 await collectionService.deleteFiles(collectionUUID, filePaths);
97 expect(webdavClient.delete).not.toHaveBeenCalled();
100 it('should remove only root files', async () => {
102 const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
103 const collectionUUID = '';
106 await collectionService.deleteFiles(collectionUUID, filePaths);
109 expect(webdavClient.delete).toHaveBeenCalledTimes(3);
110 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
111 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
112 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
115 it('should remove files with uuid prefix', async () => {
117 const filePaths: string[] = ['/root/1'];
118 const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
121 await collectionService.deleteFiles(collectionUUID, filePaths);
124 expect(webdavClient.delete).toHaveBeenCalledTimes(1);
125 expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");