16812: Removed download attribute, fixed redirectTo
[arvados-workbench2.git] / src / store / auth / auth-action.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { initAuth } from "./auth-action";
6 import { API_TOKEN_KEY } from "~/services/auth-service/auth-service";
7
8 import 'jest-localstorage-mock';
9 import { ServiceRepository, createServices } from "~/services/services";
10 import { configureStore, RootStore } from "../store";
11 import { createBrowserHistory } from "history";
12 import { mockConfig, DISCOVERY_DOC_PATH, } from '~/common/config';
13 import { ApiActions } from "~/services/api/api-actions";
14 import { ACCOUNT_LINK_STATUS_KEY } from '~/services/link-account-service/link-account-service';
15 import Axios from "axios";
16 import MockAdapter from "axios-mock-adapter";
17 import { ImportMock } from 'ts-mock-imports';
18 import * as servicesModule from "~/services/services";
19
20 describe('auth-actions', () => {
21     const axiosInst = Axios.create({ headers: {} });
22     const axiosMock = new MockAdapter(axiosInst);
23
24     let store: RootStore;
25     let services: ServiceRepository;
26     const config: any = {};
27     const actions: ApiActions = {
28         progressFn: (id: string, working: boolean) => { },
29         errorFn: (id: string, message: string) => { }
30     };
31     let importMocks: any[];
32
33     beforeEach(() => {
34         axiosMock.reset();
35         services = createServices(mockConfig({}), actions, axiosInst);
36         store = configureStore(createBrowserHistory(), services, config);
37         localStorage.clear();
38         importMocks = [];
39     });
40
41     afterEach(() => {
42         importMocks.map(m => m.restore());
43     });
44
45     it('should initialise state with user and api token from local storage', (done) => {
46
47         axiosMock
48             .onGet("/users/current")
49             .reply(200, {
50                 email: "test@test.com",
51                 first_name: "John",
52                 last_name: "Doe",
53                 uuid: "zzzzz-tpzed-abcefg",
54                 owner_uuid: "ownerUuid",
55                 is_admin: false,
56                 is_active: true,
57                 username: "jdoe",
58                 prefs: {}
59             });
60
61         axiosMock
62             .onGet("https://xc59z.arvadosapi.com/discovery/v1/apis/arvados/v1/rest")
63             .reply(200, {
64                 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
65                 keepWebServiceUrl: "",
66                 remoteHosts: {},
67                 rootUrl: "https://xc59z.arvadosapi.com",
68                 uuidPrefix: "xc59z",
69                 websocketUrl: "",
70                 workbenchUrl: "",
71                 workbench2Url: "",
72                 revision: 12345678
73             });
74
75         importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
76
77         // Only test the case when a link account operation is not being cancelled
78         sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
79         localStorage.setItem(API_TOKEN_KEY, "token");
80
81         const config: any = {
82             rootUrl: "https://zzzzz.arvadosapi.com",
83             uuidPrefix: "zzzzz",
84             remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
85             apiRevision: 12345678,
86         };
87
88         store.dispatch(initAuth(config));
89
90         store.subscribe(() => {
91             const auth = store.getState().auth;
92             if (auth.apiToken === "token" &&
93                 auth.sessions.length === 2 &&
94                 auth.sessions[0].status === 2 &&
95                 auth.sessions[1].status === 2
96             ) {
97                 try {
98                     expect(auth).toEqual({
99                         apiToken: "token",
100                         config: {
101                             apiRevision: 12345678,
102                             remoteHosts: {
103                                 "xc59z": "xc59z.arvadosapi.com",
104                             },
105                             rootUrl: "https://zzzzz.arvadosapi.com",
106                             uuidPrefix: "zzzzz",
107                         },
108                         sshKeys: [],
109                         homeCluster: "zzzzz",
110                         localCluster: "zzzzz",
111                         loginCluster: undefined,
112                         remoteHostsConfig: {
113                             "zzzzz": {
114                                 "apiRevision": 12345678,
115                                 "remoteHosts": {
116                                     "xc59z": "xc59z.arvadosapi.com",
117                                 },
118                                 "rootUrl": "https://zzzzz.arvadosapi.com",
119                                 "uuidPrefix": "zzzzz",
120                             },
121                             "xc59z": mockConfig({
122                                 apiRevision: 12345678,
123                                 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
124                                 rootUrl: "https://xc59z.arvadosapi.com",
125                                 uuidPrefix: "xc59z"
126                             })
127                         },
128                         remoteHosts: {
129                             zzzzz: "zzzzz.arvadosapi.com",
130                             xc59z: "xc59z.arvadosapi.com"
131                         },
132                         sessions: [{
133                             "active": true,
134                             "baseUrl": undefined,
135                             "clusterId": "zzzzz",
136                             "email": "test@test.com",
137                             "loggedIn": true,
138                             "remoteHost": "https://zzzzz.arvadosapi.com",
139                             "status": 2,
140                             "token": "token",
141                             "name": "John Doe",
142                             "apiRevision": 12345678,
143                             "uuid": "zzzzz-tpzed-abcefg",
144                             "userIsActive": true
145                         }, {
146                             "active": false,
147                             "baseUrl": "",
148                             "clusterId": "xc59z",
149                             "email": "",
150                             "loggedIn": false,
151                             "remoteHost": "xc59z.arvadosapi.com",
152                             "status": 2,
153                             "token": "",
154                             "name": "",
155                             "uuid": "",
156                             "apiRevision": 0,
157                         }],
158                         user: {
159                             email: "test@test.com",
160                             firstName: "John",
161                             lastName: "Doe",
162                             uuid: "zzzzz-tpzed-abcefg",
163                             ownerUuid: "ownerUuid",
164                             username: "jdoe",
165                             prefs: { profile: {} },
166                             isAdmin: false,
167                             isActive: true
168                         }
169                     });
170                     done();
171                 } catch (e) {
172                     console.log(e);
173                 }
174             }
175         });
176     });
177
178
179     // TODO: Add remaining action tests
180     /*
181        it('should fire external url to login', () => {
182        const initialState = undefined;
183        window.location.assign = jest.fn();
184        reducer(initialState, authActions.LOGIN());
185        expect(window.location.assign).toBeCalledWith(
186        `/login?return_to=${window.location.protocol}//${window.location.host}/token`
187        );
188        });
189
190        it('should fire external url to logout', () => {
191        const initialState = undefined;
192        window.location.assign = jest.fn();
193        reducer(initialState, authActions.LOGOUT());
194        expect(window.location.assign).toBeCalledWith(
195        `/logout?return_to=${location.protocol}//${location.host}`
196        );
197        });
198      */
199 });