|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// [#29158] `POST /auth/organization/invite-member` refuses a BLANK role the way |
| 4 | +// it refuses an unknown one — 400 `ROLE_NOT_FOUND`, and no `sys_invitation` |
| 5 | +// row — in both postures the defect was measured in. |
| 6 | +// |
| 7 | +// better-auth's own role check splits the role list and drops empty entries, so |
| 8 | +// a blank role yields no unknown role: on `main` before this the door answered |
| 9 | +// 200 and stored `role: ''`, and on a walled `group` boot the invitee's |
| 10 | +// acceptance wrote a `sys_member` row with no organization role. The |
| 11 | +// framework's `beforeCreateInvitation` now refuses it before any row exists; |
| 12 | +// it never defaults the role to `member`. |
| 13 | +// |
| 14 | +// Why a dogfood beside the unit pins: only the composed stack runs the vendor |
| 15 | +// route, the framework hook and the ObjectQL write together, on both postures. |
| 16 | + |
| 17 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 18 | +import crmStack from '@objectstack/example-crm'; |
| 19 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 20 | + |
| 21 | +const SYS = { context: { isSystem: true } }; |
| 22 | + |
| 23 | +for (const posture of ['single', 'group'] as const) { |
| 24 | + describe(`[#29158] ${posture}: invite-member refuses a blank role 400 ROLE_NOT_FOUND`, () => { |
| 25 | + let stack: VerifyStack; |
| 26 | + let ql: any; |
| 27 | + let token: string; |
| 28 | + let orgId: string; |
| 29 | + const previous = process.env.OS_TENANCY_POSTURE; |
| 30 | + |
| 31 | + const invitationsFor = async (email: string): Promise<Record<string, unknown>[]> => |
| 32 | + ql.find('sys_invitation', { where: { email }, ...SYS }); |
| 33 | + |
| 34 | + beforeAll(async () => { |
| 35 | + process.env.OS_TENANCY_POSTURE = posture; |
| 36 | + stack = await bootStack(crmStack as never, posture === 'single' ? {} : { multiTenant: 'posture-only' }); |
| 37 | + const adminToken = await stack.signIn(); |
| 38 | + ql = await stack.kernel.getServiceAsync<any>('objectql'); |
| 39 | + |
| 40 | + if (posture === 'single') { |
| 41 | + // The seeded admin owns the Default Organization the boot created. |
| 42 | + token = adminToken; |
| 43 | + const [org] = await ql.find('sys_organization', { where: { slug: 'default' }, limit: 1, ...SYS }); |
| 44 | + expect(org?.id, 'the boot created the Default Organization').toBeTruthy(); |
| 45 | + orgId = String(org.id); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + // Walled: the admin creates a second organization and owns it — the |
| 50 | + // issuer the card measured is the owner of an organization other than |
| 51 | + // the Default one. |
| 52 | + token = adminToken; |
| 53 | + const created = await stack.apiAs(token, 'POST', '/auth/organization/create', { |
| 54 | + name: 'Blank Role Org', |
| 55 | + slug: 'blank-role-org', |
| 56 | + }); |
| 57 | + expect(created.status, `organization/create: ${await created.clone().text()}`).toBe(200); |
| 58 | + orgId = String(((await created.json()) as { id?: string }).id ?? ''); |
| 59 | + expect(orgId).not.toBe(''); |
| 60 | + }, 180_000); |
| 61 | + |
| 62 | + afterAll(async () => { |
| 63 | + await stack?.stop?.(); |
| 64 | + if (previous === undefined) delete process.env.OS_TENANCY_POSTURE; |
| 65 | + else process.env.OS_TENANCY_POSTURE = previous; |
| 66 | + }); |
| 67 | + |
| 68 | + it('guard: the boot runs the posture under test', async () => { |
| 69 | + const tenancy = await stack.kernel.getServiceAsync<{ posture: string; degraded: boolean }>('tenancy'); |
| 70 | + expect(tenancy.posture).toBe(posture); |
| 71 | + expect(tenancy.degraded).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + const BLANK_ROLES = [ |
| 75 | + ['an empty role', ''], |
| 76 | + ['a whitespace-only role', ' '], |
| 77 | + ['an empty entry in a role list', 'member,'], |
| 78 | + ] as const; |
| 79 | + for (const [label, role] of BLANK_ROLES) { |
| 80 | + it(`${label} is refused 400 ROLE_NOT_FOUND and leaves no invitation row`, async () => { |
| 81 | + const email = `blank-${posture}-${BLANK_ROLES.findIndex(([, r]) => r === role)}@dogfood.test`; |
| 82 | + const res = await stack.apiAs(token, 'POST', '/auth/organization/invite-member', { |
| 83 | + email, |
| 84 | + role, |
| 85 | + organizationId: orgId, |
| 86 | + }); |
| 87 | + const text = await res.clone().text(); |
| 88 | + expect(res.status, text).toBe(400); |
| 89 | + const body = JSON.parse(text) as { code?: string; error?: { code?: string } }; |
| 90 | + expect(body.code ?? body.error?.code, text).toBe('ROLE_NOT_FOUND'); |
| 91 | + expect(await invitationsFor(email)).toEqual([]); |
| 92 | + }, 60_000); |
| 93 | + } |
| 94 | + |
| 95 | + it('CONTROL: a named role is unchanged — 200 and one pending row carrying it', async () => { |
| 96 | + const email = `named-${posture}@dogfood.test`; |
| 97 | + const res = await stack.apiAs(token, 'POST', '/auth/organization/invite-member', { |
| 98 | + email, |
| 99 | + role: 'member', |
| 100 | + organizationId: orgId, |
| 101 | + }); |
| 102 | + expect(res.status, await res.clone().text()).toBe(200); |
| 103 | + const rows = await invitationsFor(email); |
| 104 | + expect(rows.map((r) => [r.role, r.status])).toEqual([['member', 'pending']]); |
| 105 | + }, 60_000); |
| 106 | + }); |
| 107 | +} |
0 commit comments