Skip to content

Commit ec652a7

Browse files
test(mcp): cover OAuth creation constraints (Zoo-Code-Org#2096)
1 parent 3589f5f commit ec652a7

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎src/eslint-suppressions.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1501,7 +1501,7 @@
15011501
},
15021502
"services/mcp/__tests__/McpOAuthClientProvider.spec.ts": {
15031503
"@typescript-eslint/no-explicit-any": {
1504-
"count": 19
1504+
"count": 18
15051505
}
15061506
},
15071507
"services/mcp/__tests__/SecretStorageService.spec.ts": {

‎src/services/mcp/__tests__/McpOAuthClientProvider.spec.ts‎

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mockFetch.mockResolvedValue({
4848
})
4949

5050
import { McpOAuthClientProvider } from "../McpOAuthClientProvider"
51-
import { SecretStorageService } from "../SecretStorageService"
51+
import { SecretStorageService, StoredMcpOAuthData } from "../SecretStorageService"
5252
import { startCallbackServer, stopCallbackServer } from "../utils/callbackServer"
5353
import { discoverOAuthProtectedResourceMetadata } from "@modelcontextprotocol/sdk/client/auth.js"
5454
import * as vscode from "vscode"
@@ -61,7 +61,7 @@ function createMockSecretStorage(): SecretStorageService {
6161
const raw = store.get(key(url, clientId))
6262
return raw ? JSON.parse(raw) : undefined
6363
}),
64-
saveOAuthData: vi.fn(async (url: string, data: any, clientId?: string) => {
64+
saveOAuthData: vi.fn(async (url: string, data: StoredMcpOAuthData, clientId?: string) => {
6565
store.set(key(url, clientId), JSON.stringify(data))
6666
}),
6767
deleteOAuthData: vi.fn(async (url: string, clientId?: string) => {
@@ -875,13 +875,13 @@ describe("McpOAuthClientProvider", () => {
875875
await provider.close()
876876
})
877877

878-
it("should replace an SDK scope with the configured static scope", async () => {
878+
it("should normalize and replace an SDK scope with the configured static scope", async () => {
879879
setupCallbackServerMock()
880880
const provider = await McpOAuthClientProvider.create(
881881
"https://example.com/mcp",
882882
createMockSecretStorage(),
883883
undefined,
884-
{ clientId: "registered-client", scope: "openid offline_access" },
884+
{ clientId: "registered-client", scope: " openid \t offline_access " },
885885
)
886886

887887
await provider.redirectToAuthorization(
@@ -894,6 +894,41 @@ describe("McpOAuthClientProvider", () => {
894894
await provider.close()
895895
})
896896

897+
it("should use advertised scopes for a static client when scope is not configured", async () => {
898+
setupCallbackServerMock()
899+
vi.mocked(discoverOAuthProtectedResourceMetadata).mockResolvedValueOnce({
900+
resource: "https://example.com/",
901+
authorization_servers: ["https://auth.example.com"],
902+
})
903+
mockFetch.mockResolvedValueOnce({
904+
ok: true,
905+
json: () =>
906+
Promise.resolve({
907+
issuer: "https://auth.example.com",
908+
authorization_endpoint: "https://auth.example.com/authorize",
909+
token_endpoint: "https://auth.example.com/token",
910+
token_endpoint_auth_methods_supported: ["none"],
911+
grant_types_supported: ["authorization_code"],
912+
scopes_supported: ["openid", "profile"],
913+
}),
914+
})
915+
const provider = await McpOAuthClientProvider.create(
916+
"https://example.com/mcp",
917+
createMockSecretStorage(),
918+
undefined,
919+
{ clientId: "registered-client" },
920+
)
921+
922+
await provider.redirectToAuthorization(
923+
new URL("https://auth.example.com/authorize?client_id=registered-client"),
924+
)
925+
await provider.openBrowser()
926+
927+
const openedUri = vi.mocked(vscode.env.openExternal).mock.calls[0][0].toString()
928+
expect(new URL(openedUri).searchParams.get("scope")).toBe("openid profile")
929+
await provider.close()
930+
})
931+
897932
it("should remove an SDK scope when the configured static scope is empty", async () => {
898933
setupCallbackServerMock()
899934
const provider = await McpOAuthClientProvider.create(
@@ -912,6 +947,35 @@ describe("McpOAuthClientProvider", () => {
912947
expect(new URL(openedUri).searchParams.has("scope")).toBe(false)
913948
await provider.close()
914949
})
950+
951+
it("should omit a stored resource from another issuer during authorization", async () => {
952+
setupCallbackServerMock()
953+
const secretStorage = createMockSecretStorage()
954+
const serverUrl = "https://example.com/mcp"
955+
const clientId = "registered-client"
956+
await secretStorage.saveOAuthData(
957+
serverUrl,
958+
{
959+
tokens: { access_token: "access-token", token_type: "Bearer" },
960+
expires_at: Date.now() + 3600_000,
961+
issuer: "https://other.example.com",
962+
resource_indicator: "https://stored.example.com/",
963+
},
964+
clientId,
965+
)
966+
mockOAuthDiscovery()
967+
const provider = await McpOAuthClientProvider.create(serverUrl, secretStorage, undefined, { clientId })
968+
969+
await provider.redirectToAuthorization(
970+
new URL("https://auth.example.com/authorize?client_id=registered-client"),
971+
)
972+
await provider.openBrowser()
973+
974+
const openedUri = vi.mocked(vscode.env.openExternal).mock.calls[0][0].toString()
975+
expect(new URL(openedUri).searchParams.has("resource")).toBe(false)
976+
await provider.close()
977+
})
978+
915979
it("should omit an SDK resource parameter when resource is false", async () => {
916980
setupCallbackServerMock()
917981
const provider = await McpOAuthClientProvider.create(

0 commit comments

Comments
 (0)