Merge branch '17426-plug-ins' refs #17426
[arvados-workbench2.git] / src / services / collection-service / collection-service-files-response.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { CollectionFile } from '~/models/collection-file';
6 import { getFileFullPath, extractFilesData } from './collection-service-files-response';
7
8 describe('collection-service-files-response', () => {
9
10     describe('extractFilesData', () => {
11         it('should correctly decode URLs & file names', () => {
12             const testCases = [
13                 // input URL, input display name, expected URL, expected name
14                 ['table%201%202%203', 'table 1 2 3', 'table%201%202%203', 'table 1 2 3'],
15                 ['table%25&%3F%2A2', 'table%&?*2', 'table%25&%3F%2A2', 'table%&?*2'],
16                 ["G%C3%BCnter%27s%20file.pdf", "Günter's file.pdf", "G%C3%BCnter%27s%20file.pdf", "Günter's file.pdf"],
17                 ['G%25C3%25BCnter%27s%2520file.pdf', 'G%C3%BCnter's%20file.pdf', "G%25C3%25BCnter%27s%2520file.pdf", "G%C3%BCnter's%20file.pdf"]
18             ];
19
20             testCases.forEach(([inputURL, inputDisplayName, expectedURL, expectedName]) => {
21                 // given
22                 const collUUID = 'xxxxx-zzzzz-vvvvvvvvvvvvvvv';
23                 const xmlData = `
24                 <?xml version="1.0" encoding="UTF-8"?>
25                 <D:multistatus xmlns:D="DAV:">
26                     <D:response>
27                         <D:href>/c=xxxxx-zzzzz-vvvvvvvvvvvvvvv/</D:href>
28                         <D:propstat>
29                             <D:prop>
30                                 <D:resourcetype>
31                                     <D:collection xmlns:D="DAV:"/>
32                                 </D:resourcetype>
33                                 <D:supportedlock>
34                                     <D:lockentry xmlns:D="DAV:">
35                                         <D:lockscope>
36                                             <D:exclusive/>
37                                         </D:lockscope>
38                                         <D:locktype>
39                                             <D:write/>
40                                         </D:locktype>
41                                     </D:lockentry>
42                                 </D:supportedlock>
43                                 <D:displayname></D:displayname>
44                                 <D:getlastmodified>Fri, 26 Mar 2021 14:44:08 GMT</D:getlastmodified>
45                             </D:prop>
46                             <D:status>HTTP/1.1 200 OK</D:status>
47                         </D:propstat>
48                     </D:response>
49                     <D:response>
50                         <D:href>/c=${collUUID}/${inputURL}</D:href>
51                         <D:propstat>
52                             <D:prop>
53                                 <D:resourcetype></D:resourcetype>
54                                 <D:getcontenttype>application/pdf</D:getcontenttype>
55                                 <D:supportedlock>
56                                     <D:lockentry xmlns:D="DAV:">
57                                         <D:lockscope>
58                                             <D:exclusive/>
59                                         </D:lockscope>
60                                         <D:locktype>
61                                             <D:write/>
62                                         </D:locktype>
63                                     </D:lockentry>
64                                 </D:supportedlock>
65                                 <D:displayname>${inputDisplayName}</D:displayname>
66                                 <D:getcontentlength>3</D:getcontentlength>
67                                 <D:getlastmodified>Fri, 26 Mar 2021 14:44:08 GMT</D:getlastmodified>
68                                 <D:getetag>"166feb9c9110c008325a59"</D:getetag>
69                             </D:prop>
70                             <D:status>HTTP/1.1 200 OK</D:status>
71                         </D:propstat>
72                     </D:response>
73                 </D:multistatus>
74                 `;
75                 const parser = new DOMParser();
76                 const xmlDoc = parser.parseFromString(xmlData, "text/xml");
77
78                 // when
79                 const result = extractFilesData(xmlDoc);
80
81                 // then
82                 expect(result).toEqual([{ id: `${collUUID}/${expectedName}`, name: expectedName, path: "", size: 3, type: "file", url: `/c=${collUUID}/${expectedURL}` }]);
83             });
84         });
85     });
86
87     describe('getFileFullPath', () => {
88         it('should encode weird names', async () => {
89             // given
90             const file = {
91                 name: '#test',
92                 path: 'http://localhost',
93             } as CollectionFile;
94
95             // when
96             const result = getFileFullPath(file);
97
98             // then
99             expect(result).toBe('http://localhost/#test');
100         });
101
102     });
103 });