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 { Config, 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 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,
88 Login: { LoginCluster: "" },
92 store.dispatch(initAuth(config));
94 store.subscribe(() => {
95 const auth = store.getState().auth;
96 if (auth.apiToken === "token" &&
97 auth.sessions.length === 2 &&
98 auth.sessions[0].status === 2 &&
99 auth.sessions[1].status === 2
102 expect(auth).toEqual({
105 apiRevision: 12345678,
112 "xc59z": "xc59z.arvadosapi.com",
114 rootUrl: "https://zzzzz.arvadosapi.com",
118 extraApiToken: undefined,
119 homeCluster: "zzzzz",
120 localCluster: "zzzzz",
121 loginCluster: undefined,
124 "apiRevision": 12345678,
131 "xc59z": "xc59z.arvadosapi.com",
133 "rootUrl": "https://zzzzz.arvadosapi.com",
134 "uuidPrefix": "zzzzz",
136 "xc59z": mockConfig({
137 apiRevision: 12345678,
138 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
139 rootUrl: "https://xc59z.arvadosapi.com",
144 zzzzz: "zzzzz.arvadosapi.com",
145 xc59z: "xc59z.arvadosapi.com"
149 "baseUrl": undefined,
150 "clusterId": "zzzzz",
151 "email": "test@test.com",
153 "remoteHost": "https://zzzzz.arvadosapi.com",
157 "apiRevision": 12345678,
158 "uuid": "zzzzz-tpzed-abcefg",
163 "clusterId": "xc59z",
166 "remoteHost": "xc59z.arvadosapi.com",
174 email: "test@test.com",
177 uuid: "zzzzz-tpzed-abcefg",
178 ownerUuid: "ownerUuid",
180 prefs: { profile: {} },
194 // TODO: Add remaining action tests
196 it('should fire external url to login', () => {
197 const initialState = undefined;
198 window.location.assign = jest.fn();
199 reducer(initialState, authActions.LOGIN());
200 expect(window.location.assign).toBeCalledWith(
201 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
205 it('should fire external url to logout', () => {
206 const initialState = undefined;
207 window.location.assign = jest.fn();
208 reducer(initialState, authActions.LOGOUT());
209 expect(window.location.assign).toBeCalledWith(
210 `/logout?return_to=${location.protocol}//${location.host}`