Merge branch 'master' into 15067-tag-editing-by-ids
[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 { 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
28 describe('auth-actions', () => {
29     let reducer: (state: AuthState | undefined, action: AuthAction) => any;
30     let store: RootStore;
31     const actions: ApiActions = {
32         progressFn: (id: string, working: boolean) => { },
33         errorFn: (id: string, message: string) => { }
34     };
35
36     beforeEach(() => {
37         store = configureStore(createBrowserHistory(), createServices(mockConfig({}), actions));
38         localStorage.clear();
39         reducer = authReducer(createServices(mockConfig({}), actions));
40     });
41
42     it('should initialise state with user and api token from local storage', () => {
43
44         // Only test the case when a link account operation is not being cancelled
45         sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
46         localStorage.setItem(API_TOKEN_KEY, "token");
47         localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
48         localStorage.setItem(USER_FIRST_NAME_KEY, "John");
49         localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
50         localStorage.setItem(USER_UUID_KEY, "zzzzz-tpzed-abcefg");
51         localStorage.setItem(USER_USERNAME, "username");
52         localStorage.setItem(USER_PREFS, JSON.stringify({}));
53         localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
54         localStorage.setItem(USER_IS_ADMIN, JSON.stringify(false));
55         localStorage.setItem(USER_IS_ACTIVE, JSON.stringify(true));
56
57         const config: any = {
58             rootUrl: "https://zzzzz.arvadosapi.com",
59             uuidPrefix: "zzzzz",
60             remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
61         };
62
63         store.dispatch(initAuth(config));
64
65         expect(store.getState().auth).toEqual({
66             apiToken: "token",
67             sshKeys: [],
68             homeCluster: "zzzzz",
69             localCluster: "zzzzz",
70             loginCluster: undefined,
71             remoteHostsConfig: {
72                 "zzzzz": {
73                     "remoteHosts": {
74                         "xc59z": "xc59z.arvadosapi.com",
75                     },
76                     "rootUrl": "https://zzzzz.arvadosapi.com",
77                     "uuidPrefix": "zzzzz",
78                 },
79             },
80             remoteHosts: {
81                 zzzzz: "zzzzz.arvadosapi.com",
82                 xc59z: "xc59z.arvadosapi.com"
83             },
84             sessions: [{
85                 "active": true,
86                 "baseUrl": undefined,
87                 "clusterId": "zzzzz",
88                 "email": "test@test.com",
89                 "loggedIn": true,
90                 "remoteHost": "https://zzzzz.arvadosapi.com",
91                 "status": 2,
92                 "token": "token",
93                 "name": "John Doe"
94                 "uuid": "zzzzz-tpzed-abcefg",
95             }, {
96                 "active": false,
97                 "baseUrl": "",
98                 "clusterId": "xc59z",
99                 "email": "",
100                 "loggedIn": false,
101                 "remoteHost": "xc59z.arvadosapi.com",
102                 "status": 1,
103                 "token": "",
104                 "name": "",
105                 "uuid": "",
106             }],
107             user: {
108                 email: "test@test.com",
109                 firstName: "John",
110                 lastName: "Doe",
111                 uuid: "zzzzz-tpzed-abcefg",
112                 ownerUuid: "ownerUuid",
113                 username: "username",
114                 prefs: {},
115                 isAdmin: false,
116                 isActive: true
117             }
118         });
119     });
120
121     // TODO: Add remaining action tests
122     /*
123     it('should fire external url to login', () => {
124         const initialState = undefined;
125         window.location.assign = jest.fn();
126         reducer(initialState, authActions.LOGIN());
127         expect(window.location.assign).toBeCalledWith(
128             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
129         );
130     });
131
132     it('should fire external url to logout', () => {
133         const initialState = undefined;
134         window.location.assign = jest.fn();
135         reducer(initialState, authActions.LOGOUT());
136         expect(window.location.assign).toBeCalledWith(
137             `/logout?return_to=${location.protocol}//${location.host}`
138         );
139     });
140     */
141 });