|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * The two flow doors forward the caller's posture rung — |
| 5 | + * `AutomationContext.posture` (#28099, the door half of #28070). |
| 6 | + * |
| 7 | + * A request carries the rung its user resolves to (`ExecutionContext.posture`, |
| 8 | + * ADR-0095 D2/D3), derived once by `resolveAuthzContext`. The doors already |
| 9 | + * hold it beside the caller's positions and permission sets, which they |
| 10 | + * forward; a run that carries those grants is not re-resolved, so a rung the |
| 11 | + * door drops is a rung the run never has. The door forwards the caller's OWN |
| 12 | + * rung as-is — ⛔ never derived from the flow's author, never raised, never |
| 13 | + * filled in when the request carries none. |
| 14 | + * |
| 15 | + * What this file pins — the PRODUCER half. The consumer half, on the real |
| 16 | + * automation engine, is `runas-user-posture-rung.integration.test.ts` in |
| 17 | + * `@objectstack/service-automation`, which runs a door-shaped context (carried |
| 18 | + * grants plus a carried rung) to a data node. |
| 19 | + * |
| 20 | + * 1. The trigger door (`buildAutomationContext`) forwards `ec.posture`, and |
| 21 | + * both of its routes hand it to the engine. |
| 22 | + * 2. The action door (`dispatchFlowAction`) forwards it. |
| 23 | + * 3. A request with no rung (anonymous, or a principal the resolver gave |
| 24 | + * none) adds no key — absent, never a default rung. |
| 25 | + * 4. The request body cannot name one: `buildAutomationContext` reads the |
| 26 | + * rung off the execution context only, and a body key named `posture` |
| 27 | + * stays a flow param. |
| 28 | + */ |
| 29 | + |
| 30 | +import { describe, it, expect, vi } from 'vitest'; |
| 31 | + |
| 32 | +import { HttpDispatcher } from './http-dispatcher.js'; |
| 33 | +import { dispatchFlowAction, loadActionSubjectRecord } from './action-execution.js'; |
| 34 | +import { buildAutomationContext } from './domains/automation.js'; |
| 35 | + |
| 36 | +const FLOW_NAME = 'quote_generation'; |
| 37 | + |
| 38 | +const QUOTE = { |
| 39 | + name: 'generate_quote', |
| 40 | + label: 'Generate quote', |
| 41 | + objectName: 'crm_opportunity', |
| 42 | + type: 'flow', |
| 43 | + target: FLOW_NAME, |
| 44 | +}; |
| 45 | + |
| 46 | +function ec(posture?: string) { |
| 47 | + return { |
| 48 | + userId: 'usr_1', tenantId: 'org_1', positions: ['platform_admin'], permissions: ['admin_full_access'], systemPermissions: [], |
| 49 | + ...(posture !== undefined ? { posture } : {}), |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +/** An automation double that records the context it is handed. */ |
| 54 | +function makeAutomation() { |
| 55 | + const execute = vi.fn(async (_flow: string, _context?: any) => ({ success: true, output: {} })); |
| 56 | + const getFlow = vi.fn(async (name: string) => (name === FLOW_NAME ? { name } : null)); |
| 57 | + return { execute, getFlow }; |
| 58 | +} |
| 59 | +const contextOf = (automation: { execute: any }) => automation.execute.mock.calls[0]?.[1]; |
| 60 | +const flowDeps = (automation: any): any => ({ |
| 61 | + resolveService: async (_ctx: any, name: string) => (name === 'automation' ? automation : undefined), |
| 62 | +}); |
| 63 | +const REQUEST: any = { request: {}, environmentId: 'platform' }; |
| 64 | + |
| 65 | +describe('[#28099] the trigger door forwards the caller\'s posture rung', () => { |
| 66 | + it('`buildAutomationContext` carries `executionContext.posture` as `posture`, beside the grants it carries', () => { |
| 67 | + const ctx: any = buildAutomationContext( |
| 68 | + { recordId: 'opp_1', objectName: 'crm_opportunity', params: {} }, |
| 69 | + { request: {}, executionContext: ec('PLATFORM_ADMIN') } as any, |
| 70 | + ); |
| 71 | + expect(ctx).toMatchObject({ permissions: ['admin_full_access'], posture: 'PLATFORM_ADMIN' }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('a request with no rung adds no key: absent, never a default rung', () => { |
| 75 | + const ctx: any = buildAutomationContext( |
| 76 | + { recordId: 'opp_1', objectName: 'crm_opportunity', params: {} }, |
| 77 | + { request: {}, executionContext: ec() } as any, |
| 78 | + ); |
| 79 | + expect('posture' in ctx).toBe(false); |
| 80 | + const anonymous: any = buildAutomationContext({ params: {} }, { request: {} } as any); |
| 81 | + expect('posture' in anonymous).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('the body cannot name a rung: a flat `posture` key stays a flow param, a wrapped one never leaves `params`', () => { |
| 85 | + const flat: any = buildAutomationContext( |
| 86 | + { posture: 'PLATFORM_ADMIN' }, |
| 87 | + { request: {}, executionContext: ec('MEMBER') } as any, |
| 88 | + ); |
| 89 | + expect(flat.posture).toBe('MEMBER'); |
| 90 | + expect(flat.params.posture).toBe('PLATFORM_ADMIN'); |
| 91 | + const noRung: any = buildAutomationContext( |
| 92 | + { posture: 'PLATFORM_ADMIN', params: { posture: 'PLATFORM_ADMIN' } }, |
| 93 | + { request: {}, executionContext: ec() } as any, |
| 94 | + ); |
| 95 | + expect('posture' in noRung).toBe(false); |
| 96 | + }); |
| 97 | + |
| 98 | + for (const route of [ |
| 99 | + { label: 'POST /:name/trigger', path: `/${FLOW_NAME}/trigger` }, |
| 100 | + { label: 'legacy POST /trigger/:name', path: `/trigger/${FLOW_NAME}` }, |
| 101 | + ]) { |
| 102 | + it(`${route.label} hands the engine the rung`, async () => { |
| 103 | + const automation = makeAutomation(); |
| 104 | + const resolve = (n: string) => (n === 'automation' ? automation : undefined); |
| 105 | + const kernel: any = { getService: resolve, getServiceAsync: async (n: string) => resolve(n), context: { getService: resolve } }; |
| 106 | + await new HttpDispatcher(kernel).handleAutomation( |
| 107 | + route.path, 'POST', |
| 108 | + { recordId: 'opp_1', objectName: 'crm_opportunity', params: {} }, |
| 109 | + { request: {}, executionContext: ec('PLATFORM_ADMIN') } as any, |
| 110 | + ); |
| 111 | + expect(automation.execute).toHaveBeenCalledTimes(1); |
| 112 | + expect(contextOf(automation).posture).toBe('PLATFORM_ADMIN'); |
| 113 | + }); |
| 114 | + } |
| 115 | +}); |
| 116 | + |
| 117 | +describe('[#28099] the action door forwards the caller\'s posture rung', () => { |
| 118 | + it('`dispatchFlowAction` carries `ec.posture` as `posture`, beside the grants it carries', async () => { |
| 119 | + const automation = makeAutomation(); |
| 120 | + const subject = await loadActionSubjectRecord('crm_opportunity', 'opp_1', async () => ({ record: { id: 'opp_1' } })); |
| 121 | + await dispatchFlowAction(flowDeps(automation), REQUEST, QUOTE, { |
| 122 | + objectName: 'crm_opportunity', subject, params: {}, recordId: 'opp_1', ec: ec('PLATFORM_ADMIN'), envId: 'platform', |
| 123 | + }); |
| 124 | + expect(contextOf(automation)).toMatchObject({ permissions: ['admin_full_access'], posture: 'PLATFORM_ADMIN' }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('a request with no rung adds no key: absent, never a default rung', async () => { |
| 128 | + const automation = makeAutomation(); |
| 129 | + const subject = await loadActionSubjectRecord('crm_opportunity', 'opp_1', async () => ({ record: { id: 'opp_1' } })); |
| 130 | + await dispatchFlowAction(flowDeps(automation), REQUEST, QUOTE, { |
| 131 | + objectName: 'crm_opportunity', subject, params: {}, recordId: 'opp_1', ec: ec(), envId: 'platform', |
| 132 | + }); |
| 133 | + expect('posture' in contextOf(automation)).toBe(false); |
| 134 | + }); |
| 135 | +}); |
0 commit comments