From 82cb81d9b8baf703c46910d8ad7058a818b27248 Mon Sep 17 00:00:00 2001 From: karansaini47 Date: Sat, 16 May 2026 02:52:13 +0530 Subject: [PATCH] guard destructive tests from app database --- .env.example | 1 + .github/workflows/ci.yml | 2 ++ README.md | 1 + server/test/setup.ts | 31 +++++++++++++++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/.env.example b/.env.example index 93685fc..32b703b 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,5 @@ DATABASE_URL=postgresql://USER:PASSWORD@HOST:5432/DATABASE?schema=public +TEST_DATABASE_URL=postgresql://USER:PASSWORD@HOST:5432/TEST_DATABASE?schema=public JWT_SECRET=replace-with-a-secure-secret JWT_REFRESH_SECRET=replace-with-a-secure-refresh-secret PORT=3000 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 66c290b..373c106 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,6 +26,7 @@ jobs: --health-retries 5 env: DATABASE_URL: postgresql://postgres:postgres@localhost:5432/invoiceflow_test?schema=public + TEST_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/invoiceflow_test?schema=public JWT_SECRET: test-jwt-secret-for-ci JWT_REFRESH_SECRET: test-refresh-secret-for-ci @@ -50,6 +51,7 @@ jobs: - name: Setup test environment variables run: | echo "DATABASE_URL=postgresql://postgres:postgres@localhost:5432/invoiceflow_test?schema=public" >> $GITHUB_ENV + echo "TEST_DATABASE_URL=postgresql://postgres:postgres@localhost:5432/invoiceflow_test?schema=public" >> $GITHUB_ENV echo "JWT_SECRET=test-jwt-secret-for-ci" >> $GITHUB_ENV echo "JWT_REFRESH_SECRET=test-refresh-secret-for-ci" >> $GITHUB_ENV diff --git a/README.md b/README.md index b5de3f0..02dc79f 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ The client runs on `http://localhost:5173` and the server runs on `http://localh - Server environment variables: `server/.env` - Client environment variables: `client/.env` (if needed) - The server expects a PostgreSQL `DATABASE_URL`, not a SQLite file path. +- Use a separate PostgreSQL database for `TEST_DATABASE_URL`; the test suite is destructive and refuses to run unless it is explicitly pointed at an isolated test database. ### Production Deployment - **Backend (Render)**: Set the service root to `server`, then use `npm install && npm run deploy:build` for the build command and `npm start` for the start command. diff --git a/server/test/setup.ts b/server/test/setup.ts index e52dbbf..b209c2b 100644 --- a/server/test/setup.ts +++ b/server/test/setup.ts @@ -55,7 +55,38 @@ export const testUser = { let testUserIdInternal: string; let testAccessTokenInternal: string; +const assertIsolatedTestDatabase = () => { + const databaseUrl = process.env.DATABASE_URL; + const testDatabaseUrl = process.env.TEST_DATABASE_URL; + + if (process.env.NODE_ENV !== "test" || !databaseUrl || !testDatabaseUrl) { + throw new Error( + "Refusing to run destructive tests without NODE_ENV=test, DATABASE_URL, and TEST_DATABASE_URL set.", + ); + } + + if (databaseUrl !== testDatabaseUrl) { + throw new Error( + "Refusing to run destructive tests unless DATABASE_URL matches TEST_DATABASE_URL.", + ); + } + + const parsedUrl = new URL(testDatabaseUrl); + const databaseName = decodeURIComponent(parsedUrl.pathname.replace(/^\//, "")); + const schemaName = parsedUrl.searchParams.get("schema") ?? ""; + const isClearlyTestTarget = + databaseName.toLowerCase().includes("test") || schemaName.toLowerCase().includes("test"); + + if (!isClearlyTestTarget) { + throw new Error( + "Refusing to run destructive tests unless TEST_DATABASE_URL points to a database or schema containing 'test'.", + ); + } +}; + beforeAll(async () => { + assertIsolatedTestDatabase(); + await prisma.aiUsageLog.deleteMany(); await prisma.lineItem.deleteMany(); await prisma.invoice.deleteMany();