|
| 1 | +import { |
| 2 | + createChat, |
| 3 | + createDashboardAgentDb, |
| 4 | + listChats, |
| 5 | + softDeleteChat, |
| 6 | + type DashboardAgentDb, |
| 7 | + type DashboardAgentDbClient, |
| 8 | +} from "@internal/dashboard-agent-db"; |
| 9 | +import { postgresTest } from "@internal/testcontainers"; |
| 10 | +import type { PrismaClient } from "@trigger.dev/database"; |
| 11 | +import { readdirSync, readFileSync } from "node:fs"; |
| 12 | +import path from "node:path"; |
| 13 | +import { afterEach, describe, expect } from "vitest"; |
| 14 | + |
| 15 | +/** Replays every migration in order, so a new migration can't leave the suite on a stale schema. */ |
| 16 | +async function applyAgentSchema(prisma: PrismaClient) { |
| 17 | + const folder = path.resolve(__dirname, "../../../internal-packages/dashboard-agent-db/drizzle"); |
| 18 | + const migrations = readdirSync(folder) |
| 19 | + .filter((file) => file.endsWith(".sql")) |
| 20 | + .sort(); |
| 21 | + for (const name of migrations) { |
| 22 | + const sql = readFileSync(path.join(folder, name), "utf8"); |
| 23 | + for (const statement of sql.split("--> statement-breakpoint")) { |
| 24 | + const trimmed = statement.trim(); |
| 25 | + if (trimmed.length > 0) await prisma.$executeRawUnsafe(trimmed); |
| 26 | + } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +let agentDbClient: DashboardAgentDbClient | undefined; |
| 31 | + |
| 32 | +async function boot(prisma: PrismaClient, connectionUri: string): Promise<DashboardAgentDb> { |
| 33 | + await applyAgentSchema(prisma); |
| 34 | + agentDbClient = createDashboardAgentDb(connectionUri, { max: 2 }); |
| 35 | + return agentDbClient.db; |
| 36 | +} |
| 37 | + |
| 38 | +afterEach(async () => { |
| 39 | + await agentDbClient?.close(); |
| 40 | + agentDbClient = undefined; |
| 41 | +}); |
| 42 | + |
| 43 | +const ORG = "org_owner"; |
| 44 | +const OTHER_ORG = "org_other"; |
| 45 | +const USER = "user_owner"; |
| 46 | + |
| 47 | +describe("softDeleteChat tenant isolation", () => { |
| 48 | + postgresTest( |
| 49 | + "a soft-delete scoped to another org leaves the chat intact", |
| 50 | + async ({ prisma, postgresContainer }) => { |
| 51 | + const db = await boot(prisma, postgresContainer.getConnectionUri()); |
| 52 | + |
| 53 | + await createChat(db, { id: "chat_1", organizationId: ORG, userId: USER }); |
| 54 | + |
| 55 | + // Right user, wrong org: must not delete. |
| 56 | + const wrongOrg = await softDeleteChat(db, { |
| 57 | + chatId: "chat_1", |
| 58 | + userId: USER, |
| 59 | + organizationId: OTHER_ORG, |
| 60 | + }); |
| 61 | + expect(wrongOrg.deleted).toBe(false); |
| 62 | + expect(await listChats(db, { organizationId: ORG, userId: USER })).toHaveLength(1); |
| 63 | + |
| 64 | + // Right org and user: deletes. |
| 65 | + const rightOrg = await softDeleteChat(db, { |
| 66 | + chatId: "chat_1", |
| 67 | + userId: USER, |
| 68 | + organizationId: ORG, |
| 69 | + }); |
| 70 | + expect(rightOrg.deleted).toBe(true); |
| 71 | + expect(await listChats(db, { organizationId: ORG, userId: USER })).toHaveLength(0); |
| 72 | + }, |
| 73 | + 30_000 |
| 74 | + ); |
| 75 | +}); |
0 commit comments