1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import 'jest-localstorage-mock';
6 import Axios, { AxiosInstance } from "axios";
7 import { createBrowserHistory } from "history";
9 import { authMiddleware } from "./auth-middleware";
10 import { RootStore, configureStore } from "../store";
11 import { ServiceRepository, createServices } from "services/services";
12 import { ApiActions } from "services/api/api-actions";
13 import { mockConfig } from "common/config";
14 import { authActions } from "./auth-action";
15 import { API_TOKEN_KEY } from 'services/auth-service/auth-service';
17 describe("AuthMiddleware", () => {
19 let services: ServiceRepository;
20 let axiosInst: AxiosInstance;
21 const config: any = {};
22 const actions: ApiActions = {
23 progressFn: (id: string, working: boolean) => { },
24 errorFn: (id: string, message: string) => { }
28 axiosInst = Axios.create({ headers: {} });
29 services = createServices(mockConfig({}), actions, axiosInst);
30 store = configureStore(createBrowserHistory(), services, config);
34 it("handles LOGOUT action", () => {
35 localStorage.setItem(API_TOKEN_KEY, 'someToken');
36 Object.defineProperty(window, 'location', {
37 value: { assign: jest.fn(), href: 'http://localhost', protocol: 'http:', host: 'localhost'}
39 const next = jest.fn();
40 const middleware = authMiddleware(services)(store)(next);
41 middleware(authActions.LOGOUT({deleteLinkData: false, preservePath: false}));
42 expect(window.location.assign).toBeCalledWith(
43 `/logout?api_token=someToken&return_to=${location.protocol}//${location.host}`
45 expect(localStorage.getItem(API_TOKEN_KEY)).toBeFalsy();