The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
import Cookie from "js-cookie";
import { logout, refreshSession } from "metabase/redux/auth";
import { navigate } from "metabase/router";
import {
COOKIE_POOLING_TIMEOUT,
SESSION_KEY,
createSessionMiddleware,
} from "./session-middleware";
jest.mock("js-cookie", () => jest.fn());
jest.mock("metabase/redux/auth", () => ({
logout: jest.fn(),
refreshSession: jest.fn(() => Promise.resolve()),
}));
jest.mock("metabase/router", () => ({
...jest.requireActual("metabase/router"),
navigate: jest.fn(),
}));
const actionStub = { type: "ANY_ACTION" };
/**
* TODO: better solution is to provide configurable window object
* it will be possible after migration to more recent react-router (5+)
*
* Changes the URL used by the global JSDOM instance in the current window.
* jsdom is making more and more properties non-configurable
* ref https://github.com/jsdom/jsdom/issues/3492
*
* NOTE: This does not change the origin which would result in a security exception.
*/
function changeJSDOMURL(url: string) {
const newURL = new URL(url);
const href = `${window.origin}${newURL.pathname}${newURL.search}${newURL.hash}`;
history.replaceState(history.state, "", href);
}
const setup = () => {
const dispatchMock = jest.fn();
const storeMock = { dispatch: dispatchMock, getState: jest.fn() };
const nextMock = jest.fn();
const sessionMiddleware = createSessionMiddleware([]);
return {
handleAction: sessionMiddleware(storeMock)(nextMock),
dispatchMock,
nextMock,
};
};
describe("createSessionMiddleware", () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.useRealTimers();
});
it("should read metabase.TIMEOUT cookie to check the session", () => {
Cookie.get = jest.fn();
const { handleAction } = setup();
handleAction(actionStub);
expect(Cookie.get).toHaveBeenCalledWith(SESSION_KEY);
});
it("should call next function", () => {
Cookie.get = jest.fn();
const { handleAction, nextMock } = setup();
handleAction(actionStub);
expect(nextMock).toHaveBeenCalledWith(actionStub);
});
describe("when logged in", () => {
beforeEach(() => {
changeJSDOMURL("https://metabase.com/question/1?query=5#hash");
});
it("should not dispatch the logout action when session exists", () => {
Cookie.get = jest
.fn()
.mockImplementationOnce(() => "alive")
.mockImplementationOnce(() => "alive");
const { handleAction, dispatchMock } = setup();
handleAction(actionStub);
jest.advanceTimersByTime(COOKIE_POOLING_TIMEOUT);
expect(dispatchMock).not.toHaveBeenCalled();
});
it("should keep being logged in not dispatching any actions", () => {
Cookie.get = jest
.fn()
.mockImplementationOnce(() => "alive")
.mockImplementationOnce(() => undefined);
const { handleAction, dispatchMock } = setup();
handleAction(actionStub);
jest.advanceTimersByTime(COOKIE_POOLING_TIMEOUT);
expect(dispatchMock).toHaveBeenCalled();
expect(logout).toHaveBeenCalledWith("/question/1?query=5#hash");
});
});
describe("logged in redirect", () => {
it("should not redirect to the redirectUrl if visiting login page", async () => {
changeJSDOMURL(
"https://metabase.com/auth/login?redirect=%2Fquestion%2F1%3Fquery%3D5%23hash",
);
Cookie.get = jest
.fn()
.mockImplementationOnce(() => "stale")
.mockImplementationOnce(() => "stale");
const { handleAction, dispatchMock } = setup();
handleAction(actionStub);
jest.advanceTimersByTime(COOKIE_POOLING_TIMEOUT);
expect(dispatchMock).not.toHaveBeenCalled();
expect(navigate).not.toHaveBeenCalled();
});
it("should redirect to the redirectUrl", async () => {
changeJSDOMURL(
"https://metabase.com/some-route?redirect=%2Fquestion%2F1%3Fquery%3D5%23hash",
);
Cookie.get = jest
.fn()
.mockImplementationOnce(() => "alive")
.mockImplementationOnce(() => "alive");
const { handleAction } = setup();
handleAction(actionStub);
jest.advanceTimersByTime(COOKIE_POOLING_TIMEOUT);
expect(navigate).toHaveBeenCalledWith("/question/1?query=5#hash", {
replace: true,
});
});
});
describe("when not logged in", () => {
beforeEach(() => {
changeJSDOMURL(
"http://localhost/auth/login?redirect=%2Fquestion%2F1%3Fquery%3D5%23hash",
);
});
it("should redirect to the redirectUrl when a session appears", async () => {
Cookie.get = jest
.fn()
.mockImplementationOnce(() => undefined)
.mockImplementationOnce(() => "alive");
const { handleAction, dispatchMock } = setup();
handleAction(actionStub);
jest.advanceTimersByTime(COOKIE_POOLING_TIMEOUT);
expect(dispatchMock).toHaveBeenCalled();
expect(refreshSession).toHaveBeenCalledWith();
// wait for the refreshSession to resolve
await Promise.resolve();
expect(navigate).toHaveBeenCalledWith("/question/1?query=5#hash", {
replace: true,
});
});
});
});