|
| 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 | + ) |
0 commit comments