diff --git a/server/src/__tests__/tool-access-service.test.ts b/server/src/__tests__/tool-access-service.test.ts index 98fa6f85577..1cb9bdbb353 100644 --- a/server/src/__tests__/tool-access-service.test.ts +++ b/server/src/__tests__/tool-access-service.test.ts @@ -1017,6 +1017,72 @@ describeEmbeddedPostgres("tool access service", () => { await tempDb?.cleanup(); }); + it("finishes a resumed URL draft without authorizing every company agent", async () => { + const company = await createCompany(db); + const companyAgents = await db.insert(agents).values( + Array.from({ length: 1100 }, (_, index) => ({ + companyId: company.id, name: `Company agent ${index}`, role: "engineer", + status: "idle", adapterType: "process", + })), + ).returning({ id: agents.id }); + const service = createTestToolAccessService(db); + mockToolsList([readTool("list_pages", "Read pages")]); + const draft = await service.createConnection(company.id, connectionInput({ + name: "Saved URL draft", transport: "mcp_remote", status: "draft", enabled: false, + config: { url: "https://secure.example/mcp" }, + })); + const connected = await service.connectGalleryApp(company.id, { + reconnectConnectionId: draft.id, link: "https://secure.example/mcp", name: draft.name, + credentialValues: { "headers.Authorization": "Bearer fixture-token" }, + }, { actorType: "user", actorId: "board" }); + expect(connected.connection).toMatchObject({ id: draft.id, status: "draft", enabled: false }); + const bindingsBeforeFinish = await db.select().from(toolProfileBindings) + .where(eq(toolProfileBindings.companyId, company.id)); + + const userId = `draft-owner-${randomUUID()}`; + await grantBoardUser(db, company.id, userId, ["agents:configure"], "owner"); + const app = createRouteApp(db, boardSessionActor(company.id, "owner", userId)); + const catalogEntryIds = connected.catalog.map(entry => entry.id); + const clientOptions = db.$client.options; + const previousDebug = clientOptions.debug; + let queryCount = 0; + clientOptions.debug = () => { queryCount += 1; }; + try { + const result = await request(app) + .post(`/api/companies/${company.id}/tools/apps/${draft.id}/finish`) + .send({ enabledCatalogEntryIds: catalogEntryIds, askFirstCatalogEntryIds: catalogEntryIds, + access: { agentIds: [companyAgents[0].id] } }); + expect(result.status, JSON.stringify(result.body)).toBe(200); + expect(result.body.profileBindings).toEqual([ + expect.objectContaining({ targetType: "agent", targetId: companyAgents[0].id }), + ]); + expect(result.body.policies).toEqual([ + expect.objectContaining({ enabled: true, policyType: "require_approval" }), + ]); + } finally { clientOptions.debug = previousDebug; } + expect(bindingsBeforeFinish).toEqual([]); + expect(queryCount).toBeLessThan(200); + await expect(service.listConnectionInstalls(draft.id, company.id)).resolves.toEqual([ + expect.objectContaining({ targetType: "agent", targetId: companyAgents[0].id }), + ]); + }, 30_000); + + it("keeps a resumed gallery API-key draft unbound until its access is saved", async () => { + const company = await createCompany(db); + const service = createTestToolAccessService(db); + mockToolsList([readTool("list_pages", "Read pages")]); + await withGalleryServerUrl("zapier", PUBLIC_MCP_FIXTURE_URL, async () => { + const input = { galleryKey: "zapier", name: "Saved gallery draft", + credentialValues: { "credentials.authorization": "fixture-token" } }; + const first = await service.connectGalleryApp(company.id, input, { actorType: "user", actorId: "board" }); + const resumed = await service.connectGalleryApp(company.id, { ...input, resumeConnectionId: first.connectionId }, + { actorType: "user", actorId: "board" }); + expect(resumed.connection).toMatchObject({ id: first.connectionId, status: "draft", enabled: false }); + await expect(db.select().from(toolProfiles).where(eq(toolProfiles.companyId, company.id))).resolves.toEqual([]); + await expect(db.select().from(toolProfileBindings).where(eq(toolProfileBindings.companyId, company.id))).resolves.toEqual([]); + }); + }); + async function waitForBlockedMembershipUpdate() { for (let attempt = 0; attempt < 80; attempt += 1) { const [waiting] = await db.execute<{ waiting: boolean }>(sql` diff --git a/server/src/services/tool-access.ts b/server/src/services/tool-access.ts index 77704e9e257..318e74c4624 100644 --- a/server/src/services/tool-access.ts +++ b/server/src/services/tool-access.ts @@ -3129,7 +3129,9 @@ export function toolAccessService( // later fails: another retry may already be using the committed grant. personalPublicSetupEstablished = true; } - const restoreDraftDefaults = Boolean(revivedConnectionPrevious); + const restoreDraftDefaults = Boolean( + revivedConnectionPrevious && revivedConnectionPrevious.status !== "draft", + ); const refreshOptions = { enableAllByDefault: restoreDraftDefaults && !remoteMcpConnector, restoreDraftDefaults,