"""Live station monitor page and JSON proxy (#57).""" from unittest.mock import patch import pytest @pytest.fixture def mock_api(): with patch("blueprints.monitor.api_client") as m: yield m def _login(client, roles, is_admin=False): with client.session_transaction() as sess: sess["api_key"] = "k" sess["user"] = {"id": 2, "username": "u", "display_name": "U", "roles": roles, "is_admin": is_admin, "language_pref": "en", "theme_pref": "light", "active": True} sess["language"] = "en" def test_page_ok_for_supervisor(client, mock_api): _login(client, ["Supervisor"]) resp = client.get("/monitor") assert resp.status_code == 200 and b"stationMonitor" in resp.data def test_page_forbidden_for_operator(client, mock_api): _login(client, ["MeasurementTec"]) assert client.get("/monitor").status_code == 403 assert client.get("/monitor/api/runs").status_code == 403 def test_proxy_returns_backend_json(client, mock_api): _login(client, ["Supervisor"]) mock_api.get.return_value = [{"id": 1, "station_code": "ST-1"}] resp = client.get("/monitor/api/runs") assert resp.status_code == 200 and resp.get_json()[0]["station_code"] == "ST-1" mock_api.get.assert_called_once_with("/api/production-runs/open") def test_proxy_passes_backend_error(client, mock_api): _login(client, [], is_admin=True) mock_api.get.return_value = {"error": True, "status_code": 403, "detail": "no"} resp = client.get("/monitor/api/runs") assert resp.status_code == 403 and resp.get_json()["error"] is True