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("/api_client_authorizations/current")
64 expires_at: "2140-01-01T00:00:00.000Z"
68 .onGet("https://xc59z.arvadosapi.com/discovery/v1/apis/arvados/v1/rest")
70 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
71 keepWebServiceUrl: "",
72 keepWebInlineServiceUrl: "",
74 rootUrl: "https://xc59z.arvadosapi.com",
82 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
84 // Only test the case when a link account operation is not being cancelled
85 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
86 localStorage.setItem(API_TOKEN_KEY, "token");
89 rootUrl: "https://zzzzz.arvadosapi.com",
91 remoteHosts: { xc59z: "xc59z.arvadosapi.com" },
92 apiRevision: 12345678,
94 Login: { LoginCluster: "" },
98 store.dispatch(initAuth(config));
100 store.subscribe(() => {
101 const auth = store.getState().auth;
102 if (auth.apiToken === "token" &&
103 auth.sessions.length === 2 &&
104 auth.sessions[0].status === 2 &&
105 auth.sessions[1].status === 2
108 expect(auth).toEqual({
110 apiTokenExpiration: new Date("2140-01-01T00:00:00.000Z"),
112 apiRevision: 12345678,
119 "xc59z": "xc59z.arvadosapi.com",
121 rootUrl: "https://zzzzz.arvadosapi.com",
125 extraApiToken: undefined,
126 extraApiTokenExpiration: undefined,
127 homeCluster: "zzzzz",
128 localCluster: "zzzzz",
129 loginCluster: undefined,
132 "apiRevision": 12345678,
139 "xc59z": "xc59z.arvadosapi.com",
141 "rootUrl": "https://zzzzz.arvadosapi.com",
142 "uuidPrefix": "zzzzz",
144 "xc59z": mockConfig({
145 apiRevision: 12345678,
146 baseUrl: "https://xc59z.arvadosapi.com/arvados/v1",
147 rootUrl: "https://xc59z.arvadosapi.com",
152 zzzzz: "zzzzz.arvadosapi.com",
153 xc59z: "xc59z.arvadosapi.com"
157 "baseUrl": undefined,
158 "clusterId": "zzzzz",
159 "email": "test@test.com",
161 "remoteHost": "https://zzzzz.arvadosapi.com",
165 "apiRevision": 12345678,
166 "uuid": "zzzzz-tpzed-abcefg",
171 "clusterId": "xc59z",
174 "remoteHost": "xc59z.arvadosapi.com",
182 email: "test@test.com",
185 uuid: "zzzzz-tpzed-abcefg",
186 ownerUuid: "ownerUuid",
188 prefs: { profile: {} },
202 // TODO: Add remaining action tests
204 it('should fire external url to login', () => {
205 const initialState = undefined;
206 window.location.assign = jest.fn();
207 reducer(initialState, authActions.LOGIN());
208 expect(window.location.assign).toBeCalledWith(
209 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
213 it('should fire external url to logout', () => {
214 const initialState = undefined;
215 window.location.assign = jest.fn();
216 reducer(initialState, authActions.LOGOUT());
217 expect(window.location.assign).toBeCalledWith(
218 `/logout?return_to=${location.protocol}//${location.host}`