21720:
[arvados.git] / services / workbench2 / 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 = `<?xml version="1.0" encoding="UTF-8"?>
24                 <D:multistatus xmlns:D="DAV:">
25                     <D:response>
26                         <D:href>/c=xxxxx-zzzzz-vvvvvvvvvvvvvvv/</D:href>
27                         <D:propstat>
28                             <D:prop>
29                                 <D:resourcetype>
30                                     <D:collection xmlns:D="DAV:"/>
31                                 </D:resourcetype>
32                                 <D:supportedlock>
33                                     <D:lockentry xmlns:D="DAV:">
34                                         <D:lockscope>
35                                             <D:exclusive/>
36                                         </D:lockscope>
37                                         <D:locktype>
38                                             <D:write/>
39                                         </D:locktype>
40                                     </D:lockentry>
41                                 </D:supportedlock>
42                                 <D:displayname></D:displayname>
43                                 <D:getlastmodified>Fri, 26 Mar 2021 14:44:08 GMT</D:getlastmodified>
44                             </D:prop>
45                             <D:status>HTTP/1.1 200 OK</D:status>
46                         </D:propstat>
47                     </D:response>
48                     <D:response>
49                         <D:href>/c=${collUUID}/${inputURL}</D:href>
50                         <D:propstat>
51                             <D:prop>
52                                 <D:resourcetype></D:resourcetype>
53                                 <D:getcontenttype>application/pdf</D:getcontenttype>
54                                 <D:supportedlock>
55                                     <D:lockentry xmlns:D="DAV:">
56                                         <D:lockscope>
57                                             <D:exclusive/>
58                                         </D:lockscope>
59                                         <D:locktype>
60                                             <D:write/>
61                                         </D:locktype>
62                                     </D:lockentry>
63                                 </D:supportedlock>
64                                 <D:displayname>${inputDisplayName}</D:displayname>
65                                 <D:getcontentlength>3</D:getcontentlength>
66                                 <D:getlastmodified>Fri, 26 Mar 2021 14:44:08 GMT</D:getlastmodified>
67                                 <D:getetag>"166feb9c9110c008325a59"</D:getetag>
68                             </D:prop>
69                             <D:status>HTTP/1.1 200 OK</D:status>
70                         </D:propstat>
71                     </D:response>
72                 </D:multistatus>
73                 `;
74                 const parser = new DOMParser();
75                 const xmlDoc = parser.parseFromString(xmlData, "text/xml");
76
77                 // when
78                 const result = extractFilesData(xmlDoc);
79
80                 // then
81                 expect(result).toEqual([{ id: `${collUUID}/${expectedName}`, name: expectedName, path: "", size: 3, type: "file", url: `/c=${collUUID}/${expectedURL}` }]);
82             });
83         });
84     });
85
86     describe('getFileFullPath', () => {
87         it('should encode weird names', async () => {
88             // given
89             const file = {
90                 name: '#test',
91                 path: 'http://localhost',
92             } as CollectionFile;
93
94             // when
95             const result = getFileFullPath(file);
96
97             // then
98             expect(result).toBe('http://localhost/#test');
99         });
100
101     });
102 });