|
| 1 | +import httpx |
| 2 | +from fastmcp.server.http import set_http_request |
| 3 | +from starlette.requests import Request |
| 4 | + |
| 5 | +from flagsmith_mcp import auth |
| 6 | + |
| 7 | + |
| 8 | +def test_flagsmith_auth__header_in_http_request__forwards_to_upstream() -> None: |
| 9 | + # Given an inbound HTTP request carrying an Authorization header, exactly as |
| 10 | + # FastMCP's RequestContextMiddleware sets it under HTTP transport (--header) |
| 11 | + inbound = Request( |
| 12 | + {"type": "http", "headers": [(b"authorization", b"Api-Key caller")]} |
| 13 | + ) |
| 14 | + upstream = httpx.Request("GET", "https://api.flagsmith.com/api/v1/organisations/") |
| 15 | + |
| 16 | + # When the upstream auth flow runs within that request context |
| 17 | + with set_http_request(inbound): |
| 18 | + next(auth.FlagsmithAuth().auth_flow(upstream)) |
| 19 | + |
| 20 | + # Then the real get_http_headers picks it up and forwards it |
| 21 | + assert upstream.headers["authorization"] == "Api-Key caller" |
| 22 | + |
| 23 | + |
| 24 | +def test_flagsmith_auth__no_http_request__leaves_upstream_unchanged() -> None: |
| 25 | + # Given no active HTTP request (e.g. stdio transport) |
| 26 | + upstream = httpx.Request("GET", "https://api.flagsmith.com/api/v1/organisations/") |
| 27 | + |
| 28 | + # When |
| 29 | + next(auth.FlagsmithAuth().auth_flow(upstream)) |
| 30 | + |
| 31 | + # Then nothing is forwarded |
| 32 | + assert "authorization" not in upstream.headers |
| 33 | + |
| 34 | + |
| 35 | +def test_flagsmith_auth__static_token_no_http_request__uses_api_key() -> None: |
| 36 | + # Given a static token and no active HTTP request (e.g. stdio transport) |
| 37 | + upstream = httpx.Request("GET", "https://api.flagsmith.com/api/v1/organisations/") |
| 38 | + |
| 39 | + # When |
| 40 | + next(auth.FlagsmithAuth("ser.secret").auth_flow(upstream)) |
| 41 | + |
| 42 | + # Then the server's own token is sent as an Api-Key credential |
| 43 | + assert upstream.headers["authorization"] == "Api-Key ser.secret" |
| 44 | + |
| 45 | + |
| 46 | +def test_flagsmith_auth__forwarded_header__wins_over_static_token() -> None: |
| 47 | + # Given both a forwarded caller header and a static token |
| 48 | + inbound = Request( |
| 49 | + {"type": "http", "headers": [(b"authorization", b"Api-Key caller")]} |
| 50 | + ) |
| 51 | + upstream = httpx.Request("GET", "https://api.flagsmith.com/api/v1/organisations/") |
| 52 | + |
| 53 | + # When |
| 54 | + with set_http_request(inbound): |
| 55 | + next(auth.FlagsmithAuth("ser.secret").auth_flow(upstream)) |
| 56 | + |
| 57 | + # Then the caller's forwarded credential takes precedence |
| 58 | + assert upstream.headers["authorization"] == "Api-Key caller" |
| 59 | + |
| 60 | + |
| 61 | +def test_flagsmith_auth__upstream_already_authorized__does_not_override() -> None: |
| 62 | + # Given an upstream request that already carries a credential, and an inbound |
| 63 | + # header that differs |
| 64 | + inbound = Request( |
| 65 | + {"type": "http", "headers": [(b"authorization", b"Api-Key context")]} |
| 66 | + ) |
| 67 | + upstream = httpx.Request( |
| 68 | + "GET", |
| 69 | + "https://api.flagsmith.com/api/v1/organisations/", |
| 70 | + headers={"authorization": "Api-Key static"}, |
| 71 | + ) |
| 72 | + |
| 73 | + # When |
| 74 | + with set_http_request(inbound): |
| 75 | + next(auth.FlagsmithAuth().auth_flow(upstream)) |
| 76 | + |
| 77 | + # Then the existing credential wins |
| 78 | + assert upstream.headers["authorization"] == "Api-Key static" |
0 commit comments