21362: Search for bundler in the arvados-api-server package test
[arvados.git] / services / workbench2 / src / views-components / context-menu / actions / collection-file-viewer-action.cy.js
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 configureMockStore from 'redux-mock-store'
7 import { Provider } from 'react-redux';
8 import { CollectionFileViewerAction } from './collection-file-viewer-action';
9 import { ContextMenuKind } from '../menu-item-sort';
10 import { createTree, initTreeNode, setNode } from "models/tree";
11 import { getInlineFileUrl, sanitizeToken } from "./helpers";
12
13 const middlewares = [];
14 const mockStore = configureMockStore(middlewares);
15
16 describe('CollectionFileViewerAction', () => {
17     let defaultStore;
18     const fileUrl = "https://download.host:12345/c=abcde-4zz18-abcdefghijklmno/t=v2/token2/token3/cat.jpg";
19     const insecureKeepInlineUrl = "https://download.host:12345/";
20     const secureKeepInlineUrl = "https://*.collections.host:12345/";
21
22     beforeEach(() => {
23         let filesTree = createTree();
24         let data = {id: "000", value: {"url": fileUrl}};
25         filesTree = setNode(initTreeNode(data))(filesTree);
26
27         defaultStore = {
28             auth: {
29                 config: {
30                     keepWebServiceUrl: "https://download.host:12345/",
31                     keepWebInlineServiceUrl: insecureKeepInlineUrl,
32                     clusterConfig: {
33                         Collections: {
34                           TrustAllContent: false
35                         }
36                     }
37                 }
38             },
39             contextMenu: {
40                 resource: {
41                     uuid: "000",
42                     menuKind: ContextMenuKind.COLLECTION_FILE_ITEM,
43                 }
44             },
45             collectionPanel: {
46                 item: {
47                     uuid: ""
48                 }
49             },
50             collectionPanelFiles: filesTree
51         };
52     });
53
54     it('should hide open in new tab when unsafe', () => {
55         // given
56         const store = mockStore(defaultStore);
57
58         // when
59         cy.mount(<Provider store={store}>
60             <CollectionFileViewerAction />
61         </Provider>);
62
63         // ensure cy.mount has been successful
64         cy.get('[data-cy-root').should('exist');
65
66         // and
67         cy.get('[data-cy=open-in-new-tab]').should('have.length', 0);
68     });
69
70     it('should show open in new tab when TrustAllContent=true', () => {
71         // given
72         let initialState = defaultStore;
73         initialState.auth.config.clusterConfig.Collections.TrustAllContent = true;
74         const store = mockStore(initialState);
75
76         // when
77         cy.mount(<Provider store={store}>
78             <CollectionFileViewerAction />
79         </Provider>);
80
81         // then
82         cy.get('[data-cy=open-in-new-tab]').should('exist');
83
84         // and
85         cy.get('[data-cy=open-in-new-tab]').should(
86             'have.attr',
87             'href',
88             sanitizeToken(getInlineFileUrl(fileUrl, initialState.auth.config.keepWebServiceUrl, initialState.auth.config.keepWebInlineServiceUrl))
89         );
90     });
91
92     it('should show open in new tab when inline url is secure', () => {
93         // given
94         let initialState = defaultStore;
95         initialState.auth.config.keepWebInlineServiceUrl = secureKeepInlineUrl;
96         const store = mockStore(initialState);
97
98         // when
99         cy.mount(<Provider store={store}>
100             <CollectionFileViewerAction />
101         </Provider>);
102
103         // then
104         cy.get('[data-cy=open-in-new-tab]').should('exist');
105
106         // and
107         sanitizeToken(getInlineFileUrl(fileUrl, initialState.auth.config.keepWebServiceUrl, initialState.auth.config.keepWebInlineServiceUrl))
108     });
109 });