Files
adilallo 94cee1dc5b Stop a saved Rule from taking uploads it does not own.
A named path no longer moves another Rules file. Duplicate and Make a copy write their own copies of files they keep.
2026-11-24 18:52:33 -07:00

252 lines
6.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const findManyMock = vi.fn();
const updateManyMock = vi.fn();
const browserHoldsUploadGrantMock = vi.fn();
vi.mock("../../lib/server/db", () => ({
prisma: {
upload: {
findMany: (...args: unknown[]) => findManyMock(...args),
updateMany: (...args: unknown[]) => updateManyMock(...args),
},
},
}));
vi.mock("../../lib/server/uploads/uploadGrantCookie", () => ({
browserHoldsUploadGrant: (...args: unknown[]) =>
browserHoldsUploadGrantMock(...args),
}));
import {
linkUploadsToDraft,
linkUploadsToRule,
} from "../../lib/server/uploads/linkUploads";
const OWNED = "550e8400-e29b-41d4-a716-446655440000";
const FOREIGN = "550e8400-e29b-41d4-a716-446655440001";
const UNOWNED = "550e8400-e29b-41d4-a716-446655440002";
beforeEach(() => {
findManyMock.mockReset();
updateManyMock.mockReset();
browserHoldsUploadGrantMock.mockReset();
updateManyMock.mockResolvedValue({ count: 0 });
browserHoldsUploadGrantMock.mockResolvedValue(false);
});
describe("linkUploadsToRule", () => {
it("does not query when the document names no uploads", async () => {
await linkUploadsToRule({
ruleId: "rule-1",
document: { text: "none" },
userId: "user-1",
});
expect(findManyMock).not.toHaveBeenCalled();
expect(updateManyMock).not.toHaveBeenCalled();
});
it("does not take a file already linked to another Rule", async () => {
findManyMock.mockResolvedValueOnce([
{
id: OWNED,
userId: "user-1",
ruleId: "rule-other",
draftId: null,
},
]);
await linkUploadsToRule({
ruleId: "rule-1",
document: { logo: `/api/uploads/${OWNED}` },
userId: "user-1",
});
expect(updateManyMock).not.toHaveBeenCalled();
});
it("does not take another account's unlinked file", async () => {
findManyMock.mockResolvedValueOnce([
{
id: FOREIGN,
userId: "user-2",
ruleId: null,
draftId: null,
},
]);
await linkUploadsToRule({
ruleId: "rule-1",
document: { logo: `/api/uploads/${FOREIGN}` },
userId: "user-1",
});
expect(updateManyMock).not.toHaveBeenCalled();
});
it("links this account's unlinked file and skips a foreign one named beside it", async () => {
findManyMock.mockResolvedValueOnce([
{
id: OWNED,
userId: "user-1",
ruleId: null,
draftId: "draft-1",
},
{
id: FOREIGN,
userId: "user-2",
ruleId: "rule-other",
draftId: null,
},
]);
await linkUploadsToRule({
ruleId: "rule-1",
document: {
logo: `/api/uploads/${OWNED}`,
other: `/api/uploads/${FOREIGN}`,
},
userId: "user-1",
});
expect(updateManyMock).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({
id: { in: [OWNED] },
}),
data: expect.objectContaining({
ruleId: "rule-1",
draftId: null,
userId: "user-1",
}),
}),
);
});
it("links an unowned file when this browser holds the grant", async () => {
findManyMock.mockResolvedValueOnce([
{
id: UNOWNED,
userId: null,
ruleId: null,
draftId: null,
},
]);
browserHoldsUploadGrantMock.mockResolvedValueOnce(true);
await linkUploadsToRule({
ruleId: "rule-1",
document: { logo: `/api/uploads/${UNOWNED}` },
userId: "user-1",
});
expect(browserHoldsUploadGrantMock).toHaveBeenCalledWith(UNOWNED);
expect(updateManyMock).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({
id: { in: [UNOWNED] },
}),
}),
);
});
it("does not take an unowned file without a grant", async () => {
findManyMock.mockResolvedValueOnce([
{
id: UNOWNED,
userId: null,
ruleId: null,
draftId: null,
},
]);
await linkUploadsToRule({
ruleId: "rule-1",
document: { logo: `/api/uploads/${UNOWNED}` },
userId: "user-1",
});
expect(updateManyMock).not.toHaveBeenCalled();
});
it("may restamp files already on this Rule", async () => {
findManyMock.mockResolvedValueOnce([
{
id: OWNED,
userId: null,
ruleId: "rule-1",
draftId: null,
},
]);
await linkUploadsToRule({
ruleId: "rule-1",
document: { logo: `/api/uploads/${OWNED}` },
userId: "user-1",
});
expect(updateManyMock).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({
id: { in: [OWNED] },
}),
data: expect.objectContaining({
ruleId: "rule-1",
userId: "user-1",
}),
}),
);
});
});
describe("linkUploadsToDraft", () => {
it("does not take a file already on a Rule", async () => {
findManyMock.mockResolvedValueOnce([
{
id: OWNED,
userId: "user-1",
ruleId: "rule-1",
draftId: null,
},
]);
await linkUploadsToDraft({
draftId: "draft-1",
payload: { logo: `/api/uploads/${OWNED}` },
userId: "user-1",
});
expect(updateManyMock).not.toHaveBeenCalled();
});
it("does not take a file linked to another draft", async () => {
findManyMock.mockResolvedValueOnce([
{
id: FOREIGN,
userId: "user-2",
ruleId: null,
draftId: "draft-other",
},
]);
await linkUploadsToDraft({
draftId: "draft-1",
payload: { logo: `/api/uploads/${FOREIGN}` },
userId: "user-1",
});
expect(updateManyMock).not.toHaveBeenCalled();
});
it("links this account's unlinked file", async () => {
findManyMock.mockResolvedValueOnce([
{
id: OWNED,
userId: "user-1",
ruleId: null,
draftId: null,
},
]);
await linkUploadsToDraft({
draftId: "draft-1",
payload: { logo: `/api/uploads/${OWNED}` },
userId: "user-1",
});
expect(updateManyMock).toHaveBeenCalledWith(
expect.objectContaining({
where: expect.objectContaining({
id: { in: [OWNED] },
}),
data: {
draftId: "draft-1",
userId: "user-1",
},
}),
);
});
});