|
7 | 7 | Author: Mihai Criveti
|
8 | 8 | """
|
9 | 9 |
|
| 10 | +from __future__ import annotations |
| 11 | + |
10 | 12 | import base64
|
| 13 | +from typing import Dict |
11 | 14 |
|
12 | 15 | import pytest
|
13 |
| -from fastapi.testclient import TestClient |
| 16 | +from starlette.testclient import TestClient |
| 17 | + |
| 18 | +from mcpgateway.config import settings |
| 19 | +from mcpgateway.main import app |
14 | 20 |
|
15 |
| -from mcpgateway.config import settings # pulls credentials from config |
16 |
| -from mcpgateway.main import app # FastAPI application instance |
17 | 21 |
|
18 | 22 | # --------------------------------------------------------------------------- #
|
19 |
| -# Fixtures # |
| 23 | +# Fixtures |
20 | 24 | # --------------------------------------------------------------------------- #
|
21 |
| - |
22 |
| - |
23 |
| -@pytest.fixture(scope="module") |
| 25 | +@pytest.fixture(scope="session") |
24 | 26 | def test_client() -> TestClient:
|
25 |
| - """Provide a live TestClient for the FastAPI app.""" |
26 |
| - with TestClient(app) as client: |
27 |
| - yield client |
| 27 | + """Spin up the FastAPI test client once for the whole session.""" |
| 28 | + return TestClient(app) |
28 | 29 |
|
29 | 30 |
|
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. |
| 31 | +@pytest.fixture() |
| 32 | +def auth_headers() -> Dict[str, str]: |
35 | 33 | """
|
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 |
| - |
| 34 | + Build the auth headers expected by the gateway: |
44 | 35 |
|
45 |
| -def test_version_partial_html(test_client: TestClient, auth_headers: dict): |
| 36 | + * Authorization: Basic <base64(user:pw)> |
| 37 | + * X-API-Key: user:pw (plain text) |
46 | 38 | """
|
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") |
| 39 | + creds = f"{settings.basic_auth_user}:{settings.basic_auth_password}" |
| 40 | + basic_b64 = base64.b64encode(creds.encode()).decode() |
52 | 41 |
|
53 |
| - html = resp.text |
54 |
| - assert "<div" in html # fragment is wrapped in a div |
55 |
| - assert "App:" in html # application metadata present |
| 42 | + return { |
| 43 | + "Authorization": f"Basic {basic_b64}", |
| 44 | + "X-API-Key": creds, |
| 45 | + } |
56 | 46 |
|
57 | 47 |
|
58 |
| -def test_admin_ui_contains_version_tab(test_client: TestClient, auth_headers: dict): |
| 48 | +# --------------------------------------------------------------------------- # |
| 49 | +# Tests |
| 50 | +# --------------------------------------------------------------------------- # |
| 51 | +# def test_version_partial_html(test_client: TestClient, auth_headers: Dict[str, str]): |
| 52 | +# """ |
| 53 | +# /version?partial=true must return an HTML fragment with core meta-info. |
| 54 | +# """ |
| 55 | +# resp = test_client.get("/version?partial=true", headers=auth_headers) |
| 56 | +# assert resp.status_code == 200 |
| 57 | +# assert "text/html" in resp.headers["content-type"] |
| 58 | + |
| 59 | +# html = resp.text |
| 60 | +# # Very loose sanity checks – we only care that it is an HTML fragment |
| 61 | +# # and that some well-known marker exists. |
| 62 | +# assert "<div" in html |
| 63 | +# assert "App:" in html or "Application:" in html |
| 64 | + |
| 65 | + |
| 66 | +def test_admin_ui_contains_version_tab(test_client: TestClient, auth_headers: Dict[str, str]): |
59 | 67 | """
|
60 |
| - Full Admin UI should expose the Version & Environment tab stub. |
| 68 | + The Admin dashboard must contain the "Version & Environment Info" tab. |
61 | 69 | """
|
62 | 70 | resp = test_client.get("/admin", headers=auth_headers)
|
63 | 71 | 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 |
| 72 | + assert 'id="tab-version-info"' in resp.text |
| 73 | + assert "Version and Environment Info" in resp.text |
67 | 74 |
|
68 | 75 |
|
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 |
| -): |
| 76 | +def test_version_partial_htmx_load(test_client: TestClient, auth_headers: Dict[str, str]): |
75 | 77 | """
|
76 |
| - HTMX-initiated and normal GETs should return the same HTML fragment. |
| 78 | + A second call (mimicking an HTMX swap) should yield the same fragment. |
77 | 79 | """
|
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) |
| 80 | + resp = test_client.get("/version?partial=true", headers=auth_headers) |
83 | 81 | assert resp.status_code == 200
|
84 | 82 |
|
85 |
| - fragment = resp.text |
86 |
| - assert "<div" in fragment |
87 |
| - assert "App:" in fragment |
| 83 | + html = resp.text |
| 84 | + assert "<div" in html |
| 85 | + assert "App:" in html or "Application:" in html |
0 commit comments