Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Merge main into the audit fix, keeping both closure tests
Claude61cb74f78023
diff --cc src/lib/account-closure.dbtest.tsindex 419146e,1c51b80..2dad812--- a/src/lib/account-closure.dbtest.ts+++ b/src/lib/account-closure.dbtest.ts@@@ -7,17 -4,23 +4,24 @@@ import { after, before, test } from "noimport type pg from "pg";- import { requestAccountClosure } from "./account.ts";+ import {+ AccountMutationError,+ LAST_ADMIN_CLOSURE_MESSAGE,+ accountClosureBlock,+ requestAccountClosure,+ } from "./account.ts";import { openTestDatabase } from "./db/test-database.ts";+import { listSystemAuditEvents } from "./services/system-audit.ts";- // Asking for an account to be closed, against a real Postgres: the request- // goes in the install's audit trail with where it came from. Run with- // `npm run test:db`.+ // Closing an account from Settings is refused for the last administrator,+ // even when two administrators close at once. Run with `npm run test:db`.++ const env = { BETTER_AUTH_SECRET: "a".repeat(40), EMAIL_DRIVER: "capture" };+ const confirmation = { confirmation: "close my account" };let database: pg.Pool;- let outboxDir: string;const createdUsers: string[] = [];+ let administratorsBefore: string[] = [];before(async () => {database = await openTestDatabase();@@@ -39,35 -56,120 +57,145 @@@ async function createUser(role: "adminawait database.query(`insert into public."user" (id, name, email, role, "emailVerified")- values ($1, 'Closing Person', $2, 'user', true)+ values ($1, $2, $3, $4, true)`,- [userId, `${userId}@example.test`],+ [id, `Person ${id.slice(0, 4)}`, `${id}@example.test`, role],+ );++ return { id };+ }++ async function resetRoles() {+ await database.query(+ `delete from account_closure_requests where user_id = any($1)`,+ [createdUsers],);+ await database.query(+ `update public."user" set role = 'user' where id = any($1)`,+ [createdUsers],+ );+ }- await requestAccountClosure(- userId,- { confirmation: "close my account" },- {- client: { ipAddress: "192.0.2.80", userAgent: "closure dbtest" },- database,- env: {- EMAIL_CAPTURE_DIR: outboxDir,- EMAIL_DRIVER: "capture",- SUPPORT_EMAIL: "[email protected]",- },- },+ async function isQueued(userId: string) {+ const result = await database.query(+ `select 1 from account_closure_requests where user_id = $1`,+ [userId],);+ return result.rowCount === 1;+ }++ function refusedWith(message: string) {+ return (error: unknown) =>+ error instanceof AccountMutationError &&+ error.statusCode === 409 &&+ error.message === message;+ }++ test("the last administrator cannot close their account", async () => {+ await resetRoles();++ const admin = await createUser();++ assert.equal(+ await accountClosureBlock(admin, database, env),+ LAST_ADMIN_CLOSURE_MESSAGE,+ );+ await assert.rejects(+ requestAccountClosure(admin.id, confirmation, { database, env }),+ refusedWith(LAST_ADMIN_CLOSURE_MESSAGE),+ );+ assert.equal(await isQueued(admin.id), false);+ });++ test("an administrator can close once another one stays", async () => {+ await resetRoles();++ const admin = await createUser();+ const other = await createUser();++ assert.equal(await accountClosureBlock(admin, database, env), null);++ const result = await requestAccountClosure(admin.id, confirmation, {+ database,+ env,+ });++ assert.equal(result.alreadyQueued, false);+ assert.equal(await isQueued(admin.id), true);++ // The one left is now the last.+ await assert.rejects(+ requestAccountClosure(other.id, confirmation, { database, env }),+ refusedWith(LAST_ADMIN_CLOSURE_MESSAGE),+ );+ });++ test("someone who is not an administrator can always close", async () => {+ await resetRoles();++ const person = await createUser("user");++ assert.equal(await accountClosureBlock(person, database, env), null);+ });++ test("the hosted service lets anyone close their account", async () => {+ await resetRoles();++ const admin = await createUser();++ assert.equal(+ await accountClosureBlock(admin, database, { ...env, HOSTED_MODE: "1" }),+ null,+ );+ });++ test("two administrators closing at once cannot both go", async () => {+ await resetRoles();++ const first = await createUser();+ const second = await createUser();+ const results = await Promise.allSettled([+ requestAccountClosure(first.id, confirmation, { database, env }),+ requestAccountClosure(second.id, confirmation, { database, env }),+ ]);+ const refused = results.filter((result) => result.status === "rejected");++ assert.equal(refused.length, 1);+ assert.ok(+ refused.every(+ (result) =>+ result.status === "rejected" &&+ refusedWith(LAST_ADMIN_CLOSURE_MESSAGE)(result.reason),+ ),+ );+ assert.equal(+ Number(await isQueued(first.id)) + Number(await isQueued(second.id)),+ 1,+ );+ });++++test("a closure request records where it was made from", async () => {++ await resetRoles();++++ const person = await createUser("user");++++ await requestAccountClosure(person.id, confirmation, {++ client: { ipAddress: "192.0.2.80", userAgent: "closure dbtest" },++ database,++ env,++ });+++ const recorded = (+ await listSystemAuditEvents({ limit: 1000 }, database)+ ).filter(+ (event) =>+ event.event === "account_closure_requested" &&- event.targetUserId === userId,++ event.targetUserId === person.id,+ );++ assert.equal(recorded.length, 1);- assert.equal(recorded[0]?.actorUserId, userId);++ assert.equal(recorded[0]?.actorUserId, person.id);+ assert.equal(recorded[0]?.ipAddress, "192.0.2.80");+ assert.equal(recorded[0]?.userAgent, "closure dbtest");+});