From 1328b7124fe07e1b0ebbaf7fe471af1a5004091d Mon Sep 17 00:00:00 2001 From: yyouretoast Date: Wed, 26 Aug 2026 15:13:57 +0400 Subject: [PATCH] test(github_copilot): add unit tests for explicit token cache hit and error paths --- .../test_github_copilot_authenticator.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_litellm/llms/github_copilot/test_github_copilot_authenticator.py b/tests/test_litellm/llms/github_copilot/test_github_copilot_authenticator.py index db68eb416c6..b5e43b677e5 100644 --- a/tests/test_litellm/llms/github_copilot/test_github_copilot_authenticator.py +++ b/tests/test_litellm/llms/github_copilot/test_github_copilot_authenticator.py @@ -8,6 +8,7 @@ import pytest from litellm.llms.github_copilot.authenticator import Authenticator from litellm.llms.github_copilot.common_utils import ( GetAccessTokenError, + GetAPIKeyError, GetDeviceCodeError, RefreshAPIKeyError, ) @@ -71,6 +72,25 @@ class TestGitHubCopilotAuthenticator: mock_refresh.assert_called_once_with("user-b-custom-token") mock_file_open.assert_not_called() + # Second call with the same token should hit in-memory cache without calling _refresh_api_key again + cached_key = authenticator.get_api_key(access_token="user-b-custom-token") + assert cached_key == "token-b-session-key" + assert mock_refresh.call_count == 1 + + def test_get_api_key_with_explicit_token_missing_token_in_response(self, authenticator): + """Test that get_api_key raises GetAPIKeyError when API response lacks token.""" + with patch.object(authenticator, "_refresh_api_key", return_value={}): + with pytest.raises(GetAPIKeyError): + authenticator.get_api_key(access_token="token-without-key") + + def test_get_api_key_with_explicit_token_refresh_error(self, authenticator): + """Test that get_api_key handles RefreshAPIKeyError when refreshing explicit token.""" + with patch.object( + authenticator, "_refresh_api_key", side_effect=RefreshAPIKeyError(message="Refresh failed", status_code=401) + ): + with pytest.raises(GetAPIKeyError): + authenticator.get_api_key(access_token="failing-token") + def test_get_github_headers(self, authenticator): """Test that GitHub headers are correctly generated.""" headers = authenticator._get_github_headers()