|
| 1 | +from fastapi import FastAPI, Request |
| 2 | +from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware |
| 3 | + |
| 4 | +from app.middlewares import setup_middleware |
| 5 | +from app.routers.admin import get_client_ip |
| 6 | +from config import server_settings |
| 7 | +from tests.api import client |
| 8 | + |
| 9 | + |
| 10 | +def test_get_client_ip_no_proxy(): |
| 11 | + """Test that get_client_ip returns the direct client host when no proxy is involved.""" |
| 12 | + # Use a real Request object with a minimal ASGI scope |
| 13 | + scope = { |
| 14 | + "type": "http", |
| 15 | + "client": ("1.1.1.1", 12345), |
| 16 | + "headers": [], |
| 17 | + } |
| 18 | + request = Request(scope=scope) |
| 19 | + assert get_client_ip(request) == "1.1.1.1" |
| 20 | + |
| 21 | + |
| 22 | +def test_get_client_ip_with_proxy_middleware_behavior(access_token): |
| 23 | + """ |
| 24 | + Test the behavior of IP detection via the TestClient. |
| 25 | + Since we enabled proxy_headers in conftest.py, the middleware should process X-Forwarded-For. |
| 26 | + """ |
| 27 | + |
| 28 | + # We use an endpoint that returns the IP, like /api/admin/token (indirectly via notification) |
| 29 | + # or we can check a subscription info endpoint which also uses request.client.host |
| 30 | + |
| 31 | + # In tests/conftest.py we set: |
| 32 | + # server_settings.proxy_headers = True |
| 33 | + # server_settings.forwarded_allow_ips = "*" |
| 34 | + |
| 35 | + # This means X-Forwarded-For should be trusted. |
| 36 | + ip = "203.0.113.10" |
| 37 | + # We use a known endpoint that returns IP in response for easy verification |
| 38 | + # Based on grep, user_subscription_info returns IP |
| 39 | + |
| 40 | + from tests.api.helpers import ( |
| 41 | + create_core, |
| 42 | + create_group, |
| 43 | + create_user, |
| 44 | + delete_core, |
| 45 | + delete_group, |
| 46 | + delete_user, |
| 47 | + ) |
| 48 | + |
| 49 | + core = create_core(access_token) |
| 50 | + group = create_group(access_token) |
| 51 | + user = create_user( |
| 52 | + access_token, |
| 53 | + group_ids=[group["id"]], |
| 54 | + payload={"username": "iptestuser"}, |
| 55 | + ) |
| 56 | + try: |
| 57 | + response = client.get(f"{user['subscription_url']}/info", headers={"X-Forwarded-For": ip}) |
| 58 | + assert response.status_code == 200 |
| 59 | + # The subscription router uses request.client.host |
| 60 | + # If ProxyHeadersMiddleware is working, it will swap request.client.host with the value from X-Forwarded-For |
| 61 | + assert response.json()["ip"] == ip |
| 62 | + finally: |
| 63 | + delete_user(access_token, "iptestuser") |
| 64 | + delete_group(access_token, group["id"]) |
| 65 | + delete_core(access_token, core["id"]) |
| 66 | + |
| 67 | + |
| 68 | +def test_proxy_headers_disabled_logic(): |
| 69 | + """ |
| 70 | + Verify that if proxy_headers was False, the middleware wouldn't be added. |
| 71 | + This is a logic check on the middleware setup. |
| 72 | + """ |
| 73 | + |
| 74 | + app = FastAPI() |
| 75 | + original_val = server_settings.proxy_headers |
| 76 | + try: |
| 77 | + server_settings.proxy_headers = False |
| 78 | + setup_middleware(app) |
| 79 | + |
| 80 | + # Check if ProxyHeadersMiddleware is in the app.user_middleware list |
| 81 | + # Note: Middleware are wrapped, but we can check the classes |
| 82 | + middleware_classes = [m.cls for m in app.user_middleware] |
| 83 | + assert ProxyHeadersMiddleware not in middleware_classes |
| 84 | + |
| 85 | + # Now enable it |
| 86 | + app_with_proxy = FastAPI() |
| 87 | + server_settings.proxy_headers = True |
| 88 | + setup_middleware(app_with_proxy) |
| 89 | + middleware_classes_proxy = [m.cls for m in app_with_proxy.user_middleware] |
| 90 | + assert ProxyHeadersMiddleware in middleware_classes_proxy |
| 91 | + finally: |
| 92 | + server_settings.proxy_headers = original_val |
0 commit comments