Skip to content

Commit 154b754

Browse files
committed
separate test in a new file
1 parent 1fea1f3 commit 154b754

File tree

2 files changed

+57
-47
lines changed

2 files changed

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

tests/server/fastmcp/auth/test_auth_integration.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
ClientRegistrationOptions,
2828
RevocationOptions,
2929
create_auth_routes,
30-
create_protected_resource_routes,
3130
)
3231
from mcp.shared.auth import (
3332
OAuthClientInformationFull,
@@ -251,32 +250,6 @@ async def registered_client(test_client: httpx.AsyncClient, request):
251250
return client_info
252251

253252

254-
@pytest.fixture
255-
def protected_resource_app():
256-
"""Fixture to create protected resource routes for testing."""
257-
258-
# Create the protected resource routes
259-
protected_resource_routes = create_protected_resource_routes(
260-
resource_url=AnyHttpUrl("https://example.com/resource"),
261-
authorization_servers=[AnyHttpUrl("https://auth.example.com/authorization")],
262-
scopes_supported=["read", "write"],
263-
resource_name="Example Resource",
264-
resource_documentation=AnyHttpUrl("https://docs.example.com/resource"),
265-
)
266-
267-
app = Starlette(routes=protected_resource_routes)
268-
return app
269-
270-
271-
@pytest.fixture
272-
async def protected_resource_test_client(protected_resource_app: Starlette):
273-
"""Fixture to create an HTTP client for the protected resource app."""
274-
async with httpx.AsyncClient(
275-
transport=httpx.ASGITransport(app=protected_resource_app), base_url="https://mcptest.com"
276-
) as client:
277-
yield client
278-
279-
280253
@pytest.fixture
281254
def pkce_challenge():
282255
"""Create a PKCE challenge with code_verifier and code_challenge."""
@@ -1225,23 +1198,3 @@ async def test_authorize_invalid_scope(self, test_client: httpx.AsyncClient, reg
12251198
assert "state" in query_params
12261199
assert query_params["state"][0] == "test_state"
12271200

1228-
1229-
class TestProtectedResourceMetadata:
1230-
"""Test the Protected Resource Metadata model."""
1231-
1232-
@pytest.mark.anyio
1233-
async def test_metadata_endpoint(self, protected_resource_test_client: httpx.AsyncClient):
1234-
"""Test the OAuth 2.0 Protected Resource metadata endpoint."""
1235-
1236-
response = await protected_resource_test_client.get("/.well-known/oauth-protected-resource")
1237-
metadata = response.json()
1238-
assert metadata == snapshot(
1239-
{
1240-
"resource": "https://example.com/resource",
1241-
"authorization_servers": ["https://auth.example.com/authorization"],
1242-
"scopes_supported": ["read", "write"],
1243-
"resource_name": "Example Resource",
1244-
"resource_documentation": "https://docs.example.com/resource",
1245-
"bearer_methods_supported": ["header"],
1246-
}
1247-
)

0 commit comments

Comments
 (0)