Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(external-rest-api): preserve invitation authority failures
Josué8b30b625a0f0
diff --git a/external-rest-api/package-lock.json b/external-rest-api/package-lock.jsonindex d0a88c089..2b51e3ec9 100644--- a/external-rest-api/package-lock.json+++ b/external-rest-api/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-external-rest-api",- "version": "0.1.103",+ "version": "0.1.104","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-external-rest-api",- "version": "0.1.103",+ "version": "0.1.104","dependencies": {"@clerum/jwt-key-policy": "file:../packages/jwt-key-policy","cors": "^2.8.5",diff --git a/external-rest-api/package.json b/external-rest-api/package.jsonindex 0bfedb7a9..817d9395e 100644--- a/external-rest-api/package.json+++ b/external-rest-api/package.json@@ -1,6 +1,6 @@{"name": "clerum-external-rest-api",- "version": "0.1.103",+ "version": "0.1.104","description": "External REST API for profile, team management, and RPC token brokerage","main": "dist/main.js","engines": {diff --git a/external-rest-api/src/releaseManifest.ts b/external-rest-api/src/releaseManifest.tsindex 1577f12ce..c43b2ae76 100644--- a/external-rest-api/src/releaseManifest.ts+++ b/external-rest-api/src/releaseManifest.ts@@ -8,7 +8,7 @@ export type ReleaseManifest = {export const releaseManifest: ReleaseManifest = {releaseId: 'v0.10.0',- externalRestApiVersion: '0.1.103',+ externalRestApiVersion: '0.1.104',rpcProxyVersion: '0.1.126',desktopVersion: '0.10.0',minimumDesktopVersion: '0.1.252',diff --git a/external-rest-api/src/services/invitationsService.ts b/external-rest-api/src/services/invitationsService.tsindex 2575e4435..6f8d51393 100644--- a/external-rest-api/src/services/invitationsService.ts+++ b/external-rest-api/src/services/invitationsService.ts@@ -150,6 +150,7 @@ export async function setupInvitationPassword()return { data }} catch (error) {+ if (error instanceof ControlApiError && [429, 503].includes(error.status)) throw errorconst message = error instanceof Error ? error.message : ''if (message.includes('(404)')) return { error: 'not_found' }if (message.includes('(403)')) return { error: 'forbidden' }diff --git a/external-rest-api/test/r2m1.invitationAdmission.public.test.ts b/external-rest-api/test/r2m1.invitationAdmission.public.test.tsnew file mode 100644index 000000000..0721bd634--- /dev/null+++ b/external-rest-api/test/r2m1.invitationAdmission.public.test.ts@@ -0,0 +1,121 @@+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'+import express from 'express'+import request from 'supertest'+import { config } from '../src/config.js'+import { createInvitationsRouter } from '../src/routes/invitations.js'++const authTokenMock = vi.hoisted(() => ({+ verifyToken: vi.fn(),+}))++vi.mock('../src/authToken.js', () => authTokenMock)++const fetchMock = vi.fn<typeof fetch>()++function makeApp() {+ const app = express()+ app.use(express.json())+ app.use(createInvitationsRouter())+ return app+}++function upstreamResponse(status: number, body: unknown, headers: HeadersInit = {}) {+ return new Response(JSON.stringify(body), {+ status,+ headers: { 'content-type': 'application/json', ...Object.fromEntries(new Headers(headers)) },+ })+}++describe('accepted invitation password authority outcomes', () => {+ let previousControlApiBaseUrl: string+ let previousControlApiServiceToken: string++ beforeEach(() => {+ authTokenMock.verifyToken.mockReset().mockReturnValue({+ userId: 'synthetic-user-id',+ email: '[email protected]',+ teamId: '',+ role: 'member',+ exp: 4_102_444_800,+ })+ previousControlApiBaseUrl = config.controlApiBaseUrl+ previousControlApiServiceToken = config.controlApiServiceToken+ config.controlApiBaseUrl = 'http://control-api.synthetic.test/api/v1'+ config.controlApiServiceToken = 'synthetic-internal-service-token'+ fetchMock.mockReset()+ vi.stubGlobal('fetch', fetchMock)+ })++ afterEach(() => {+ config.controlApiBaseUrl = previousControlApiBaseUrl+ config.controlApiServiceToken = previousControlApiServiceToken+ vi.unstubAllGlobals()+ })++ async function submitPassword() {+ return request(makeApp())+ .post('/invitations/password')+ .set('authorization', 'Bearer synthetic-authenticated-session')+ .send({ invitationId: 'synthetic-invitation-row', password: 'Synthetic-Password-123' })+ }++ it('preserves busy-owner denial and permits a later retry without consuming the invitation', async () => {+ fetchMock+ .mockResolvedValueOnce(+ upstreamResponse(+ 429,+ { error: 'verification_busy', retryAfterSeconds: 11 },+ { 'retry-after': '11', 'x-ratelimit-remaining': '0' }+ )+ )+ .mockResolvedValueOnce(+ upstreamResponse(200, {+ id: 'synthetic-invitation-row',+ passwordUpdated: true,+ status: 'accepted',+ })+ )++ const denied = await submitPassword()++ expect(denied.status).toBe(429)+ expect(denied.body).toEqual({ error: 'rate_limited', retryAfterSeconds: 11 })+ expect(denied.headers['retry-after']).toBe('11')+ expect(denied.headers['x-ratelimit-remaining']).toBeUndefined()+ expect(JSON.stringify(denied.body)).not.toContain('verification_busy')+ expect(denied.headers['set-cookie']).toBeUndefined()++ const retried = await submitPassword()++ expect(retried.status).toBe(200)+ expect(retried.body).toEqual({+ id: 'synthetic-invitation-row',+ passwordUpdated: true,+ status: 'accepted',+ })+ expect(String(retried.headers['set-cookie'])).toContain(+ 'profile_session=synthetic-authenticated-session'+ )+ expect(fetchMock).toHaveBeenCalledTimes(2)+ })++ it('preserves authority unavailability without presenting a password-format error', async () => {+ fetchMock.mockResolvedValueOnce(+ upstreamResponse(+ 503,+ { error: 'authority_failure', detail: 'private database diagnostic' },+ { 'retry-after': '1200', 'x-ratelimit-remaining': '0' }+ )+ )++ const response = await submitPassword()++ expect(response.status).toBe(503)+ expect(response.body).toEqual({ error: 'authority_unavailable', retryAfterSeconds: 900 })+ expect(response.headers['retry-after']).toBe('900')+ expect(response.headers['set-cookie']).toBeUndefined()+ expect(JSON.stringify(response.body)).not.toContain('invalid_password')+ expect(JSON.stringify(response.body)).not.toContain('database')+ expect(fetchMock).toHaveBeenCalledOnce()+ })+})