1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { mount, configure } from 'enzyme';
7 import Adapter from 'enzyme-adapter-react-16';
8 import configureMockStore from 'redux-mock-store'
9 import { Provider } from 'react-redux';
10 import { CollectionFileViewerAction } from './collection-file-viewer-action';
11 import { ContextMenuKind } from '../menu-item-sort';
12 import { createTree, initTreeNode, setNode, getNodeValue } from "models/tree";
13 import { getInlineFileUrl, sanitizeToken } from "./helpers";
15 const middlewares = [];
16 const mockStore = configureMockStore(middlewares);
18 configure({ adapter: new Adapter() });
20 describe('CollectionFileViewerAction', () => {
22 const fileUrl = "https://download.host:12345/c=abcde-4zz18-abcdefghijklmno/t=v2/token2/token3/cat.jpg";
23 const insecureKeepInlineUrl = "https://download.host:12345/";
24 const secureKeepInlineUrl = "https://*.collections.host:12345/";
27 let filesTree = createTree();
28 let data = {id: "000", value: {"url": fileUrl}};
29 filesTree = setNode(initTreeNode(data))(filesTree);
34 keepWebServiceUrl: "https://download.host:12345/",
35 keepWebInlineServiceUrl: insecureKeepInlineUrl,
38 TrustAllContent: false
46 menuKind: ContextMenuKind.COLLECTION_FILE_ITEM,
54 collectionPanelFiles: filesTree
58 it('should hide open in new tab when unsafe', () => {
60 const store = mockStore(defaultStore);
63 const wrapper = mount(<Provider store={store}>
64 <CollectionFileViewerAction />
68 expect(wrapper).not.toBeUndefined();
71 expect(wrapper.find("a")).toHaveLength(0);
74 it('should show open in new tab when TrustAllContent=true', () => {
76 let initialState = defaultStore;
77 initialState.auth.config.clusterConfig.Collections.TrustAllContent = true;
78 const store = mockStore(initialState);
81 const wrapper = mount(<Provider store={store}>
82 <CollectionFileViewerAction />
86 expect(wrapper).not.toBeUndefined();
89 expect(wrapper.find("a").prop("href"))
90 .toEqual(sanitizeToken(getInlineFileUrl(fileUrl,
91 initialState.auth.config.keepWebServiceUrl,
92 initialState.auth.config.keepWebInlineServiceUrl))
96 it('should show open in new tab when inline url is secure', () => {
98 let initialState = defaultStore;
99 initialState.auth.config.keepWebInlineServiceUrl = secureKeepInlineUrl;
100 const store = mockStore(initialState);
103 const wrapper = mount(<Provider store={store}>
104 <CollectionFileViewerAction />
108 expect(wrapper).not.toBeUndefined();
111 expect(wrapper.find("a").prop("href"))
112 .toEqual(sanitizeToken(getInlineFileUrl(fileUrl,
113 initialState.auth.config.keepWebServiceUrl,
114 initialState.auth.config.keepWebInlineServiceUrl))