|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A new organization's own seed resolves its founder (#34038). |
| 5 | + * |
| 6 | + * On a walled boot, better-auth's `POST /auth/organization/create` writes the |
| 7 | + * `sys_organization` row and THEN the founder's `sys_member` row, as two |
| 8 | + * separate adapter writes (better-auth's `createOrganization`: `createOrganization`, |
| 9 | + * then `createMember`, then `afterCreateOrganization`). This package's per-org |
| 10 | + * seed replay is the organization insert's middleware. When it ran on that |
| 11 | + * insert, the new organization had no member at all, and an organization-scoped |
| 12 | + * seed load resolves a person through membership in the load organization |
| 13 | + * (`seed-org-scoped-references.integration.test.ts` in `@objectstack/runtime` |
| 14 | + * pins that rule). So every person reference the app's seed declared landed |
| 15 | + * NULL with "not a member of organization ORG", the founder's included. |
| 16 | + * |
| 17 | + * ## What is real |
| 18 | + * |
| 19 | + * `bootStack` (`@objectstack/verify`): an `ObjectKernel` over in-process |
| 20 | + * SQLite, the real `AuthPlugin` (better-auth organizations), `SecurityPlugin`, |
| 21 | + * the real `AppPlugin` seed replayer, and THIS package's `OrganizationsPlugin`, |
| 22 | + * under `OS_TENANCY_POSTURE=isolated`. The organization is created through the |
| 23 | + * product's own door by a signed-up user. Nothing is stubbed. Stored rows are |
| 24 | + * read back through the engine as SYSTEM. |
| 25 | + */ |
| 26 | + |
| 27 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 28 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 29 | +import { OrganizationsPlugin } from './organizations-plugin.js'; |
| 30 | + |
| 31 | +const WALLED_BOOT_ENV = [ |
| 32 | + ['OS_TENANCY_POSTURE', 'isolated'], |
| 33 | + // A walled deployment must declare what a new user joins, or this package's |
| 34 | + // own boot gate refuses it. |
| 35 | + ['OS_AUTH_MEMBERSHIP_POLICY', 'invite-only'], |
| 36 | +] as const; |
| 37 | + |
| 38 | +const FOUNDER_EMAIL = 'seed-founder@verify.test'; |
| 39 | +const FOUNDER_NAME = 'Founder Fiona'; |
| 40 | +const STRANGER_EMAIL = 'seed-stranger@verify.test'; |
| 41 | +const STRANGER_NAME = 'Stranger Sam'; |
| 42 | + |
| 43 | +const TASK = 'seedfounder_task'; |
| 44 | + |
| 45 | +const APP = { |
| 46 | + manifest: { |
| 47 | + id: 'com.example.seedfounder', |
| 48 | + namespace: 'seedfounder', |
| 49 | + version: '0.0.1', |
| 50 | + type: 'app', |
| 51 | + name: 'Seed Founder Fixture', |
| 52 | + }, |
| 53 | + objects: [ |
| 54 | + { |
| 55 | + name: TASK, |
| 56 | + label: 'Task', |
| 57 | + fields: { |
| 58 | + name: { type: 'text', label: 'Name' }, |
| 59 | + assignee: { type: 'user', label: 'Assignee', reference: 'sys_user' }, |
| 60 | + reviewer: { type: 'lookup', label: 'Reviewer', reference: 'sys_user' }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + ], |
| 64 | + data: [ |
| 65 | + { |
| 66 | + object: TASK, |
| 67 | + mode: 'upsert', |
| 68 | + externalId: 'name', |
| 69 | + records: [ |
| 70 | + // Every person reference the seed can carry names the founder. |
| 71 | + { name: 'Welcome task', assignee: FOUNDER_NAME, reviewer: FOUNDER_NAME, owner_id: FOUNDER_NAME }, |
| 72 | + // A real person who is not a member of the new organization. |
| 73 | + { name: 'Outside task', assignee: STRANGER_NAME }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ], |
| 77 | +}; |
| 78 | + |
| 79 | +const SYS = { context: { isSystem: true } } as const; |
| 80 | + |
| 81 | +/** Boot the walled stack once at module scope and stop it: the first boot's loading is paid here, never in a clocked hook. */ |
| 82 | +async function warmUp(): Promise<void> { |
| 83 | + const before = { ...process.env }; |
| 84 | + for (const [name, value] of WALLED_BOOT_ENV) process.env[name] = value; |
| 85 | + try { |
| 86 | + const stack = await bootStack({ manifest: { ...APP.manifest, id: 'com.example.seedfounder.warmup' }, objects: [] } as never, { |
| 87 | + extraPlugins: [new OrganizationsPlugin()], |
| 88 | + }); |
| 89 | + await stack.stop(); |
| 90 | + } finally { |
| 91 | + for (const key of Object.keys(process.env)) if (!(key in before)) delete process.env[key]; |
| 92 | + Object.assign(process.env, before); |
| 93 | + } |
| 94 | +} |
| 95 | +await warmUp(); |
| 96 | + |
| 97 | +describe('a new organization’s own seed resolves its founder (#34038)', () => { |
| 98 | + const restoreEnv: Array<[string, string | undefined]> = []; |
| 99 | + let stack: VerifyStack; |
| 100 | + let ql: any; |
| 101 | + let founderId: string; |
| 102 | + let strangerId: string; |
| 103 | + let organizationId: string; |
| 104 | + |
| 105 | + const tasksOf = async (orgId: string): Promise<Record<string, Record<string, unknown>>> => { |
| 106 | + const rows: Array<Record<string, unknown>> = await ql.find(TASK, { where: { organization_id: orgId }, ...SYS }); |
| 107 | + return Object.fromEntries(rows.map((r) => [String(r.name), r])); |
| 108 | + }; |
| 109 | + |
| 110 | + beforeAll(async () => { |
| 111 | + for (const [name, value] of WALLED_BOOT_ENV) { |
| 112 | + restoreEnv.push([name, process.env[name]]); |
| 113 | + process.env[name] = value; |
| 114 | + } |
| 115 | + stack = await bootStack(APP as never, { extraPlugins: [new OrganizationsPlugin()] }); |
| 116 | + ql = await stack.kernel.getServiceAsync('objectql'); |
| 117 | + |
| 118 | + const founder = await stack.signUp(FOUNDER_EMAIL, undefined, FOUNDER_NAME); |
| 119 | + await stack.signUp(STRANGER_EMAIL, undefined, STRANGER_NAME); |
| 120 | + founderId = (await ql.findOne('sys_user', { where: { email: FOUNDER_EMAIL }, ...SYS }))?.id; |
| 121 | + strangerId = (await ql.findOne('sys_user', { where: { email: STRANGER_EMAIL }, ...SYS }))?.id; |
| 122 | + |
| 123 | + const created = await stack.apiAs(founder, 'POST', '/auth/organization/create', { |
| 124 | + name: 'Seed Founder Co', |
| 125 | + slug: 'seed-founder-co', |
| 126 | + }); |
| 127 | + expect(created.status, `organization/create: ${await created.clone().text()}`).toBe(200); |
| 128 | + organizationId = ((await created.json()) as { id: string }).id; |
| 129 | + }, 120_000); |
| 130 | + |
| 131 | + afterAll(async () => { |
| 132 | + await stack?.stop(); |
| 133 | + for (const [name, value] of restoreEnv.reverse()) { |
| 134 | + if (value === undefined) delete process.env[name]; |
| 135 | + else process.env[name] = value; |
| 136 | + } |
| 137 | + }); |
| 138 | + |
| 139 | + it('PREMISE: the wall is up, the founder is a member of the new organization and the stranger is not', async () => { |
| 140 | + const tenancy: any = await stack.kernel.getServiceAsync('tenancy'); |
| 141 | + expect(tenancy.posture).toBe('isolated'); |
| 142 | + expect(tenancy.isolationActive).toBe(true); |
| 143 | + expect(founderId).toBeTruthy(); |
| 144 | + expect(strangerId).toBeTruthy(); |
| 145 | + const members: Array<Record<string, unknown>> = await ql.find('sys_member', { where: { organization_id: organizationId }, ...SYS }); |
| 146 | + expect(members.map((m) => m.user_id)).toEqual([founderId]); |
| 147 | + }); |
| 148 | + |
| 149 | + it('the replay seeded the new organization, and every person reference naming the founder resolves to the founder', async () => { |
| 150 | + const tasks = await tasksOf(organizationId); |
| 151 | + expect(Object.keys(tasks).sort()).toEqual(['Outside task', 'Welcome task']); |
| 152 | + expect(tasks['Welcome task']).toMatchObject({ assignee: founderId, reviewer: founderId, owner_id: founderId }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('control: a real person who is not a member of the new organization still resolves to nothing', async () => { |
| 156 | + const tasks = await tasksOf(organizationId); |
| 157 | + expect(tasks['Outside task']?.assignee ?? null).toBeNull(); |
| 158 | + }); |
| 159 | +}); |
0 commit comments