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 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",
61 .onGet("https://xc59z.arvadosapi.com/discovery/v1/apis/arvados/v1/rest")
63 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
64 keepWebServiceUrl: "",
66 rootUrl: "https://xc59z.arvadosapi.com",
74 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
76 // Only test the case when a link account operation is not being cancelled
77 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
78 localStorage.setItem(API_TOKEN_KEY, "token");
81 rootUrl: "https://zzzzz.arvadosapi.com",
83 remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
84 apiRevision: 12345678,
87 store.dispatch(initAuth(config));
89 store.subscribe(() => {
90 const auth = store.getState().auth;
91 if (auth.apiToken === "token" &&
92 auth.sessions.length === 2 &&
93 auth.sessions[0].status === 2 &&
94 auth.sessions[1].status === 2
97 expect(auth).toEqual({
100 apiRevision: 12345678,
102 "xc59z": "xc59z.arvadosapi.com",
104 rootUrl: "https://zzzzz.arvadosapi.com",
108 homeCluster: "zzzzz",
109 localCluster: "zzzzz",
110 loginCluster: undefined,
113 "apiRevision": 12345678,
115 "xc59z": "xc59z.arvadosapi.com",
117 "rootUrl": "https://zzzzz.arvadosapi.com",
118 "uuidPrefix": "zzzzz",
120 "xc59z": mockConfig({
121 apiRevision: 12345678,
122 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
123 rootUrl: "https://xc59z.arvadosapi.com",
128 zzzzz: "zzzzz.arvadosapi.com",
129 xc59z: "xc59z.arvadosapi.com"
133 "baseUrl": undefined,
134 "clusterId": "zzzzz",
135 "email": "test@test.com",
137 "remoteHost": "https://zzzzz.arvadosapi.com",
141 "apiRevision": 12345678,
142 "uuid": "zzzzz-tpzed-abcefg",
147 "clusterId": "xc59z",
150 "remoteHost": "xc59z.arvadosapi.com",
158 email: "test@test.com",
161 uuid: "zzzzz-tpzed-abcefg",
162 ownerUuid: "ownerUuid",
164 prefs: { profile: {} },
178 // TODO: Add remaining action tests
180 it('should fire external url to login', () => {
181 const initialState = undefined;
182 window.location.assign = jest.fn();
183 reducer(initialState, authActions.LOGIN());
184 expect(window.location.assign).toBeCalledWith(
185 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
189 it('should fire external url to logout', () => {
190 const initialState = undefined;
191 window.location.assign = jest.fn();
192 reducer(initialState, authActions.LOGOUT());
193 expect(window.location.assign).toBeCalledWith(
194 `/logout?return_to=${location.protocol}//${location.host}`