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