d58c91594e1858d29a562027e03b0e113e1cadec
[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, USER_IDENTITY_URL, USER_PREFS
15 } from "~/services/auth-service/auth-service";
16
17 import 'jest-localstorage-mock';
18 import { createServices } from "~/services/services";
19 import { configureStore, RootStore } from "../store";
20 import createBrowserHistory from "history/createBrowserHistory";
21 import { Config, mockConfig } from '~/common/config';
22 import { ApiActions } from "~/services/api/api-actions";
23
24 describe('auth-actions', () => {
25     let reducer: (state: AuthState | undefined, action: AuthAction) => any;
26     let store: RootStore;
27     const actions: ApiActions = {
28         progressFn: (id: string, working: boolean) => {},
29         errorFn: (id: string, message: string) => {}
30     };
31
32     beforeEach(() => {
33         store = configureStore(createBrowserHistory(), createServices(mockConfig({}), actions));
34         localStorage.clear();
35         reducer = authReducer(createServices(mockConfig({}), actions));
36     });
37
38     it('should initialise state with user and api token from local storage', () => {
39
40         localStorage.setItem(API_TOKEN_KEY, "token");
41         localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
42         localStorage.setItem(USER_FIRST_NAME_KEY, "John");
43         localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
44         localStorage.setItem(USER_UUID_KEY, "uuid");
45         localStorage.setItem(USER_IDENTITY_URL, "identityUrl");
46         localStorage.setItem(USER_PREFS, JSON.stringify({}));
47         localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
48         localStorage.setItem(USER_IS_ADMIN, JSON.stringify("false"));
49
50         const config: any = {
51             remoteHosts: { "xc59": "xc59.api.arvados.com" }
52         };
53
54         store.dispatch(initAuth(config));
55
56         expect(store.getState().auth).toEqual({
57             apiToken: "token",
58             sshKeys: [],
59             user: {
60                 email: "test@test.com",
61                 firstName: "John",
62                 lastName: "Doe",
63                 uuid: "uuid",
64                 ownerUuid: "ownerUuid",
65                 identityUrl: "identityUrl",
66                 prefs: {},
67                 isAdmin: false
68             }
69         });
70     });
71
72     // TODO: Add remaining action tests
73     /*
74     it('should fire external url to login', () => {
75         const initialState = undefined;
76         window.location.assign = jest.fn();
77         reducer(initialState, authActions.LOGIN());
78         expect(window.location.assign).toBeCalledWith(
79             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
80         );
81     });
82
83     it('should fire external url to logout', () => {
84         const initialState = undefined;
85         window.location.assign = jest.fn();
86         reducer(initialState, authActions.LOGOUT());
87         expect(window.location.assign).toBeCalledWith(
88             `/logout?return_to=${location.protocol}//${location.host}`
89         );
90     });
91     */
92 });
93
94