b759fd1a46d9cb622e9567196887667d71e6a9ab
[arvados-workbench2.git] / src / services / collection-service / collection-service.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
10
11 describe('collection-service', () => {
12     let collectionService: CollectionService;
13     let serverApi: AxiosInstance;
14     let axiosMock: MockAdapter;
15     let webdavClient: any;
16     let authService;
17     let actions;
18
19     beforeEach(() => {
20         serverApi = axios.create();
21         axiosMock = new MockAdapter(serverApi);
22         webdavClient = {
23             delete: jest.fn(),
24         } as any;
25         authService = {} as AuthService;
26         actions = {
27             progressFn: jest.fn(),
28         } as any;
29         collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
30         collectionService.update = jest.fn();
31     });
32
33     describe('get', () => {
34         it('should make a list request with uuid filtering', async () => {
35             serverApi.get = jest.fn(() => Promise.resolve(
36                 { data: { items: [{}] } }
37             ));
38             const uuid = 'zzzzz-4zz18-0123456789abcde'
39             await collectionService.get(uuid);
40             expect(serverApi.get).toHaveBeenCalledWith(
41                 '/collections', {
42                     params: {
43                         filters: `[["uuid","=","zzzzz-4zz18-0123456789abcde"]]`,
44                         include_old_versions: true,
45                     },
46                 }
47             );
48         });
49
50         it('should be able to request specific fields', async () => {
51             serverApi.get = jest.fn(() => Promise.resolve(
52                 { data: { items: [{}] } }
53             ));
54             const uuid = 'zzzzz-4zz18-0123456789abcde'
55             await collectionService.get(uuid, undefined, ['manifestText']);
56             expect(serverApi.get).toHaveBeenCalledWith(
57                 '/collections', {
58                     params: {
59                         filters: `[["uuid","=","zzzzz-4zz18-0123456789abcde"]]`,
60                         include_old_versions: true,
61                         select: `["manifest_text"]`
62                     },
63                 }
64             );
65         });
66     });
67
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> = {
72                 name: 'foo',
73             };
74             const expected = {
75                 collection: {
76                     ...data,
77                     preserve_version: true,
78                 },
79                 select: ['uuid', 'name', 'version', 'modified_at'],
80             }
81             collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
82             await collectionService.update('uuid', data);
83             expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
84         });
85     });
86
87     describe('deleteFiles', () => {
88         it('should remove no files', async () => {
89             // given
90             const filePaths: string[] = [];
91             const collectionUUID = '';
92
93             // when
94             await collectionService.deleteFiles(collectionUUID, filePaths);
95
96             // then
97             expect(webdavClient.delete).not.toHaveBeenCalled();
98         });
99
100         it('should remove only root files', async () => {
101             // given
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 = '';
104
105             // when
106             await collectionService.deleteFiles(collectionUUID, filePaths);
107
108             // then
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");
113         });
114
115         it('should remove files with uuid prefix', async () => {
116             // given
117             const filePaths: string[] = ['/root/1'];
118             const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
119
120             // when
121             await collectionService.deleteFiles(collectionUUID, filePaths);
122
123             // then
124             expect(webdavClient.delete).toHaveBeenCalledTimes(1);
125             expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");
126         });
127     });
128 });