Skip to content

Commit 20596e5

Browse files
yannj-frpcarletonKludexfelixweinberger
authored
Add test for ProtectedResourceMetadataParsing (#1236)
Co-authored-by: Paul Carleton <[email protected]> Co-authored-by: Marcelo Trylesinski <[email protected]> Co-authored-by: Felix Weinberger <[email protected]>
1 parent 7e93a9f commit 20596e5

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Integration tests for MCP Oauth Protected Resource.
3+
"""
4+
5+
import httpx
6+
import pytest
7+
from inline_snapshot import snapshot
8+
from pydantic import AnyHttpUrl
9+
from starlette.applications import Starlette
10+
11+
from mcp.server.auth.routes import create_protected_resource_routes
12+
13+
14+
@pytest.fixture
15+
def test_app():
16+
"""Fixture to create protected resource routes for testing."""
17+
18+
# Create the protected resource routes
19+
protected_resource_routes = create_protected_resource_routes(
20+
resource_url=AnyHttpUrl("https://example.com/resource"),
21+
authorization_servers=[AnyHttpUrl("https://auth.example.com/authorization")],
22+
scopes_supported=["read", "write"],
23+
resource_name="Example Resource",
24+
resource_documentation=AnyHttpUrl("https://docs.example.com/resource"),
25+
)
26+
27+
app = Starlette(routes=protected_resource_routes)
28+
return app
29+
30+
31+
@pytest.fixture
32+
async def test_client(test_app: Starlette):
33+
"""Fixture to create an HTTP client for the protected resource app."""
34+
async with httpx.AsyncClient(transport=httpx.ASGITransport(app=test_app), base_url="https://mcptest.com") as client:
35+
yield client
36+
37+
38+
@pytest.mark.anyio
39+
async def test_metadata_endpoint(test_client: httpx.AsyncClient):
40+
"""Test the OAuth 2.0 Protected Resource metadata endpoint."""
41+
42+
response = await test_client.get("/.well-known/oauth-protected-resource")
43+
assert response.json() == snapshot(
44+
{
45+
"resource": "https://example.com/resource",
46+
"authorization_servers": ["https://auth.example.com/authorization"],
47+
"scopes_supported": ["read", "write"],
48+
"resource_name": "Example Resource",
49+
"resource_documentation": "https://docs.example.com/resource",
50+
"bearer_methods_supported": ["header"],
51+
}
52+
)

tests/server/fastmcp/auth/test_auth_integration.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,8 @@ class TestAuthEndpoints:
342342
@pytest.mark.anyio
343343
async def test_metadata_endpoint(self, test_client: httpx.AsyncClient):
344344
"""Test the OAuth 2.0 metadata endpoint."""
345-
print("Sending request to metadata endpoint")
345+
346346
response = await test_client.get("/.well-known/oauth-authorization-server")
347-
print(f"Got response: {response.status_code}")
348-
if response.status_code != 200:
349-
print(f"Response content: {response.content}")
350347
assert response.status_code == 200
351348

352349
metadata = response.json()
@@ -399,9 +396,7 @@ async def test_token_invalid_auth_code(
399396
"redirect_uri": "https://client.example.com/callback",
400397
},
401398
)
402-
print(f"Status code: {response.status_code}")
403-
print(f"Response body: {response.content}")
404-
print(f"Response JSON: {response.json()}")
399+
405400
assert response.status_code == 400
406401
error_response = response.json()
407402
assert error_response["error"] == "invalid_grant"

0 commit comments

Comments
 (0)