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);
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 request with default selected fields', 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(
42 `/collections/${uuid}`, {
44 select: JSON.stringify(defaultCollectionSelectedFields.map(snakeCase)),
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(
57 `/collections/${uuid}`, {
59 select: `["manifest_text"]`
66 describe('update', () => {
67 it('should call put selecting updated fields + others', async () => {
68 serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
69 const data: Partial<CollectionResource> = {
75 preserve_version: true,
77 select: ['uuid', 'name', 'version', 'modified_at'],
79 collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
80 await collectionService.update('uuid', data);
81 expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
85 describe('deleteFiles', () => {
86 it('should remove no files', async () => {
88 const filePaths: string[] = [];
89 const collectionUUID = '';
92 await collectionService.deleteFiles(collectionUUID, filePaths);
95 expect(webdavClient.delete).not.toHaveBeenCalled();
98 it('should remove only root files', async () => {
100 const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
101 const collectionUUID = '';
104 await collectionService.deleteFiles(collectionUUID, filePaths);
107 expect(webdavClient.delete).toHaveBeenCalledTimes(3);
108 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
109 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
110 expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
113 it('should remove files with uuid prefix', async () => {
115 const filePaths: string[] = ['/root/1'];
116 const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
119 await collectionService.deleteFiles(collectionUUID, filePaths);
122 expect(webdavClient.delete).toHaveBeenCalledTimes(1);
123 expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");