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";
18 } from "~/services/auth-service/auth-service";
20 import 'jest-localstorage-mock';
21 import { createServices } from "~/services/services";
22 import { configureStore, RootStore } from "../store";
23 import createBrowserHistory from "history/createBrowserHistory";
24 import { Config, mockConfig } from '~/common/config';
25 import { ApiActions } from "~/services/api/api-actions";
27 describe('auth-actions', () => {
28 let reducer: (state: AuthState | undefined, action: AuthAction) => any;
30 const actions: ApiActions = {
31 progressFn: (id: string, working: boolean) => { },
32 errorFn: (id: string, message: string) => { }
36 store = configureStore(createBrowserHistory(), createServices(mockConfig({}), actions));
38 reducer = authReducer(createServices(mockConfig({}), actions));
41 it('should initialise state with user and api token from local storage', () => {
43 localStorage.setItem(API_TOKEN_KEY, "token");
44 localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
45 localStorage.setItem(USER_FIRST_NAME_KEY, "John");
46 localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
47 localStorage.setItem(USER_UUID_KEY, "zzzzz-tpzed-abcefg");
48 localStorage.setItem(USER_USERNAME, "username");
49 localStorage.setItem(USER_PREFS, JSON.stringify({}));
50 localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
51 localStorage.setItem(USER_IS_ADMIN, JSON.stringify(false));
52 localStorage.setItem(USER_IS_ACTIVE, JSON.stringify(true));
55 rootUrl: "https://zzzzz.arvadosapi.com",
57 remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
60 store.dispatch(initAuth(config));
62 expect(store.getState().auth).toEqual({
66 localCluster: "zzzzz",
67 remoteHostsConfig: {},
69 zzzzz: "zzzzz.arvadosapi.com",
70 xc59z: "xc59z.arvadosapi.com"
76 "email": "test@test.com",
78 "remoteHost": "https://zzzzz.arvadosapi.com",
81 "username": "John Doe"
88 "remoteHost": "xc59z.arvadosapi.com",
94 email: "test@test.com",
97 uuid: "zzzzz-tpzed-abcefg",
98 ownerUuid: "ownerUuid",
107 // TODO: Add remaining action tests
109 it('should fire external url to login', () => {
110 const initialState = undefined;
111 window.location.assign = jest.fn();
112 reducer(initialState, authActions.LOGIN());
113 expect(window.location.assign).toBeCalledWith(
114 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
118 it('should fire external url to logout', () => {
119 const initialState = undefined;
120 window.location.assign = jest.fn();
121 reducer(initialState, authActions.LOGOUT());
122 expect(window.location.assign).toBeCalledWith(
123 `/logout?return_to=${location.protocol}//${location.host}`