Skip to content

Commit 317eed0

Browse files
committed
fixes
1 parent aa993f7 commit 317eed0

2 files changed

Lines changed: 97 additions & 90 deletions

File tree

tests/cassettes.db

224 KB
Binary file not shown.

tests/core/test_base_client.py

Lines changed: 97 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ def platform_env_vars():
103103
}
104104

105105

106+
@pytest.fixture
107+
def mock_platform_auth():
108+
"""Context manager that patches get_auth_data and parse_access_token for PlatformSettings tests."""
109+
mock_auth_data = MagicMock()
110+
mock_auth_data.access_token = "test-access-token"
111+
mock_auth_data.refresh_token = None
112+
113+
with (
114+
patch(
115+
"uipath.llm_client.settings.platform.settings.get_auth_data",
116+
return_value=mock_auth_data,
117+
),
118+
patch(
119+
"uipath.llm_client.settings.platform.settings.parse_access_token",
120+
return_value={"client_id": "test-client-id"},
121+
),
122+
):
123+
yield
124+
125+
106126
@pytest.fixture
107127
def passthrough_api_config():
108128
"""API config for passthrough mode."""
@@ -197,37 +217,32 @@ def test_api_flavor_and_version(self):
197217
class TestSettingsFactory:
198218
"""Tests for get_default_client_settings factory function."""
199219

200-
def test_default_returns_agenthub(self, platform_env_vars):
220+
def test_default_returns_agenthub(self, platform_env_vars, mock_platform_auth):
201221
"""Test that default backend is agenthub."""
202-
with patch.dict(os.environ, platform_env_vars, clear=False):
203-
# Remove UIPATH_LLM_SERVICE if set
204-
env = {**platform_env_vars}
205-
env.pop("UIPATH_LLM_SERVICE", None)
206-
with patch.dict(os.environ, env, clear=True):
207-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
208-
settings = get_default_client_settings()
209-
assert isinstance(settings, PlatformSettings)
210-
211-
def test_explicit_agenthub(self, platform_env_vars):
222+
env = {**platform_env_vars}
223+
env.pop("UIPATH_LLM_SERVICE", None)
224+
with patch.dict(os.environ, env, clear=True):
225+
settings = get_default_client_settings()
226+
assert isinstance(settings, PlatformSettings)
227+
228+
def test_explicit_agenthub(self, platform_env_vars, mock_platform_auth):
212229
"""Test explicit agenthub backend."""
213230
with patch.dict(os.environ, platform_env_vars, clear=True):
214-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
215-
settings = get_default_client_settings(backend="agenthub")
216-
assert isinstance(settings, PlatformSettings)
231+
settings = get_default_client_settings(backend="agenthub")
232+
assert isinstance(settings, PlatformSettings)
217233

218234
def test_explicit_llmgateway(self, llmgw_env_vars):
219235
"""Test explicit llmgateway backend."""
220236
with patch.dict(os.environ, llmgw_env_vars, clear=True):
221237
settings = get_default_client_settings(backend="llmgateway")
222238
assert isinstance(settings, LLMGatewaySettings)
223239

224-
def test_env_var_agenthub(self, platform_env_vars):
240+
def test_env_var_agenthub(self, platform_env_vars, mock_platform_auth):
225241
"""Test UIPATH_LLM_SERVICE=agenthub from environment."""
226242
env = {**platform_env_vars, "UIPATH_LLM_SERVICE": "agenthub"}
227243
with patch.dict(os.environ, env, clear=True):
228-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
229-
settings = get_default_client_settings()
230-
assert isinstance(settings, PlatformSettings)
244+
settings = get_default_client_settings()
245+
assert isinstance(settings, PlatformSettings)
231246

232247
def test_env_var_llmgateway(self, llmgw_env_vars):
233248
"""Test UIPATH_LLM_SERVICE=llmgateway from environment."""
@@ -466,37 +481,34 @@ def test_auth_singleton_reuses_instance(self, llmgw_env_vars):
466481
class TestPlatformSettings:
467482
"""Tests for PlatformSettings."""
468483

469-
def test_build_base_url_passthrough(self, platform_env_vars, passthrough_api_config):
484+
def test_build_base_url_passthrough(self, platform_env_vars, mock_platform_auth, passthrough_api_config):
470485
"""Test build_base_url for passthrough mode."""
471486
with patch.dict(os.environ, platform_env_vars, clear=True):
472-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
473-
settings = PlatformSettings()
474-
url = settings.build_base_url(
475-
model_name="gpt-4o",
476-
api_config=passthrough_api_config,
477-
)
478-
assert "agenthub_/llm/raw/vendor/openai/model/gpt-4o/completions" in url
479-
480-
def test_build_base_url_normalized(self, platform_env_vars, normalized_api_config):
487+
settings = PlatformSettings()
488+
url = settings.build_base_url(
489+
model_name="gpt-4o",
490+
api_config=passthrough_api_config,
491+
)
492+
assert "agenthub_/llm/raw/vendor/openai/model/gpt-4o/completions" in url
493+
494+
def test_build_base_url_normalized(self, platform_env_vars, mock_platform_auth, normalized_api_config):
481495
"""Test build_base_url for normalized mode."""
482496
with patch.dict(os.environ, platform_env_vars, clear=True):
483-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
484-
settings = PlatformSettings()
485-
url = settings.build_base_url(
486-
model_name="gpt-4o",
487-
api_config=normalized_api_config,
488-
)
489-
assert "agenthub_/llm/api/chat/completions" in url
490-
491-
def test_build_auth_headers_has_default_config(self, platform_env_vars):
497+
settings = PlatformSettings()
498+
url = settings.build_base_url(
499+
model_name="gpt-4o",
500+
api_config=normalized_api_config,
501+
)
502+
assert "agenthub_/llm/api/chat/completions" in url
503+
504+
def test_build_auth_headers_has_default_config(self, platform_env_vars, mock_platform_auth):
492505
"""Test build_auth_headers includes default agenthub_config."""
493506
with patch.dict(os.environ, platform_env_vars, clear=True):
494-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
495-
settings = PlatformSettings()
496-
headers = settings.build_auth_headers()
497-
assert headers == {"X-UiPath-AgentHub-Config": "agentsruntime"}
507+
settings = PlatformSettings()
508+
headers = settings.build_auth_headers()
509+
assert headers == {"X-UiPath-AgentHub-Config": "agentsruntime"}
498510

499-
def test_build_auth_headers_with_tracing(self, platform_env_vars):
511+
def test_build_auth_headers_with_tracing(self, platform_env_vars, mock_platform_auth):
500512
"""Test build_auth_headers includes tracing headers when set."""
501513
env = {
502514
**platform_env_vars,
@@ -505,22 +517,20 @@ def test_build_auth_headers_with_tracing(self, platform_env_vars):
505517
"UIPATH_JOB_KEY": "test-job",
506518
}
507519
with patch.dict(os.environ, env, clear=True):
508-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
509-
settings = PlatformSettings()
510-
headers = settings.build_auth_headers()
511-
assert headers["X-UiPath-AgentHub-Config"] == "test-config"
512-
assert headers["X-UiPath-ProcessKey"] == "test-process"
513-
assert headers["X-UiPath-JobKey"] == "test-job"
514-
515-
def test_build_auth_pipeline_returns_auth(self, platform_env_vars):
520+
settings = PlatformSettings()
521+
headers = settings.build_auth_headers()
522+
assert headers["X-UiPath-AgentHub-Config"] == "test-config"
523+
assert headers["X-UiPath-ProcessKey"] == "test-process"
524+
assert headers["X-UiPath-JobKey"] == "test-job"
525+
526+
def test_build_auth_pipeline_returns_auth(self, platform_env_vars, mock_platform_auth):
516527
"""Test build_auth_pipeline returns an Auth instance."""
517528
from httpx import Auth
518529

519530
with patch.dict(os.environ, platform_env_vars, clear=True):
520-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
521-
settings = PlatformSettings()
522-
auth = settings.build_auth_pipeline()
523-
assert isinstance(auth, Auth)
531+
settings = PlatformSettings()
532+
auth = settings.build_auth_pipeline()
533+
assert isinstance(auth, Auth)
524534

525535

526536
# ============================================================================
@@ -541,61 +551,58 @@ def clear_auth_singleton(self):
541551
yield
542552
SingletonMeta._instances.pop(PlatformAuth, None)
543553

544-
def test_auth_flow_adds_bearer_token(self, platform_env_vars):
554+
def test_auth_flow_adds_bearer_token(self, platform_env_vars, mock_platform_auth):
545555
"""Test auth_flow adds Authorization header."""
546556
from uipath.llm_client.settings.platform.auth import PlatformAuth
547557

548558
with patch.dict(os.environ, platform_env_vars, clear=True):
549-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
550-
settings = PlatformSettings()
551-
auth = PlatformAuth(settings=settings)
552-
request = Request("GET", "https://example.com")
553-
flow = auth.auth_flow(request)
554-
modified_request = next(flow)
555-
assert "Authorization" in modified_request.headers
556-
assert modified_request.headers["Authorization"] == "Bearer test-access-token"
559+
settings = PlatformSettings()
560+
auth = PlatformAuth(settings=settings)
561+
request = Request("GET", "https://example.com")
562+
flow = auth.auth_flow(request)
563+
modified_request = next(flow)
564+
assert "Authorization" in modified_request.headers
565+
assert modified_request.headers["Authorization"] == "Bearer test-access-token"
557566

558-
def test_auth_flow_refreshes_on_401(self, platform_env_vars):
567+
def test_auth_flow_refreshes_on_401(self, platform_env_vars, mock_platform_auth):
559568
"""Test auth_flow refreshes token on 401 response."""
560569
from uipath.llm_client.settings.platform.auth import PlatformAuth
561570

562571
with patch.dict(os.environ, platform_env_vars, clear=True):
563-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
564-
settings = PlatformSettings()
572+
settings = PlatformSettings()
565573

566-
# Mock get_access_token to return a new token on refresh
567-
with patch.object(PlatformAuth, "get_access_token", return_value="initial-token"):
568-
auth = PlatformAuth(settings=settings)
574+
# Mock get_access_token to return a new token on refresh
575+
with patch.object(PlatformAuth, "get_access_token", return_value="initial-token"):
576+
auth = PlatformAuth(settings=settings)
569577

570-
request = Request("GET", "https://example.com")
571-
flow = auth.auth_flow(request)
578+
request = Request("GET", "https://example.com")
579+
flow = auth.auth_flow(request)
572580

573-
# First yield - initial request
574-
modified_request = next(flow)
575-
assert "Bearer" in modified_request.headers["Authorization"]
581+
# First yield - initial request
582+
modified_request = next(flow)
583+
assert "Bearer" in modified_request.headers["Authorization"]
576584

577-
# Simulate 401 response
578-
mock_response = MagicMock(spec=Response)
579-
mock_response.status_code = 401
585+
# Simulate 401 response
586+
mock_response = MagicMock(spec=Response)
587+
mock_response.status_code = 401
580588

581-
# Mock the get_access_token method to return a new token
582-
with patch.object(PlatformAuth, "get_access_token", return_value="refreshed-token"):
583-
try:
584-
retry_request = flow.send(mock_response)
585-
assert retry_request.headers["Authorization"] == "Bearer refreshed-token"
586-
except StopIteration:
587-
pass
589+
# Mock the get_access_token method to return a new token
590+
with patch.object(PlatformAuth, "get_access_token", return_value="refreshed-token"):
591+
try:
592+
retry_request = flow.send(mock_response)
593+
assert retry_request.headers["Authorization"] == "Bearer refreshed-token"
594+
except StopIteration:
595+
pass
588596

589-
def test_auth_singleton_reuses_instance(self, platform_env_vars):
597+
def test_auth_singleton_reuses_instance(self, platform_env_vars, mock_platform_auth):
590598
"""Test that PlatformAuth is a singleton."""
591599
from uipath.llm_client.settings.platform.auth import PlatformAuth
592600

593601
with patch.dict(os.environ, platform_env_vars, clear=True):
594-
with patch("uipath.llm_client.settings.platform.settings.get_auth_data"):
595-
settings = PlatformSettings()
596-
auth1 = PlatformAuth(settings=settings)
597-
auth2 = PlatformAuth(settings=settings)
598-
assert auth1 is auth2
602+
settings = PlatformSettings()
603+
auth1 = PlatformAuth(settings=settings)
604+
auth2 = PlatformAuth(settings=settings)
605+
assert auth1 is auth2
599606

600607

601608
# ============================================================================

0 commit comments

Comments
 (0)