123 lines
5.7 KiB
TypeScript
123 lines
5.7 KiB
TypeScript
import { PgDialect } from "drizzle-orm/pg-core";
|
|
import type { SQL } from "drizzle-orm";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const authorizeTicket = vi.fn();
|
|
const selectLimit = vi.fn();
|
|
const selectWhere = vi.fn((condition: SQL) => { void condition; return { limit: selectLimit }; });
|
|
const insertReturning = vi.fn();
|
|
const insertValues = vi.fn(() => ({ returning: insertReturning }));
|
|
const updateWhere = vi.fn();
|
|
const writeObject = vi.fn();
|
|
const deleteObject = vi.fn();
|
|
const inspectImage = vi.fn();
|
|
const tx = { insert: vi.fn(() => ({ values: insertValues })), update: vi.fn(() => ({ set: () => ({ where: updateWhere }) })) };
|
|
const notifyCommission = vi.fn();
|
|
|
|
vi.mock("@/lib/commission/tickets", () => ({ authorizeTicket }));
|
|
vi.mock("@/lib/commission/server", () => ({ notifyCommission }));
|
|
vi.mock("@/lib/commission/push", () => ({ sendCommissionMessagePush: vi.fn() }));
|
|
vi.mock("@/lib/media/storage", () => ({ getMediaStorage: async () => ({ write: writeObject, delete: deleteObject }) }));
|
|
vi.mock("@/lib/media/inspect", () => ({ inspectImage }));
|
|
vi.mock("@/lib/security/rate-limit", () => ({ limitRequest: async () => undefined }));
|
|
vi.mock("next/server", () => ({ after: vi.fn() }));
|
|
vi.mock("@/db", () => ({ getDb: () => ({
|
|
select: () => ({ from: () => ({ where: selectWhere }) }),
|
|
transaction: async (run: (value: typeof tx) => Promise<unknown>) => run(tx),
|
|
}) }));
|
|
|
|
const { POST } = await import("./route");
|
|
const ticketId = "11111111-1111-4111-8111-111111111111";
|
|
const parentId = "22222222-2222-4222-8222-222222222222";
|
|
const messageId = "33333333-3333-4333-8333-333333333333";
|
|
|
|
function request(replyToId: string) {
|
|
const body = new FormData();
|
|
body.set("text", "A reply");
|
|
body.set("replyToId", replyToId);
|
|
return new Request(`https://guide.sudloh.com/api/commission/tickets/${ticketId}/messages`, {
|
|
method: "POST", headers: { Origin: "https://guide.sudloh.com" }, body,
|
|
});
|
|
}
|
|
|
|
function context() {
|
|
return { params: Promise.resolve({ id: ticketId }) } as RouteContext<"/api/commission/tickets/[id]/messages">;
|
|
}
|
|
|
|
describe("commission message replies", () => {
|
|
beforeEach(() => {
|
|
process.env.BETTER_AUTH_URL = "https://guide.sudloh.com";
|
|
vi.clearAllMocks();
|
|
authorizeTicket.mockResolvedValue({ ticket: { status: "open", userId: "user-1", title: "A ticket" },
|
|
user: { id: "user-1", name: "User" } });
|
|
selectLimit.mockResolvedValue([{ id: parentId }]);
|
|
insertReturning.mockResolvedValue([{ id: messageId }]);
|
|
writeObject.mockResolvedValue(undefined);
|
|
deleteObject.mockResolvedValue(undefined);
|
|
inspectImage.mockResolvedValue(undefined);
|
|
});
|
|
|
|
it("accepts a reply only after looking up its parent in the same ticket", async () => {
|
|
const response = await POST(request(parentId), context());
|
|
expect(response.status).toBe(201);
|
|
const condition = new PgDialect().sqlToQuery(selectWhere.mock.calls[0][0]);
|
|
expect(condition.sql).toContain('"ticket_id"');
|
|
expect(condition.params).toContain(ticketId);
|
|
expect(condition.params).toContain(parentId);
|
|
expect(insertValues).toHaveBeenCalledWith(expect.objectContaining({ replyToId: parentId }));
|
|
});
|
|
|
|
it("rejects a parent that is absent from this ticket", async () => {
|
|
selectLimit.mockResolvedValue([]);
|
|
const response = await POST(request(parentId), context());
|
|
expect(response.status).toBe(404);
|
|
expect(insertValues).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("stores seven images in one message", async () => {
|
|
const body = new FormData();
|
|
for (let index = 0; index < 7; index++)
|
|
body.append("image", new File(["image"], `${index}.png`, { type: "image/png" }));
|
|
const response = await POST(new Request(`https://guide.sudloh.com/api/commission/tickets/${ticketId}/messages`, {
|
|
method: "POST", headers: { Origin: "https://guide.sudloh.com" }, body,
|
|
}), context());
|
|
expect(response.status).toBe(201);
|
|
expect(writeObject).toHaveBeenCalledTimes(7);
|
|
expect(insertValues).toHaveBeenCalledWith(expect.objectContaining({
|
|
imageObjectKeys: writeObject.mock.calls.map(([key]) => key),
|
|
}));
|
|
});
|
|
|
|
it("rejects more than ten images before uploading", async () => {
|
|
const body = new FormData();
|
|
for (let index = 0; index < 11; index++)
|
|
body.append("image", new File(["image"], `${index}.png`, { type: "image/png" }));
|
|
const response = await POST(new Request(`https://guide.sudloh.com/api/commission/tickets/${ticketId}/messages`, {
|
|
method: "POST", headers: { Origin: "https://guide.sudloh.com" }, body,
|
|
}), context());
|
|
expect(response.status).toBe(400);
|
|
expect(writeObject).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("cleans up uploaded images when saving the message fails", async () => {
|
|
insertReturning.mockRejectedValueOnce(new Error("database unavailable"));
|
|
const body = new FormData();
|
|
body.append("image", new File(["image"], "first.png", { type: "image/png" }));
|
|
const response = await POST(new Request(`https://guide.sudloh.com/api/commission/tickets/${ticketId}/messages`, {
|
|
method: "POST", headers: { Origin: "https://guide.sudloh.com" }, body,
|
|
}), context());
|
|
expect(response.status).toBe(500);
|
|
expect(deleteObject).toHaveBeenCalledWith(writeObject.mock.calls[0][0]);
|
|
});
|
|
|
|
it("rejects an image larger than ten MiB", async () => {
|
|
const body = new FormData();
|
|
body.append("image", new File([new Uint8Array(10 * 1024 * 1024 + 1)], "large.png", { type: "image/png" }));
|
|
const response = await POST(new Request(`https://guide.sudloh.com/api/commission/tickets/${ticketId}/messages`, {
|
|
method: "POST", headers: { Origin: "https://guide.sudloh.com" }, body,
|
|
}), context());
|
|
expect(response.status).toBe(415);
|
|
expect(writeObject).not.toHaveBeenCalled();
|
|
});
|
|
});
|