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 { ServiceRepository, 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';
27 import axios from "axios";
28 import MockAdapter from "axios-mock-adapter";
30 describe('auth-actions', () => {
31 const axiosMock = new MockAdapter(axios);
33 let reducer: (state: AuthState | undefined, action: AuthAction) => any;
35 let services: ServiceRepository;
36 const actions: ApiActions = {
37 progressFn: (id: string, working: boolean) => { },
38 errorFn: (id: string, message: string) => { }
43 services = createServices(mockConfig({}), actions, axios);
44 store = configureStore(createBrowserHistory(), services);
46 reducer = authReducer(services);
49 it('should initialise state with user and api token from local storage', () => {
52 .onGet("/users/current")
54 email: "test@test.com",
57 uuid: "zzzzz-tpzed-abcefg",
58 ownerUuid: "ownerUuid",
65 // Only test the case when a link account operation is not being cancelled
66 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
67 localStorage.setItem(API_TOKEN_KEY, "token");
70 rootUrl: "https://zzzzz.arvadosapi.com",
72 remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
75 store.dispatch(initAuth(config));
77 expect(store.getState().auth).toEqual({
81 localCluster: "zzzzz",
82 loginCluster: undefined,
86 "xc59z": "xc59z.arvadosapi.com",
88 "rootUrl": "https://zzzzz.arvadosapi.com",
89 "uuidPrefix": "zzzzz",
93 zzzzz: "zzzzz.arvadosapi.com",
94 xc59z: "xc59z.arvadosapi.com"
100 "email": "test@test.com",
102 "remoteHost": "https://zzzzz.arvadosapi.com",
106 "uuid": "zzzzz-tpzed-abcefg",
110 "clusterId": "xc59z",
113 "remoteHost": "xc59z.arvadosapi.com",
120 email: "test@test.com",
123 uuid: "zzzzz-tpzed-abcefg",
124 ownerUuid: "ownerUuid",
133 // TODO: Add remaining action tests
135 it('should fire external url to login', () => {
136 const initialState = undefined;
137 window.location.assign = jest.fn();
138 reducer(initialState, authActions.LOGIN());
139 expect(window.location.assign).toBeCalledWith(
140 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
144 it('should fire external url to logout', () => {
145 const initialState = undefined;
146 window.location.assign = jest.fn();
147 reducer(initialState, authActions.LOGOUT());
148 expect(window.location.assign).toBeCalledWith(
149 `/logout?return_to=${location.protocol}//${location.host}`