From f0b247f4496a8f0016cc0805a04e012fcf4b2870 Mon Sep 17 00:00:00 2001 From: Ajax Davis Date: Fri, 16 Jan 2026 03:49:04 +1000 Subject: [PATCH] fix(integration-tests): use actual username from API in MCP tests The test was using ctx.auth.username from env vars, which may not match the actual username of the API key owner. Now fetches the real username via /api/user/profile endpoint before running MCP tests. --- .../mcp/mcp-http.integration.test.ts | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/apps/web/src/test/integration/mcp/mcp-http.integration.test.ts b/apps/web/src/test/integration/mcp/mcp-http.integration.test.ts index 391cdc7..3a14916 100644 --- a/apps/web/src/test/integration/mcp/mcp-http.integration.test.ts +++ b/apps/web/src/test/integration/mcp/mcp-http.integration.test.ts @@ -21,13 +21,32 @@ interface JsonRpcResponse { }; } +interface UserProfile { + id: string; + name: string; + username: string; + email: string; +} + describe('MCP HTTP Endpoint', () => { let ctx: IntegrationTestContext; let testCollection: { id: string; slug: string } | null = null; + let actualUsername: string | null = null; beforeAll(async () => { ctx = getTestContext(); + // Get the actual username of the API key owner (not from env var which may be stale) + const profileResult = await ctx.apiKeyClient.get<{ success: boolean; data: UserProfile }>( + '/api/user/profile' + ); + if (profileResult.ok && profileResult.data.data.username) { + actualUsername = profileResult.data.data.username; + } else { + console.log('Warning: Could not get actual username, falling back to ctx.auth.username'); + actualUsername = ctx.auth.username; + } + // Create a test collection for MCP tests with unique name testCollection = await ctx.factories.collection.create({ name: `MCP Test Collection ${Date.now()}`, @@ -53,7 +72,7 @@ describe('MCP HTTP Endpoint', () => { protocol: string; transport: string; endpoint: string; - }>(`/api/mcp/${ctx.auth.username}/${testCollection.slug}/http`); + }>(`/api/mcp/${actualUsername}/${testCollection.slug}/http`); expect(result.ok).toBe(true); if (result.ok) { @@ -72,7 +91,7 @@ describe('MCP HTTP Endpoint', () => { } const result = await ctx.apiKeyClient.post( - `/api/mcp/${ctx.auth.username}/${testCollection.slug}/http`, + `/api/mcp/${actualUsername}/${testCollection.slug}/http`, { jsonrpc: '2.0', method: 'initialize', @@ -95,7 +114,7 @@ describe('MCP HTTP Endpoint', () => { } const result = await ctx.publicClient.post( - `/api/mcp/${ctx.auth.username}/${testCollection.slug}/http`, + `/api/mcp/${actualUsername}/${testCollection.slug}/http`, { jsonrpc: '2.0', method: 'initialize', @@ -116,7 +135,7 @@ describe('MCP HTTP Endpoint', () => { } const result = await ctx.apiKeyClient.post( - `/api/mcp/${ctx.auth.username}/${testCollection.slug}/http`, + `/api/mcp/${actualUsername}/${testCollection.slug}/http`, { jsonrpc: '2.0', method: 'tools/list', @@ -144,7 +163,7 @@ describe('MCP HTTP Endpoint', () => { } const result = await ctx.apiKeyClient.post( - `/api/mcp/${ctx.auth.username}/${testCollection.slug}/http`, + `/api/mcp/${actualUsername}/${testCollection.slug}/http`, { jsonrpc: '2.0', method: 'nonexistent/method', @@ -168,7 +187,7 @@ describe('MCP HTTP Endpoint', () => { // Use collection ID instead of slug const result = await ctx.apiKeyClient.post( - `/api/mcp/${ctx.auth.username}/${testCollection.id}/http`, + `/api/mcp/${actualUsername}/${testCollection.id}/http`, { jsonrpc: '2.0', method: 'initialize',