67 lines
2.6 KiB
TypeScript
67 lines
2.6 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
loadChatContext: vi.fn(),
|
|
requireChatPermission: vi.fn(),
|
|
loadFilesContext: vi.fn(),
|
|
requireFilesPermission: vi.fn(),
|
|
getOwnedFileScanStatus: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/modules/chat/routes/chatAccess.server", () => ({
|
|
loadChatContext: mocks.loadChatContext,
|
|
requireChatPermission: mocks.requireChatPermission,
|
|
}));
|
|
vi.mock("~/utils/integrations/fileAccess.server", () => ({
|
|
loadFilesContext: mocks.loadFilesContext,
|
|
requireFilesPermission: mocks.requireFilesPermission,
|
|
}));
|
|
vi.mock("~/utils/integrations/fileService.server", () => ({ getOwnedFileScanStatus: mocks.getOwnedFileScanStatus }));
|
|
|
|
import { loader } from "./chat.attachments.$fileId.status";
|
|
|
|
const chatContext = {
|
|
tenantId: "tenant_1",
|
|
userId: "user_1",
|
|
tenantUserId: "tenant_user_1",
|
|
permissions: ["chat.write", "files.write"],
|
|
hasModule: true,
|
|
};
|
|
const filesContext = { tenantId: "tenant_1", userId: "user_1", permissions: ["files.write"], hasModule: true };
|
|
|
|
describe("chat attachment scan status resource", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.loadChatContext.mockResolvedValue(chatContext);
|
|
mocks.loadFilesContext.mockResolvedValue(filesContext);
|
|
mocks.requireChatPermission.mockImplementation((context) => context);
|
|
mocks.requireFilesPermission.mockImplementation((context) => context);
|
|
mocks.getOwnedFileScanStatus.mockResolvedValue("clean");
|
|
});
|
|
|
|
it("returns only the owner-scoped scan status behind chat and files write gates", async () => {
|
|
const response = await loader({
|
|
request: new Request("http://app.localhost/demo/chat/attachments/file_1/status"),
|
|
params: { tenant: "demo", fileId: "file_1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.headers.get("cache-control")).toBe("private, no-store");
|
|
await expect(response.json()).resolves.toEqual({ scanStatus: "clean" });
|
|
expect(mocks.getOwnedFileScanStatus).toHaveBeenCalledWith({ tenantId: "tenant_1", userId: "user_1", fileId: "file_1" });
|
|
expect(mocks.requireFilesPermission).toHaveBeenCalledWith(filesContext, "files.write");
|
|
});
|
|
|
|
it("does not query a file when chat and files contexts disagree", async () => {
|
|
mocks.loadFilesContext.mockResolvedValue({ ...filesContext, userId: "user_other" });
|
|
const response = await loader({
|
|
request: new Request("http://app.localhost/demo/chat/attachments/file_1/status"),
|
|
params: { tenant: "demo", fileId: "file_1" },
|
|
context: {},
|
|
});
|
|
|
|
expect(response.status).toBe(404);
|
|
expect(mocks.getOwnedFileScanStatus).not.toHaveBeenCalled();
|
|
});
|
|
});
|