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: "" },
90 Workbench: { UserProfileFormFields: {} }
94 // Set up auth, confirm that no extra token was requested
95 await store.dispatch(initAuth(config))
96 expect(store.getState().auth.apiToken).toBeDefined();
97 expect(store.getState().auth.extraApiToken).toBeUndefined();
99 // Ask for an extra token
100 await store.dispatch(getNewExtraToken());
101 expect(store.getState().auth.apiToken).toBeDefined();
102 expect(store.getState().auth.extraApiToken).toBeDefined();
103 const extraToken = store.getState().auth.extraApiToken;
105 // Ask for a cached extra token
106 await store.dispatch(getNewExtraToken(true));
107 expect(store.getState().auth.extraApiToken).toBe(extraToken);
109 // Ask for a new extra token, should make a second api request
110 await store.dispatch(getNewExtraToken(false));
111 expect(store.getState().auth.extraApiToken).toBeDefined();
112 expect(store.getState().auth.extraApiToken).not.toBe(extraToken);
115 it('requests remote token data to login cluster', async () => {
116 const localClusterTokenExpiration = "2020-01-01T00:00:00.000Z";
117 const loginClusterTokenExpiration = "2140-01-01T00:00:00.000Z";
119 .onGet("/users/current")
121 email: "test@test.com",
124 uuid: "zzzz1-tpzed-abcefg",
125 owner_uuid: "ownerUuid",
131 .onGet("https://zzzz1.example.com/discovery/v1/apis/arvados/v1/rest")
133 baseUrl: "https://zzzz1.example.com/arvados/v1",
134 keepWebServiceUrl: "",
135 keepWebInlineServiceUrl: "",
137 rootUrl: "https://zzzz1.example.com",
144 // Local cluster -- cached token
145 .onGet("https://zzzzz.example.com/arvados/v1/api_client_authorizations/current")
147 uuid: 'zzzz1-gj3su-aaaaaaa',
148 expires_at: localClusterTokenExpiration,
149 api_token: 'tokensecret',
151 // Login cluster -- authoritative token copy
152 .onGet("https://zzzz1.example.com/arvados/v1/api_client_authorizations/current")
154 uuid: 'zzzz1-gj3su-aaaaaaa',
155 expires_at: loginClusterTokenExpiration,
156 api_token: 'tokensecret',
159 const config: any = {
160 rootUrl: "https://zzzzz.example.com",
162 remoteHosts: { zzzz1: "zzzz1.example.com" },
163 apiRevision: 12345678,
165 Login: { LoginCluster: "zzzz1" },
166 Workbench: { UserProfileFormFields: {} }
170 const remoteHostConfig = await getRemoteHostConfig(config.remoteHosts.zzzz1, axiosInst);
171 expect(remoteHostConfig).not.toBeFalsy;
172 services = createServices(remoteHostConfig!, actions, axiosInst);
174 importMocks.push(ImportMock.mockFunction(authActionSessionModule, 'getRemoteHostConfig', remoteHostConfig));
175 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
177 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
178 localStorage.setItem(API_TOKEN_KEY, "v2/zzzz1-gj3su-aaaaaaa/tokensecret");
180 await store.dispatch(initAuth(config));
181 expect(store.getState().auth.apiToken).toBeDefined();
182 expect(localClusterTokenExpiration).not.toBe(loginClusterTokenExpiration);
183 expect(store.getState().auth.apiTokenExpiration).toEqual(new Date(loginClusterTokenExpiration));
186 it('should initialise state with user and api token from local storage', (done) => {
188 .onGet("/users/current")
190 email: "test@test.com",
193 uuid: "zzzzz-tpzed-abcefg",
194 owner_uuid: "ownerUuid",
200 .onGet("/api_client_authorizations/current")
202 expires_at: "2140-01-01T00:00:00.000Z"
204 .onGet("https://xc59z.example.com/discovery/v1/apis/arvados/v1/rest")
206 baseUrl: "https://xc59z.example.com/arvados/v1",
207 keepWebServiceUrl: "",
208 keepWebInlineServiceUrl: "",
210 rootUrl: "https://xc59z.example.com",
218 importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services));
220 // Only test the case when a link account operation is not being cancelled
221 sessionStorage.setItem(ACCOUNT_LINK_STATUS_KEY, "0");
222 localStorage.setItem(API_TOKEN_KEY, "token");
224 const config: any = {
225 rootUrl: "https://zzzzz.example.com",
227 remoteHosts: { xc59z: "xc59z.example.com" },
228 apiRevision: 12345678,
230 Login: { LoginCluster: "" },
231 Workbench: { UserProfileFormFields: {} }
235 store.dispatch(initAuth(config));
237 store.subscribe(() => {
238 const auth = store.getState().auth;
239 if (auth.apiToken === "token" &&
240 auth.sessions.length === 2 &&
241 auth.sessions[0].status === SessionStatus.VALIDATED &&
242 auth.sessions[1].status === SessionStatus.VALIDATED
245 expect(auth).toEqual({
247 apiTokenExpiration: new Date("2140-01-01T00:00:00.000Z"),
248 apiTokenLocation: "localStorage",
250 apiRevision: 12345678,
255 Workbench: { UserProfileFormFields: {} }
258 "xc59z": "xc59z.example.com",
260 rootUrl: "https://zzzzz.example.com",
264 extraApiToken: undefined,
265 extraApiTokenExpiration: undefined,
266 homeCluster: "zzzzz",
267 localCluster: "zzzzz",
268 loginCluster: undefined,
271 "apiRevision": 12345678,
276 Workbench: { UserProfileFormFields: {} }
279 "xc59z": "xc59z.example.com",
281 "rootUrl": "https://zzzzz.example.com",
282 "uuidPrefix": "zzzzz",
284 "xc59z": mockConfig({
285 apiRevision: 12345678,
286 baseUrl: "https://xc59z.example.com/arvados/v1",
287 rootUrl: "https://xc59z.example.com",
292 zzzzz: "zzzzz.example.com",
293 xc59z: "xc59z.example.com"
297 "baseUrl": undefined,
298 "clusterId": "zzzzz",
299 "email": "test@test.com",
301 "remoteHost": "https://zzzzz.example.com",
305 "apiRevision": 12345678,
306 "uuid": "zzzzz-tpzed-abcefg",
311 "clusterId": "xc59z",
314 "remoteHost": "xc59z.example.com",
322 email: "test@test.com",
325 uuid: "zzzzz-tpzed-abcefg",
326 ownerUuid: "ownerUuid",
328 prefs: { profile: {} },
342 // TODO: Add remaining action tests
344 it('should fire external url to login', () => {
345 const initialState = undefined;
346 window.location.assign = jest.fn();
347 reducer(initialState, authActions.LOGIN());
348 expect(window.location.assign).toBeCalledWith(
349 `/login?return_to=${window.location.protocol}//${window.location.host}/token`
353 it('should fire external url to logout', () => {
354 const initialState = undefined;
355 window.location.assign = jest.fn();
356 reducer(initialState, authActions.LOGOUT());
357 expect(window.location.assign).toBeCalledWith(
358 `/logout?return_to=${location.protocol}//${location.host}`