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: "",
66 keepWebInlineServiceUrl: "",
68 rootUrl: "https://xc59z.arvadosapi.com",
76 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
78 // Only test the case when a link account operation is not being cancelled
79 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
80 localStorage.setItem(API_TOKEN_KEY, "token");
83 rootUrl: "https://zzzzz.arvadosapi.com",
85 remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
86 apiRevision: 12345678,
89 store.dispatch(initAuth(config));
91 store.subscribe(() => {
92 const auth = store.getState().auth;
93 if (auth.apiToken === "token" &&
94 auth.sessions.length === 2 &&
95 auth.sessions[0].status === 2 &&
96 auth.sessions[1].status === 2
99 expect(auth).toEqual({
102 apiRevision: 12345678,
104 "xc59z": "xc59z.arvadosapi.com",
106 rootUrl: "https://zzzzz.arvadosapi.com",
110 homeCluster: "zzzzz",
111 localCluster: "zzzzz",
112 loginCluster: undefined,
115 "apiRevision": 12345678,
117 "xc59z": "xc59z.arvadosapi.com",
119 "rootUrl": "https://zzzzz.arvadosapi.com",
120 "uuidPrefix": "zzzzz",
122 "xc59z": mockConfig({
123 apiRevision: 12345678,
124 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
125 rootUrl: "https://xc59z.arvadosapi.com",
130 zzzzz: "zzzzz.arvadosapi.com",
131 xc59z: "xc59z.arvadosapi.com"
135 "baseUrl": undefined,
136 "clusterId": "zzzzz",
137 "email": "test@test.com",
139 "remoteHost": "https://zzzzz.arvadosapi.com",
143 "apiRevision": 12345678,
144 "uuid": "zzzzz-tpzed-abcefg",
149 "clusterId": "xc59z",
152 "remoteHost": "xc59z.arvadosapi.com",
160 email: "test@test.com",
163 uuid: "zzzzz-tpzed-abcefg",
164 ownerUuid: "ownerUuid",
166 prefs: { profile: {} },
180 // TODO: Add remaining action tests
182 it('should fire external url to login', () => {
183 const initialState = undefined;
184 window.location.assign = jest.fn();
185 reducer(initialState, authActions.LOGIN());
186 expect(window.location.assign).toBeCalledWith(
187 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
191 it('should fire external url to logout', () => {
192 const initialState = undefined;
193 window.location.assign = jest.fn();
194 reducer(initialState, authActions.LOGOUT());
195 expect(window.location.assign).toBeCalledWith(
196 `/logout?return_to=${location.protocol}//${location.host}`