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, DISCOVERY_DOC_PATH, } 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 config: any = {};
27 const actions: ApiActions = {
28 progressFn: (id: string, working: boolean) => { },
29 errorFn: (id: string, message: string) => { }
31 let importMocks: any[];
35 services = createServices(mockConfig({}), actions, axiosInst);
36 store = configureStore(createBrowserHistory(), services, config);
42 importMocks.map(m => m.restore());
45 it('should initialise state with user and api token from local storage', (done) => {
48 .onGet("/users/current")
50 email: "test@test.com",
53 uuid: "zzzzz-tpzed-abcefg",
54 owner_uuid: "ownerUuid",
62 .onGet("https://xc59z.arvadosapi.com/discovery/v1/apis/arvados/v1/rest")
64 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
65 keepWebServiceUrl: "",
67 rootUrl: "https://xc59z.arvadosapi.com",
75 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
77 // Only test the case when a link account operation is not being cancelled
78 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
79 localStorage.setItem(API_TOKEN_KEY, "token");
82 rootUrl: "https://zzzzz.arvadosapi.com",
84 remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
85 apiRevision: 12345678,
88 store.dispatch(initAuth(config));
90 store.subscribe(() => {
91 const auth = store.getState().auth;
92 if (auth.apiToken === "token" &&
93 auth.sessions.length === 2 &&
94 auth.sessions[0].status === 2 &&
95 auth.sessions[1].status === 2
98 expect(auth).toEqual({
101 apiRevision: 12345678,
103 "xc59z": "xc59z.arvadosapi.com",
105 rootUrl: "https://zzzzz.arvadosapi.com",
109 homeCluster: "zzzzz",
110 localCluster: "zzzzz",
111 loginCluster: undefined,
114 "apiRevision": 12345678,
116 "xc59z": "xc59z.arvadosapi.com",
118 "rootUrl": "https://zzzzz.arvadosapi.com",
119 "uuidPrefix": "zzzzz",
121 "xc59z": mockConfig({
122 apiRevision: 12345678,
123 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
124 rootUrl: "https://xc59z.arvadosapi.com",
129 zzzzz: "zzzzz.arvadosapi.com",
130 xc59z: "xc59z.arvadosapi.com"
134 "baseUrl": undefined,
135 "clusterId": "zzzzz",
136 "email": "test@test.com",
138 "remoteHost": "https://zzzzz.arvadosapi.com",
142 "apiRevision": 12345678,
143 "uuid": "zzzzz-tpzed-abcefg",
148 "clusterId": "xc59z",
151 "remoteHost": "xc59z.arvadosapi.com",
159 email: "test@test.com",
162 uuid: "zzzzz-tpzed-abcefg",
163 ownerUuid: "ownerUuid",
165 prefs: { profile: {} },
179 // TODO: Add remaining action tests
181 it('should fire external url to login', () => {
182 const initialState = undefined;
183 window.location.assign = jest.fn();
184 reducer(initialState, authActions.LOGIN());
185 expect(window.location.assign).toBeCalledWith(
186 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
190 it('should fire external url to logout', () => {
191 const initialState = undefined;
192 window.location.assign = jest.fn();
193 reducer(initialState, authActions.LOGOUT());
194 expect(window.location.assign).toBeCalledWith(
195 `/logout?return_to=${location.protocol}//${location.host}`