1313# limitations under the License.
1414
1515
16- from typing import Any , Optional
17- import pytest
1816import os
19- from toolbox_adk import ToolboxToolset , CredentialStrategy , ToolboxTool
17+ from typing import Any , Optional
2018from unittest .mock import MagicMock
21- from google .adk .auth .auth_credential import AuthCredential , AuthCredentialTypes , OAuth2Auth
19+
20+ import pytest
21+ from google .adk .auth .auth_credential import (
22+ AuthCredential ,
23+ AuthCredentialTypes ,
24+ OAuth2Auth ,
25+ )
26+
27+ from toolbox_adk import CredentialStrategy , ToolboxTool , ToolboxToolset
2228
2329# Ensure TOOLBOX_VERSION is set for the fixture
2430if "TOOLBOX_VERSION" not in os .environ :
25- os .environ ["TOOLBOX_VERSION" ] = "0.0.1" # Use a valid version or mock
31+ os .environ ["TOOLBOX_VERSION" ] = "0.0.1" # Use a valid version or mock
32+
2633
2734@pytest .mark .asyncio
2835@pytest .mark .usefixtures ("toolbox_server" )
@@ -35,58 +42,58 @@ class TestToolboxAdkIntegration:
3542 async def test_load_toolset_and_run (self ):
3643 # We assume the toolbox server (started by fixture) is at localhost:5000 (default)
3744 # Auth: TOOLBOX_IDENTITY for simplicity in this local test as we don't have ADK identity setup.
38-
45+
3946 toolset = ToolboxToolset (
4047 server_url = "http://localhost:5000" ,
4148 toolset_name = "my-toolset" ,
42- credentials = CredentialStrategy .TOOLBOX_IDENTITY ()
49+ credentials = CredentialStrategy .TOOLBOX_IDENTITY (),
4350 )
44-
51+
4552 try :
4653 tools = await toolset .get_tools ()
4754 assert len (tools ) > 0
48-
55+
4956 # Find 'get-row-by-id'
5057 tool = next ((t for t in tools if t .name == "get-row-by-id" ), None )
5158 assert tool is not None
5259 assert isinstance (tool , ToolboxTool )
53-
60+
5461 # Run it
5562 # Mocking ToolContext as we are running directly
5663 ctx = MagicMock ()
5764 result = await tool .run_async ({"id" : "1" }, ctx )
58-
65+
5966 assert "row1" in result
60-
67+
6168 finally :
6269 await toolset .close ()
6370
6471 async def test_partial_loading_by_names (self ):
6572 toolset = ToolboxToolset (
6673 server_url = "http://localhost:5000" ,
6774 tool_names = ["get-n-rows" ],
68- credentials = CredentialStrategy .TOOLBOX_IDENTITY ()
75+ credentials = CredentialStrategy .TOOLBOX_IDENTITY (),
6976 )
7077 try :
7178 tools = await toolset .get_tools ()
7279 assert len (tools ) == 1
7380 assert tools [0 ].name == "get-n-rows"
74-
81+
7582 # Run it
7683 ctx = MagicMock ()
7784 result = await tools [0 ].run_async ({"num_rows" : "1" }, ctx )
7885 assert "row1" in result
79-
86+
8087 finally :
8188 await toolset .close ()
82-
89+
8390 async def test_bound_params_e2e (self ):
8491 # Test binding param at toolset level
8592 toolset = ToolboxToolset (
8693 server_url = "http://localhost:5000" ,
8794 tool_names = ["get-n-rows" ],
8895 bound_params = {"num_rows" : "2" },
89- credentials = CredentialStrategy .TOOLBOX_IDENTITY ()
96+ credentials = CredentialStrategy .TOOLBOX_IDENTITY (),
9097 )
9198 try :
9299 tools = await toolset .get_tools ()
@@ -103,53 +110,54 @@ async def test_3lo_flow_simulation(self):
103110 server_url = "http://localhost:5000" ,
104111 toolset_name = "auth-test-toolset" ,
105112 credentials = CredentialStrategy .USER_IDENTITY (
106- client_id = "test-client-id" ,
107- client_secret = "test-client-secret"
108- )
113+ client_id = "test-client-id" , client_secret = "test-client-secret"
114+ ),
109115 )
110-
116+
111117 try :
112118 # 2. Get Tools (This hits the real server)
113119 # We filter for a simple tool if possible, or just take the first one.
114120 # We assume the fixture loads a manifest with at least one tool.
115121 tools = await toolset .get_tools ()
116122 assert len (tools ) > 0
117-
123+
118124 # Pick a tool
119125 tool = tools [0 ]
120126 assert isinstance (tool , ToolboxTool )
121-
127+
122128 # 3. Simulate First Call (No Credential)
123129 # Create a mock context that behaves like ADK's ReadonlyContext
124130 mock_ctx_first = MagicMock ()
125131 # Simulate "No Auth Response Found"
126132 mock_ctx_first .get_auth_response .return_value = None
127-
133+
128134 print ("Running tool first time (expecting auth request)..." )
129135 result_first = await tool .run_async ({}, mock_ctx_first )
130-
136+
131137 # 4. Verify Interruption
132138 # The wrapper should catch the missing creds and request them.
133- assert result_first is None , "Tool should return None to signal auth requirement"
139+ assert (
140+ result_first is None
141+ ), "Tool should return None to signal auth requirement"
134142 mock_ctx_first .request_credential .assert_called_once ()
135-
143+
136144 # Inspect the requested config
137145 auth_config = mock_ctx_first .request_credential .call_args [0 ][0 ]
138146 assert auth_config .raw_auth_credential .oauth2 .client_id == "test-client-id"
139-
147+
140148 # 5. Simulate Runner Handling & Second Call (With Credential)
141149 # The runner would get the token. We simulate passing it back in context.
142150 mock_ctx_second = MagicMock ()
143-
151+
144152 # Simulate "Auth Response Found"
145153 mock_creds = AuthCredential (
146154 auth_type = AuthCredentialTypes .OAUTH2 ,
147- oauth2 = OAuth2Auth (access_token = "fake-access-token" )
155+ oauth2 = OAuth2Auth (access_token = "fake-access-token" ),
148156 )
149157 mock_ctx_second .get_auth_response .return_value = mock_creds
150-
158+
151159 print ("Running tool second time (expecting success or server error)..." )
152-
160+
153161 try :
154162 result_second = await tool .run_async ({}, mock_ctx_second )
155163 assert result_second is not None
0 commit comments