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";
26 import { ACCOUNT_LINK_STATUS_KEY} from '~/services/link-account-service/link-account-service';
28 describe('auth-actions', () => {
29 let reducer: (state: AuthState | undefined, action: AuthAction) => any;
31 const actions: ApiActions = {
32 progressFn: (id: string, working: boolean) => { },
33 errorFn: (id: string, message: string) => { }
37 store = configureStore(createBrowserHistory(), createServices(mockConfig({}), actions));
39 reducer = authReducer(createServices(mockConfig({}), actions));
42 it('should initialise state with user and api token from local storage', () => {
44 // Only test the case when a link account operation is not being cancelled
45 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
46 localStorage.setItem(API_TOKEN_KEY, "token");
47 localStorage.setItem(USER_EMAIL_KEY, "test@test.com");
48 localStorage.setItem(USER_FIRST_NAME_KEY, "John");
49 localStorage.setItem(USER_LAST_NAME_KEY, "Doe");
50 localStorage.setItem(USER_UUID_KEY, "zzzzz-tpzed-abcefg");
51 localStorage.setItem(USER_USERNAME, "username");
52 localStorage.setItem(USER_PREFS, JSON.stringify({}));
53 localStorage.setItem(USER_OWNER_UUID_KEY, "ownerUuid");
54 localStorage.setItem(USER_IS_ADMIN, JSON.stringify(false));
55 localStorage.setItem(USER_IS_ACTIVE, JSON.stringify(true));
58 rootUrl: "https://zzzzz.arvadosapi.com",
60 remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
63 store.dispatch(initAuth(config));
65 expect(store.getState().auth).toEqual({
69 localCluster: "zzzzz",
70 remoteHostsConfig: {},
72 zzzzz: "zzzzz.arvadosapi.com",
73 xc59z: "xc59z.arvadosapi.com"
79 "email": "test@test.com",
81 "remoteHost": "https://zzzzz.arvadosapi.com",
84 "username": "John Doe"
91 "remoteHost": "xc59z.arvadosapi.com",
97 email: "test@test.com",
100 uuid: "zzzzz-tpzed-abcefg",
101 ownerUuid: "ownerUuid",
102 username: "username",
110 // TODO: Add remaining action tests
112 it('should fire external url to login', () => {
113 const initialState = undefined;
114 window.location.assign = jest.fn();
115 reducer(initialState, authActions.LOGIN());
116 expect(window.location.assign).toBeCalledWith(
117 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
121 it('should fire external url to logout', () => {
122 const initialState = undefined;
123 window.location.assign = jest.fn();
124 reducer(initialState, authActions.LOGOUT());
125 expect(window.location.assign).toBeCalledWith(
126 `/logout?return_to=${location.protocol}//${location.host}`