15159: Add unit tests for collection-file-viewer-action and TrustAllContent / secure... 15159-disable-open-in-new-tab-when-cannot-serve-inline
authorStephen Smith <stephen@curii.com>
Wed, 25 Aug 2021 05:28:36 +0000 (01:28 -0400)
committerStephen Smith <stephen@curii.com>
Wed, 25 Aug 2021 05:28:36 +0000 (01:28 -0400)
Arvados-DCO-1.1-Signed-off-by: Stephen Smith <stephen@curii.com>

src/views-components/context-menu/actions/collection-file-viewer-action.test.tsx [new file with mode: 0644]

diff --git a/src/views-components/context-menu/actions/collection-file-viewer-action.test.tsx b/src/views-components/context-menu/actions/collection-file-viewer-action.test.tsx
new file mode 100644 (file)
index 0000000..8b90f58
--- /dev/null
@@ -0,0 +1,117 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import React from 'react';
+import { mount, configure } from 'enzyme';
+import Adapter from 'enzyme-adapter-react-16';
+import configureMockStore from 'redux-mock-store'
+import { Provider } from 'react-redux';
+import { CollectionFileViewerAction } from './collection-file-viewer-action';
+import { ContextMenuKind } from 'views-components/context-menu/context-menu';
+import { createTree, initTreeNode, setNode, getNodeValue } from "models/tree";
+import { getInlineFileUrl, sanitizeToken } from "./helpers";
+
+const middlewares = [];
+const mockStore = configureMockStore(middlewares);
+
+configure({ adapter: new Adapter() });
+
+describe('CollectionFileViewerAction', () => {
+    let defaultStore;
+    const fileUrl = "https://download.host:12345/c=abcde-4zz18-abcdefghijklmno/t=v2/token2/token3/cat.jpg";
+    const insecureKeepInlineUrl = "https://download.host:12345/";
+    const secureKeepInlineUrl = "https://*.collections.host:12345/";
+
+    beforeEach(() => {
+        let filesTree = createTree();
+        let data = {id: "000", value: {"url": fileUrl}};
+        filesTree = setNode(initTreeNode(data))(filesTree);
+
+        defaultStore = {
+            auth: {
+                config: {
+                    keepWebServiceUrl: "https://download.host:12345/",
+                    keepWebInlineServiceUrl: insecureKeepInlineUrl,
+                    clusterConfig: {
+                        Collections: {
+                          TrustAllContent: false
+                        }
+                    }
+                }
+            },
+            contextMenu: {
+                resource: {
+                    uuid: "000",
+                    menuKind: ContextMenuKind.COLLECTION_FILE_ITEM,
+                }
+            },
+            collectionPanel: {
+                item: {
+                    uuid: ""
+                }
+            },
+            collectionPanelFiles: filesTree
+        };
+    });
+
+    it('should hide open in new tab when unsafe', () => {
+        // given
+        const store = mockStore(defaultStore);
+
+        // when
+        const wrapper = mount(<Provider store={store}>
+            <CollectionFileViewerAction />
+        </Provider>);
+
+        // then
+        expect(wrapper).not.toBeUndefined();
+
+        // and
+        expect(wrapper.find("a")).toHaveLength(0);
+    });
+
+    it('should show open in new tab when TrustAllContent=true', () => {
+        // given
+        let initialState = defaultStore;
+        initialState.auth.config.clusterConfig.Collections.TrustAllContent = true;
+        const store = mockStore(initialState);
+
+        // when
+        const wrapper = mount(<Provider store={store}>
+            <CollectionFileViewerAction />
+        </Provider>);
+
+        // then
+        expect(wrapper).not.toBeUndefined();
+
+        // and
+        expect(wrapper.find("a").prop("href"))
+            .toEqual(sanitizeToken(getInlineFileUrl(fileUrl,
+                initialState.auth.config.keepWebServiceUrl,
+                initialState.auth.config.keepWebInlineServiceUrl))
+            );
+    });
+
+    it('should show open in new tab when inline url is secure', () => {
+        // given
+        let initialState = defaultStore;
+        initialState.auth.config.keepWebInlineServiceUrl = secureKeepInlineUrl;
+        const store = mockStore(initialState);
+
+        // when
+        const wrapper = mount(<Provider store={store}>
+            <CollectionFileViewerAction />
+        </Provider>);
+
+        // then
+        expect(wrapper).not.toBeUndefined();
+
+        // and
+        expect(wrapper.find("a").prop("href"))
+            .toEqual(sanitizeToken(getInlineFileUrl(fileUrl,
+                initialState.auth.config.keepWebServiceUrl,
+                initialState.auth.config.keepWebInlineServiceUrl))
+            );
+    });
+});