From: Peter Amstutz Date: Fri, 2 Oct 2020 21:40:19 +0000 (-0400) Subject: 16941: Make it possible to mock retrieving cluster config X-Git-Tag: 2.1.0~2^2~3 X-Git-Url: https://git.arvados.org/arvados-workbench2.git/commitdiff_plain/d924f14c5a81265fbd0f550ccf498fe791fc5b54 16941: Make it possible to mock retrieving cluster config Fix auth-action.test using cluster config mock. Arvados-DCO-1.1-Signed-off-by: Peter Amstutz --- diff --git a/src/store/auth/auth-action-session.ts b/src/store/auth/auth-action-session.ts index 5fc8cffb..f0093f9f 100644 --- a/src/store/auth/auth-action-session.ts +++ b/src/store/auth/auth-action-session.ts @@ -20,10 +20,10 @@ import { AuthService } from "~/services/auth-service/auth-service"; import { snackbarActions, SnackbarKind } from "~/store/snackbar/snackbar-actions"; import * as jsSHA from "jssha"; -const getClusterConfig = async (origin: string): Promise => { +const getClusterConfig = async (origin: string, apiClient: AxiosInstance): Promise => { let configFromDD: Config | undefined; try { - const dd = (await Axios.get(`${origin}/${DISCOVERY_DOC_PATH}`)).data; + const dd = (await apiClient.get(`${origin}/${DISCOVERY_DOC_PATH}`)).data; configFromDD = { baseUrl: normalizeURLPath(dd.baseUrl), keepWebServiceUrl: dd.keepWebServiceUrl, @@ -41,9 +41,9 @@ const getClusterConfig = async (origin: string): Promise => { }; } catch { } - // Try the new public config endpoint + // Try public config endpoint try { - const config = (await Axios.get(`${origin}/${CLUSTER_CONFIG_PATH}`)).data; + const config = (await apiClient.get(`${origin}/${CLUSTER_CONFIG_PATH}`)).data; return { ...buildConfig(config), apiRevision: configFromDD ? configFromDD.apiRevision : 0 }; } catch { } @@ -55,7 +55,9 @@ const getClusterConfig = async (origin: string): Promise => { return null; }; -const getRemoteHostConfig = async (remoteHost: string): Promise => { +const getRemoteHostConfig = async (remoteHost: string, useApiClient?: AxiosInstance): Promise => { + const apiClient = useApiClient || Axios.create({ headers: {} }); + let url = remoteHost; if (url.indexOf('://') < 0) { url = 'https://' + url; @@ -63,14 +65,14 @@ const getRemoteHostConfig = async (remoteHost: string): Promise = const origin = new URL(url).origin; // Maybe it is an API server URL, try fetching config and discovery doc - let r = await getClusterConfig(origin); + let r = await getClusterConfig(origin, apiClient); if (r !== null) { return r; } // Maybe it is a Workbench2 URL, try getting config.json try { - r = await getClusterConfig((await Axios.get(`${origin}/config.json`)).data.API_HOST); + r = await getClusterConfig((await apiClient.get(`${origin}/config.json`)).data.API_HOST, apiClient); if (r !== null) { return r; } @@ -78,7 +80,7 @@ const getRemoteHostConfig = async (remoteHost: string): Promise = // Maybe it is a Workbench1 URL, try getting status.json try { - r = await getClusterConfig((await Axios.get(`${origin}/status.json`)).data.apiBaseURL); + r = await getClusterConfig((await apiClient.get(`${origin}/status.json`)).data.apiBaseURL, apiClient); if (r !== null) { return r; } @@ -121,7 +123,7 @@ export const validateCluster = async (config: Config, useToken: string): }; }; -export const validateSession = (session: Session, activeSession: Session) => +export const validateSession = (session: Session, activeSession: Session, useApiClient?: AxiosInstance) => async (dispatch: Dispatch): Promise => { dispatch(authActions.UPDATE_SESSION({ ...session, status: SessionStatus.BEING_VALIDATED })); session.loggedIn = false; @@ -138,7 +140,7 @@ export const validateSession = (session: Session, activeSession: Session) => }; let fail: Error | null = null; - const config = await getRemoteHostConfig(session.remoteHost); + const config = await getRemoteHostConfig(session.remoteHost, useApiClient); if (config !== null) { dispatch(authActions.REMOTE_CLUSTER_CONFIG({ config })); try { @@ -169,7 +171,7 @@ export const validateSession = (session: Session, activeSession: Session) => return session; }; -export const validateSessions = () => +export const validateSessions = (useApiClient?: AxiosInstance) => async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { const sessions = getState().auth.sessions; const activeSession = getActiveSession(sessions); @@ -187,7 +189,7 @@ export const validateSessions = () => override it using Dispatch. This pattern is used in a bunch of different places in Workbench2. */ - await dispatch(validateSession(session, activeSession)); + await dispatch(validateSession(session, activeSession, useApiClient)); } catch (e) { dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.message, @@ -311,7 +313,7 @@ export const initSessions = (authService: AuthService, config: Config, user: Use (dispatch: Dispatch) => { const sessions = authService.buildSessions(config, user); dispatch(authActions.SET_SESSIONS(sessions)); - dispatch(validateSessions()); + dispatch(validateSessions(authService.apiClient)); }; export const loadSiteManagerPanel = () => diff --git a/src/store/auth/auth-action.test.ts b/src/store/auth/auth-action.test.ts index 8a17fe9f..83a699a7 100644 --- a/src/store/auth/auth-action.test.ts +++ b/src/store/auth/auth-action.test.ts @@ -9,7 +9,7 @@ import 'jest-localstorage-mock'; import { ServiceRepository, createServices } from "~/services/services"; import { configureStore, RootStore } from "../store"; import { createBrowserHistory } from "history"; -import { mockConfig } from '~/common/config'; +import { mockConfig, DISCOVERY_DOC_PATH, } from '~/common/config'; import { ApiActions } from "~/services/api/api-actions"; import { ACCOUNT_LINK_STATUS_KEY } from '~/services/link-account-service/link-account-service'; import Axios from "axios"; @@ -57,6 +57,20 @@ describe('auth-actions', () => { prefs: {} }); + axiosMock + .onGet("https://xc59z.arvadosapi.com/discovery/v1/apis/arvados/v1/rest") + .reply(200, { + baseUrl: "https://xc59z.arvadosapi.com/arvados/v1", + keepWebServiceUrl: "", + remoteHosts: {}, + rootUrl: "https://xc59z.arvadosapi.com", + uuidPrefix: "xc59z", + websocketUrl: "", + workbenchUrl: "", + workbench2Url: "", + revision: 12345678 + }); + importMocks.push(ImportMock.mockFunction(servicesModule, 'createServices', services)); // Only test the case when a link account operation is not being cancelled @@ -103,6 +117,12 @@ describe('auth-actions', () => { "rootUrl": "https://zzzzz.arvadosapi.com", "uuidPrefix": "zzzzz", }, + "xc59z": mockConfig({ + apiRevision: 12345678, + baseUrl: "https://xc59z.arvadosapi.com/arvados/v1", + rootUrl: "https://xc59z.arvadosapi.com", + uuidPrefix: "xc59z" + }) }, remoteHosts: { zzzzz: "zzzzz.arvadosapi.com", @@ -120,6 +140,7 @@ describe('auth-actions', () => { "name": "John Doe", "apiRevision": 12345678, "uuid": "zzzzz-tpzed-abcefg", + "userIsActive": true }, { "active": false, "baseUrl": "", @@ -156,22 +177,22 @@ describe('auth-actions', () => { // TODO: Add remaining action tests /* - it('should fire external url to login', () => { - const initialState = undefined; - window.location.assign = jest.fn(); - reducer(initialState, authActions.LOGIN()); - expect(window.location.assign).toBeCalledWith( - `/login?return_to=${window.location.protocol}//${window.location.host}/token` - ); - }); - - it('should fire external url to logout', () => { - const initialState = undefined; - window.location.assign = jest.fn(); - reducer(initialState, authActions.LOGOUT()); - expect(window.location.assign).toBeCalledWith( - `/logout?return_to=${location.protocol}//${location.host}` - ); - }); - */ + it('should fire external url to login', () => { + const initialState = undefined; + window.location.assign = jest.fn(); + reducer(initialState, authActions.LOGIN()); + expect(window.location.assign).toBeCalledWith( + `/login?return_to=${window.location.protocol}//${window.location.host}/token` + ); + }); + + it('should fire external url to logout', () => { + const initialState = undefined; + window.location.assign = jest.fn(); + reducer(initialState, authActions.LOGOUT()); + expect(window.location.assign).toBeCalledWith( + `/logout?return_to=${location.protocol}//${location.host}` + ); + }); + */ });