Skip to content

Commit 5780d4d

Browse files
committed
test: enhance OAuth 2.0 Protected Resource tests for path-based resources
- Renamed `test_metadata_endpoint` to `test_metadata_endpoint_with_path` for clarity. - Added a new test `test_metadata_endpoint_root_path_returns_404` to verify 404 response for root path. - Introduced fixtures `root_resource_app` and `root_resource_client` for testing root-level resources. - Added `test_metadata_endpoint_without_path` to validate metadata retrieval for root-level resources.
1 parent d3f8564 commit 5780d4d

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

tests/server/auth/test_protected_resource.py

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ async def test_client(test_app: Starlette):
3636

3737

3838
@pytest.mark.anyio
39-
async def test_metadata_endpoint(test_client: httpx.AsyncClient):
40-
"""Test the OAuth 2.0 Protected Resource metadata endpoint."""
39+
async def test_metadata_endpoint_with_path(test_client: httpx.AsyncClient):
40+
"""Test the OAuth 2.0 Protected Resource metadata endpoint for path-based resource."""
4141

42-
response = await test_client.get("/.well-known/oauth-protected-resource")
42+
# For resource with path "/resource", metadata should be accessible at the path-aware location
43+
response = await test_client.get("/.well-known/oauth-protected-resource/resource")
4344
assert response.json() == snapshot(
4445
{
4546
"resource": "https://example.com/resource",
@@ -50,3 +51,55 @@ async def test_metadata_endpoint(test_client: httpx.AsyncClient):
5051
"bearer_methods_supported": ["header"],
5152
}
5253
)
54+
55+
56+
@pytest.mark.anyio
57+
async def test_metadata_endpoint_root_path_returns_404(test_client: httpx.AsyncClient):
58+
"""Test that root path returns 404 for path-based resource."""
59+
60+
# Root path should return 404 for path-based resources
61+
response = await test_client.get("/.well-known/oauth-protected-resource")
62+
assert response.status_code == 404
63+
64+
65+
@pytest.fixture
66+
def root_resource_app():
67+
"""Fixture to create protected resource routes for root-level resource."""
68+
69+
# Create routes for a resource without path component
70+
protected_resource_routes = create_protected_resource_routes(
71+
resource_url=AnyHttpUrl("https://example.com"),
72+
authorization_servers=[AnyHttpUrl("https://auth.example.com")],
73+
scopes_supported=["read"],
74+
resource_name="Root Resource",
75+
)
76+
77+
app = Starlette(routes=protected_resource_routes)
78+
return app
79+
80+
81+
@pytest.fixture
82+
async def root_resource_client(root_resource_app: Starlette):
83+
"""Fixture to create an HTTP client for the root resource app."""
84+
async with httpx.AsyncClient(
85+
transport=httpx.ASGITransport(app=root_resource_app), base_url="https://mcptest.com"
86+
) as client:
87+
yield client
88+
89+
90+
@pytest.mark.anyio
91+
async def test_metadata_endpoint_without_path(root_resource_client: httpx.AsyncClient):
92+
"""Test metadata endpoint for root-level resource."""
93+
94+
# For root resource, metadata should be at standard location
95+
response = await root_resource_client.get("/.well-known/oauth-protected-resource")
96+
assert response.status_code == 200
97+
assert response.json() == snapshot(
98+
{
99+
"resource": "https://example.com/",
100+
"authorization_servers": ["https://auth.example.com/"],
101+
"scopes_supported": ["read"],
102+
"resource_name": "Root Resource",
103+
"bearer_methods_supported": ["header"],
104+
}
105+
)

0 commit comments

Comments
 (0)