|
| 1 | +import pytest |
| 2 | +import importlib |
| 3 | + |
| 4 | +from starlette.testclient import TestClient |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def clear_env(monkeypatch): |
| 8 | + """ |
| 9 | + A helper fixture to remove/clear environment variables before each test |
| 10 | + so each test starts from a known baseline. |
| 11 | + """ |
| 12 | + monkeypatch.delenv("HASSIO_MODE", raising=False) |
| 13 | + monkeypatch.delenv("HASSIO_INGRESS_PATH", raising=False) |
| 14 | + |
| 15 | + |
| 16 | +def _reload_app_and_config(): |
| 17 | + """ |
| 18 | + Helper that re-imports get_config and reloads app.fastapi. |
| 19 | +
|
| 20 | + This helps ensure that changing environment variables |
| 21 | + re-applies to the loaded config and routes. |
| 22 | + """ |
| 23 | + # Force a re-import of get_config |
| 24 | + from app import config |
| 25 | + importlib.reload(config) |
| 26 | + # Force a reload of the entire FastAPI module |
| 27 | + from app import fastapi |
| 28 | + importlib.reload(fastapi) |
| 29 | + # Return the reloaded references |
| 30 | + return fastapi.app, config.get_config() |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.asyncio |
| 34 | +async def test_no_hassio_env(clear_env): |
| 35 | + """ |
| 36 | + With no HASSIO_MODE and no HASSIO_INGRESS_PATH set, |
| 37 | + we expect HASSIO_MODE=None, HASSIO_INGRESS_PATH=None, and |
| 38 | + routes are just "/api" for the API + root HTML at "/". |
| 39 | + """ |
| 40 | + app, conf = _reload_app_and_config() |
| 41 | + |
| 42 | + assert conf.HASSIO_MODE is None |
| 43 | + assert conf.HASSIO_INGRESS_PATH is None |
| 44 | + assert conf.base_path == "" |
| 45 | + |
| 46 | + client = TestClient(app) |
| 47 | + |
| 48 | + # /api route should work normally |
| 49 | + resp_api = client.get("/api/frames") |
| 50 | + assert resp_api.status_code != 404, "API route at /api should be accessible." |
| 51 | + |
| 52 | + # Root HTML |
| 53 | + resp_root = client.get("/") |
| 54 | + assert resp_root.status_code == 200, "Root HTML / should be served normally." |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.asyncio |
| 58 | +async def test_hassio_mode_public(clear_env, monkeypatch): |
| 59 | + """ |
| 60 | + HASSIO_MODE=public means we only mount /api/log as a 'public' router |
| 61 | + and do *not* serve the normal HTML routes. |
| 62 | + """ |
| 63 | + monkeypatch.setenv("HASSIO_MODE", "public") |
| 64 | + |
| 65 | + app, conf = _reload_app_and_config() |
| 66 | + |
| 67 | + assert conf.HASSIO_MODE == "public" |
| 68 | + assert conf.HASSIO_INGRESS_PATH is None |
| 69 | + assert conf.base_path == "" |
| 70 | + |
| 71 | + client = TestClient(app) |
| 72 | + |
| 73 | + # The /api route is still there |
| 74 | + resp_api = client.get("/api/frames") |
| 75 | + assert resp_api.status_code == 404, "/api route should not be accessible in public mode." |
| 76 | + |
| 77 | + resp_api = client.post("/api/log") |
| 78 | + assert resp_api.status_code != 404, "/api/log route should still be accessible in public mode." |
| 79 | + |
| 80 | + # But the root HTML route ("/") won't be mounted. |
| 81 | + # We expect e.g. a 404 or similar because serve_html = False in "public" mode. |
| 82 | + resp_root = client.get("/") |
| 83 | + assert resp_root.status_code == 404, ( |
| 84 | + "In public (ingress-public) mode, the HTML root (/) is typically not served." |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.asyncio |
| 89 | +async def test_hassio_mode_ingress(clear_env, monkeypatch): |
| 90 | + """ |
| 91 | + HASSIO_MODE=ingress with a HASSIO_INGRESS_PATH means the 'base_path' |
| 92 | + is set to that path. Our APIs and HTML routes are prefixed with that base. |
| 93 | + """ |
| 94 | + custom_ingress = "/my_ingress_path" |
| 95 | + monkeypatch.setenv("HASSIO_MODE", "ingress") |
| 96 | + monkeypatch.setenv("HASSIO_INGRESS_PATH", custom_ingress) |
| 97 | + |
| 98 | + app, conf = _reload_app_and_config() |
| 99 | + |
| 100 | + assert conf.HASSIO_MODE == "ingress" |
| 101 | + assert conf.HASSIO_INGRESS_PATH == custom_ingress |
| 102 | + assert conf.base_path == custom_ingress |
| 103 | + |
| 104 | + client = TestClient(app) |
| 105 | + |
| 106 | + # Check that /my_ingress_path/api is the new route |
| 107 | + resp_api = client.get(f"{custom_ingress}/api/frames") |
| 108 | + assert resp_api.status_code != 404, ( |
| 109 | + f"Expected to find the /api routes under base_path={custom_ingress}." |
| 110 | + ) |
| 111 | + |
| 112 | + # Root HTML is also served at /my_ingress_path |
| 113 | + resp_root = client.get(custom_ingress) |
| 114 | + assert resp_root.status_code == 200, "HTML root should be served at /{ingress_path} in ingress mode." |
| 115 | + |
| 116 | + # Check that /api (without the ingress path) returns 404 |
| 117 | + # because in ingress mode, everything is behind the prefix |
| 118 | + resp_api_no_prefix = client.get("/api/frames") |
| 119 | + assert resp_api_no_prefix.status_code == 404, ( |
| 120 | + "In ingress mode, /api/* should not exist without the base path prefix." |
| 121 | + ) |
0 commit comments