fix(simulation): use current user for stored schemes

This commit is contained in:
2026-07-17 16:49:20 +08:00
parent 810a39a1dc
commit 9b16e4e0a5
3 changed files with 87 additions and 6 deletions
+64 -2
View File
@@ -108,6 +108,12 @@ def _load_simulation_module(monkeypatch):
)
def _build_authenticated_client(module) -> TestClient:
app = build_test_app(module.router, "/api/v1")
app.dependency_overrides[module.get_current_keycloak_username] = lambda: "alice"
return TestClient(app)
def test_run_project_endpoint_returns_plain_text(monkeypatch):
module = _load_simulation_module(monkeypatch)
monkeypatch.setattr(module, "run_project", lambda network: f"report::{network}")
@@ -339,6 +345,33 @@ def test_valve_close_endpoint_passes_scheme_name(monkeypatch):
}
def test_burst_endpoint_passes_current_username(monkeypatch):
module = _load_simulation_module(monkeypatch)
captured = {}
def fake_burst_analysis(**kwargs):
captured.update(kwargs)
monkeypatch.setattr(module, "burst_analysis", fake_burst_analysis)
client = _build_authenticated_client(module)
response = client.get(
"/api/v1/burst_analysis/",
params={
"network": "demo",
"modify_pattern_start_time": "2025-01-02T03:04:05+08:00",
"burst_ID": ["P1"],
"burst_size": [10.0],
"modify_total_duration": 900,
"scheme_name": "burst_case_01",
},
)
assert response.status_code == 200
assert response.text == '"success"'
assert captured["username"] == "alice"
def test_flushing_endpoint_passes_required_scheme_name(monkeypatch):
module = _load_simulation_module(monkeypatch)
captured = {}
@@ -348,7 +381,7 @@ def test_flushing_endpoint_passes_required_scheme_name(monkeypatch):
return "ok"
monkeypatch.setattr(module, "flushing_analysis", fake_flushing_analysis)
client = TestClient(build_test_app(module.router, "/api/v1"))
client = _build_authenticated_client(module)
response = client.get(
"/api/v1/flushing_analysis/",
@@ -374,12 +407,41 @@ def test_flushing_endpoint_passes_required_scheme_name(monkeypatch):
"drainage_node_ID": "N1",
"flushing_flow": 100.0,
"scheme_name": "flush_case_01",
"username": "alice",
}
def test_contaminant_endpoint_passes_current_username(monkeypatch):
module = _load_simulation_module(monkeypatch)
captured = {}
def fake_contaminant_simulation(**kwargs):
captured.update(kwargs)
return "ok"
monkeypatch.setattr(module, "contaminant_simulation", fake_contaminant_simulation)
client = _build_authenticated_client(module)
response = client.get(
"/api/v1/contaminant_simulation/",
params={
"network": "demo",
"start_time": "2025-01-02T03:04:05+08:00",
"source": "N1",
"concentration": 10.0,
"duration": 900,
"scheme_name": "contaminant_case_01",
},
)
assert response.status_code == 200
assert response.text == "ok"
assert captured["username"] == "alice"
def test_contaminant_endpoint_requires_scheme_name(monkeypatch):
module = _load_simulation_module(monkeypatch)
client = TestClient(build_test_app(module.router, "/api/v1"))
client = _build_authenticated_client(module)
response = client.get(
"/api/v1/contaminant_simulation/",