77
88import base64
99import json
10- import sys
1110import os
11+ import sys
1212import urllib .parse
1313from collections .abc import AsyncGenerator
1414from typing import Any
2020@pytest .fixture
2121def proxy_server (monkeypatch ):
2222 """Import the proxy OAuth demo server with safe environment + stubs."""
23- import os
2423
2524 # Avoid real outbound calls by pretending the upstream endpoints were
2625 # supplied explicitly via env vars – this makes `fetch_upstream_metadata`
2726 # construct metadata locally instead of performing an HTTP GET.
28- os .environ .setdefault ("UPSTREAM_AUTHORIZATION_ENDPOINT" , "https://upstream.example.com/authorize" )
29- os .environ .setdefault ("UPSTREAM_TOKEN_ENDPOINT" , "https://upstream.example.com/token" )
27+ os .environ .setdefault (
28+ "UPSTREAM_AUTHORIZATION_ENDPOINT" , "https://upstream.example.com/authorize"
29+ )
30+ os .environ .setdefault (
31+ "UPSTREAM_TOKEN_ENDPOINT" , "https://upstream.example.com/token"
32+ )
3033 os .environ .setdefault ("UPSTREAM_JWKS_URI" , "https://upstream.example.com/jwks" )
3134 os .environ .setdefault ("UPSTREAM_CLIENT_ID" , "client123" )
3235 os .environ .setdefault ("UPSTREAM_CLIENT_SECRET" , "secret123" )
3336
3437 # Deferred import so the env vars above are in effect.
35- from proxy_auth import combo_server as proxy_server_module
36-
3738 # Stub library-level fetch_upstream_metadata to avoid network I/O.
3839 from mcp .server .auth .proxy import routes as proxy_routes
3940
40- < << << << Updated upstream
41- == == == =
4241 # Handle imports whether running from root or project directory
4342 try :
4443 # Try direct import first (when running from project directory)
@@ -51,7 +50,6 @@ def proxy_server(monkeypatch):
5150 sys .path .insert (0 , project_dir )
5251 from proxy_auth import combo_server as proxy_server_module
5352
54- >> >> >> > Stashed changes
5553 async def _fake_metadata () -> dict [str , Any ]: # noqa: D401
5654 return {
5755 "issuer" : proxy_server_module .UPSTREAM_BASE ,
@@ -61,7 +59,9 @@ async def _fake_metadata() -> dict[str, Any]: # noqa: D401
6159 "jwks_uri" : "" ,
6260 }
6361
64- monkeypatch .setattr (proxy_routes , "fetch_upstream_metadata ", _fake_metadata , raising = True )
62+ monkeypatch .setattr (
63+ proxy_routes , "fetch_upstream_metadata" , _fake_metadata , raising = True
64+ )
6565 return proxy_server_module
6666
6767
@@ -74,7 +74,9 @@ def app(proxy_server):
7474@pytest .fixture
7575async def client (app ) -> AsyncGenerator [httpx .AsyncClient , None ]:
7676 """Async HTTP client bound to the in-memory ASGI application."""
77- async with httpx .AsyncClient (transport = httpx .ASGITransport (app = app ), base_url = "http :// testserver ") as c :
77+ async with httpx .AsyncClient (
78+ transport = httpx .ASGITransport (app = app ), base_url = "http://testserver"
79+ ) as c :
7880 yield c
7981
8082
@@ -122,7 +124,9 @@ async def test_authorize_redirect(client, proxy_server):
122124 location = r .headers ["location" ]
123125 parsed = urllib .parse .urlparse (location )
124126 assert parsed .scheme .startswith ("http" )
125- assert parsed .netloc == urllib .parse .urlparse (proxy_server .UPSTREAM_AUTHORIZE ).netloc
127+ assert (
128+ parsed .netloc == urllib .parse .urlparse (proxy_server .UPSTREAM_AUTHORIZE ).netloc
129+ )
126130
127131 qs = urllib .parse .parse_qs (parsed .query )
128132 # Proxy should inject client_id & default scope
@@ -139,7 +143,8 @@ async def test_revoke_proxy(client, monkeypatch, proxy_server):
139143 async def _mock_post (self , url , data = None , timeout = 10 , ** kwargs ): # noqa: D401
140144 if url .endswith ("/revoke" ):
141145 return httpx .Response (200 , json = {"revoked" : True })
142- # For the test client's own request to /revoke, delegate to original implementation
146+ # For the test client's own request to /revoke,
147+ # delegate to original implementation
143148 return await original_post (self , url , data = data , timeout = timeout , ** kwargs )
144149
145150 monkeypatch .setattr (httpx .AsyncClient , "post" , _mock_post , raising = True )
@@ -220,9 +225,13 @@ async def test_user_info_tool(monkeypatch, proxy_server):
220225 from mcp .server .auth .provider import AccessToken # local import to avoid cycles
221226
222227 def _fake_get_access_token (): # noqa: D401
223- return AccessToken (token = dummy_token , client_id = "client123 ", scopes = ["openid "], expires_at = None )
228+ return AccessToken (
229+ token = dummy_token , client_id = "client123" , scopes = ["openid" ], expires_at = None
230+ )
224231
225- monkeypatch .setattr (auth_context , "get_access_token ", _fake_get_access_token , raising = True )
232+ monkeypatch .setattr (
233+ auth_context , "get_access_token" , _fake_get_access_token , raising = True
234+ )
226235
227236 result = await proxy_server .mcp .call_tool ("user_info" , {})
228237
@@ -233,5 +242,7 @@ def _fake_get_access_token(): # noqa: D401
233242 raw = result # fallback
234243
235244 assert raw ["authenticated" ] is True
236- assert ("userid " in raw and raw ["userid "] == "test - user ") or ("user_id " in raw and raw ["user_id "] == "test - user ")
237- assert raw ["username "] == "tester "
245+ assert ("userid" in raw and raw ["userid" ] == "test-user" ) or (
246+ "user_id" in raw and raw ["user_id" ] == "test-user"
247+ )
248+ assert raw ["username" ] == "tester"
0 commit comments