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