1fe34381d941372a79b0b3835703895807cffc29
[arvados-workbench2.git] / src / store / auth / auth-middleware.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import 'jest-localstorage-mock';
6 import Axios, { AxiosInstance } from "axios";
7 import { createBrowserHistory } from "history";
8
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';
16
17 describe("AuthMiddleware", () => {
18     let store: RootStore;
19     let services: ServiceRepository;
20     let axiosInst: AxiosInstance;
21     const actions: ApiActions = {
22         progressFn: (id: string, working: boolean) => { },
23         errorFn: (id: string, message: string) => { }
24     };
25
26     beforeEach(() => {
27         axiosInst = Axios.create({ headers: {} });
28         services = createServices(mockConfig({}), actions, axiosInst);
29         store = configureStore(createBrowserHistory(), services);
30         localStorage.clear();
31     });
32
33     it("handles LOGOUT action", () => {
34         localStorage.setItem(API_TOKEN_KEY, 'someToken');
35         window.location.assign = jest.fn();
36         const next = jest.fn();
37         const middleware = authMiddleware(services)(store)(next);
38         middleware(authActions.LOGOUT({deleteLinkData: false}));
39         expect(window.location.assign).toBeCalledWith(
40             `/logout?return_to=${location.protocol}//${location.host}`
41         );
42         expect(localStorage.getItem(API_TOKEN_KEY)).toBeFalsy();
43     });
44 });