|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Integration tests for /version and the Version tab in the Admin UI. |
| 4 | +
|
| 5 | +Copyright 2025 |
| 6 | +SPDX-License-Identifier: Apache-2.0 |
| 7 | +Author: Mihai Criveti |
| 8 | +""" |
| 9 | + |
| 10 | +import base64 |
| 11 | + |
| 12 | +import pytest |
| 13 | +from fastapi.testclient import TestClient |
| 14 | + |
| 15 | +from mcpgateway.config import settings # pulls credentials from config |
| 16 | +from mcpgateway.main import app # FastAPI application instance |
| 17 | + |
| 18 | +# --------------------------------------------------------------------------- # |
| 19 | +# Fixtures # |
| 20 | +# --------------------------------------------------------------------------- # |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(scope="module") |
| 24 | +def test_client() -> TestClient: |
| 25 | + """Provide a live TestClient for the FastAPI app.""" |
| 26 | + with TestClient(app) as client: |
| 27 | + yield client |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def auth_headers() -> dict[str, str]: |
| 32 | + """ |
| 33 | + Build a Basic-Auth header from *current* config settings so the tests keep |
| 34 | + working even if credentials change via environment variables or .env file. |
| 35 | + """ |
| 36 | + raw = f"{settings.basic_auth_user}:{settings.basic_auth_password}".encode() |
| 37 | + return {"Authorization": "Basic " + base64.b64encode(raw).decode()} |
| 38 | + |
| 39 | + |
| 40 | +# --------------------------------------------------------------------------- # |
| 41 | +# Tests # |
| 42 | +# --------------------------------------------------------------------------- # |
| 43 | + |
| 44 | + |
| 45 | +def test_version_partial_html(test_client: TestClient, auth_headers: dict): |
| 46 | + """ |
| 47 | + /version?partial=true must return an HTML fragment with core meta-info. |
| 48 | + """ |
| 49 | + resp = test_client.get("/version?partial=true", headers=auth_headers) |
| 50 | + assert resp.status_code == 200 |
| 51 | + assert resp.headers["content-type"].startswith("text/html") |
| 52 | + |
| 53 | + html = resp.text |
| 54 | + assert "<div" in html # fragment is wrapped in a div |
| 55 | + assert "App:" in html # application metadata present |
| 56 | + |
| 57 | + |
| 58 | +def test_admin_ui_contains_version_tab(test_client: TestClient, auth_headers: dict): |
| 59 | + """ |
| 60 | + Full Admin UI should expose the Version & Environment tab stub. |
| 61 | + """ |
| 62 | + resp = test_client.get("/admin", headers=auth_headers) |
| 63 | + assert resp.status_code == 200 |
| 64 | + page = resp.text |
| 65 | + assert 'id="tab-version-info"' in page |
| 66 | + assert "Version and Environment Info" in page |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize("hx_request", [False, True]) |
| 70 | +def test_version_partial_htmx_load( |
| 71 | + test_client: TestClient, |
| 72 | + auth_headers: dict, |
| 73 | + hx_request: bool, |
| 74 | +): |
| 75 | + """ |
| 76 | + HTMX-initiated and normal GETs should return the same HTML fragment. |
| 77 | + """ |
| 78 | + headers = auth_headers.copy() |
| 79 | + if hx_request: |
| 80 | + headers["HX-Request"] = "true" # header HTMX automatically adds |
| 81 | + |
| 82 | + resp = test_client.get("/version?partial=true", headers=headers) |
| 83 | + assert resp.status_code == 200 |
| 84 | + |
| 85 | + fragment = resp.text |
| 86 | + assert "<div" in fragment |
| 87 | + assert "App:" in fragment |
0 commit comments