Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(control-api): r10-l1 preserve password work fixture cleanup
Josué369ae2de7329
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 7bd4d8ca0..c1b7b2031 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.763",+ "version": "0.1.764","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.763",+ "version": "0.1.764","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/grok-provider-attempt-contract": "file:../packages/grok-provider-attempt-contract",diff --git a/control-api/package.json b/control-api/package.jsonindex 2f7c6ca06..0ce474a7d 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.763",+ "version": "0.1.764","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.ts b/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.tsnew file mode 100644index 000000000..0bdf4a15d--- /dev/null+++ b/control-api/test/passwordWorkOwnership.operation.realPostgres.integration.test.ts@@ -0,0 +1,181 @@+import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'+import bcrypt from 'bcryptjs'+import { randomBytes } from 'node:crypto'+import { Pool } from 'pg'+import { initDb } from '../src/db.js'+import { acquirePasswordWork } from '../src/services/auth/passwordWorkOwnership.js'+import {+ acceptInvitationForEmail,+ createSilentInvitationForTeams,+ setInvitationPasswordForEmail,+ setInvitationPasswordForUser,+ withMemberPasswordWorkLease,+} from '../src/services/directory/membership.js'+import { createPostgresCommitReplyBlackhole } from './helpers/realPostgresCancellation.js'+import { endPoolAndWaitForClients } from './helpers/realPostgresTeardown.js'++const holder = vi.hoisted(() => ({ pool: null as unknown as Pool }))+vi.mock('../src/db.js', async importOriginal => {+ const real = await importOriginal<typeof import('../src/db.js')>()+ return {+ ...real,+ pool: { query: (...args: unknown[]) => (holder.pool.query as Function)(...args) },+ withTransaction: (+ work: Parameters<typeof real.withTransaction>[0],+ _pool?: Parameters<typeof real.withTransaction>[1],+ options?: Parameters<typeof real.withTransaction>[2]+ ) => real.withTransaction(work, holder.pool, options),+ }+})++const adminUrl = process.env.CONTROL_API_REAL_PG_ADMIN_URL+const realPg = adminUrl ? describe : describe.skip+realPg('password-work operation preservation on PostgreSQL 16', () => {+ const database = `password_operation_${randomBytes(6).toString('hex')}`+ let admin: Pool+ let local: Pool+ let connectionString: string+ beforeAll(async () => {+ admin = new Pool({ connectionString: adminUrl })+ await admin.query(`CREATE DATABASE "${database}"`)+ const url = new URL(adminUrl!)+ url.pathname = `/${database}`+ connectionString = url.toString()+ local = new Pool({ connectionString: url.toString(), max: 1 })+ holder.pool = local+ await initDb(local)+ }, 60_000)+ beforeEach(async () => {+ vi.restoreAllMocks()+ await local.query('DELETE FROM password_verification_work')+ })+ afterAll(async () => {+ try {+ vi.restoreAllMocks()+ await endPoolAndWaitForClients(local)+ if (admin) await admin.query(`DROP DATABASE IF EXISTS "${database}"`)+ } finally {+ await admin?.end()+ }+ })++ async function ownerCount() {+ return (await local.query('SELECT COUNT(*)::int AS count FROM password_verification_work'))+ .rows[0].count+ }+ async function resetFixture() {+ const email = `${randomBytes(8).toString('hex')}@example.invalid`+ const user = (await local.query('INSERT INTO users(email) VALUES ($1) RETURNING id', [email]))+ .rows[0]+ const invitation = await createSilentInvitationForTeams({+ inviteeName: 'Synthetic preservation member',+ email,+ purpose: 'password_reset',+ })+ return { email, userId: user.id as string, invitation }+ }++ it('releases an unused reservation and retains an original outer work failure', async () => {+ const failure = new Error('preservation work failure')+ await expect(+ withMemberPasswordWorkLease(async () => {+ throw failure+ })+ ).rejects.toBe(failure)+ expect(await ownerCount()).toBe(0)+ })++ it('keeps storage release failure above the outer work failure', async () => {+ try {+ await expect(+ withMemberPasswordWorkLease(async () => {+ await local.query('ALTER TABLE password_verification_work RENAME TO preservation_work')+ throw new Error('private work failure')+ })+ ).rejects.toMatchObject({ status: 503, message: 'authority_unavailable' })+ } finally {+ await local.query('ALTER TABLE preservation_work RENAME TO password_verification_work')+ }+ // Unconfirmed release remains occupied; only this disposable fixture is reset by beforeEach.+ expect(await ownerCount()).toBe(1)+ })++ it('composes an accepted admin/member operation with its existing reservation and one pool', async () => {+ const email = `${randomBytes(8).toString('hex')}@example.invalid`+ const invitation = await createSilentInvitationForTeams({+ email,+ inviteeName: 'Synthetic admin member',+ purpose: 'admin_desktop_access',+ })+ const result = await withMemberPasswordWorkLease(async lease => {+ const accepted = await acceptInvitationForEmail(email, invitation.token, invitation.id)+ if (!('data' in accepted)) throw new Error('fixture acceptance failed')+ expect(await ownerCount()).toBe(1)+ return setInvitationPasswordForUser(+ accepted.data.userId!,+ email,+ invitation.id,+ 'Synthetic-preservation-password',+ lease+ )+ })+ expect(result).toMatchObject({+ data: { passwordUpdated: true, purpose: 'admin_desktop_access' },+ })+ expect(await ownerCount()).toBe(0)+ const row = (await local.query('SELECT password_hash FROM users WHERE email = $1', [email]))+ .rows[0]+ expect(await bcrypt.compare('Synthetic-preservation-password', row.password_hash)).toBe(true)+ })++ it('retains a committed raw supplied lease and does not release a successor owner', async () => {+ const fixture = await resetFixture()+ const lease = await acquirePasswordWork()+ const result = await setInvitationPasswordForEmail(+ fixture.email,+ fixture.invitation.id,+ 'Synthetic-preservation-password',+ fixture.userId,+ lease!+ )+ expect(result).toMatchObject({ data: { passwordUpdated: true, status: 'accepted' } })+ const successor = await acquirePasswordWork()+ expect(successor).not.toBeNull()+ try {+ await lease!.release()+ expect(await ownerCount()).toBe(1)+ } finally {+ await successor?.release()+ }+ })++ it('rolls back a credential mutation when its transactional owner DELETE fails', async () => {+ const fixture = await resetFixture()+ await local.query(`CREATE FUNCTION preservation_delete_failure() RETURNS trigger LANGUAGE plpgsql+ AS $$ BEGIN RAISE EXCEPTION 'private fixture release detail'; END $$`)+ await local.query(`CREATE TRIGGER preservation_delete_failure BEFORE DELETE+ ON password_verification_work FOR EACH ROW EXECUTE FUNCTION preservation_delete_failure()`)+ try {+ await expect(+ setInvitationPasswordForEmail(+ fixture.email,+ fixture.invitation.id,+ 'Synthetic-preservation-password',+ fixture.userId+ )+ ).rejects.toMatchObject({ status: 503, message: 'authority_unavailable' })+ const row = (+ await local.query(+ `SELECT i.status, u.password_hash FROM invitations i+ JOIN users u ON u.id = $2 WHERE i.id = $1`,+ [fixture.invitation.id, fixture.userId]+ )+ ).rows[0]+ expect(row).toMatchObject({ status: 'pending', password_hash: null })+ expect(await ownerCount()).toBe(1)+ } finally {+ await local.query('DROP TRIGGER preservation_delete_failure ON password_verification_work')+ await local.query('DROP FUNCTION preservation_delete_failure()')+ }+ })+})diff --git a/control-api/test/passwordWorkOwnership.operation.test.ts b/control-api/test/passwordWorkOwnership.operation.test.tsnew file mode 100644index 000000000..6e75bd60e--- /dev/null+++ b/control-api/test/passwordWorkOwnership.operation.test.ts@@ -0,0 +1,113 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import fc from 'fast-check'+import { PasswordAdmissionError } from '../src/services/auth/passwordCredentialVerification.js'+import { acquirePasswordWork } from '../src/services/auth/passwordWorkOwnership.js'+import { withMemberPasswordWorkLease } from '../src/services/directory/membership.js'++// Own-service lifecycle seam. Real PostgreSQL and wire faults are verified separately.+const state = vi.hoisted(() => ({+ busy: false,+ failAcquire: false,+ failRelease: false,+ commit: 'ok' as 'ok' | 'ack_cleanup' | 'unknown' | 'reject',+}))+const query = vi.hoisted(() => vi.fn())+vi.mock('../src/db.js', async importOriginal => {+ const real = await importOriginal<typeof import('../src/db.js')>()+ return {+ ...real,+ withTransaction: async (+ work: (db: unknown) => Promise<unknown>,+ _pool: unknown,+ options?: { onCommitAcknowledged?: () => void; onCommitOutcomeUnknown?: () => void }+ ) => {+ const value = await work({ query })+ if (options && state.commit === 'unknown') {+ options.onCommitOutcomeUnknown?.()+ state.failRelease = true+ throw new Error('private unknown commit detail')+ }+ if (options && state.commit === 'reject') throw new Error('private definite rejection')+ options?.onCommitAcknowledged?.()+ if (options && state.commit === 'ack_cleanup') throw new Error('private cleanup detail')+ return value+ },+ }+})++describe('password-work reservation preservation', () => {+ beforeEach(() => {+ state.busy = state.failAcquire = state.failRelease = false+ state.commit = 'ok'+ query.mockReset().mockImplementation(async (sql: string) => {+ if (sql.includes('INSERT INTO password_verification_work')) {+ if (state.failAcquire) throw new Error('private acquisition failure')+ return { rows: state.busy ? [] : [{ singleton: true }] }+ }+ if (sql.includes('DELETE FROM password_verification_work')) {+ if (state.failRelease) throw new Error('private release failure')+ return { rows: [{ singleton: true }] }+ }+ return { rows: [] }+ })+ })++ it('preserves the original outer result and releases the unused reservation once', async () => {+ const value = { retained: true }+ expect(await withMemberPasswordWorkLease(async () => value)).toBe(value)+ expect(query.mock.calls.filter(([sql]) => sql.includes('DELETE FROM'))).toHaveLength(1)+ })++ it('preserves the original work exception after successful release', async () => {+ const failure = new Error('original work')+ await expect(+ withMemberPasswordWorkLease(async () => {+ throw failure+ })+ ).rejects.toBe(failure)+ })++ it.each([false, true])('release failure wins over outer work failure=%s safely', async fails => {+ state.failRelease = true+ const outcome = withMemberPasswordWorkLease(async () => {+ if (fails) throw new Error('private work failure')+ return 'result'+ })+ await expect(outcome).rejects.toMatchObject({+ status: 503,+ publicError: 'authority_unavailable',+ })+ await expect(outcome).rejects.toBeInstanceOf(PasswordAdmissionError)+ })++ it('denies a busy owner without executing the caller', async () => {+ state.busy = true+ const work = vi.fn()+ await expect(withMemberPasswordWorkLease(work)).rejects.toMatchObject({+ status: 429,+ retryAfterSeconds: 8,+ reason: 'verification_busy',+ })+ expect(work).not.toHaveBeenCalled()+ })++ it('sanitizes acquisition failure before executing the caller', async () => {+ state.failAcquire = true+ const work = vi.fn()+ await expect(withMemberPasswordWorkLease(work)).rejects.toMatchObject({+ message: 'authority_unavailable',+ status: 503,+ retryAfterSeconds: 2,+ })+ expect(work).not.toHaveBeenCalled()+ })++ it('caches raw verifier release including an unconfirmed failure', async () => {+ const lease = await acquirePasswordWork()+ state.failRelease = true+ const first = lease!.release()+ expect(lease!.release()).toBe(first)+ await expect(first).rejects.toThrow('private release failure')+ expect(query.mock.calls.filter(([sql]) => sql.includes('DELETE FROM'))).toHaveLength(1)+ })+})