From 847f19027dbe050bdceb835f4c8c67d386159a72 Mon Sep 17 00:00:00 2001 From: Lucas Di Pentima Date: Tue, 1 Sep 2020 20:09:59 -0300 Subject: [PATCH] 16679: Adds test for auth middleware's logout handling. This confirms that the API endpoint is being called and the token is removed from local storage. Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima --- src/store/auth/auth-middleware.test.ts | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/store/auth/auth-middleware.test.ts diff --git a/src/store/auth/auth-middleware.test.ts b/src/store/auth/auth-middleware.test.ts new file mode 100644 index 00000000..1fe34381 --- /dev/null +++ b/src/store/auth/auth-middleware.test.ts @@ -0,0 +1,44 @@ +// 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 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); + 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})); + expect(window.location.assign).toBeCalledWith( + `/logout?return_to=${location.protocol}//${location.host}` + ); + expect(localStorage.getItem(API_TOKEN_KEY)).toBeFalsy(); + }); +}); \ No newline at end of file -- 2.30.2