|
3 | 3 | import json |
4 | 4 | import os |
5 | 5 | from http.server import BaseHTTPRequestHandler, HTTPServer |
| 6 | +from io import BytesIO |
6 | 7 | from threading import Thread |
7 | | -from unittest.mock import patch |
| 8 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 9 | +from urllib.error import HTTPError |
8 | 10 |
|
9 | 11 | import pytest |
10 | 12 |
|
@@ -314,3 +316,199 @@ def log_message(self, format, *args): |
314 | 316 | # Cleanup any leaked env vars |
315 | 317 | for key in ["GITHUB_TOKEN", "GITLAB_TOKEN", "JIRA_API_TOKEN", "JIRA_URL", "JIRA_EMAIL", "GIT_USER_NAME", "GIT_USER_EMAIL"]: |
316 | 318 | os.environ.pop(key, None) |
| 319 | + |
| 320 | + |
| 321 | +# --------------------------------------------------------------------------- |
| 322 | +# _fetch_credential — auth failure propagation (issue #1043) |
| 323 | +# --------------------------------------------------------------------------- |
| 324 | + |
| 325 | + |
| 326 | +class TestFetchCredentialAuthFailures: |
| 327 | + @pytest.mark.asyncio |
| 328 | + async def test_raises_permission_error_on_401_without_caller_token(self, monkeypatch): |
| 329 | + """_fetch_credential raises PermissionError when backend returns 401 with BOT_TOKEN.""" |
| 330 | + monkeypatch.setenv("BACKEND_API_URL", "http://backend.svc.cluster.local/api") |
| 331 | + monkeypatch.setenv("PROJECT_NAME", "test-project") |
| 332 | + monkeypatch.setenv("BOT_TOKEN", "bot-token") |
| 333 | + |
| 334 | + ctx = _make_context(session_id="sess-1") |
| 335 | + # No caller token — uses BOT_TOKEN directly |
| 336 | + |
| 337 | + err = HTTPError("http://backend.svc.cluster.local/api/...", 401, "Unauthorized", {}, BytesIO(b"")) |
| 338 | + with patch("urllib.request.urlopen", side_effect=err): |
| 339 | + with pytest.raises(PermissionError, match="authentication failed with HTTP 401"): |
| 340 | + await _fetch_credential(ctx, "github") |
| 341 | + |
| 342 | + @pytest.mark.asyncio |
| 343 | + async def test_raises_permission_error_on_403_without_caller_token(self, monkeypatch): |
| 344 | + """_fetch_credential raises PermissionError when backend returns 403 with BOT_TOKEN.""" |
| 345 | + monkeypatch.setenv("BACKEND_API_URL", "http://backend.svc.cluster.local/api") |
| 346 | + monkeypatch.setenv("PROJECT_NAME", "test-project") |
| 347 | + monkeypatch.setenv("BOT_TOKEN", "bot-token") |
| 348 | + |
| 349 | + ctx = _make_context(session_id="sess-1") |
| 350 | + |
| 351 | + err = HTTPError("http://backend.svc.cluster.local/api/...", 403, "Forbidden", {}, BytesIO(b"")) |
| 352 | + with patch("urllib.request.urlopen", side_effect=err): |
| 353 | + with pytest.raises(PermissionError, match="authentication failed with HTTP 403"): |
| 354 | + await _fetch_credential(ctx, "google") |
| 355 | + |
| 356 | + @pytest.mark.asyncio |
| 357 | + async def test_raises_permission_error_when_caller_and_bot_both_fail(self, monkeypatch): |
| 358 | + """_fetch_credential raises PermissionError when caller token 401s and BOT_TOKEN also fails.""" |
| 359 | + monkeypatch.setenv("BACKEND_API_URL", "http://backend.svc.cluster.local/api") |
| 360 | + monkeypatch.setenv("PROJECT_NAME", "test-project") |
| 361 | + monkeypatch.setenv("BOT_TOKEN", "bot-token") |
| 362 | + |
| 363 | + ctx = _make_context(session_id="sess-1", current_user_id="user@example.com") |
| 364 | + ctx.caller_token = "Bearer expired-caller-token" |
| 365 | + |
| 366 | + caller_err = HTTPError("http://...", 401, "Unauthorized", {}, BytesIO(b"")) |
| 367 | + fallback_err = HTTPError("http://...", 403, "Forbidden", {}, BytesIO(b"")) |
| 368 | + |
| 369 | + with patch("urllib.request.urlopen", side_effect=[caller_err, fallback_err]): |
| 370 | + with pytest.raises(PermissionError, match="caller token expired and BOT_TOKEN fallback also failed"): |
| 371 | + await _fetch_credential(ctx, "github") |
| 372 | + |
| 373 | + @pytest.mark.asyncio |
| 374 | + async def test_does_not_raise_on_non_auth_http_errors(self, monkeypatch): |
| 375 | + """_fetch_credential returns {} for non-auth HTTP errors (404, 500, etc.).""" |
| 376 | + monkeypatch.setenv("BACKEND_API_URL", "http://backend.svc.cluster.local/api") |
| 377 | + monkeypatch.setenv("PROJECT_NAME", "test-project") |
| 378 | + |
| 379 | + ctx = _make_context(session_id="sess-1") |
| 380 | + |
| 381 | + err = HTTPError("http://...", 404, "Not Found", {}, BytesIO(b"")) |
| 382 | + with patch("urllib.request.urlopen", side_effect=err): |
| 383 | + result = await _fetch_credential(ctx, "github") |
| 384 | + |
| 385 | + assert result == {} |
| 386 | + |
| 387 | + @pytest.mark.asyncio |
| 388 | + async def test_caller_token_fallback_succeeds_when_bot_token_works(self, monkeypatch): |
| 389 | + """_fetch_credential returns data when caller token 401s but BOT_TOKEN fallback succeeds.""" |
| 390 | + monkeypatch.setenv("BACKEND_API_URL", "http://backend.svc.cluster.local/api") |
| 391 | + monkeypatch.setenv("PROJECT_NAME", "test-project") |
| 392 | + monkeypatch.setenv("BOT_TOKEN", "valid-bot-token") |
| 393 | + |
| 394 | + ctx = _make_context(session_id="sess-1", current_user_id="user@example.com") |
| 395 | + ctx.caller_token = "Bearer expired-caller-token" |
| 396 | + |
| 397 | + caller_err = HTTPError("http://...", 401, "Unauthorized", {}, BytesIO(b"")) |
| 398 | + |
| 399 | + mock_response = MagicMock() |
| 400 | + mock_response.read.return_value = json.dumps({"token": "gh-tok-via-bot"}).encode() |
| 401 | + mock_response.__enter__ = lambda s: s |
| 402 | + mock_response.__exit__ = MagicMock(return_value=False) |
| 403 | + |
| 404 | + with patch("urllib.request.urlopen", side_effect=[caller_err, mock_response]): |
| 405 | + result = await _fetch_credential(ctx, "github") |
| 406 | + |
| 407 | + assert result.get("token") == "gh-tok-via-bot" |
| 408 | + |
| 409 | + |
| 410 | +# --------------------------------------------------------------------------- |
| 411 | +# populate_runtime_credentials — raises on auth failure (issue #1043) |
| 412 | +# --------------------------------------------------------------------------- |
| 413 | + |
| 414 | + |
| 415 | +class TestPopulateRuntimeCredentialsAuthFailures: |
| 416 | + @pytest.mark.asyncio |
| 417 | + async def test_raises_when_github_auth_fails(self, monkeypatch): |
| 418 | + """populate_runtime_credentials raises PermissionError when GitHub auth fails.""" |
| 419 | + monkeypatch.setenv("BACKEND_API_URL", "http://backend.svc.cluster.local/api") |
| 420 | + monkeypatch.setenv("PROJECT_NAME", "test-project") |
| 421 | + |
| 422 | + ctx = _make_context(session_id="sess-1") |
| 423 | + |
| 424 | + async def _fail_github(context, cred_type): |
| 425 | + if cred_type == "github": |
| 426 | + raise PermissionError("github authentication failed with HTTP 401") |
| 427 | + return {} |
| 428 | + |
| 429 | + with patch("ambient_runner.platform.auth._fetch_credential", side_effect=_fail_github): |
| 430 | + with pytest.raises(PermissionError, match="Credential refresh failed due to authentication errors"): |
| 431 | + await populate_runtime_credentials(ctx) |
| 432 | + |
| 433 | + @pytest.mark.asyncio |
| 434 | + async def test_raises_when_multiple_providers_fail(self, monkeypatch): |
| 435 | + """populate_runtime_credentials raises PermissionError listing all auth failures.""" |
| 436 | + monkeypatch.setenv("BACKEND_API_URL", "http://backend.svc.cluster.local/api") |
| 437 | + monkeypatch.setenv("PROJECT_NAME", "test-project") |
| 438 | + |
| 439 | + ctx = _make_context(session_id="sess-1") |
| 440 | + |
| 441 | + async def _fail_all(context, cred_type): |
| 442 | + raise PermissionError(f"{cred_type} authentication failed with HTTP 401") |
| 443 | + |
| 444 | + with patch("ambient_runner.platform.auth._fetch_credential", side_effect=_fail_all): |
| 445 | + with pytest.raises(PermissionError) as exc_info: |
| 446 | + await populate_runtime_credentials(ctx) |
| 447 | + |
| 448 | + msg = str(exc_info.value) |
| 449 | + assert "authentication errors" in msg |
| 450 | + |
| 451 | + @pytest.mark.asyncio |
| 452 | + async def test_succeeds_when_all_credentials_empty_no_auth_error(self, monkeypatch): |
| 453 | + """populate_runtime_credentials does not raise when credentials are simply missing (not auth failures).""" |
| 454 | + monkeypatch.setenv("BACKEND_API_URL", "http://backend.svc.cluster.local/api") |
| 455 | + monkeypatch.setenv("PROJECT_NAME", "test-project") |
| 456 | + |
| 457 | + ctx = _make_context(session_id="sess-1") |
| 458 | + |
| 459 | + with patch("ambient_runner.platform.auth._fetch_credential", return_value={}): |
| 460 | + # Should not raise — empty credentials just means no integrations configured |
| 461 | + await populate_runtime_credentials(ctx) |
| 462 | + |
| 463 | + |
| 464 | +# --------------------------------------------------------------------------- |
| 465 | +# refresh_credentials_tool — reports isError on auth failure (issue #1043) |
| 466 | +# --------------------------------------------------------------------------- |
| 467 | + |
| 468 | + |
| 469 | +class TestRefreshCredentialsTool: |
| 470 | + def _make_tool_decorator(self): |
| 471 | + """Create a mock sdk_tool decorator that preserves the function.""" |
| 472 | + def mock_tool(name, description, schema): |
| 473 | + def decorator(func): |
| 474 | + return func |
| 475 | + return decorator |
| 476 | + return mock_tool |
| 477 | + |
| 478 | + @pytest.mark.asyncio |
| 479 | + async def test_returns_is_error_on_auth_failure(self): |
| 480 | + """refresh_credentials_tool returns isError=True when populate_runtime_credentials raises PermissionError.""" |
| 481 | + from ambient_runner.bridges.claude.tools import create_refresh_credentials_tool |
| 482 | + |
| 483 | + mock_context = MagicMock() |
| 484 | + tool_fn = create_refresh_credentials_tool(mock_context, self._make_tool_decorator()) |
| 485 | + |
| 486 | + with patch( |
| 487 | + "ambient_runner.platform.auth.populate_runtime_credentials", |
| 488 | + new_callable=AsyncMock, |
| 489 | + side_effect=PermissionError("github authentication failed with HTTP 401"), |
| 490 | + ): |
| 491 | + result = await tool_fn({}) |
| 492 | + |
| 493 | + assert result.get("isError") is True |
| 494 | + assert "github authentication failed" in result["content"][0]["text"] |
| 495 | + |
| 496 | + @pytest.mark.asyncio |
| 497 | + async def test_returns_success_on_successful_refresh(self): |
| 498 | + """refresh_credentials_tool returns success message when credentials refresh succeeds.""" |
| 499 | + from ambient_runner.bridges.claude.tools import create_refresh_credentials_tool |
| 500 | + |
| 501 | + mock_context = MagicMock() |
| 502 | + tool_fn = create_refresh_credentials_tool(mock_context, self._make_tool_decorator()) |
| 503 | + |
| 504 | + with patch( |
| 505 | + "ambient_runner.platform.auth.populate_runtime_credentials", |
| 506 | + new_callable=AsyncMock, |
| 507 | + ), patch( |
| 508 | + "ambient_runner.platform.utils.get_active_integrations", |
| 509 | + return_value=["github", "jira"], |
| 510 | + ): |
| 511 | + result = await tool_fn({}) |
| 512 | + |
| 513 | + assert result.get("isError") is None or result.get("isError") is False |
| 514 | + assert "successfully" in result["content"][0]["text"].lower() |
0 commit comments