Merge branch '22509-go1236'
[arvados.git] / services / workbench2 / src / views-components / token-dialog / token-dialog.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 { TokenDialogComponent } from './token-dialog';
7 import { ThemeProvider } from "@mui/material";
8 import { CustomTheme } from 'common/custom-theme';
9 import { combineReducers, createStore } from "redux";
10 import { Provider } from "react-redux";
11
12 describe('<CurrentTokenDialog />', () => {
13   let props;
14   let store;
15
16   beforeEach(() => {
17     props = {
18       classes: {},
19       token: 'xxxtokenxxx',
20       apiHost: 'example.com',
21       open: true,
22       dispatch: cy.spy().as('dispatch'),
23     };
24
25     const initialAuthState = {
26       localCluster: "zzzzz",
27       remoteHostsConfig: {},
28       sessions: {},
29     };
30
31     store = createStore(combineReducers({
32       auth: (state = initialAuthState, action) => state,
33     }));
34   });
35
36   describe('Get API Token dialog', () => {
37     beforeEach(() => {
38       cy.mount(
39         <Provider store={store}>
40           <ThemeProvider theme={CustomTheme}>
41             <TokenDialogComponent {...props} />
42           </ThemeProvider>
43         </Provider>);
44     });
45
46     it('should include API host and token', () => {
47       cy.get('pre').contains('export ARVADOS_API_HOST=example.com');
48       cy.get('pre').contains('export ARVADOS_API_TOKEN=xxxtokenxxx');
49     });
50
51     it('should show the token expiration if present', () => {
52       expect(props.tokenExpiration).to.be.undefined;
53       cy.get('[data-cy=details-attribute-value]').contains('This token does not have an expiration date');
54
55       const someDate = '2140-01-01T00:00:00.000Z'
56       props.tokenExpiration = new Date(someDate);
57       cy.mount(
58         <Provider store={store}>
59           <ThemeProvider theme={CustomTheme}>
60             <TokenDialogComponent {...props} />
61           </ThemeProvider>
62         </Provider>);
63       cy.get('[data-cy=details-attribute-value]').contains(props.tokenExpiration.toLocaleString());
64     });
65
66     it('should show a create new token button when allowed', () => {
67       expect(!!props.canCreateNewTokens).to.equal(false);
68       cy.contains('GET NEW TOKEN').should('not.exist');
69
70       props.canCreateNewTokens = true;
71       cy.mount(
72         <Provider store={store}>
73           <ThemeProvider theme={CustomTheme}>
74             <TokenDialogComponent {...props} />
75           </ThemeProvider>
76         </Provider>);
77       cy.contains('GET NEW TOKEN').should('exist');
78     });
79   });
80
81   describe('Copy link to clipboard button', () => {
82     beforeEach(() => {
83       cy.mount(
84         <Provider store={store}>
85           <ThemeProvider theme={CustomTheme}>
86             <TokenDialogComponent {...props} />
87           </ThemeProvider>
88         </Provider>);
89     });
90
91     it('should copy API TOKEN to the clipboard', () => {
92       cy.get('button').contains('Copy').click();
93       cy.get('@dispatch').should('be.calledWith', {
94         payload: {
95           hideDuration: 2000,
96           kind: 1,
97           message: 'Shell code block copied',
98         },
99         type: 'OPEN_SNACKBAR',
100       });
101     });
102   });
103 });