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