115 lines
4.2 KiB
TypeScript
115 lines
4.2 KiB
TypeScript
// @vitest-environment node
|
|
import type { ActionFunctionArgs } from "react-router";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const { adminSession, transaction } = vi.hoisted(() => ({
|
|
adminSession: {
|
|
id: "session-current",
|
|
adminUserId: "admin-1",
|
|
adminUser: { id: "admin-1", email: "admin@example.test" },
|
|
},
|
|
transaction: {
|
|
adminSession: { updateMany: vi.fn() },
|
|
},
|
|
}));
|
|
|
|
vi.mock("@hivegrid/db/rls-context.server", () => ({
|
|
runWithRlsTransaction: vi.fn(async (_transaction, operation: () => Promise<unknown>) => operation()),
|
|
}));
|
|
vi.mock("~/services/admin/admin-action-audit.server", async () => {
|
|
const actual = await vi.importActual<typeof import("~/services/admin/admin-action-audit.server")>("~/services/admin/admin-action-audit.server");
|
|
return { ...actual, recordAdminAction: vi.fn() };
|
|
});
|
|
vi.mock("~/services/admin/admin-identity.server", () => ({
|
|
requireAdminIdentityPermission: vi.fn(),
|
|
runAsAdminIdentity: vi.fn(async (_request, _permission, operation) => operation(adminSession)),
|
|
}));
|
|
vi.mock("~/utils/session.server", () => ({ validateCSRFToken: vi.fn() }));
|
|
vi.mock("~/utils/db.server", () => ({
|
|
db: { $transaction: vi.fn(async (operation) => operation(transaction)) },
|
|
}));
|
|
|
|
import { recordAdminAction } from "~/services/admin/admin-action-audit.server";
|
|
import { requireAdminIdentityPermission, runAsAdminIdentity } from "~/services/admin/admin-identity.server";
|
|
import { validateCSRFToken } from "~/utils/session.server";
|
|
import { action } from "./security";
|
|
|
|
function request(values: Record<string, string>) {
|
|
return new Request("https://admin.example.test/settings/security", {
|
|
method: "POST",
|
|
body: new URLSearchParams(values),
|
|
});
|
|
}
|
|
|
|
function callAction(values: Record<string, string>) {
|
|
return action({
|
|
request: request(values),
|
|
params: {},
|
|
context: {},
|
|
unstable_pattern: "admin/settings/security",
|
|
} as ActionFunctionArgs);
|
|
}
|
|
|
|
describe("admin security settings action", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(requireAdminIdentityPermission).mockResolvedValue(adminSession as never);
|
|
transaction.adminSession.updateMany.mockResolvedValue({ count: 1 });
|
|
});
|
|
|
|
it("revokes another own session and writes the mandatory audit in the same transaction", async () => {
|
|
const response = await callAction({
|
|
intent: "revoke-session",
|
|
sessionId: "session-other",
|
|
reason: "Verlorenes Arbeitsgerät",
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
await expect(response.json()).resolves.toEqual({
|
|
ok: true,
|
|
messageKey: "admin.sales.security.toast.sessionRevoked",
|
|
});
|
|
expect(validateCSRFToken).toHaveBeenCalledOnce();
|
|
expect(requireAdminIdentityPermission).toHaveBeenCalledWith(expect.any(Request), "admin_identity.sessions.revoke");
|
|
expect(runAsAdminIdentity).toHaveBeenCalledWith(expect.any(Request), "admin_identity.sessions.revoke", expect.any(Function));
|
|
expect(transaction.adminSession.updateMany).toHaveBeenCalledWith({
|
|
where: { id: "session-other", adminUserId: adminSession.adminUserId, revokedAt: null },
|
|
data: { revokedAt: expect.any(Date) },
|
|
});
|
|
expect(recordAdminAction).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
action: "security.sessionRevoked",
|
|
actorUserId: adminSession.adminUserId,
|
|
targetId: "session-other",
|
|
reason: "Verlorenes Arbeitsgerät",
|
|
}),
|
|
transaction
|
|
);
|
|
});
|
|
|
|
it("rejects revoking the current session before any mutation", async () => {
|
|
const response = await callAction({
|
|
intent: "revoke-session",
|
|
sessionId: adminSession.id,
|
|
reason: "Sitzung schließen",
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
expect(transaction.adminSession.updateMany).not.toHaveBeenCalled();
|
|
expect(recordAdminAction).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("fails closed when the selected session no longer exists", async () => {
|
|
transaction.adminSession.updateMany.mockResolvedValueOnce({ count: 0 });
|
|
|
|
const response = await callAction({
|
|
intent: "revoke-session",
|
|
sessionId: "session-missing",
|
|
reason: "Verlorenes Arbeitsgerät",
|
|
});
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(recordAdminAction).not.toHaveBeenCalled();
|
|
});
|
|
});
|