|
| 1 | +import { randomUUID } from "node:crypto"; |
| 2 | +import { eq, sql } from "drizzle-orm"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { cleanupResidualUserData } from "../../services/user-erasure"; |
| 5 | +import { db } from "../index"; |
| 6 | +import { adminAuditLog, appLog, user, verification } from "../schema"; |
| 7 | + |
| 8 | +const skipDatabase = process.env.OPENMAPX_RUN_DATABASE_TESTS !== "1"; |
| 9 | + |
| 10 | +describe.skipIf(skipDatabase)("user erasure constraints with PostgreSQL", () => { |
| 11 | + it("keeps every direct user foreign key on an explicit erase-safe action", async () => { |
| 12 | + const result = await db.execute(sql` |
| 13 | + SELECT child.relname AS table_name, |
| 14 | + child_column.attname AS column_name, |
| 15 | + constraint.confdeltype AS delete_action |
| 16 | + FROM pg_constraint constraint |
| 17 | + JOIN pg_class parent ON parent.oid = constraint.confrelid |
| 18 | + JOIN pg_class child ON child.oid = constraint.conrelid |
| 19 | + JOIN pg_attribute child_column |
| 20 | + ON child_column.attrelid = child.oid |
| 21 | + AND child_column.attnum = constraint.conkey[1] |
| 22 | + WHERE constraint.contype = 'f' |
| 23 | + AND parent.relname = 'user' |
| 24 | + ORDER BY child.relname, child_column.attname |
| 25 | + `); |
| 26 | + |
| 27 | + const rows = Array.from(result as Iterable<Record<string, unknown>>).map( |
| 28 | + (row) => `${row.table_name}.${row.column_name}:${row.delete_action}`, |
| 29 | + ); |
| 30 | + expect(rows).toEqual([ |
| 31 | + "account.user_id:c", |
| 32 | + "admin_audit_log.actor_id:n", |
| 33 | + "admin_job.created_by:n", |
| 34 | + "installed_extension.installed_by:n", |
| 35 | + "installed_integration.installed_by:n", |
| 36 | + "integration_secret.updated_by:n", |
| 37 | + "labeled_place.user_id:c", |
| 38 | + "mangrove_keypair.user_id:c", |
| 39 | + "mobile_auth_handoff.user_id:c", |
| 40 | + "oauth_access_token.user_id:c", |
| 41 | + "oauth_client.user_id:c", |
| 42 | + "oauth_consent.user_id:c", |
| 43 | + "oauth_refresh_token.user_id:c", |
| 44 | + "parked_location.user_id:c", |
| 45 | + "passkey.user_id:c", |
| 46 | + "personal_timeline_connection.user_id:c", |
| 47 | + "personal_vehicle.user_id:c", |
| 48 | + "saved_list.user_id:c", |
| 49 | + "service_secret.updated_by:n", |
| 50 | + "session.user_id:c", |
| 51 | + "share_link.user_id:c", |
| 52 | + "two_factor.user_id:c", |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + it("scrubs residual identifiers from verification, audit, and persisted logs", async () => { |
| 57 | + const suffix = randomUUID(); |
| 58 | + const userId = `erasure-user-${suffix}`; |
| 59 | + const email = `erasure-${suffix}@example.test`; |
| 60 | + const auditId = randomUUID(); |
| 61 | + let userInserted = false; |
| 62 | + let auditInserted = false; |
| 63 | + let appLogId: number | undefined; |
| 64 | + try { |
| 65 | + await db.insert(user).values({ id: userId, name: "Erasure test", email }); |
| 66 | + userInserted = true; |
| 67 | + await db.insert(verification).values({ |
| 68 | + id: randomUUID(), |
| 69 | + identifier: `change-email:${userId}:${email}`, |
| 70 | + value: userId, |
| 71 | + expiresAt: new Date(Date.now() + 60_000), |
| 72 | + }); |
| 73 | + await db.insert(adminAuditLog).values({ |
| 74 | + id: auditId, |
| 75 | + actorId: userId, |
| 76 | + targetId: userId, |
| 77 | + targetType: "user", |
| 78 | + action: "test", |
| 79 | + details: { subject: userId, contact: email }, |
| 80 | + ipAddress: "192.0.2.1", |
| 81 | + userAgent: "erasure-test", |
| 82 | + }); |
| 83 | + auditInserted = true; |
| 84 | + const [insertedLog] = await db |
| 85 | + .insert(appLog) |
| 86 | + .values({ |
| 87 | + level: "warn", |
| 88 | + source: "test", |
| 89 | + msg: `failure for ${email}`, |
| 90 | + metadata: { subject: userId }, |
| 91 | + }) |
| 92 | + .returning({ id: appLog.id }); |
| 93 | + appLogId = insertedLog?.id; |
| 94 | + |
| 95 | + await cleanupResidualUserData({ id: userId, email }); |
| 96 | + await db.delete(user).where(eq(user.id, userId)); |
| 97 | + |
| 98 | + expect(await db.select().from(verification).where(eq(verification.value, userId))).toEqual( |
| 99 | + [], |
| 100 | + ); |
| 101 | + expect( |
| 102 | + await db |
| 103 | + .select() |
| 104 | + .from(appLog) |
| 105 | + .where(eq(appLog.id, appLogId as number)), |
| 106 | + ).toEqual([]); |
| 107 | + const [audit] = await db.select().from(adminAuditLog).where(eq(adminAuditLog.id, auditId)); |
| 108 | + expect(audit).toMatchObject({ |
| 109 | + actorId: null, |
| 110 | + targetId: null, |
| 111 | + details: null, |
| 112 | + ipAddress: null, |
| 113 | + userAgent: null, |
| 114 | + }); |
| 115 | + } finally { |
| 116 | + if (userInserted) await db.delete(user).where(eq(user.id, userId)); |
| 117 | + if (auditInserted) await db.delete(adminAuditLog).where(eq(adminAuditLog.id, auditId)); |
| 118 | + if (appLogId !== undefined) await db.delete(appLog).where(eq(appLog.id, appLogId)); |
| 119 | + } |
| 120 | + }); |
| 121 | +}); |
0 commit comments