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