fix(topology): secure and unify demo serving
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from topology_api.app import create_app
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
HTML_PATH = ROOT / "data" / "wiki" / "guanghan_topology_demo.html"
|
||||
CONFIG_PATH = ROOT / "topology_api" / "config.py"
|
||||
|
||||
|
||||
class TopologyApiServingTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
app = create_app()
|
||||
app.config.update(TESTING=True)
|
||||
cls.client = app.test_client()
|
||||
|
||||
def test_app_serves_demo_page_and_health(self) -> None:
|
||||
health = self.client.get("/health")
|
||||
self.assertEqual(health.status_code, 200)
|
||||
self.assertEqual(health.get_json(), {"status": "ok"})
|
||||
|
||||
page = self.client.get("/guanghan_topology_demo.html")
|
||||
self.assertEqual(page.status_code, 200)
|
||||
self.assertIn("广寒基地拓扑结构", page.get_data(as_text=True))
|
||||
page.close()
|
||||
|
||||
def test_page_uses_same_origin_api(self) -> None:
|
||||
html = HTML_PATH.read_text(encoding="utf-8")
|
||||
self.assertNotIn("http://localhost:5000", html)
|
||||
self.assertIn("const API_BASE = ''", html)
|
||||
|
||||
def test_config_has_no_default_password(self) -> None:
|
||||
source = CONFIG_PATH.read_text(encoding="utf-8")
|
||||
self.assertNotRegex(
|
||||
source,
|
||||
r'os\.environ\.get\(\s*["\']PGPASSWORD["\']\s*,',
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user