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 } from "~/services/auth-service/auth-service";
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";
23 describe('auth-actions', () => {
24 let reducer: (state: AuthState | undefined, action: AuthAction) => any;
26 const actions: ApiActions = {
27 progressFn: (id: string, working: boolean) => {},
28 errorFn: (id: string, message: string) => {}
32 store = configureStore(createBrowserHistory(), createServices(mockConfig({}), actions));
34 reducer = authReducer(createServices(mockConfig({}), actions));
37 it('should initialise state with user and api token from local storage', () => {
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");
46 store.dispatch(initAuth());
48 expect(store.getState().auth).toEqual({
51 email: "test@test.com",
55 ownerUuid: "ownerUuid"
60 // TODO: Add remaining action tests
62 it('should fire external url to login', () => {
63 const initialState = undefined;
64 window.location.assign = jest.fn();
65 reducer(initialState, authActions.LOGIN());
66 expect(window.location.assign).toBeCalledWith(
67 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
71 it('should fire external url to logout', () => {
72 const initialState = undefined;
73 window.location.assign = jest.fn();
74 reducer(initialState, authActions.LOGOUT());
75 expect(window.location.assign).toBeCalledWith(
76 `/logout?return_to=${location.protocol}//${location.host}`