1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { getNewExtraToken, 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 } 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, { AxiosInstance } from "axios";
16 import MockAdapter from "axios-mock-adapter";
17 import { ImportMock } from 'ts-mock-imports';
18 import * as servicesModule from "services/services";
19 import * as authActionSessionModule from "./auth-action-session";
20 import { SessionStatus } from "models/session";
21 import { getRemoteHostConfig } from "./auth-action-session";
23 describe('auth-actions', () => {
24 let axiosInst: AxiosInstance;
25 let axiosMock: MockAdapter;
28 let services: ServiceRepository;
29 const config: any = {};
30 const actions: ApiActions = {
31 progressFn: (id: string, working: boolean) => { },
32 errorFn: (id: string, message: string) => { }
34 let importMocks: any[];
37 axiosInst = Axios.create({ headers: {} });
38 axiosMock = new MockAdapter(axiosInst);
39 services = createServices(mockConfig({}), actions, axiosInst);
40 store = configureStore(createBrowserHistory(), services, config);
46 importMocks.map(m => m.restore());
49 it('creates an extra token', async () => {
51 .onGet("/users/current")
53 email: "test@test.com",
56 uuid: "zzzzz-tpzed-abcefg",
57 owner_uuid: "ownerUuid",
63 .onGet("/api_client_authorizations/current")
65 expires_at: "2140-01-01T00:00:00.000Z",
66 api_token: 'extra token',
68 .onPost("/api_client_authorizations")
70 uuid: 'zzzzz-gj3su-xxxxxxxxxx',
71 apiToken: 'extra token',
73 .onPost("/api_client_authorizations")
75 uuid: 'zzzzz-gj3su-xxxxxxxxxx',
76 apiToken: 'extra additional token',
79 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
80 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
81 localStorage.setItem(API_TOKEN_KEY, "token");
84 rootUrl: "https://zzzzz.example.com",
87 apiRevision: 12345678,
89 Login: { LoginCluster: "" },
93 // Set up auth, confirm that no extra token was requested
94 await store.dispatch(initAuth(config))
95 expect(store.getState().auth.apiToken).toBeDefined();
96 expect(store.getState().auth.extraApiToken).toBeUndefined();
98 // Ask for an extra token
99 await store.dispatch(getNewExtraToken());
100 expect(store.getState().auth.apiToken).toBeDefined();
101 expect(store.getState().auth.extraApiToken).toBeDefined();
102 const extraToken = store.getState().auth.extraApiToken;
104 // Ask for a cached extra token
105 await store.dispatch(getNewExtraToken(true));
106 expect(store.getState().auth.extraApiToken).toBe(extraToken);
108 // Ask for a new extra token, should make a second api request
109 await store.dispatch(getNewExtraToken(false));
110 expect(store.getState().auth.extraApiToken).toBeDefined();
111 expect(store.getState().auth.extraApiToken).not.toBe(extraToken);
114 it('requests remote token data to login cluster', async () => {
115 const localClusterTokenExpiration = "2020-01-01T00:00:00.000Z";
116 const loginClusterTokenExpiration = "2140-01-01T00:00:00.000Z";
118 .onGet("/users/current")
120 email: "test@test.com",
123 uuid: "zzzz1-tpzed-abcefg",
124 owner_uuid: "ownerUuid",
130 .onGet("https://zzzz1.example.com/discovery/v1/apis/arvados/v1/rest")
132 baseUrl: "https://zzzz1.example.com/arvados/v1",
133 keepWebServiceUrl: "",
134 keepWebInlineServiceUrl: "",
136 rootUrl: "https://zzzz1.example.com",
143 // Local cluster -- cached token
144 .onGet("https://zzzzz.example.com/arvados/v1/api_client_authorizations/current")
146 uuid: 'zzzz1-gj3su-aaaaaaa',
147 expires_at: localClusterTokenExpiration,
148 api_token: 'tokensecret',
150 // Login cluster -- authoritative token copy
151 .onGet("https://zzzz1.example.com/arvados/v1/api_client_authorizations/current")
153 uuid: 'zzzz1-gj3su-aaaaaaa',
154 expires_at: loginClusterTokenExpiration,
155 api_token: 'tokensecret',
158 const config: any = {
159 rootUrl: "https://zzzzz.example.com",
161 remoteHosts: { zzzz1: "zzzz1.example.com" },
162 apiRevision: 12345678,
164 Login: { LoginCluster: "zzzz1" },
168 const remoteHostConfig = await getRemoteHostConfig(config.remoteHosts.zzzz1, axiosInst);
169 expect(remoteHostConfig).not.toBeFalsy;
170 services = createServices(remoteHostConfig!, actions, axiosInst);
172 importMocks.push(ImportMock.mockFunction(authActionSessionModule, 'getRemoteHostConfig', remoteHostConfig));
173 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
175 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
176 localStorage.setItem(API_TOKEN_KEY, "v2/zzzz1-gj3su-aaaaaaa/tokensecret");
178 await store.dispatch(initAuth(config));
179 expect(store.getState().auth.apiToken).toBeDefined();
180 expect(localClusterTokenExpiration).not.toBe(loginClusterTokenExpiration);
181 expect(store.getState().auth.apiTokenExpiration).toEqual(new Date(loginClusterTokenExpiration));
184 it('should initialise state with user and api token from local storage', (done) => {
186 .onGet("/users/current")
188 email: "test@test.com",
191 uuid: "zzzzz-tpzed-abcefg",
192 owner_uuid: "ownerUuid",
198 .onGet("/api_client_authorizations/current")
200 expires_at: "2140-01-01T00:00:00.000Z"
202 .onGet("https://xc59z.example.com/discovery/v1/apis/arvados/v1/rest")
204 baseUrl: "https://xc59z.example.com/arvados/v1",
205 keepWebServiceUrl: "",
206 keepWebInlineServiceUrl: "",
208 rootUrl: "https://xc59z.example.com",
216 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
218 // Only test the case when a link account operation is not being cancelled
219 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
220 localStorage.setItem(API_TOKEN_KEY, "token");
222 const config: any = {
223 rootUrl: "https://zzzzz.example.com",
225 remoteHosts: { xc59z: "xc59z.example.com" },
226 apiRevision: 12345678,
228 Login: { LoginCluster: "" },
232 store.dispatch(initAuth(config));
234 store.subscribe(() => {
235 const auth = store.getState().auth;
236 if (auth.apiToken === "token" &&
237 auth.sessions.length === 2 &&
238 auth.sessions[0].status === SessionStatus.VALIDATED &&
239 auth.sessions[1].status === SessionStatus.VALIDATED
242 expect(auth).toEqual({
244 apiTokenExpiration: new Date("2140-01-01T00:00:00.000Z"),
245 apiTokenLocation: "localStorage",
247 apiRevision: 12345678,
254 "xc59z": "xc59z.example.com",
256 rootUrl: "https://zzzzz.example.com",
260 extraApiToken: undefined,
261 extraApiTokenExpiration: undefined,
262 homeCluster: "zzzzz",
263 localCluster: "zzzzz",
264 loginCluster: undefined,
267 "apiRevision": 12345678,
274 "xc59z": "xc59z.example.com",
276 "rootUrl": "https://zzzzz.example.com",
277 "uuidPrefix": "zzzzz",
279 "xc59z": mockConfig({
280 apiRevision: 12345678,
281 baseUrl: "https://xc59z.example.com/arvados/v1",
282 rootUrl: "https://xc59z.example.com",
287 zzzzz: "zzzzz.example.com",
288 xc59z: "xc59z.example.com"
292 "baseUrl": undefined,
293 "clusterId": "zzzzz",
294 "email": "test@test.com",
296 "remoteHost": "https://zzzzz.example.com",
300 "apiRevision": 12345678,
301 "uuid": "zzzzz-tpzed-abcefg",
306 "clusterId": "xc59z",
309 "remoteHost": "xc59z.example.com",
317 email: "test@test.com",
320 uuid: "zzzzz-tpzed-abcefg",
321 ownerUuid: "ownerUuid",
323 prefs: { profile: {} },
337 // TODO: Add remaining action tests
339 it('should fire external url to login', () => {
340 const initialState = undefined;
341 window.location.assign = jest.fn();
342 reducer(initialState, authActions.LOGIN());
343 expect(window.location.assign).toBeCalledWith(
344 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
348 it('should fire external url to logout', () => {
349 const initialState = undefined;
350 window.location.assign = jest.fn();
351 reducer(initialState, authActions.LOGOUT());
352 expect(window.location.assign).toBeCalledWith(
353 `/logout?return_to=${location.protocol}//${location.host}`