phase6-roles-proof.ts57 lines · main
| 1 | /** |
| 2 | * Local Phase 6 proof: roles create / list / assign on Doltgres. |
| 3 | * Usage: |
| 4 | * BRIVEN_ENGINE_DATABASE_URL=postgres://... bun apps/api/scripts/phase6-roles-proof.ts |
| 5 | */ |
| 6 | import { initAuthCoreSdk, isAuthCoreInitialized } from '../src/services/auth-core/engine.js'; |
| 7 | import { |
| 8 | assignBrivenEngineRole, |
| 9 | createBrivenEngineRole, |
| 10 | getBrivenEngineUserRoles, |
| 11 | listBrivenEngineRoles, |
| 12 | userHasPermission, |
| 13 | } from '../src/services/auth-core/roles.js'; |
| 14 | |
| 15 | const roleName = `phase6_proof_${Date.now().toString(36)}`; |
| 16 | const fakeUserId = `beu_proof_${Date.now().toString(36)}`; |
| 17 | |
| 18 | async function main() { |
| 19 | await initAuthCoreSdk(); |
| 20 | if (!isAuthCoreInitialized()) { |
| 21 | console.error('FAIL: engine not initialized'); |
| 22 | process.exit(1); |
| 23 | } |
| 24 | |
| 25 | const created = await createBrivenEngineRole(roleName, ['read', 'write'], { |
| 26 | tenantId: 'public', |
| 27 | }); |
| 28 | console.log('create', created); |
| 29 | if (!created.ok) process.exit(1); |
| 30 | |
| 31 | const listed = await listBrivenEngineRoles({ tenantId: 'public' }); |
| 32 | const found = listed.roles.some((r) => r.name === roleName); |
| 33 | console.log('list has role', found, 'count', listed.roles.length); |
| 34 | if (!found) process.exit(1); |
| 35 | |
| 36 | const assigned = await assignBrivenEngineRole(fakeUserId, roleName, { |
| 37 | tenantId: 'public', |
| 38 | }); |
| 39 | console.log('assign', assigned); |
| 40 | if (!assigned.ok) process.exit(1); |
| 41 | |
| 42 | const ur = await getBrivenEngineUserRoles(fakeUserId, { tenantId: 'public' }); |
| 43 | console.log('user roles', ur); |
| 44 | if (!ur.roles.includes(roleName)) process.exit(1); |
| 45 | if (!ur.permissions.includes('read')) process.exit(1); |
| 46 | |
| 47 | const ok = await userHasPermission(fakeUserId, 'write', { tenantId: 'public' }); |
| 48 | console.log('has write', ok); |
| 49 | if (!ok) process.exit(1); |
| 50 | |
| 51 | console.log('PHASE6_ROLES_PROOF_OK', { roleName, fakeUserId }); |
| 52 | } |
| 53 | |
| 54 | main().catch((e) => { |
| 55 | console.error(e); |
| 56 | process.exit(1); |
| 57 | }); |