|
@@ -0,0 +1,63 @@
|
|
|
|
|
+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
|
|
|
+import { MemoryRouter } from "react-router";
|
|
|
|
|
+import { useSyncExternalStore } from "react";
|
|
|
|
|
+import { afterEach, expect, it, vi } from "vitest";
|
|
|
|
|
+import { authContext } from "../../lib/api/bos";
|
|
|
|
|
+import { UNAUTHORIZED_EVENT, setToken } from "../../lib/api/http";
|
|
|
|
|
+import type { AuthContext } from "../../lib/api/types";
|
|
|
|
|
+import { dismissSessionNotice, getSessionNotice, subscribeSessionNotice } from "../../lib/session-notice";
|
|
|
|
|
+import { SessionProvider, useSession } from "../session";
|
|
|
|
|
+
|
|
|
|
|
+vi.mock("../../lib/api/bos", async (load) => ({
|
|
|
|
|
+ ...await load<typeof import("../../lib/api/bos")>(),
|
|
|
|
|
+ authContext: vi.fn(),
|
|
|
|
|
+}));
|
|
|
|
|
+
|
|
|
|
|
+const context: AuthContext = {
|
|
|
|
|
+ user_id: "fixture-user",
|
|
|
|
|
+ username: "fixture",
|
|
|
|
|
+ role_code: "enterprise",
|
|
|
|
|
+ subject_type: "customer",
|
|
|
|
|
+ subject_id: "fixture-customer",
|
|
|
|
|
+ entry_only: false,
|
|
|
|
|
+ scope_mode: "self",
|
|
|
|
|
+ display_name: "测试用户",
|
|
|
|
|
+ subject_name: "测试客户",
|
|
|
|
|
+ mobile_hint: null,
|
|
|
|
|
+ scope: { kind: "self", region_node_ids: [], customer_ids: [], team_ids: [] },
|
|
|
|
|
+ acct_status: "normal",
|
|
|
|
|
+ must_change_password: false,
|
|
|
|
|
+ permissions: [],
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+afterEach(() => {
|
|
|
|
|
+ cleanup();
|
|
|
|
|
+ dismissSessionNotice();
|
|
|
|
|
+ setToken(null);
|
|
|
|
|
+ sessionStorage.removeItem("oss:returnTo");
|
|
|
|
|
+ vi.mocked(authContext).mockReset();
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+function SessionProbe() {
|
|
|
|
|
+ const { ctx, signIn } = useSession();
|
|
|
|
|
+ const notice = useSyncExternalStore(subscribeSessionNotice, getSessionNotice);
|
|
|
|
|
+ return <>
|
|
|
|
|
+ {notice && <p role="status">{notice}</p>}
|
|
|
|
|
+ {ctx && <p>{ctx.username}</p>}
|
|
|
|
|
+ <button type="button" onClick={() => void signIn("fixture-token").catch(() => {})}>重新登录</button>
|
|
|
|
|
+ </>;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+it("登录失败保留会话过期提示,成功重新登录后立即清除", async () => {
|
|
|
|
|
+ vi.mocked(authContext).mockRejectedValueOnce(new Error("登录失败")).mockResolvedValueOnce(context);
|
|
|
|
|
+ render(<MemoryRouter><SessionProvider><SessionProbe /></SessionProvider></MemoryRouter>);
|
|
|
|
|
+ act(() => { window.dispatchEvent(new Event(UNAUTHORIZED_EVENT)); });
|
|
|
|
|
+ expect(screen.getByRole("status").textContent).toBe("登录已过期,请重新登录。");
|
|
|
|
|
+
|
|
|
|
|
+ await act(async () => { fireEvent.click(screen.getByRole("button", { name: "重新登录" })); });
|
|
|
|
|
+ expect(screen.getByRole("status").textContent).toBe("登录已过期,请重新登录。");
|
|
|
|
|
+
|
|
|
|
|
+ fireEvent.click(screen.getByRole("button", { name: "重新登录" }));
|
|
|
|
|
+ await screen.findByText("fixture");
|
|
|
|
|
+ expect(screen.queryByRole("status")).toBeNull();
|
|
|
|
|
+});
|