|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import pytest |
| 16 | +from unittest.mock import MagicMock, patch, ANY, AsyncMock |
| 17 | +from toolbox_adk.client import ToolboxClient |
| 18 | +from toolbox_adk.credentials import CredentialStrategy |
| 19 | + |
| 20 | +class TestToolboxClient: |
| 21 | + |
| 22 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 23 | + def test_init_no_auth(self, mock_core_client): |
| 24 | + creds = CredentialStrategy.TOOLBOX_IDENTITY() |
| 25 | + client = ToolboxClient("http://server", credentials=creds) |
| 26 | + |
| 27 | + mock_core_client.assert_called_with( |
| 28 | + server_url="http://server", |
| 29 | + client_headers={} |
| 30 | + ) |
| 31 | + |
| 32 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 33 | + def test_init_manual_token(self, mock_core_client): |
| 34 | + creds = CredentialStrategy.MANUAL_TOKEN("abc") |
| 35 | + client = ToolboxClient("http://server", credentials=creds) |
| 36 | + |
| 37 | + mock_core_client.assert_called_with( |
| 38 | + server_url="http://server", |
| 39 | + client_headers={"Authorization": "Bearer abc"} |
| 40 | + ) |
| 41 | + |
| 42 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 43 | + def test_init_additional_headers(self, mock_core_client): |
| 44 | + creds = CredentialStrategy.TOOLBOX_IDENTITY() |
| 45 | + headers = {"X-Custom": "Val"} |
| 46 | + client = ToolboxClient( |
| 47 | + "http://server", |
| 48 | + credentials=creds, |
| 49 | + additional_headers=headers |
| 50 | + ) |
| 51 | + |
| 52 | + mock_core_client.assert_called_with( |
| 53 | + server_url="http://server", |
| 54 | + client_headers={"X-Custom": "Val"} |
| 55 | + ) |
| 56 | + |
| 57 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 58 | + @patch("toolbox_adk.client.id_token.fetch_id_token") |
| 59 | + def test_adc_auth_flow_success(self, mock_fetch_token, mock_core_client): |
| 60 | + mock_fetch_token.return_value = "id_token_123" |
| 61 | + |
| 62 | + creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS("http://aud") |
| 63 | + client = ToolboxClient("http://server", credentials=creds) |
| 64 | + |
| 65 | + # Verify a callable was passed |
| 66 | + args, kwargs = mock_core_client.call_args |
| 67 | + assert "Authorization" in kwargs["client_headers"] |
| 68 | + token_getter = kwargs["client_headers"]["Authorization"] |
| 69 | + assert callable(token_getter) |
| 70 | + |
| 71 | + # Verify callable behavior |
| 72 | + token = token_getter() |
| 73 | + assert token == "Bearer id_token_123" |
| 74 | + mock_fetch_token.assert_called() |
| 75 | + |
| 76 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 77 | + @patch("toolbox_adk.client.id_token.fetch_id_token") |
| 78 | + @patch("toolbox_adk.client.google.auth.default") |
| 79 | + def test_adc_auth_flow_fallback(self, mock_default, mock_fetch_token, mock_core_client): |
| 80 | + # unexpected error on fetch_id_token |
| 81 | + mock_fetch_token.side_effect = Exception("No metadata") |
| 82 | + |
| 83 | + mock_creds = MagicMock() |
| 84 | + mock_creds.valid = False |
| 85 | + mock_creds.id_token = "fallback_id_token" |
| 86 | + mock_default.return_value = (mock_creds, "proj") |
| 87 | + |
| 88 | + creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS("http://aud") |
| 89 | + client = ToolboxClient("http://server", credentials=creds) |
| 90 | + |
| 91 | + token_getter = mock_core_client.call_args[1]["client_headers"]["Authorization"] |
| 92 | + token = token_getter() |
| 93 | + |
| 94 | + assert token == "Bearer fallback_id_token" |
| 95 | + mock_creds.refresh.assert_called() |
| 96 | + |
| 97 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 98 | + def test_manual_creds(self, mock_core_client): |
| 99 | + mock_g_creds = MagicMock() |
| 100 | + mock_g_creds.valid = False |
| 101 | + mock_g_creds.token = "oauth_token" |
| 102 | + |
| 103 | + creds = CredentialStrategy.MANUAL_CREDS(mock_g_creds) |
| 104 | + client = ToolboxClient("http://server", credentials=creds) |
| 105 | + |
| 106 | + token_getter = mock_core_client.call_args[1]["client_headers"]["Authorization"] |
| 107 | + token = token_getter() |
| 108 | + |
| 109 | + assert token == "Bearer oauth_token" |
| 110 | + assert token == "Bearer oauth_token" |
| 111 | + mock_g_creds.refresh.assert_called() |
| 112 | + |
| 113 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 114 | + def test_init_validation_errors(self, mock_core_client): |
| 115 | + # ADC missing audience |
| 116 | + with pytest.raises(ValueError, match="target_audience is required"): |
| 117 | + # Fix: only pass target_audience as keyword arg OR positional, not both mixed in a way that causes overlap if defined so |
| 118 | + # Actually simpler: just pass raw None |
| 119 | + ToolboxClient("url", credentials=CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS(None)) |
| 120 | + |
| 121 | + # Manual token missing token |
| 122 | + with pytest.raises(ValueError, match="token is required"): |
| 123 | + ToolboxClient("url", credentials=CredentialStrategy.MANUAL_TOKEN(None)) |
| 124 | + |
| 125 | + # Manual creds missing credentials |
| 126 | + with pytest.raises(ValueError, match="credentials object is required"): |
| 127 | + ToolboxClient("url", credentials=CredentialStrategy.MANUAL_CREDS(None)) |
| 128 | + |
| 129 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 130 | + @patch("toolbox_adk.client.id_token.fetch_id_token") |
| 131 | + @patch("toolbox_adk.client.google.auth.default") |
| 132 | + def test_adc_auth_flow_fallback_access_token(self, mock_default, mock_fetch_token, mock_core_client): |
| 133 | + # fetch_id_token fails |
| 134 | + mock_fetch_token.side_effect = Exception("No metadata") |
| 135 | + |
| 136 | + mock_creds = MagicMock() |
| 137 | + mock_creds.valid = False |
| 138 | + mock_creds.id_token = None # No ID token |
| 139 | + mock_creds.token = "access_token_123" |
| 140 | + mock_default.return_value = (mock_creds, "proj") |
| 141 | + |
| 142 | + creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS("http://aud") |
| 143 | + client = ToolboxClient("http://server", credentials=creds) |
| 144 | + |
| 145 | + token_getter = mock_core_client.call_args[1]["client_headers"]["Authorization"] |
| 146 | + token = token_getter() |
| 147 | + |
| 148 | + assert token == "Bearer access_token_123" |
| 149 | + mock_creds.refresh.assert_called() |
| 150 | + |
| 151 | + @pytest.mark.asyncio |
| 152 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 153 | + async def test_delegation(self, mock_core_client): |
| 154 | + mock_instance = mock_core_client.return_value |
| 155 | + mock_instance.load_toolset = AsyncMock(return_value=["t1"]) |
| 156 | + mock_instance.close = AsyncMock() |
| 157 | + |
| 158 | + client = ToolboxClient("http://server") |
| 159 | + tools = await client.load_toolset("my-set", extra="arg") |
| 160 | + |
| 161 | + mock_instance.load_toolset.assert_awaited_with("my-set", extra="arg") |
| 162 | + assert tools == ["t1"] |
| 163 | + |
| 164 | + await client.close() |
| 165 | + mock_instance.close.assert_awaited() |
0 commit comments