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 { 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 loginCluster: undefined,
74 "xc59z": "xc59z.arvadosapi.com",
76 "rootUrl": "https://zzzzz.arvadosapi.com",
77 "uuidPrefix": "zzzzz",
81 zzzzz: "zzzzz.arvadosapi.com",
82 xc59z: "xc59z.arvadosapi.com"
88 "email": "test@test.com",
90 "remoteHost": "https://zzzzz.arvadosapi.com",
93 "username": "John Doe"
100 "remoteHost": "xc59z.arvadosapi.com",
106 email: "test@test.com",
109 uuid: "zzzzz-tpzed-abcefg",
110 ownerUuid: "ownerUuid",
111 username: "username",
119 // TODO: Add remaining action tests
121 it('should fire external url to login', () => {
122 const initialState = undefined;
123 window.location.assign = jest.fn();
124 reducer(initialState, authActions.LOGIN());
125 expect(window.location.assign).toBeCalledWith(
126 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
130 it('should fire external url to logout', () => {
131 const initialState = undefined;
132 window.location.assign = jest.fn();
133 reducer(initialState, authActions.LOGOUT());
134 expect(window.location.assign).toBeCalledWith(
135 `/logout?return_to=${location.protocol}//${location.host}`