Files
outline/server/commands/attachmentCreator.test.ts
Tom MoorandClaude Opus 5 6f8072eb68 fix: Avoid duplicate key errors when re-creating attachments by id (#13317)
Import tasks pass a known id to attachmentCreator so a retry can resume,
relying on a caught UniqueConstraintError to skip attachments that already
landed. The file was uploaded to storage before the insert though, so every
skipped attachment was re-uploaded in full and then orphaned, as the storage
key was derived from a fresh uuid rather than the supplied id.

Return the existing attachment before touching storage when the id is already
in use, and derive the key from the supplied id so a collision overwrites the
same object instead of leaking a new one. The lookup is scoped to the team, so
an id belonging elsewhere still falls through to the insert and collides there
as before.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-05 21:08:24 -04:00

98 lines
2.9 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { afterEach, describe, expect, it, vi } from "vitest";
import { Attachment } from "@server/models";
import FileStorage from "@server/storage/files";
import { AttachmentPreset } from "@shared/types";
import { createContext } from "@server/context";
import { buildTeam, buildUser } from "@server/test/factories";
import attachmentCreator from "./attachmentCreator";
describe("attachmentCreator", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("returns the existing attachment without re-uploading when the id was already used", async () => {
const user = await buildUser();
const id = randomUUID();
const store = vi.spyOn(FileStorage, "store").mockResolvedValue(undefined);
const first = await attachmentCreator({
id,
name: "image.png",
type: "image/png",
buffer: Buffer.from("one"),
preset: AttachmentPreset.DocumentAttachment,
user,
ctx: createContext({ user }),
});
const second = await attachmentCreator({
id,
name: "image.png",
type: "image/png",
buffer: Buffer.from("one"),
preset: AttachmentPreset.DocumentAttachment,
user,
ctx: createContext({ user }),
});
expect(first?.id).toBe(id);
expect(second?.id).toBe(id);
expect(second?.key).toBe(first?.key);
expect(store).toHaveBeenCalledTimes(1);
expect(await Attachment.count({ where: { id } })).toBe(1);
});
it("derives the storage key from the supplied id so a retry reuses it", async () => {
const user = await buildUser();
const id = randomUUID();
vi.spyOn(FileStorage, "store").mockResolvedValue(undefined);
const attachment = await attachmentCreator({
id,
name: "image.png",
type: "image/png",
buffer: Buffer.from("one"),
preset: AttachmentPreset.DocumentAttachment,
user,
ctx: createContext({ user }),
});
expect(attachment?.key).toContain(id);
});
it("does not resolve an id belonging to another team", async () => {
const otherTeam = await buildTeam();
const otherUser = await buildUser({ teamId: otherTeam.id });
const user = await buildUser();
const id = randomUUID();
vi.spyOn(FileStorage, "store").mockResolvedValue(undefined);
await attachmentCreator({
id,
name: "secret.png",
type: "image/png",
buffer: Buffer.from("one"),
preset: AttachmentPreset.DocumentAttachment,
user: otherUser,
ctx: createContext({ user: otherUser }),
});
await expect(
attachmentCreator({
id,
name: "image.png",
type: "image/png",
buffer: Buffer.from("two"),
preset: AttachmentPreset.DocumentAttachment,
user,
ctx: createContext({ user }),
})
).rejects.toThrow();
const attachment = await Attachment.findByPk(id);
expect(attachment?.teamId).toBe(otherTeam.id);
});
});