21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / workbench2 / src / views-components / context-menu / actions / collection-file-viewer-action.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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";
14
15 const middlewares = [];
16 const mockStore = configureMockStore(middlewares);
17
18 configure({ adapter: new Adapter() });
19
20 describe('CollectionFileViewerAction', () => {
21     let defaultStore;
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/";
25
26     beforeEach(() => {
27         let filesTree = createTree();
28         let data = {id: "000", value: {"url": fileUrl}};
29         filesTree = setNode(initTreeNode(data))(filesTree);
30
31         defaultStore = {
32             auth: {
33                 config: {
34                     keepWebServiceUrl: "https://download.host:12345/",
35                     keepWebInlineServiceUrl: insecureKeepInlineUrl,
36                     clusterConfig: {
37                         Collections: {
38                           TrustAllContent: false
39                         }
40                     }
41                 }
42             },
43             contextMenu: {
44                 resource: {
45                     uuid: "000",
46                     menuKind: ContextMenuKind.COLLECTION_FILE_ITEM,
47                 }
48             },
49             collectionPanel: {
50                 item: {
51                     uuid: ""
52                 }
53             },
54             collectionPanelFiles: filesTree
55         };
56     });
57
58     it('should hide open in new tab when unsafe', () => {
59         // given
60         const store = mockStore(defaultStore);
61
62         // when
63         const wrapper = mount(<Provider store={store}>
64             <CollectionFileViewerAction />
65         </Provider>);
66
67         // then
68         expect(wrapper).not.toBeUndefined();
69
70         // and
71         expect(wrapper.find("a")).toHaveLength(0);
72     });
73
74     it('should show open in new tab when TrustAllContent=true', () => {
75         // given
76         let initialState = defaultStore;
77         initialState.auth.config.clusterConfig.Collections.TrustAllContent = true;
78         const store = mockStore(initialState);
79
80         // when
81         const wrapper = mount(<Provider store={store}>
82             <CollectionFileViewerAction />
83         </Provider>);
84
85         // then
86         expect(wrapper).not.toBeUndefined();
87
88         // and
89         expect(wrapper.find("a").prop("href"))
90             .toEqual(sanitizeToken(getInlineFileUrl(fileUrl,
91                 initialState.auth.config.keepWebServiceUrl,
92                 initialState.auth.config.keepWebInlineServiceUrl))
93             );
94     });
95
96     it('should show open in new tab when inline url is secure', () => {
97         // given
98         let initialState = defaultStore;
99         initialState.auth.config.keepWebInlineServiceUrl = secureKeepInlineUrl;
100         const store = mockStore(initialState);
101
102         // when
103         const wrapper = mount(<Provider store={store}>
104             <CollectionFileViewerAction />
105         </Provider>);
106
107         // then
108         expect(wrapper).not.toBeUndefined();
109
110         // and
111         expect(wrapper.find("a").prop("href"))
112             .toEqual(sanitizeToken(getInlineFileUrl(fileUrl,
113                 initialState.auth.config.keepWebServiceUrl,
114                 initialState.auth.config.keepWebInlineServiceUrl))
115             );
116     });
117 });