|
| 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 | +import pytest |
| 15 | +import pytest_asyncio |
| 16 | + |
| 17 | +from toolbox_core.client import ToolboxClient |
| 18 | +from toolbox_core.tool import ToolboxTool |
| 19 | + |
| 20 | + |
| 21 | +# --- Shared Fixtures Defined at Module Level --- |
| 22 | +@pytest_asyncio.fixture(scope="function") |
| 23 | +async def toolbox(): |
| 24 | + """Creates a ToolboxClient instance shared by all tests in this module.""" |
| 25 | + toolbox = ToolboxClient("http://localhost:5000") |
| 26 | + try: |
| 27 | + yield toolbox |
| 28 | + finally: |
| 29 | + await toolbox.close() |
| 30 | + |
| 31 | + |
| 32 | +@pytest_asyncio.fixture(scope="function") |
| 33 | +async def get_n_rows_tool(toolbox: ToolboxClient) -> ToolboxTool: |
| 34 | + """Load the 'get-n-rows' tool using the shared toolbox client.""" |
| 35 | + tool = await toolbox.load_tool("get-n-rows") |
| 36 | + assert tool.__name__ == "get-n-rows" |
| 37 | + return tool |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +@pytest.mark.usefixtures("toolbox_server") |
| 42 | +class TestBasicE2E: |
| 43 | + @pytest.mark.parametrize( |
| 44 | + "toolset_name, expected_length, expected_tools", |
| 45 | + [ |
| 46 | + ("my-toolset", 1, ["get-row-by-id"]), |
| 47 | + ("my-toolset-2", 2, ["get-n-rows", "get-row-by-id"]), |
| 48 | + ], |
| 49 | + ) |
| 50 | + async def test_load_toolset_specific( |
| 51 | + self, |
| 52 | + toolbox: ToolboxClient, |
| 53 | + toolset_name: str, |
| 54 | + expected_length: int, |
| 55 | + expected_tools: list[str], |
| 56 | + ): |
| 57 | + """Load a specific toolset""" |
| 58 | + toolset = await toolbox.load_toolset(toolset_name) |
| 59 | + assert len(toolset) == expected_length |
| 60 | + tool_names = {tool.__name__ for tool in toolset} |
| 61 | + assert tool_names == set(expected_tools) |
| 62 | + |
| 63 | + async def test_run_tool(self, get_n_rows_tool: ToolboxTool): |
| 64 | + """Invoke a tool.""" |
| 65 | + response = await get_n_rows_tool(num_rows="2") |
| 66 | + |
| 67 | + assert isinstance(response, str) |
| 68 | + assert "row1" in response |
| 69 | + assert "row2" in response |
| 70 | + assert "row3" not in response |
| 71 | + |
| 72 | + async def test_run_tool_missing_params(self, get_n_rows_tool: ToolboxTool): |
| 73 | + """Invoke a tool with missing params.""" |
| 74 | + with pytest.raises(TypeError, match="missing a required argument: 'num_rows'"): |
| 75 | + await get_n_rows_tool() |
| 76 | + |
| 77 | + async def test_run_tool_wrong_param_type(self, get_n_rows_tool: ToolboxTool): |
| 78 | + """Invoke a tool with wrong param type.""" |
| 79 | + with pytest.raises( |
| 80 | + Exception, |
| 81 | + match='provided parameters were invalid: unable to parse value for "num_rows": .* not type "string"', |
| 82 | + ): |
| 83 | + await get_n_rows_tool(num_rows=2) |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.asyncio |
| 87 | +@pytest.mark.usefixtures("toolbox_server") |
| 88 | +class TestBindParams: |
| 89 | + async def test_bind_params( |
| 90 | + self, toolbox: ToolboxClient, get_n_rows_tool: ToolboxTool |
| 91 | + ): |
| 92 | + """Bind a param to an existing tool.""" |
| 93 | + new_tool = get_n_rows_tool.bind_parameters({"num_rows": "3"}) |
| 94 | + response = await new_tool() |
| 95 | + assert isinstance(response, str) |
| 96 | + assert "row1" in response |
| 97 | + assert "row2" in response |
| 98 | + assert "row3" in response |
| 99 | + assert "row4" not in response |
| 100 | + |
| 101 | + async def test_bind_params_callable( |
| 102 | + self, toolbox: ToolboxClient, get_n_rows_tool: ToolboxTool |
| 103 | + ): |
| 104 | + """Bind a callable param to an existing tool.""" |
| 105 | + new_tool = get_n_rows_tool.bind_parameters({"num_rows": lambda: "3"}) |
| 106 | + response = await new_tool() |
| 107 | + assert isinstance(response, str) |
| 108 | + assert "row1" in response |
| 109 | + assert "row2" in response |
| 110 | + assert "row3" in response |
| 111 | + assert "row4" not in response |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.asyncio |
| 115 | +@pytest.mark.usefixtures("toolbox_server") |
| 116 | +class TestAuth: |
| 117 | + async def test_run_tool_unauth_with_auth( |
| 118 | + self, toolbox: ToolboxClient, auth_token2: str |
| 119 | + ): |
| 120 | + """Tests running a tool that doesn't require auth, with auth provided.""" |
| 121 | + tool = await toolbox.load_tool( |
| 122 | + "get-row-by-id", auth_token_getters={"my-test-auth": lambda: auth_token2} |
| 123 | + ) |
| 124 | + response = await tool(id="2") |
| 125 | + assert "row2" in response |
| 126 | + |
| 127 | + async def test_run_tool_no_auth(self, toolbox: ToolboxClient): |
| 128 | + """Tests running a tool requiring auth without providing auth.""" |
| 129 | + tool = await toolbox.load_tool("get-row-by-id-auth") |
| 130 | + with pytest.raises( |
| 131 | + Exception, |
| 132 | + match="tool invocation not authorized. Please make sure your specify correct auth headers", |
| 133 | + ): |
| 134 | + await tool(id="2") |
| 135 | + |
| 136 | + async def test_run_tool_wrong_auth(self, toolbox: ToolboxClient, auth_token2: str): |
| 137 | + """Tests running a tool with incorrect auth. The tool |
| 138 | + requires a different authentication than the one provided.""" |
| 139 | + tool = await toolbox.load_tool("get-row-by-id-auth") |
| 140 | + auth_tool = tool.add_auth_token_getters({"my-test-auth": lambda: auth_token2}) |
| 141 | + with pytest.raises( |
| 142 | + Exception, |
| 143 | + match="tool invocation not authorized", |
| 144 | + ): |
| 145 | + await auth_tool(id="2") |
| 146 | + |
| 147 | + async def test_run_tool_auth(self, toolbox: ToolboxClient, auth_token1: str): |
| 148 | + """Tests running a tool with correct auth.""" |
| 149 | + tool = await toolbox.load_tool("get-row-by-id-auth") |
| 150 | + auth_tool = tool.add_auth_token_getters({"my-test-auth": lambda: auth_token1}) |
| 151 | + response = await auth_tool(id="2") |
| 152 | + assert "row2" in response |
| 153 | + |
| 154 | + async def test_run_tool_param_auth_no_auth(self, toolbox: ToolboxClient): |
| 155 | + """Tests running a tool with a param requiring auth, without auth.""" |
| 156 | + tool = await toolbox.load_tool("get-row-by-email-auth") |
| 157 | + with pytest.raises( |
| 158 | + Exception, |
| 159 | + match="One or more of the following authn services are required to invoke this tool: my-test-auth", |
| 160 | + ): |
| 161 | + await tool() |
| 162 | + |
| 163 | + async def test_run_tool_param_auth(self, toolbox: ToolboxClient, auth_token1: str): |
| 164 | + """Tests running a tool with a param requiring auth, with correct auth.""" |
| 165 | + tool = await toolbox.load_tool( |
| 166 | + "get-row-by-email-auth", |
| 167 | + auth_token_getters={"my-test-auth": lambda: auth_token1}, |
| 168 | + ) |
| 169 | + response = await tool() |
| 170 | + assert "row4" in response |
| 171 | + assert "row5" in response |
| 172 | + assert "row6" in response |
| 173 | + |
| 174 | + async def test_run_tool_param_auth_no_field( |
| 175 | + self, toolbox: ToolboxClient, auth_token1: str |
| 176 | + ): |
| 177 | + """Tests running a tool with a param requiring auth, with insufficient auth.""" |
| 178 | + tool = await toolbox.load_tool( |
| 179 | + "get-row-by-content-auth", |
| 180 | + auth_token_getters={"my-test-auth": lambda: auth_token1}, |
| 181 | + ) |
| 182 | + with pytest.raises( |
| 183 | + Exception, |
| 184 | + match="no field named row_data in claims", |
| 185 | + ): |
| 186 | + await tool() |
0 commit comments