15027: Cleans up unused imports.
[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             remoteHostsConfig: {},
71             remoteHosts: {
72                 zzzzz: "zzzzz.arvadosapi.com",
73                 xc59z: "xc59z.arvadosapi.com"
74             },
75             sessions: [{
76                 "active": true,
77                 "baseUrl": undefined,
78                 "clusterId": "zzzzz",
79                 "email": "test@test.com",
80                 "loggedIn": true,
81                 "remoteHost": "https://zzzzz.arvadosapi.com",
82                 "status": 2,
83                 "token": "token",
84                 "username": "John Doe"
85             }, {
86                 "active": false,
87                 "baseUrl": "",
88                 "clusterId": "xc59z",
89                 "email": "",
90                 "loggedIn": false,
91                 "remoteHost": "xc59z.arvadosapi.com",
92                 "status": 0,
93                 "token": "",
94                 "username": ""
95             }],
96             user: {
97                 email: "test@test.com",
98                 firstName: "John",
99                 lastName: "Doe",
100                 uuid: "zzzzz-tpzed-abcefg",
101                 ownerUuid: "ownerUuid",
102                 username: "username",
103                 prefs: {},
104                 isAdmin: false,
105                 isActive: true
106             }
107         });
108     });
109
110     // TODO: Add remaining action tests
111     /*
112     it('should fire external url to login', () => {
113         const initialState = undefined;
114         window.location.assign = jest.fn();
115         reducer(initialState, authActions.LOGIN());
116         expect(window.location.assign).toBeCalledWith(
117             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
118         );
119     });
120
121     it('should fire external url to logout', () => {
122         const initialState = undefined;
123         window.location.assign = jest.fn();
124         reducer(initialState, authActions.LOGOUT());
125         expect(window.location.assign).toBeCalledWith(
126             `/logout?return_to=${location.protocol}//${location.host}`
127         );
128     });
129     */
130 });