from fastapi.testclient import TestClient from app.integration_config import ( IntegrationConfigStore, IntegrationConfigUpdate, OpcConnectionUpdate, get_config_store, ) from app.main import app def _password_store(tmp_path): store = IntegrationConfigStore(tmp_path / "config.json", tmp_path / "secret.json") current = store.load() store.save( IntegrationConfigUpdate( source_mode="opcua", connection=OpcConnectionUpdate( **current.connection.model_dump(exclude={"password_configured"}), password="internal-secret", ), tags=current.tags, ) ) return store def test_runtime_requires_internal_token_when_password_exists(tmp_path, monkeypatch): store = _password_store(tmp_path) monkeypatch.delenv("INTEGRATION_INTERNAL_TOKEN", raising=False) app.dependency_overrides[get_config_store] = lambda: store try: response = TestClient(app).get("/api/integration/runtime") assert response.status_code == 503 assert "internal-secret" not in response.text finally: app.dependency_overrides.clear() def test_runtime_returns_password_only_with_matching_token(tmp_path, monkeypatch): store = _password_store(tmp_path) monkeypatch.setenv("INTEGRATION_INTERNAL_TOKEN", "node-red-token") app.dependency_overrides[get_config_store] = lambda: store try: client = TestClient(app) assert client.get("/api/integration/runtime").status_code == 401 response = client.get( "/api/integration/runtime", headers={"X-Integration-Token": "node-red-token"}, ) assert response.status_code == 200 assert response.json()["connection"]["password"] == "internal-secret" finally: app.dependency_overrides.clear()