X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/6de571a660401585bc96dd92fd2563e9b64c58c6..2f83fcd45b4b23db2bb5bb4afbe1e863ebd77ec6:/services/workbench2/src/store/auth/auth-middleware.test.ts diff --git a/services/workbench2/src/store/auth/auth-middleware.test.ts b/services/workbench2/src/store/auth/auth-middleware.test.ts new file mode 100644 index 0000000000..5a0364ebf9 --- /dev/null +++ b/services/workbench2/src/store/auth/auth-middleware.test.ts @@ -0,0 +1,45 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + +import 'jest-localstorage-mock'; +import Axios, { AxiosInstance } from "axios"; +import { createBrowserHistory } from "history"; + +import { authMiddleware } from "./auth-middleware"; +import { RootStore, configureStore } from "../store"; +import { ServiceRepository, createServices } from "services/services"; +import { ApiActions } from "services/api/api-actions"; +import { mockConfig } from "common/config"; +import { authActions } from "./auth-action"; +import { API_TOKEN_KEY } from 'services/auth-service/auth-service'; + +describe("AuthMiddleware", () => { + let store: RootStore; + let services: ServiceRepository; + let axiosInst: AxiosInstance; + const config: any = {}; + const actions: ApiActions = { + progressFn: (id: string, working: boolean) => { }, + errorFn: (id: string, message: string) => { } + }; + + beforeEach(() => { + axiosInst = Axios.create({ headers: {} }); + services = createServices(mockConfig({}), actions, axiosInst); + store = configureStore(createBrowserHistory(), services, config); + localStorage.clear(); + }); + + it("handles LOGOUT action", () => { + localStorage.setItem(API_TOKEN_KEY, 'someToken'); + window.location.assign = jest.fn(); + const next = jest.fn(); + const middleware = authMiddleware(services)(store)(next); + middleware(authActions.LOGOUT({deleteLinkData: false, preservePath: false})); + expect(window.location.assign).toBeCalledWith( + `/logout?api_token=someToken&return_to=${location.protocol}//${location.host}` + ); + expect(localStorage.getItem(API_TOKEN_KEY)).toBeFalsy(); + }); +});