Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
test(control-api): u6(api) exercise shared-identity context branch end-to-end
Diego Zacarias29953e83f1fa
diff --git a/control-api/package-lock.json b/control-api/package-lock.jsonindex 65c1b78e9..de12042d3 100644--- a/control-api/package-lock.json+++ b/control-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-api",- "version": "0.1.435",+ "version": "0.1.436","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-api",- "version": "0.1.435",+ "version": "0.1.436","dependencies": {"@clerum/image-policy": "file:../packages/image-policy","@clerum/llm-providers": "file:../packages/llm-providers",diff --git a/control-api/package.json b/control-api/package.jsonindex 70ff5c36e..6c66376d8 100644--- a/control-api/package.json+++ b/control-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-api",- "version": "0.1.435",+ "version": "0.1.436","description": "Internal Clerum control plane REST API + UI for managing CRDs and secrets","main": "dist/main.js","engines": {diff --git a/control-api/test/oauth.store.sharedGrants.realPostgres.integration.test.ts b/control-api/test/oauth.store.sharedGrants.realPostgres.integration.test.tsindex 263f6bf54..987f95894 100644--- a/control-api/test/oauth.store.sharedGrants.realPostgres.integration.test.ts+++ b/control-api/test/oauth.store.sharedGrants.realPostgres.integration.test.ts@@ -125,6 +125,32 @@ describeRealPostgres('oauth store — shared grants (real Postgres)', () => {expect(b?.bootstrappedByUserId).toBe('user-9')})+ // mini-spec 05 §4 — the audit-integrity CHECK: a `shared` row with a NULL+ // `bootstrapped_by_user_id` (or NULL `context_id`) is REJECTED by the schema,+ // so team activity is never uninspectable. The layer under test is the real+ // migration/schema stood up by initDb (T1: the constraint's real producer),+ // driven by an adversarial raw INSERT — not a hand-built fixture fed into code.+ it('CHECK rejects a shared row with NULL bootstrapped_by_user_id or NULL context_id', async () => {+ const insertShared = (contextId: string | null, bootstrappedBy: string | null) =>+ dbPool.query(+ `INSERT INTO oauth_grants (+ owner_kind, recipe_namespace, recipe_name, user_id, context_id,+ oauth_client_id, grant_kind, bootstrapped_by_user_id,+ provider, access_token_encrypted, refresh_token_encrypted,+ access_token_expires_at, updated_at+ ) VALUES ('mcpserver', $1, 'gdrive-check', NULL, $2, 'google-drive',+ 'shared', $3, 'google', 'enc', NULL, NULL, NOW())`,+ [config.mcpServersNamespace, contextId, bootstrappedBy]+ )++ // NULL bootstrapped_by_user_id → violates oauth_grants_kind_userid_check.+ await expect(insertShared('ctx-chk', null)).rejects.toThrow(/oauth_grants_kind_userid_check/)+ // NULL context_id → same CHECK (shared identity has no coordinate).+ await expect(insertShared(null, 'user-1')).rejects.toThrow(/oauth_grants_kind_userid_check/)+ // Control: both present → accepted.+ await expect(insertShared('ctx-chk', 'user-1')).resolves.toBeTruthy()+ })+it('a user grant and a shared grant of the same server do not collide', async () => {const server = 'gdrive-c'await upsertOAuthGrant(db, KEY, {diff --git a/control-api/test/routes.mcpOauth.test.ts b/control-api/test/routes.mcpOauth.test.tsindex d9eebca13..0636f310e 100644--- a/control-api/test/routes.mcpOauth.test.ts+++ b/control-api/test/routes.mcpOauth.test.ts@@ -228,6 +228,67 @@ describe('routes/mcp-oauth — POST /mcp-oauth/user-token (U1)', () => {expect(grantQuery).toBeUndefined()})+ // Happy-negative for the context branch (U1 dispatch, U6 activation): when NO+ // shared grant exists for the server's authoritative contextRef, the broker+ // returns 404 no_grant — the same fail-closed outcome the user branch gives,+ // driven through the REAL shared getOAuthGrant SQL (mockPoolQuery → 0 rows).+ it('context flavor: 404 no_grant when no shared grant exists for spec.contextRef', async () => {+ seedOauthServer(gateway, { name: 'gdrive', grantScope: 'context', contextRef: 'ctx-A' })+ // Default mock already resolves { rows: [], rowCount: 0 } → getOAuthGrant null.+ const res = await request(app)+ .post('/api/v1/mcp-oauth/user-token')+ .set('Authorization', `Bearer ${controlToken()}`)+ .send({ mcpServerName: 'gdrive', userId: 'user-2' })++ expect(res.status).toBe(404)+ expect(res.body.error).toBe('no_grant')+ expect(res.body.token).toBeUndefined()+ // The lookup that ran was the SHARED coordinate keyed by contextRef, no userId.+ const grantQuery = mockPoolQuery.mock.calls.find(+ ([sql]) =>+ typeof sql === 'string' &&+ sql.includes('FROM oauth_grants') &&+ sql.includes("grant_kind = 'shared'")+ )+ expect(grantQuery?.[1]).toEqual(['mcpserver', MCP_NS, 'gdrive', 'ctx-A', 'google-drive'])+ })++ // Fail-closed: a context-flavor server that is missing its CRD-required+ // spec.contextRef must NOT resolve to any grant. The broker rejects with 400+ // server_missing_context BEFORE any DB lookup (mini-spec 05 §2 — the shared+ // key coordinate is unresolvable, so there is nothing safe to query).+ it('context flavor: 400 server_missing_context when spec.contextRef is absent, no grant lookup', async () => {+ void gateway.createResource(+ 'mcpservers',+ {+ metadata: { name: 'no-ctx' },+ spec: {+ // contextRef deliberately omitted.+ auth: { type: 'oauth' },+ oauth: {+ id: 'google-drive',+ provider: 'google',+ clientIdRef: { name: 'google-creds', key: 'client-id' },+ clientSecretRef: { name: 'google-creds', key: 'client-secret' },+ grantScope: 'context',+ },+ },+ },+ MCP_NS+ )+ const res = await request(app)+ .post('/api/v1/mcp-oauth/user-token')+ .set('Authorization', `Bearer ${controlToken()}`)+ .send({ mcpServerName: 'no-ctx', userId: 'user-2' })++ expect(res.status).toBe(400)+ expect(res.body.error).toBe('server_missing_context')+ const grantQuery = mockPoolQuery.mock.calls.find(+ ([sql]) => typeof sql === 'string' && sql.includes('FROM oauth_grants')+ )+ expect(grantQuery).toBeUndefined()+ })+it('accepts a body contextId that matches spec.contextRef', async () => {seedOauthServer(gateway, { name: 'gdrive', grantScope: 'context', contextRef: 'ctx-A' })const future = new Date(Date.now() + 3600_000)diff --git a/control-api/test/services.access.mcpInvocable.grantGate.realPostgres.integration.test.ts b/control-api/test/services.access.mcpInvocable.grantGate.realPostgres.integration.test.tsindex a9aa4c712..5ffaf094e 100644--- a/control-api/test/services.access.mcpInvocable.grantGate.realPostgres.integration.test.ts+++ b/control-api/test/services.access.mcpInvocable.grantGate.realPostgres.integration.test.ts@@ -128,6 +128,41 @@ describeRealPostgres('mcpInvocable grant-presence gate (real Postgres)', () => {expect(await list(g, 'bob')).toEqual(['gdrive-s'])})+ // mini-spec 04 §5c literal "with OR without the caller's own user grant": the+ // "without" half is the test above (alice never consented). This is the "with"+ // half — a member who ALSO holds a stray per-user grant on the same server is+ // servable BECAUSE the shared grant exists (not because of their user grant).+ it('context-flavor: a member holding a stray user grant is still servable via the shared grant', async () => {+ const g = gatewayWith([{ name: 'gdrive-w', grantScope: 'context', contextRef: 'ctx-1' }])+ // dave happens to also carry a per-user grant on this (context-flavor) server.+ await upsertOAuthGrant(db, KEY, {+ grantKind: 'user',+ ownerKind: 'mcpserver',+ recipeNamespace: NS,+ recipeName: 'gdrive-w',+ userId: 'dave',+ oauthClientId: CLIENT_ID,+ provider: 'google',+ accessToken: 'U',+ })+ // The user grant alone does not make a context server invocable.+ expect(await list(g, 'dave')).toEqual([])++ // Once the shared grant is bootstrapped (by anyone), dave is servable —+ // driven by the SHARED key, with his own user grant present and irrelevant.+ await bootstrapSharedOAuthGrant(db, KEY, {+ ownerKind: 'mcpserver',+ recipeNamespace: NS,+ recipeName: 'gdrive-w',+ contextId: 'ctx-1',+ oauthClientId: CLIENT_ID,+ bootstrappedByUserId: 'erin',+ provider: 'google',+ accessToken: 'S',+ })+ expect(await list(g, 'dave')).toEqual(['gdrive-w'])+ })+it('a user grant does NOT satisfy a context-flavor server (disjoint keyspaces)', async () => {const g = gatewayWith([{ name: 'gdrive-x', grantScope: 'context', contextRef: 'ctx-1' }])// A stray user grant on the same server/client for the caller.