1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { initAuth } from "./auth-action";
6 import { API_TOKEN_KEY } from "~/services/auth-service/auth-service";
8 import 'jest-localstorage-mock';
9 import { ServiceRepository, createServices } from "~/services/services";
10 import { configureStore, RootStore } from "../store";
11 import createBrowserHistory from "history/createBrowserHistory";
12 import { mockConfig } from '~/common/config';
13 import { ApiActions } from "~/services/api/api-actions";
14 import { ACCOUNT_LINK_STATUS_KEY } from '~/services/link-account-service/link-account-service';
15 import Axios from "axios";
16 import MockAdapter from "axios-mock-adapter";
17 import { ImportMock } from 'ts-mock-imports';
18 import * as servicesModule from "~/services/services";
20 describe('auth-actions', () => {
21 const axiosInst = Axios.create({ headers: {} });
22 const axiosMock = new MockAdapter(axiosInst);
25 let services: ServiceRepository;
26 const actions: ApiActions = {
27 progressFn: (id: string, working: boolean) => { },
28 errorFn: (id: string, message: string) => { }
30 let importMocks: any[];
34 services = createServices(mockConfig({}), actions, axiosInst);
35 store = configureStore(createBrowserHistory(), services);
41 importMocks.map(m => m.restore());
44 it('should initialise state with user and api token from local storage', (done) => {
47 .onGet("/users/current")
49 email: "test@test.com",
52 uuid: "zzzzz-tpzed-abcefg",
53 owner_uuid: "ownerUuid",
60 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
62 // Only test the case when a link account operation is not being cancelled
63 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
64 localStorage.setItem(API_TOKEN_KEY, "token");
67 rootUrl: "https://zzzzz.arvadosapi.com",
69 remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
72 store.dispatch(initAuth(config));
74 store.subscribe(() => {
75 const auth = store.getState().auth;
76 if (auth.apiToken === "token" &&
77 auth.sessions.length === 2 &&
78 auth.sessions[0].status === 2 &&
79 auth.sessions[1].status === 2
82 expect(auth).toEqual({
86 "xc59z": "xc59z.arvadosapi.com",
88 rootUrl: "https://zzzzz.arvadosapi.com",
93 localCluster: "zzzzz",
94 loginCluster: undefined,
98 "xc59z": "xc59z.arvadosapi.com",
100 "rootUrl": "https://zzzzz.arvadosapi.com",
101 "uuidPrefix": "zzzzz",
105 zzzzz: "zzzzz.arvadosapi.com",
106 xc59z: "xc59z.arvadosapi.com"
110 "baseUrl": undefined,
111 "clusterId": "zzzzz",
112 "email": "test@test.com",
114 "remoteHost": "https://zzzzz.arvadosapi.com",
118 "uuid": "zzzzz-tpzed-abcefg",
122 "clusterId": "xc59z",
125 "remoteHost": "xc59z.arvadosapi.com",
132 email: "test@test.com",
135 uuid: "zzzzz-tpzed-abcefg",
136 ownerUuid: "ownerUuid",
138 prefs: { profile: {} },
152 // TODO: Add remaining action tests
154 it('should fire external url to login', () => {
155 const initialState = undefined;
156 window.location.assign = jest.fn();
157 reducer(initialState, authActions.LOGIN());
158 expect(window.location.assign).toBeCalledWith(
159 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
163 it('should fire external url to logout', () => {
164 const initialState = undefined;
165 window.location.assign = jest.fn();
166 reducer(initialState, authActions.LOGOUT());
167 expect(window.location.assign).toBeCalledWith(
168 `/logout?return_to=${location.protocol}//${location.host}`