|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -from unittest.mock import ANY, AsyncMock, MagicMock, patch |
| 15 | +import unittest |
| 16 | +from unittest.mock import AsyncMock, MagicMock, patch |
16 | 17 |
|
17 | 18 | import pytest |
18 | 19 |
|
19 | | -from toolbox_adk.client import ToolboxClient |
20 | | -from toolbox_adk.credentials import CredentialStrategy |
| 20 | +from toolbox_adk import CredentialStrategy, ToolboxClient |
| 21 | +from toolbox_adk.client import CredentialType |
21 | 22 |
|
22 | 23 |
|
23 | | -class TestToolboxClient: |
| 24 | +@pytest.mark.asyncio |
| 25 | +class TestToolboxClientAuth: |
| 26 | + """Unit tests for Client Auth logic.""" |
24 | 27 |
|
25 | 28 | @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
26 | | - def test_init_no_auth(self, mock_core_client): |
| 29 | + async def test_init_toolbox_identity(self, mock_core_client): |
| 30 | + """Test init with TOOLBOX_IDENTITY (no auth headers).""" |
27 | 31 | creds = CredentialStrategy.TOOLBOX_IDENTITY() |
28 | | - client = ToolboxClient("http://server", credentials=creds) |
| 32 | + client = ToolboxClient(server_url="http://test", credentials=creds) |
29 | 33 |
|
30 | | - mock_core_client.assert_called_with( |
31 | | - server_url="http://server", client_headers={} |
32 | | - ) |
| 34 | + # Verify core client created with empty headers for auth |
| 35 | + _, kwargs = mock_core_client.call_args |
| 36 | + assert "client_headers" in kwargs |
| 37 | + headers = kwargs["client_headers"] |
| 38 | + assert "Authorization" not in headers |
33 | 39 |
|
34 | 40 | @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
35 | | - def test_init_manual_token(self, mock_core_client): |
36 | | - creds = CredentialStrategy.MANUAL_TOKEN("abc") |
37 | | - client = ToolboxClient("http://server", credentials=creds) |
| 41 | + @patch("toolbox_adk.client.id_token.fetch_id_token") |
| 42 | + @patch("toolbox_adk.client.google.auth.default") |
| 43 | + @patch("toolbox_adk.client.transport.requests.Request") |
| 44 | + async def test_init_adc_success_fetch_id_token( |
| 45 | + self, mock_req, mock_default, mock_fetch_id, mock_core_client |
| 46 | + ): |
| 47 | + """Test ADC strategy where fetch_id_token succeeds.""" |
| 48 | + mock_fetch_id.return_value = "id-token-123" |
38 | 49 |
|
39 | | - mock_core_client.assert_called_with( |
40 | | - server_url="http://server", client_headers={"Authorization": "Bearer abc"} |
| 50 | + creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS( |
| 51 | + target_audience="aud" |
41 | 52 | ) |
| 53 | + client = ToolboxClient(server_url="http://test", credentials=creds) |
42 | 54 |
|
43 | | - @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
44 | | - def test_init_additional_headers(self, mock_core_client): |
45 | | - creds = CredentialStrategy.TOOLBOX_IDENTITY() |
46 | | - headers = {"X-Custom": "Val"} |
47 | | - client = ToolboxClient( |
48 | | - "http://server", credentials=creds, additional_headers=headers |
49 | | - ) |
| 55 | + _, kwargs = mock_core_client.call_args |
| 56 | + headers = kwargs["client_headers"] |
| 57 | + assert "Authorization" in headers |
| 58 | + token_getter = headers["Authorization"] |
| 59 | + assert callable(token_getter) |
50 | 60 |
|
51 | | - mock_core_client.assert_called_with( |
52 | | - server_url="http://server", client_headers={"X-Custom": "Val"} |
53 | | - ) |
| 61 | + # Call the getter |
| 62 | + token_val = token_getter() |
| 63 | + assert token_val == "Bearer id-token-123" |
| 64 | + mock_fetch_id.assert_called() |
54 | 65 |
|
55 | 66 | @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
56 | 67 | @patch("toolbox_adk.client.id_token.fetch_id_token") |
57 | | - def test_adc_auth_flow_success(self, mock_fetch_token, mock_core_client): |
58 | | - mock_fetch_token.return_value = "id_token_123" |
| 68 | + @patch("toolbox_adk.client.google.auth.default") |
| 69 | + @patch("toolbox_adk.client.transport.requests.Request") |
| 70 | + async def test_init_adc_fallback_creds( |
| 71 | + self, mock_req, mock_default, mock_fetch_id, mock_core_client |
| 72 | + ): |
| 73 | + """Test ADC strategy fallback to default() when fetch_id_token fails.""" |
| 74 | + mock_fetch_id.side_effect = Exception("No metadata server") |
59 | 75 |
|
60 | | - creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS("http://aud") |
61 | | - client = ToolboxClient("http://server", credentials=creds) |
| 76 | + # Mock default creds |
| 77 | + mock_creds = MagicMock() |
| 78 | + mock_creds.valid = False |
| 79 | + mock_creds.id_token = "fallback-id-token" |
| 80 | + mock_default.return_value = (mock_creds, "proj") |
62 | 81 |
|
63 | | - # Verify a callable was passed |
64 | | - args, kwargs = mock_core_client.call_args |
65 | | - assert "Authorization" in kwargs["client_headers"] |
66 | | - token_getter = kwargs["client_headers"]["Authorization"] |
67 | | - assert callable(token_getter) |
| 82 | + creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS( |
| 83 | + target_audience="aud" |
| 84 | + ) |
| 85 | + client = ToolboxClient(server_url="http://test", credentials=creds) |
68 | 86 |
|
69 | | - # Verify callable behavior |
| 87 | + token_getter = mock_core_client.call_args[1]["client_headers"]["Authorization"] |
70 | 88 | token = token_getter() |
71 | | - assert token == "Bearer id_token_123" |
72 | | - mock_fetch_token.assert_called() |
| 89 | + assert token == "Bearer fallback-id-token" |
| 90 | + mock_creds.refresh.assert_called() # Because we set valid=False |
73 | 91 |
|
74 | 92 | @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
75 | 93 | @patch("toolbox_adk.client.id_token.fetch_id_token") |
76 | 94 | @patch("toolbox_adk.client.google.auth.default") |
77 | | - def test_adc_auth_flow_fallback( |
78 | | - self, mock_default, mock_fetch_token, mock_core_client |
| 95 | + @patch("toolbox_adk.client.transport.requests.Request") |
| 96 | + async def test_init_adc_fallback_creds_token( |
| 97 | + self, mock_req, mock_default, mock_fetch_id, mock_core_client |
79 | 98 | ): |
80 | | - # unexpected error on fetch_id_token |
81 | | - mock_fetch_token.side_effect = Exception("No metadata") |
| 99 | + """Test ADC fallback when creds have .token but no .id_token.""" |
| 100 | + mock_fetch_id.side_effect = Exception("No metadata server") |
82 | 101 |
|
83 | 102 | mock_creds = MagicMock() |
84 | | - mock_creds.valid = False |
85 | | - mock_creds.id_token = "fallback_id_token" |
| 103 | + mock_creds.valid = True |
| 104 | + del mock_creds.id_token # Simulate no id_token attr or None |
| 105 | + mock_creds.token = "access-token-123" # e.g. user creds |
86 | 106 | mock_default.return_value = (mock_creds, "proj") |
87 | 107 |
|
88 | | - creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS("http://aud") |
89 | | - client = ToolboxClient("http://server", credentials=creds) |
90 | | - |
| 108 | + creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS( |
| 109 | + target_audience="aud" |
| 110 | + ) |
| 111 | + client = ToolboxClient(server_url="http://test", credentials=creds) |
91 | 112 | token_getter = mock_core_client.call_args[1]["client_headers"]["Authorization"] |
92 | | - token = token_getter() |
| 113 | + assert token_getter() == "Bearer access-token-123" |
93 | 114 |
|
94 | | - assert token == "Bearer fallback_id_token" |
95 | | - mock_creds.refresh.assert_called() |
| 115 | + @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
| 116 | + async def test_init_manual_token(self, mock_core_client): |
| 117 | + creds = CredentialStrategy.MANUAL_TOKEN(token="abc") |
| 118 | + client = ToolboxClient("http://test", credentials=creds) |
| 119 | + headers = mock_core_client.call_args[1]["client_headers"] |
| 120 | + assert headers["Authorization"] == "Bearer abc" |
96 | 121 |
|
97 | 122 | @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" |
| 123 | + async def test_init_manual_creds(self, mock_core_client): |
| 124 | + mock_google_creds = MagicMock() |
| 125 | + mock_google_creds.valid = True |
| 126 | + mock_google_creds.token = "creds-token" |
102 | 127 |
|
103 | | - creds = CredentialStrategy.MANUAL_CREDS(mock_g_creds) |
104 | | - client = ToolboxClient("http://server", credentials=creds) |
| 128 | + creds = CredentialStrategy.MANUAL_CREDS(credentials=mock_google_creds) |
| 129 | + client = ToolboxClient("http://test", credentials=creds) |
105 | 130 |
|
106 | 131 | 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() |
| 132 | + assert token_getter() == "Bearer creds-token" |
112 | 133 |
|
113 | 134 | @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( |
120 | | - "url", |
121 | | - credentials=CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS(None), |
122 | | - ) |
123 | | - |
124 | | - # Manual token missing token |
125 | | - with pytest.raises(ValueError, match="token is required"): |
126 | | - ToolboxClient("url", credentials=CredentialStrategy.MANUAL_TOKEN(None)) |
127 | | - |
128 | | - # Manual creds missing credentials |
129 | | - with pytest.raises(ValueError, match="credentials object is required"): |
130 | | - ToolboxClient("url", credentials=CredentialStrategy.MANUAL_CREDS(None)) |
| 135 | + async def test_init_user_identity(self, mock_core_client): |
| 136 | + creds = CredentialStrategy.USER_IDENTITY(client_id="c", client_secret="s") |
| 137 | + client = ToolboxClient("http://test", credentials=creds) |
131 | 138 |
|
132 | | - @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
133 | | - @patch("toolbox_adk.client.id_token.fetch_id_token") |
134 | | - @patch("toolbox_adk.client.google.auth.default") |
135 | | - def test_adc_auth_flow_fallback_access_token( |
136 | | - self, mock_default, mock_fetch_token, mock_core_client |
137 | | - ): |
138 | | - # fetch_id_token fails |
139 | | - mock_fetch_token.side_effect = Exception("No metadata") |
| 139 | + token_getter = mock_core_client.call_args[1]["client_headers"]["Authorization"] |
| 140 | + # Should be empty initially |
| 141 | + assert token_getter() == "" |
140 | 142 |
|
141 | | - mock_creds = MagicMock() |
142 | | - mock_creds.valid = False |
143 | | - mock_creds.id_token = None # No ID token |
144 | | - mock_creds.token = "access_token_123" |
145 | | - mock_default.return_value = (mock_creds, "proj") |
| 143 | + # Set context |
| 144 | + from toolbox_adk.client import USER_TOKEN_CONTEXT_VAR |
146 | 145 |
|
147 | | - creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS("http://aud") |
148 | | - client = ToolboxClient("http://server", credentials=creds) |
| 146 | + token = USER_TOKEN_CONTEXT_VAR.set("user-tok") |
| 147 | + try: |
| 148 | + assert token_getter() == "Bearer user-tok" |
| 149 | + finally: |
| 150 | + USER_TOKEN_CONTEXT_VAR.reset(token) |
149 | 151 |
|
150 | | - token_getter = mock_core_client.call_args[1]["client_headers"]["Authorization"] |
151 | | - token = token_getter() |
| 152 | + async def test_validation_errors(self): |
| 153 | + with pytest.raises(ValueError): |
| 154 | + # ADC requires audience |
| 155 | + creds = CredentialStrategy.APPLICATION_DEFAULT_CREDENTIALS(target_audience="") |
| 156 | + ToolboxClient("http://test", credentials=creds) |
152 | 157 |
|
153 | | - assert token == "Bearer access_token_123" |
154 | | - mock_creds.refresh.assert_called() |
| 158 | + with pytest.raises(ValueError): |
| 159 | + creds = CredentialStrategy.MANUAL_TOKEN(token="") |
| 160 | + ToolboxClient("http://test", credentials=creds) |
| 161 | + |
| 162 | + with pytest.raises(ValueError): |
| 163 | + creds = CredentialStrategy.MANUAL_CREDS(credentials=None) |
| 164 | + ToolboxClient("http://test", credentials=creds) |
155 | 165 |
|
156 | | - @pytest.mark.asyncio |
157 | 166 | @patch("toolbox_adk.client.toolbox_core.ToolboxClient") |
158 | | - async def test_delegation(self, mock_core_client): |
159 | | - mock_instance = mock_core_client.return_value |
160 | | - mock_instance.load_toolset = AsyncMock(return_value=["t1"]) |
161 | | - mock_instance.close = AsyncMock() |
| 167 | + async def test_load_methods(self, mock_core_client_class): |
| 168 | + # Setup mock instance |
| 169 | + mock_instance = AsyncMock() |
| 170 | + mock_core_client_class.return_value = mock_instance |
162 | 171 |
|
163 | | - client = ToolboxClient("http://server") |
164 | | - tools = await client.load_toolset("my-set", extra="arg") |
| 172 | + client = ToolboxClient( |
| 173 | + "http://test", credentials=CredentialStrategy.TOOLBOX_IDENTITY() |
| 174 | + ) |
165 | 175 |
|
166 | | - mock_instance.load_toolset.assert_awaited_with("my-set", extra="arg") |
167 | | - assert tools == ["t1"] |
| 176 | + # Test load_toolset |
| 177 | + await client.load_toolset("ts", foo="bar") |
| 178 | + mock_instance.load_toolset.assert_called_with("ts", foo="bar") |
168 | 179 |
|
| 180 | + # Test load_tool |
| 181 | + await client.load_tool("t", baz="qux") |
| 182 | + mock_instance.load_tool.assert_called_with("t", baz="qux") |
| 183 | + |
| 184 | + # Test close |
169 | 185 | await client.close() |
170 | | - mock_instance.close.assert_awaited() |
| 186 | + mock_instance.close.assert_called_once() |
| 187 | + |
| 188 | + # Test property |
| 189 | + assert client.credential_config is not None |
0 commit comments