b0449c0aa8474b398edcbac41ddae3096bc1f1a7
[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 { authReducer, AuthState } from "./auth-reducer";
6 import { AuthAction, initAuth } from "./auth-action";
7 import {
8     API_TOKEN_KEY,
9     USER_EMAIL_KEY,
10     USER_FIRST_NAME_KEY,
11     USER_LAST_NAME_KEY,
12     USER_OWNER_UUID_KEY,
13     USER_UUID_KEY,
14     USER_IS_ADMIN,
15     USER_IS_ACTIVE,
16     USER_USERNAME,
17     USER_PREFS
18 } from "~/services/auth-service/auth-service";
19
20 import 'jest-localstorage-mock';
21 import { ServiceRepository, createServices } from "~/services/services";
22 import { configureStore, RootStore } from "../store";
23 import createBrowserHistory from "history/createBrowserHistory";
24 import { mockConfig } from '~/common/config';
25 import { ApiActions } from "~/services/api/api-actions";
26 import { ACCOUNT_LINK_STATUS_KEY } from '~/services/link-account-service/link-account-service';
27 import axios from "axios";
28 import MockAdapter from "axios-mock-adapter";
29
30 describe('auth-actions', () => {
31     const axiosMock = new MockAdapter(axios);
32
33     let reducer: (state: AuthState | undefined, action: AuthAction) => any;
34     let store: RootStore;
35     let services: ServiceRepository;
36     const actions: ApiActions = {
37         progressFn: (id: string, working: boolean) => { },
38         errorFn: (id: string, message: string) => { }
39     };
40
41     beforeEach(() => {
42         axiosMock.reset();
43         services = createServices(mockConfig({}), actions, axios);
44         store = configureStore(createBrowserHistory(), services);
45         localStorage.clear();
46         reducer = authReducer(services);
47     });
48
49     it('should initialise state with user and api token from local storage', () => {
50
51         axiosMock
52             .onGet("/users/current")
53             .reply(200, {
54                 email: "test@test.com",
55                 first_name: "John",
56                 last_name: "Doe",
57                 uuid: "zzzzz-tpzed-abcefg",
58                 ownerUuid: "ownerUuid",
59                 is_admin: false,
60                 is_active: true,
61                 username: "jdoe",
62                 prefs: {}
63             });
64
65         // Only test the case when a link account operation is not being cancelled
66         sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
67         localStorage.setItem(API_TOKEN_KEY, "token");
68
69         const config: any = {
70             rootUrl: "https://zzzzz.arvadosapi.com",
71             uuidPrefix: "zzzzz",
72             remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
73         };
74
75         store.dispatch(initAuth(config));
76
77         expect(store.getState().auth).toEqual({
78             apiToken: "token",
79             sshKeys: [],
80             homeCluster: "zzzzz",
81             localCluster: "zzzzz",
82             loginCluster: undefined,
83             remoteHostsConfig: {
84                 "zzzzz": {
85                     "remoteHosts": {
86                         "xc59z": "xc59z.arvadosapi.com",
87                     },
88                     "rootUrl": "https://zzzzz.arvadosapi.com",
89                     "uuidPrefix": "zzzzz",
90                 },
91             },
92             remoteHosts: {
93                 zzzzz: "zzzzz.arvadosapi.com",
94                 xc59z: "xc59z.arvadosapi.com"
95             },
96             sessions: [{
97                 "active": true,
98                 "baseUrl": undefined,
99                 "clusterId": "zzzzz",
100                 "email": "test@test.com",
101                 "loggedIn": true,
102                 "remoteHost": "https://zzzzz.arvadosapi.com",
103                 "status": 2,
104                 "token": "token",
105                 "name": "John Doe"
106                 "uuid": "zzzzz-tpzed-abcefg",
107             }, {
108                 "active": false,
109                 "baseUrl": "",
110                 "clusterId": "xc59z",
111                 "email": "",
112                 "loggedIn": false,
113                 "remoteHost": "xc59z.arvadosapi.com",
114                 "status": 1,
115                 "token": "",
116                 "name": "",
117                 "uuid": "",
118             }],
119             user: {
120                 email: "test@test.com",
121                 firstName: "John",
122                 lastName: "Doe",
123                 uuid: "zzzzz-tpzed-abcefg",
124                 ownerUuid: "ownerUuid",
125                 username: "jdoe",
126                 prefs: {},
127                 isAdmin: false,
128                 isActive: true
129             }
130         });
131     });
132
133     // TODO: Add remaining action tests
134     /*
135     it('should fire external url to login', () => {
136         const initialState = undefined;
137         window.location.assign = jest.fn();
138         reducer(initialState, authActions.LOGIN());
139         expect(window.location.assign).toBeCalledWith(
140             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
141         );
142     });
143
144     it('should fire external url to logout', () => {
145         const initialState = undefined;
146         window.location.assign = jest.fn();
147         reducer(initialState, authActions.LOGOUT());
148         expect(window.location.assign).toBeCalledWith(
149             `/logout?return_to=${location.protocol}//${location.host}`
150         );
151     });
152     */
153 });