From ba41b499ded1f6ab6dc32343501b73b768b18a4a Mon Sep 17 00:00:00 2001 From: Sylvain Leclerc Date: Wed, 11 Sep 2024 11:29:53 +0200 Subject: [PATCH] fix(front): add unit tests for standalone mode logic Signed-off-by: Sylvain Leclerc --- tests/test_front.py | 101 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 tests/test_front.py diff --git a/tests/test_front.py b/tests/test_front.py new file mode 100644 index 0000000000..4dbc162e7b --- /dev/null +++ b/tests/test_front.py @@ -0,0 +1,101 @@ +from pathlib import Path + +import pytest +from fastapi import FastAPI +from starlette.testclient import TestClient + +from antarest.front import RedirectMiddleware, add_front_app + + +@pytest.fixture +def base_back_app() -> FastAPI: + """ + A simple app which has only one backend endpoint + """ + app = FastAPI(title=__name__) + + @app.get(path="/api/a-backend-endpoint") + def get_from_api() -> str: + return "back" + + return app + + +@pytest.fixture +def resources_dir(tmp_path: Path) -> Path: + resource_dir = tmp_path / "resources" + resource_dir.mkdir() + webapp_dir = resource_dir / "webapp" + webapp_dir.mkdir() + with open(webapp_dir / "index.html", mode="w") as f: + f.write("index") + with open(webapp_dir / "front.css", mode="w") as f: + f.write("css") + return resource_dir + + +@pytest.fixture +def app_with_home(base_back_app) -> FastAPI: + """ + A simple app which has only a home endpoint and one backend endpoint + """ + + @base_back_app.get(path="/") + def home() -> str: + return "home" + + return base_back_app + + +@pytest.fixture +def redirect_app(app_with_home: FastAPI) -> FastAPI: + """ + Same as app with redirect middleware + """ + route_paths = [r.path for r in app_with_home.routes] # type: ignore + app_with_home.add_middleware(RedirectMiddleware, route_paths=route_paths) + return app_with_home + + +def test_redirect_middleware_does_not_modify_home(redirect_app: FastAPI) -> None: + # A simple app which has only a home endpoint and one backend endpoint + client = TestClient(redirect_app) + response = client.get("/") + assert response.status_code == 200 + assert response.json() == "home" + + +def test_redirect_middleware_redirects_unknown_routes_to_home(redirect_app: FastAPI) -> None: + # A simple app which has only a home endpoint and one backend endpoint + client = TestClient(redirect_app) + response = client.get("/a-front-route") + assert response.status_code == 200 + assert response.json() == "home" + + +def test_redirect_middleware_does_not_redirect_backend_routes(redirect_app: FastAPI) -> None: + client = TestClient(redirect_app) + response = client.get("/api/a-backend-endpoint") + assert response.status_code == 200 + assert response.json() == "back" + + +def test_frontend_paths(base_back_app, resources_dir: Path) -> None: + add_front_app(base_back_app, resources_dir, "/api") + client = TestClient(base_back_app) + + config_response = client.get("/config.json") + assert config_response.status_code == 200 + assert config_response.json() == {"restEndpoint": "/api", "wsEndpoint": "/api/ws"} + + index_response = client.get("/index.html") + assert index_response.status_code == 200 + assert index_response.text == "index" + + front_route_response = client.get("/any-route") + assert front_route_response.status_code == 200 + assert front_route_response.text == "index" + + front_static_file_response = client.get("/static/front.css") + assert front_static_file_response.status_code == 200 + assert front_static_file_response.text == "css"