Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/routes/app.$tenant/settings/security.test.tsx
T

100 lines
4.7 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
requireAuth: vi.fn(),
getUserInfo: vi.fn(),
validateCSRFToken: vi.fn(),
getActiveSessionByToken: vi.fn(),
listActiveUserSessions: vi.fn(),
revokeUserSession: vi.fn(),
revokeAllUserSessionsExcept: vi.fn(),
markSessionTrusted: vi.fn(),
listTrustedDevices: vi.fn(),
revokeTrustedDevice: vi.fn(),
revokeAllTrustedDevices: vi.fn(),
}));
vi.mock("~/utils/loaders.middleware", () => ({ requireAuth: (...a: unknown[]) => mocks.requireAuth(...a) }));
vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn(async () => ({ t: (key: string) => key })) }));
vi.mock("~/utils/session.server", () => ({
getUserInfo: (...a: unknown[]) => mocks.getUserInfo(...a),
validateCSRFToken: (...a: unknown[]) => mocks.validateCSRFToken(...a),
}));
vi.mock("~/services/auth/sessionStore.server", () => ({
getActiveSessionByToken: (...a: unknown[]) => mocks.getActiveSessionByToken(...a),
listActiveUserSessions: (...a: unknown[]) => mocks.listActiveUserSessions(...a),
revokeUserSession: (...a: unknown[]) => mocks.revokeUserSession(...a),
revokeAllUserSessionsExcept: (...a: unknown[]) => mocks.revokeAllUserSessionsExcept(...a),
markSessionTrusted: (...a: unknown[]) => mocks.markSessionTrusted(...a),
}));
vi.mock("~/utils/auth/mfa/trusted-device.server", () => ({
listTrustedDevices: (...a: unknown[]) => mocks.listTrustedDevices(...a),
revokeTrustedDevice: (...a: unknown[]) => mocks.revokeTrustedDevice(...a),
revokeAllTrustedDevices: (...a: unknown[]) => mocks.revokeAllTrustedDevices(...a),
}));
import { action, loader } from "./security";
const params = { tenant: "acme" };
beforeEach(() => {
vi.clearAllMocks();
mocks.getUserInfo.mockResolvedValue({ userId: "u1", sessionToken: "tok-current", csrf: "csrf-1" });
mocks.getActiveSessionByToken.mockResolvedValue({ id: "s1" });
mocks.listActiveUserSessions.mockResolvedValue([
{ id: "s1", lastSeenAt: new Date(), ipAddress: "1.1.1.1", userAgent: "Firefox", deviceLabel: null, approxLocation: null, trustedAt: null },
{ id: "s2", lastSeenAt: new Date(), ipAddress: "2.2.2.2", userAgent: "Safari", deviceLabel: null, approxLocation: null, trustedAt: null },
]);
mocks.listTrustedDevices.mockResolvedValue([{ id: "td1", deviceLabel: null, userAgent: "Firefox", approxLocation: "DE" }]);
});
function postRequest(fields: Record<string, string>) {
const body = new URLSearchParams(fields);
return new Request("https://example.test/acme/settings/security", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body,
});
}
describe("security route loader", () => {
it("builds a ready device-list VM and flags the current session", async () => {
const data = await loader({ request: new Request("https://example.test/acme/settings/security"), params, context: {} } as never);
expect(mocks.requireAuth).toHaveBeenCalled();
expect(data.vm.status).toBe("ready");
if (data.vm.status !== "ready") return;
expect(data.vm.data.devices.find((d) => d.id === "s1")?.isCurrent).toBe(true);
expect(data.vm.data.otherActiveCount).toBe(1);
expect(data.csrf).toBe("csrf-1");
});
});
describe("security route action", () => {
it("revokes a single session by id", async () => {
const res = await action({ request: postRequest({ csrf: "csrf-1", intent: "revoke", sessionId: "s2" }), params, context: {} } as never);
expect(mocks.validateCSRFToken).toHaveBeenCalled();
expect(mocks.revokeUserSession).toHaveBeenCalledWith({ userId: "u1", sessionId: "s2" });
expect((res as Response).status).toBe(302);
});
it("revokes all other sessions except the current one", async () => {
await action({ request: postRequest({ csrf: "csrf-1", intent: "revoke-all" }), params, context: {} } as never);
expect(mocks.revokeAllUserSessionsExcept).toHaveBeenCalledWith({ userId: "u1", sessionId: "s1" });
});
it("marks a session trusted", async () => {
await action({ request: postRequest({ csrf: "csrf-1", intent: "trust", sessionId: "s2" }), params, context: {} } as never);
expect(mocks.markSessionTrusted).toHaveBeenCalledWith({ userId: "u1", sessionId: "s2" });
});
it("forgets a single trusted device by id", async () => {
await action({ request: postRequest({ csrf: "csrf-1", intent: "forget-device", deviceId: "td1" }), params, context: {} } as never);
expect(mocks.revokeTrustedDevice).toHaveBeenCalledWith({ userId: "u1", deviceId: "td1" });
});
it("forgets all trusted devices", async () => {
await action({ request: postRequest({ csrf: "csrf-1", intent: "forget-all-devices" }), params, context: {} } as never);
expect(mocks.revokeAllTrustedDevices).toHaveBeenCalledWith("u1");
});
});