11
11
12
12
import httpx
13
13
import pytest
14
+ from inline_snapshot import snapshot
14
15
from pydantic import AnyHttpUrl
15
16
from starlette .applications import Starlette
16
17
@@ -197,7 +198,7 @@ def mock_oauth_provider():
197
198
198
199
199
200
@pytest .fixture
200
- def auth_app (mock_oauth_provider ):
201
+ def auth_app (mock_oauth_provider : MockOAuthProvider ):
201
202
# Create auth router
202
203
auth_routes = create_auth_routes (
203
204
mock_oauth_provider ,
@@ -218,25 +219,7 @@ def auth_app(mock_oauth_provider):
218
219
219
220
220
221
@pytest .fixture
221
- def protected_resource_app (auth_app ):
222
- """Fixture to create protected resource routes for testing."""
223
-
224
- # Create the protected resource routes
225
- protected_resource_routes = create_protected_resource_routes (
226
- resource_url = AnyHttpUrl ("https://example.com/resource" ),
227
- authorization_servers = [AnyHttpUrl ("https://auth.example.com/authorization" )],
228
- scopes_supported = ["read" , "write" ],
229
- resource_name = "Example Resource" ,
230
- resource_documentation = AnyHttpUrl ("https://docs.example.com/resource" ),
231
- )
232
-
233
- # add routes to the auth app
234
- auth_app .router .routes .extend (protected_resource_routes )
235
- return auth_app
236
-
237
-
238
- @pytest .fixture
239
- async def test_client (auth_app ):
222
+ async def test_client (auth_app : Starlette ):
240
223
async with httpx .AsyncClient (transport = httpx .ASGITransport (app = auth_app ), base_url = "https://mcptest.com" ) as client :
241
224
yield client
242
225
@@ -268,6 +251,32 @@ async def registered_client(test_client: httpx.AsyncClient, request):
268
251
return client_info
269
252
270
253
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
+
271
280
@pytest .fixture
272
281
def pkce_challenge ():
273
282
"""Create a PKCE challenge with code_verifier and code_challenge."""
@@ -354,7 +363,7 @@ class TestAuthEndpoints:
354
363
@pytest .mark .anyio
355
364
async def test_metadata_endpoint (self , test_client : httpx .AsyncClient ):
356
365
"""Test the OAuth 2.0 metadata endpoint."""
357
-
366
+
358
367
response = await test_client .get ("/.well-known/oauth-authorization-server" )
359
368
assert response .status_code == 200
360
369
@@ -1221,15 +1230,19 @@ class TestProtectedResourceMetadata:
1221
1230
"""Test the Protected Resource Metadata model."""
1222
1231
1223
1232
@pytest .mark .anyio
1224
- async def test_metadata_endpoint (self , protected_resource_app : Starlette , test_client : httpx .AsyncClient ):
1233
+ async def test_metadata_endpoint (self , protected_resource_test_client : httpx .AsyncClient ):
1225
1234
"""Test the OAuth 2.0 Protected Resource metadata endpoint."""
1226
-
1227
- response = await test_client .get ("/.well-known/oauth-protected-resource" )
1235
+
1236
+ response = await protected_resource_test_client .get ("/.well-known/oauth-protected-resource" )
1228
1237
assert response .status_code == 200
1229
1238
metadata = response .json ()
1230
- assert metadata ["resource" ] == "https://example.com/resource"
1231
- assert metadata ["authorization_servers" ] == ["https://auth.example.com/authorization" ]
1232
- assert metadata ["scopes_supported" ] == ["read" , "write" ]
1233
- assert metadata ["resource_name" ] == "Example Resource"
1234
- assert metadata ["resource_documentation" ] == "https://docs.example.com/resource"
1235
- assert metadata ["bearer_methods_supported" ] == ["header" ]
1239
+ assert metadata == snapshot (
1240
+ {
1241
+ "resource" : "https://example.com/resource" ,
1242
+ "authorization_servers" : ["https://auth.example.com/authorization" ],
1243
+ "scopes_supported" : ["read" , "write" ],
1244
+ "resource_name" : "Example Resource" ,
1245
+ "resource_documentation" : "https://docs.example.com/resource" ,
1246
+ "bearer_methods_supported" : ["header" ],
1247
+ }
1248
+ )
0 commit comments