Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
198 lines
7.8 KiB
TypeScript
198 lines
7.8 KiB
TypeScript
import { act, render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
import OnlineUsersPage from "./page";
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
|
|
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
|
|
|
const auth = vi.hoisted(() => ({ hasModule: vi.fn(() => true), loading: false }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
|
|
}));
|
|
|
|
const svc = vi.hoisted(() => ({ listOnlineUsers: vi.fn(), signOutOnlineUser: vi.fn() }));
|
|
vi.mock("@/app/lib/services/onlineUsers", async (importOriginal) => ({
|
|
...(await importOriginal<typeof import("@/app/lib/services/onlineUsers")>()),
|
|
listOnlineUsers: svc.listOnlineUsers,
|
|
signOutOnlineUser: svc.signOutOnlineUser,
|
|
}));
|
|
|
|
const NOW = Date.UTC(2026, 10, 20, 4, 0, 0);
|
|
|
|
function row(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
id: "bob.admin",
|
|
name: "Bob Lim",
|
|
roles: ["Finance"],
|
|
lastSeenAt: NOW - 60_000,
|
|
clientIp: "203.0.113.7",
|
|
activeSince: NOW - 3_600_000,
|
|
platform: "AD",
|
|
self: false,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function page(kind: "ADMIN" | "MEMBER", rows: unknown[], counts = { adminCount: 2, memberCount: 5 }) {
|
|
return { kind, windowMinutes: 15, ...counts, total: rows.length, rows };
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
memoryRouter.reset("/online-users");
|
|
auth.hasModule.mockReturnValue(true);
|
|
svc.listOnlineUsers.mockResolvedValue(page("ADMIN", [row(), row({ id: "jane.admin", name: "Jane", self: true })]));
|
|
svc.signOutOnlineUser.mockResolvedValue(undefined);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
describe("OnlineUsersPage", () => {
|
|
it("renders the not-authorised state without the BO_ONLINE_USERS grant", async () => {
|
|
auth.hasModule.mockReturnValue(false);
|
|
|
|
render(<OnlineUsersPage />);
|
|
|
|
expect(await screen.findByTestId("not-authorized")).toBeInTheDocument();
|
|
expect(svc.listOnlineUsers).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("lists active admins for the last 15 minutes with both counts", async () => {
|
|
render(<OnlineUsersPage />);
|
|
|
|
expect(await screen.findByText("Bob Lim")).toBeInTheDocument();
|
|
expect(svc.listOnlineUsers).toHaveBeenCalledWith(expect.objectContaining({ kind: "ADMIN", windowMinutes: 15 }));
|
|
expect(screen.getByRole("button", { name: "Admins (2)" })).toHaveAttribute("aria-pressed", "true");
|
|
expect(screen.getByRole("button", { name: "Members (5)" })).toBeInTheDocument();
|
|
const bob = screen.getByText("Bob Lim").closest("tr")!;
|
|
expect(within(bob).getByText("203.0.113.7")).toBeInTheDocument();
|
|
expect(within(bob).getByText("Finance")).toBeInTheDocument();
|
|
});
|
|
|
|
it("offers no sign-out on your own row", async () => {
|
|
render(<OnlineUsersPage />);
|
|
|
|
const own = (await screen.findByText("Jane")).closest("tr")!;
|
|
expect(within(own).getByText("You")).toBeInTheDocument();
|
|
expect(within(own).queryByRole("button", { name: /sign out/i })).toBeNull();
|
|
const other = screen.getByText("Bob Lim").closest("tr")!;
|
|
expect(within(other).getByRole("button", { name: "Sign out" })).toBeInTheDocument();
|
|
});
|
|
|
|
it("switches to members and the 60-minute window", async () => {
|
|
const user = userEvent.setup();
|
|
render(<OnlineUsersPage />);
|
|
await screen.findByText("Bob Lim");
|
|
|
|
svc.listOnlineUsers.mockResolvedValue(page("MEMBER", [row({ id: "MY1", name: "Ali", roles: undefined })]));
|
|
await user.click(screen.getByRole("button", { name: "Members (5)" }));
|
|
expect(await screen.findByText("MY1")).toBeInTheDocument();
|
|
await user.click(screen.getByRole("button", { name: "Last hour" }));
|
|
|
|
await waitFor(() =>
|
|
expect(svc.listOnlineUsers).toHaveBeenLastCalledWith(
|
|
expect.objectContaining({ kind: "MEMBER", windowMinutes: 60 }),
|
|
),
|
|
);
|
|
});
|
|
|
|
it("signs someone out only with a reason, then refreshes", async () => {
|
|
const user = userEvent.setup();
|
|
render(<OnlineUsersPage />);
|
|
const other = (await screen.findByText("Bob Lim")).closest("tr")!;
|
|
|
|
await user.click(within(other).getByRole("button", { name: "Sign out" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
const confirm = within(dialog).getByRole("button", { name: "Sign out" });
|
|
expect(confirm).toBeDisabled();
|
|
await user.type(within(dialog).getByLabelText("Reason"), " left the company ");
|
|
await user.click(confirm);
|
|
|
|
await waitFor(() =>
|
|
expect(svc.signOutOnlineUser).toHaveBeenCalledWith({ kind: "ADMIN", id: "bob.admin", reason: "left the company" }),
|
|
);
|
|
expect(await screen.findByRole("status")).toHaveTextContent("Bob Lim was signed out.");
|
|
expect(svc.listOnlineUsers.mock.calls.length).toBeGreaterThanOrEqual(2);
|
|
});
|
|
|
|
it("shows the self sign-out refusal from the server", async () => {
|
|
const user = userEvent.setup();
|
|
svc.signOutOnlineUser.mockRejectedValue(
|
|
new ApiError("x", 400, { code: "OnlineUserCannotSignOutSelf" }),
|
|
);
|
|
render(<OnlineUsersPage />);
|
|
const other = (await screen.findByText("Bob Lim")).closest("tr")!;
|
|
|
|
await user.click(within(other).getByRole("button", { name: "Sign out" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
await user.type(within(dialog).getByLabelText("Reason"), "x");
|
|
await user.click(within(dialog).getByRole("button", { name: "Sign out" }));
|
|
|
|
expect(await within(dialog).findByRole("alert")).toHaveTextContent("You can't sign out your own account.");
|
|
});
|
|
|
|
it("shows a load failure with a retry", async () => {
|
|
svc.listOnlineUsers.mockRejectedValue(new Error("down"));
|
|
|
|
render(<OnlineUsersPage />);
|
|
|
|
expect(await screen.findByText("Couldn't load online users.")).toBeInTheDocument();
|
|
});
|
|
|
|
it("refreshes every 30 seconds", async () => {
|
|
vi.useFakeTimers({ shouldAdvanceTime: true });
|
|
render(<OnlineUsersPage />);
|
|
await screen.findByText("Bob Lim");
|
|
const before = svc.listOnlineUsers.mock.calls.length;
|
|
|
|
await act(async () => {
|
|
await vi.advanceTimersByTimeAsync(30_000);
|
|
});
|
|
|
|
expect(svc.listOnlineUsers.mock.calls.length).toBe(before + 1);
|
|
});
|
|
it("does not refresh while the browser tab is hidden", async () => {
|
|
vi.useFakeTimers({ shouldAdvanceTime: true });
|
|
render(<OnlineUsersPage />);
|
|
await screen.findByText("Bob Lim");
|
|
const before = svc.listOnlineUsers.mock.calls.length;
|
|
const visibility = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");
|
|
|
|
await act(async () => {
|
|
await vi.advanceTimersByTimeAsync(30_000);
|
|
});
|
|
|
|
expect(svc.listOnlineUsers.mock.calls.length).toBe(before);
|
|
visibility.mockRestore();
|
|
});
|
|
|
|
it("clears the previous tab's rows while the other tab loads", async () => {
|
|
const user = userEvent.setup();
|
|
render(<OnlineUsersPage />);
|
|
await screen.findByText("Bob Lim");
|
|
|
|
svc.listOnlineUsers.mockReturnValue(new Promise(() => {}));
|
|
await user.click(screen.getByRole("button", { name: "Members (5)" }));
|
|
|
|
await waitFor(() => expect(screen.queryByText("Bob Lim")).toBeNull());
|
|
});
|
|
|
|
it("shows the super-admin refusal from the server", async () => {
|
|
const user = userEvent.setup();
|
|
svc.signOutOnlineUser.mockRejectedValue(new ApiError("x", 403, { code: "OnlineUserCannotSignOutSuperAdmin" }));
|
|
render(<OnlineUsersPage />);
|
|
const other = (await screen.findByText("Bob Lim")).closest("tr")!;
|
|
|
|
await user.click(within(other).getByRole("button", { name: "Sign out" }));
|
|
const dialog = await screen.findByRole("dialog");
|
|
await user.type(within(dialog).getByLabelText("Reason"), "x");
|
|
await user.click(within(dialog).getByRole("button", { name: "Sign out" }));
|
|
|
|
expect(await within(dialog).findByRole("alert")).toHaveTextContent("Only a super admin can sign out a super admin.");
|
|
});
|
|
});
|