Merge branch 'master'
[arvados-workbench2.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
22 describe('auth-actions', () => {
23     let reducer: (state: AuthState | undefined, action: AuthAction) => any;
24     let store: RootStore;
25
26     beforeEach(() => {
27         store = configureStore(createBrowserHistory(), createServices(mockConfig({})));
28         localStorage.clear();
29         reducer = authReducer(createServices(mockConfig({})));
30     });
31
32     it('should initialise state with user and api token from local storage', () => {
33
34         localStorage.setItem(API_TOKEN_KEY, "token");
35         localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
36         localStorage.setItem(USER_FIRST_NAME_KEY, "John");
37         localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
38         localStorage.setItem(USER_UUID_KEY, "uuid");
39         localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
40
41         store.dispatch(initAuth());
42
43         expect(store.getState().auth).toEqual({
44             apiToken: "token",
45             user: {
46                 email: "test@test.com",
47                 firstName: "John",
48                 lastName: "Doe",
49                 uuid: "uuid",
50                 ownerUuid: "ownerUuid"
51             }
52         });
53     });
54
55     // TODO: Add remaining action tests
56     /*
57     it('should fire external url to login', () => {
58         const initialState = undefined;
59         window.location.assign = jest.fn();
60         reducer(initialState, authActions.LOGIN());
61         expect(window.location.assign).toBeCalledWith(
62             `/login?return_to=${window.location.protocol}//${window.location.host}/token`
63         );
64     });
65
66     it('should fire external url to logout', () => {
67         const initialState = undefined;
68         window.location.assign = jest.fn();
69         reducer(initialState, authActions.LOGOUT());
70         expect(window.location.assign).toBeCalledWith(
71             `/logout?return_to=${location.protocol}//${location.host}`
72         );
73     });
74     */
75 });
76
77