fix(auth): redirect expired sessions before permission checks
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { createTheme, ThemeProvider } from "@mui/material/styles";
|
||||
import { act, render, screen } from "@testing-library/react";
|
||||
import { act, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { signIn } from "next-auth/react";
|
||||
|
||||
import { useAuthStore } from "@/store/authStore";
|
||||
import { SessionExpiryDialog } from "./SessionExpiryDialog";
|
||||
@@ -10,6 +11,8 @@ jest.mock("next-auth/react", () => ({
|
||||
|
||||
describe("SessionExpiryDialog", () => {
|
||||
beforeEach(() => {
|
||||
jest.useFakeTimers();
|
||||
jest.mocked(signIn).mockReset().mockResolvedValue(undefined);
|
||||
useAuthStore.setState({
|
||||
accessToken: null,
|
||||
sessionExpired: true,
|
||||
@@ -19,6 +22,7 @@ describe("SessionExpiryDialog", () => {
|
||||
|
||||
afterEach(() => {
|
||||
act(() => useAuthStore.getState().clearSessionExpired());
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
it("renders above every regular application overlay", () => {
|
||||
@@ -36,4 +40,40 @@ describe("SessionExpiryDialog", () => {
|
||||
zIndex: theme.zIndex.tooltip + 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("shows the expired state before automatically starting login", () => {
|
||||
window.history.replaceState({}, "", "/network-simulation?tab=history");
|
||||
|
||||
render(<SessionExpiryDialog />);
|
||||
|
||||
expect(screen.getByText("登录已过期")).toBeInTheDocument();
|
||||
expect(screen.getByText("即将自动跳转到登录页面。"))
|
||||
.toBeInTheDocument();
|
||||
expect(signIn).not.toHaveBeenCalled();
|
||||
|
||||
act(() => {
|
||||
jest.advanceTimersByTime(2_000);
|
||||
});
|
||||
|
||||
expect(signIn).toHaveBeenCalledTimes(1);
|
||||
expect(signIn).toHaveBeenCalledWith("keycloak", {
|
||||
callbackUrl: "/network-simulation?tab=history",
|
||||
redirect: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("allows a retry when starting automatic login fails", async () => {
|
||||
jest.mocked(signIn)
|
||||
.mockRejectedValueOnce(new Error("network unavailable"))
|
||||
.mockResolvedValueOnce(undefined);
|
||||
|
||||
render(<SessionExpiryDialog />);
|
||||
|
||||
await act(async () => {
|
||||
jest.advanceTimersByTime(2_000);
|
||||
});
|
||||
fireEvent.click(screen.getByRole("button", { name: "重新认证" }));
|
||||
|
||||
expect(signIn).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user