1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { authReducer, AuthState } from "./auth-reducer";
6 import { AuthAction, initAuth } from "./auth-action";
14 USER_IS_ADMIN, USER_IDENTITY_URL, USER_PREFS
15 } from "~/services/auth-service/auth-service";
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";
24 describe('auth-actions', () => {
25 let reducer: (state: AuthState | undefined, action: AuthAction) => any;
27 const actions: ApiActions = {
28 progressFn: (id: string, working: boolean) => {},
29 errorFn: (id: string, message: string) => {}
33 store = configureStore(createBrowserHistory(), createServices(mockConfig({}), actions));
35 reducer = authReducer(createServices(mockConfig({}), actions));
38 it('should initialise state with user and api token from local storage', () => {
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"));
51 remoteHosts: { "xc59": "xc59.api.arvados.com" }
54 store.dispatch(initAuth(config));
56 expect(store.getState().auth).toEqual({
62 "clusterId": undefined,
63 "email": "test@test.com",
65 "remoteHost": undefined,
68 "username": "John Doe"
75 "remoteHost": "xc59.api.arvados.com",
81 email: "test@test.com",
85 ownerUuid: "ownerUuid",
86 identityUrl: "identityUrl",
93 // TODO: Add remaining action tests
95 it('should fire external url to login', () => {
96 const initialState = undefined;
97 window.location.assign = jest.fn();
98 reducer(initialState, authActions.LOGIN());
99 expect(window.location.assign).toBeCalledWith(
100 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
104 it('should fire external url to logout', () => {
105 const initialState = undefined;
106 window.location.assign = jest.fn();
107 reducer(initialState, authActions.LOGOUT());
108 expect(window.location.assign).toBeCalledWith(
109 `/logout?return_to=${location.protocol}//${location.host}`