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