17337: Improves unit test readability & expandability.
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Fri, 26 Mar 2021 19:02:29 +0000 (16:02 -0300)
committerLucas Di Pentima <lucas@di-pentima.com.ar>
Fri, 26 Mar 2021 19:02:29 +0000 (16:02 -0300)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

src/services/collection-service/collection-service-files-response.test.ts

index 17a1f38820929d6aa8a7809216891db972ded805..b2480c4bd2429c12dc0b5de8a05fd95773bb4bdd 100644 (file)
@@ -8,30 +8,79 @@ import { getFileFullPath, extractFilesData } from './collection-service-files-re
 describe('collection-service-files-response', () => {
 
     describe('extractFilesData', () => {
-        it('should extract', () => {
-            // given
-            const xmlString = '<D:multistatus xmlns:D="DAV:"><D:response><D:href>/c=xxxxx-zzzzz-vvvvvvvvvvvvvvv/</D:href><D:propstat><D:prop><D:resourcetype><D:collection xmlns:D="DAV:"/></D:resourcetype><D:getlastmodified>Wed, 24 Feb 2021 22:16:19 GMT</D:getlastmodified><D:supportedlock><D:lockentry xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:displayname></D:displayname></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response><D:response><D:href>/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/2</D:href><D:propstat><D:prop><D:getcontentlength>1582976</D:getcontentlength><D:getetag>"1666cee048aa7f98182780"</D:getetag><D:resourcetype></D:resourcetype><D:displayname>2</D:displayname><D:getlastmodified>Wed, 24 Feb 2021 22:16:19 GMT</D:getlastmodified><D:getcontenttype>text/plain; charset=utf-8</D:getcontenttype><D:supportedlock><D:lockentry xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response><D:response><D:href>/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/table%201%202%203</D:href><D:propstat><D:prop><D:resourcetype></D:resourcetype><D:getcontentlength>133352</D:getcontentlength><D:getetag>"1666cee048aa7f98208e8"</D:getetag><D:displayname>table 1 2 3</D:displayname><D:getlastmodified>Wed, 24 Feb 2021 22:16:19 GMT</D:getlastmodified><D:getcontenttype>text/plain; charset=utf-8</D:getcontenttype><D:supportedlock><D:lockentry xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>';
-            const parser = new DOMParser();
-            const xmlDoc = parser.parseFromString(xmlString, "text/xml");
+        it('should correctly decode URLs & file names', () => {
+            const testCases = [
+                // input URL, input display name, expected URL, expected name
+                ['table%201%202%203', 'table 1 2 3', 'table 1 2 3', 'table 1 2 3'],
+                ['table%25&amp;%3F%2A2', 'table%&amp;?*2', 'table%&?*2', 'table%&?*2'],
+                ["G%C3%BCnter%27s%20file.pdf", "Günter&#39;s file.pdf", "Günter's file.pdf", "Günter's file.pdf"],
+                ['G%25C3%25BCnter%27s%2520file.pdf', 'G%C3%BCnter&#39;s%20file.pdf', "G%C3%BCnter's%20file.pdf", "G%C3%BCnter's%20file.pdf"]
+            ];
 
-            // when
-            const result = extractFilesData(xmlDoc);
+            testCases.forEach(([inputURL, inputDisplayName, expectedURL, expectedName]) => {
+                // given
+                const collUUID = 'xxxxx-zzzzz-vvvvvvvvvvvvvvv';
+                const xmlData = `
+                <?xml version="1.0" encoding="UTF-8"?>
+                <D:multistatus xmlns:D="DAV:">
+                    <D:response>
+                        <D:href>/c=xxxxx-zzzzz-vvvvvvvvvvvvvvv/</D:href>
+                        <D:propstat>
+                            <D:prop>
+                                <D:resourcetype>
+                                    <D:collection xmlns:D="DAV:"/>
+                                </D:resourcetype>
+                                <D:supportedlock>
+                                    <D:lockentry xmlns:D="DAV:">
+                                        <D:lockscope>
+                                            <D:exclusive/>
+                                        </D:lockscope>
+                                        <D:locktype>
+                                            <D:write/>
+                                        </D:locktype>
+                                    </D:lockentry>
+                                </D:supportedlock>
+                                <D:displayname></D:displayname>
+                                <D:getlastmodified>Fri, 26 Mar 2021 14:44:08 GMT</D:getlastmodified>
+                            </D:prop>
+                            <D:status>HTTP/1.1 200 OK</D:status>
+                        </D:propstat>
+                    </D:response>
+                    <D:response>
+                        <D:href>/c=${collUUID}/${inputURL}</D:href>
+                        <D:propstat>
+                            <D:prop>
+                                <D:resourcetype></D:resourcetype>
+                                <D:getcontenttype>application/pdf</D:getcontenttype>
+                                <D:supportedlock>
+                                    <D:lockentry xmlns:D="DAV:">
+                                        <D:lockscope>
+                                            <D:exclusive/>
+                                        </D:lockscope>
+                                        <D:locktype>
+                                            <D:write/>
+                                        </D:locktype>
+                                    </D:lockentry>
+                                </D:supportedlock>
+                                <D:displayname>${inputDisplayName}</D:displayname>
+                                <D:getcontentlength>3</D:getcontentlength>
+                                <D:getlastmodified>Fri, 26 Mar 2021 14:44:08 GMT</D:getlastmodified>
+                                <D:getetag>"166feb9c9110c008325a59"</D:getetag>
+                            </D:prop>
+                            <D:status>HTTP/1.1 200 OK</D:status>
+                        </D:propstat>
+                    </D:response>
+                </D:multistatus>
+                `;
+                const parser = new DOMParser();
+                const xmlDoc = parser.parseFromString(xmlData, "text/xml");
 
-            // then
-            expect(result).toEqual([{ id: "zzzzz-xxxxx-vvvvvvvvvvvvvvv/2", name: "2", path: "", size: 1582976, type: "file", url: "/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/2" }, { id: "zzzzz-xxxxx-vvvvvvvvvvvvvvv/table 1 2 3", name: "table 1 2 3", path: "", size: 133352, type: "file", url: "/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/table 1 2 3" }]);
-        });
+                // when
+                const result = extractFilesData(xmlDoc);
 
-        it('should extract ecoded data and do not encode already encoded props', () => {
-            // given
-            const xmlString = '<?xml version="1.0" encoding="UTF-8"?><D:multistatus xmlns:D="DAV:"><D:response><D:href>/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/</D:href><D:propstat><D:prop><D:resourcetype><D:collection xmlns:D="DAV:"/></D:resourcetype><D:getlastmodified>Fri, 26 Mar 2021 11:45:50 GMT</D:getlastmodified><D:supportedlock><D:lockentry xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:displayname></D:displayname></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response><D:response><D:href>/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/table%25&amp;%3F%2A2</D:href><D:propstat><D:prop><D:resourcetype></D:resourcetype><D:getcontentlength>3</D:getcontentlength><D:getlastmodified>Fri, 26 Mar 2021 11:45:50 GMT</D:getlastmodified><D:getetag>"166fe1e1a403fb683"</D:getetag><D:getcontenttype>text/plain; charset=utf-8</D:getcontenttype><D:supportedlock><D:lockentry xmlns:D="DAV:"><D:lockscope><D:exclusive/></D:lockscope><D:locktype><D:write/></D:locktype></D:lockentry></D:supportedlock><D:displayname>table%&amp;?*2</D:displayname></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat></D:response></D:multistatus>';
-            const parser = new DOMParser();
-            const xmlDoc = parser.parseFromString(xmlString, "text/xml");
-
-            // when
-            const result = extractFilesData(xmlDoc);
-
-            // then
-            expect(result).toEqual([{ id: "zzzzz-xxxxx-vvvvvvvvvvvvvvv/table%&?*2", name: "table%&?*2", path: "", size: 3, type: "file", url: "/c=zzzzz-xxxxx-vvvvvvvvvvvvvvv/table%&?*2" }]);
+                // then
+                expect(result).toEqual([{ id: `${collUUID}/${expectedURL}`, name: expectedName, path: "", size: 3, type: "file", url: `/c=${collUUID}/${expectedURL}` }]);
+            });
         });
     });