113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
import { beforeEach, describe, expect, test, vi } from "vitest";
|
|
|
|
const getTenantIdMock = vi.fn().mockResolvedValue("t-1");
|
|
const verifyPermissionMock = vi.fn().mockResolvedValue(undefined);
|
|
const getPluginForTenantMock = vi.fn();
|
|
const auditFindManyMock = vi.fn();
|
|
|
|
vi.mock("~/utils/services/.server/urlService", () => ({
|
|
getTenantIdFromUrl: (...args: unknown[]) => getTenantIdMock(...args),
|
|
}));
|
|
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({
|
|
verifyUserHasPermission: (...args: unknown[]) => verifyPermissionMock(...args),
|
|
}));
|
|
vi.mock("~/locale/i18next.server", () => ({
|
|
getTranslations: vi.fn(async () => ({
|
|
t: (key: string, fallback?: string) => fallback ?? key,
|
|
})),
|
|
}));
|
|
vi.mock("~/modules/plugins/services/.server/PluginLifecycleService", () => ({
|
|
getPluginForTenant: (...args: unknown[]) => getPluginForTenantMock(...args),
|
|
}));
|
|
vi.mock("~/utils/db.server", () => ({
|
|
dbRead: {
|
|
pluginCapabilityUse: {
|
|
findMany: (...args: unknown[]) => auditFindManyMock(...args),
|
|
},
|
|
},
|
|
}));
|
|
|
|
beforeEach(() => {
|
|
getTenantIdMock.mockClear();
|
|
verifyPermissionMock.mockReset();
|
|
verifyPermissionMock.mockResolvedValue(undefined);
|
|
getPluginForTenantMock.mockReset();
|
|
auditFindManyMock.mockReset();
|
|
});
|
|
|
|
describe("plugin audit route", () => {
|
|
test("loader stops before plugin lookup and audit reads when audit permission is denied", async () => {
|
|
const denial = new Response("Forbidden", { status: 403 });
|
|
verifyPermissionMock.mockRejectedValueOnce(denial);
|
|
const { loader } = await import("./plugins.$id.audit.js");
|
|
|
|
await expect(
|
|
loader({
|
|
request: new Request("http://localhost/app/t-1/settings/plugins/pl-1/audit"),
|
|
params: { tenant: "t-1", id: "pl-1" },
|
|
context: {},
|
|
})
|
|
).rejects.toBe(denial);
|
|
|
|
expect(verifyPermissionMock).toHaveBeenCalledWith(expect.any(Request), "app.settings.auditTrails.view", "t-1");
|
|
expect(getPluginForTenantMock).not.toHaveBeenCalled();
|
|
expect(auditFindManyMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("loader caps and orders capability filter options deterministically", async () => {
|
|
getPluginForTenantMock.mockResolvedValue({
|
|
id: "pl-row-1",
|
|
pluginId: "com.acme.audit",
|
|
version: "1.0.0",
|
|
});
|
|
auditFindManyMock
|
|
.mockResolvedValueOnce([
|
|
{
|
|
id: "audit-1",
|
|
pluginInvocationId: "inv-1",
|
|
capability: "log",
|
|
outcome: "success",
|
|
errorCode: null,
|
|
durationMs: 12,
|
|
payloadHash: "hash-1",
|
|
timestamp: new Date("2026-05-27T10:00:00.000Z"),
|
|
},
|
|
])
|
|
.mockResolvedValueOnce([{ capability: "secret.read" }, { capability: "http.outbound" }, { capability: "log" }]);
|
|
const { loader } = await import("./plugins.$id.audit.js");
|
|
|
|
const response = await loader({
|
|
request: new Request("http://localhost/app/acme/settings/plugins/pl-row-1/audit"),
|
|
params: { tenant: "acme", id: "pl-row-1" },
|
|
context: {},
|
|
});
|
|
|
|
await expect(response.json()).resolves.toMatchObject({
|
|
capabilityOptions: ["http.outbound", "log", "secret.read"],
|
|
});
|
|
expect(auditFindManyMock).toHaveBeenNthCalledWith(
|
|
1,
|
|
expect.objectContaining({
|
|
select: {
|
|
id: true,
|
|
pluginInvocationId: true,
|
|
capability: true,
|
|
outcome: true,
|
|
errorCode: true,
|
|
durationMs: true,
|
|
payloadHash: true,
|
|
timestamp: true,
|
|
},
|
|
})
|
|
);
|
|
expect(auditFindManyMock).toHaveBeenNthCalledWith(
|
|
2,
|
|
expect.objectContaining({
|
|
distinct: ["capability"],
|
|
orderBy: [{ capability: "asc" }, { timestamp: "desc" }],
|
|
take: 32,
|
|
})
|
|
);
|
|
});
|
|
});
|