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";
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" },
70 apiRevision: 12345678,
73 store.dispatch(initAuth(config));
75 store.subscribe(() => {
76 const auth = store.getState().auth;
77 if (auth.apiToken === "token" &&
78 auth.sessions.length === 2 &&
79 auth.sessions[0].status === 2 &&
80 auth.sessions[1].status === 2
83 expect(auth).toEqual({
86 apiRevision: 12345678,
88 "xc59z": "xc59z.arvadosapi.com",
90 rootUrl: "https://zzzzz.arvadosapi.com",
95 localCluster: "zzzzz",
96 loginCluster: undefined,
99 "apiRevision": 12345678,
101 "xc59z": "xc59z.arvadosapi.com",
103 "rootUrl": "https://zzzzz.arvadosapi.com",
104 "uuidPrefix": "zzzzz",
108 zzzzz: "zzzzz.arvadosapi.com",
109 xc59z: "xc59z.arvadosapi.com"
113 "baseUrl": undefined,
114 "clusterId": "zzzzz",
115 "email": "test@test.com",
117 "remoteHost": "https://zzzzz.arvadosapi.com",
121 "apiRevision": 12345678,
122 "uuid": "zzzzz-tpzed-abcefg",
126 "clusterId": "xc59z",
129 "remoteHost": "xc59z.arvadosapi.com",
137 email: "test@test.com",
140 uuid: "zzzzz-tpzed-abcefg",
141 ownerUuid: "ownerUuid",
143 prefs: { profile: {} },
157 // TODO: Add remaining action tests
159 it('should fire external url to login', () => {
160 const initialState = undefined;
161 window.location.assign = jest.fn();
162 reducer(initialState, authActions.LOGIN());
163 expect(window.location.assign).toBeCalledWith(
164 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
168 it('should fire external url to logout', () => {
169 const initialState = undefined;
170 window.location.assign = jest.fn();
171 reducer(initialState, authActions.LOGOUT());
172 expect(window.location.assign).toBeCalledWith(
173 `/logout?return_to=${location.protocol}//${location.host}`