|
| 1 | +# Copyright 2024 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 | +"""End-to-end tests for the toolbox SDK interacting with the toolbox server. |
| 16 | +
|
| 17 | +This file covers the following use cases: |
| 18 | +
|
| 19 | +1. Loading a tool. |
| 20 | +2. Loading a specific toolset. |
| 21 | +3. Loading the default toolset (contains all tools). |
| 22 | +4. Running a tool with |
| 23 | + a. Missing params. |
| 24 | + b. Wrong param type. |
| 25 | +5. Running a tool with no required auth, with auth provided. |
| 26 | +6. Running a tool with required auth: |
| 27 | + a. No auth provided. |
| 28 | + b. Wrong auth provided: The tool requires a different authentication |
| 29 | + than the one provided. |
| 30 | + c. Correct auth provided. |
| 31 | +7. Running a tool with a parameter that requires auth: |
| 32 | + a. No auth provided. |
| 33 | + b. Correct auth provided. |
| 34 | + c. Auth provided does not contain the required claim. |
| 35 | +""" |
| 36 | +import pytest |
| 37 | +import pytest_asyncio |
| 38 | + |
| 39 | +from toolbox_core.client import ToolboxClient |
| 40 | +from toolbox_core.tool import ToolboxTool |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.asyncio |
| 44 | +@pytest.mark.usefixtures("toolbox_server") |
| 45 | +class TestE2EClient: |
| 46 | + @pytest_asyncio.fixture(scope="function") |
| 47 | + async def toolbox(self): |
| 48 | + toolbox = ToolboxClient("http://localhost:5000") |
| 49 | + try: |
| 50 | + yield toolbox |
| 51 | + finally: |
| 52 | + await toolbox.close() |
| 53 | + |
| 54 | + @pytest_asyncio.fixture(scope="function") |
| 55 | + async def get_n_rows_tool(self, toolbox: ToolboxClient) -> ToolboxTool: |
| 56 | + tool = await toolbox.load_tool("get-n-rows") |
| 57 | + assert tool.__name__ == "get-n-rows" |
| 58 | + return tool |
| 59 | + |
| 60 | + #### Basic e2e tests |
| 61 | + @pytest.mark.parametrize( |
| 62 | + "toolset_name, expected_length, expected_tools", |
| 63 | + [ |
| 64 | + ("my-toolset", 1, ["get-row-by-id"]), |
| 65 | + ("my-toolset-2", 2, ["get-n-rows", "get-row-by-id"]), |
| 66 | + ], |
| 67 | + ) |
| 68 | + async def test_load_toolset_specific( |
| 69 | + self, |
| 70 | + toolbox: ToolboxClient, |
| 71 | + toolset_name: str, |
| 72 | + expected_length: int, |
| 73 | + expected_tools: list[str], |
| 74 | + ): |
| 75 | + toolset = await toolbox.load_toolset(toolset_name) |
| 76 | + assert len(toolset) == expected_length |
| 77 | + tool_names = {tool.__name__ for tool in toolset} |
| 78 | + assert tool_names == set(expected_tools) |
| 79 | + |
| 80 | + async def test_run_tool(self, get_n_rows_tool: ToolboxTool): |
| 81 | + response = await get_n_rows_tool(num_rows="2") |
| 82 | + |
| 83 | + assert isinstance(response, str) |
| 84 | + assert "row1" in response |
| 85 | + assert "row2" in response |
| 86 | + assert "row3" not in response |
| 87 | + |
| 88 | + async def test_run_tool_missing_params(self, get_n_rows_tool): |
| 89 | + with pytest.raises(TypeError, match="missing a required argument: 'num_rows'"): |
| 90 | + await get_n_rows_tool() |
| 91 | + |
| 92 | + async def test_run_tool_wrong_param_type(self, get_n_rows_tool: ToolboxTool): |
| 93 | + with pytest.raises( |
| 94 | + Exception, |
| 95 | + match='provided parameters were invalid: unable to parse value for "num_rows": .* not type "string"', |
| 96 | + ): |
| 97 | + await get_n_rows_tool(num_rows=2) # Pass the integer value |
| 98 | + |
| 99 | + ##### Auth tests |
| 100 | + @pytest.mark.asyncio |
| 101 | + async def test_run_tool_unauth_with_auth(self, toolbox, auth_token2): |
| 102 | + """Tests running a tool that doesn't require auth, with auth provided.""" |
| 103 | + tool = await toolbox.load_tool( |
| 104 | + "get-row-by-id", auth_token_getters={"my-test-auth": lambda: auth_token2} |
| 105 | + ) |
| 106 | + response = await tool(id="2") |
| 107 | + assert "row2" in response |
| 108 | + |
| 109 | + async def test_run_tool_no_auth(self, toolbox): |
| 110 | + """Tests running a tool requiring auth without providing auth.""" |
| 111 | + tool = await toolbox.load_tool( |
| 112 | + "get-row-by-id-auth", |
| 113 | + ) |
| 114 | + with pytest.raises( |
| 115 | + Exception, |
| 116 | + match="One of more of the following authn services are required to invoke this tool: my-test-auth", |
| 117 | + ): |
| 118 | + await tool(id="2") |
| 119 | + |
| 120 | + async def test_run_tool_wrong_auth(self, toolbox, auth_token2): |
| 121 | + """Tests running a tool with incorrect auth.""" |
| 122 | + tool = await toolbox.load_tool( |
| 123 | + "get-row-by-id-auth", |
| 124 | + ) |
| 125 | + auth_tool = tool.add_auth_token_getters("my-test-auth", lambda: auth_token2) |
| 126 | + with pytest.raises( |
| 127 | + Exception, |
| 128 | + match="tool invocation not authorized", |
| 129 | + ): |
| 130 | + await auth_tool(id="2") |
| 131 | + |
| 132 | + async def test_run_tool_auth(self, toolbox, auth_token1): |
| 133 | + """Tests running a tool with correct auth.""" |
| 134 | + tool = await toolbox.load_tool( |
| 135 | + "get-row-by-id-auth", |
| 136 | + ) |
| 137 | + auth_tool = tool.add_auth_token_getters("my-test-auth", lambda: auth_token1) |
| 138 | + response = await auth_tool(id="2") |
| 139 | + assert "row2" in response |
| 140 | + |
| 141 | + async def test_run_tool_param_auth_no_auth(self, toolbox): |
| 142 | + """Tests running a tool with a param requiring auth, without auth.""" |
| 143 | + tool = await toolbox.load_tool("get-row-by-email-auth") |
| 144 | + with pytest.raises( |
| 145 | + Exception, |
| 146 | + match="One of more of the following authn services are required to invoke this tool: my-test-auth", |
| 147 | + ): |
| 148 | + await tool(email="") |
| 149 | + |
| 150 | + async def test_run_tool_param_auth(self, toolbox, auth_token1): |
| 151 | + """Tests running a tool with a param requiring auth, with correct auth.""" |
| 152 | + tool = await toolbox.load_tool( |
| 153 | + "get-row-by-email-auth", |
| 154 | + auth_token_getters={"my-test-auth": lambda: auth_token1}, |
| 155 | + ) |
| 156 | + response = await tool() |
| 157 | + assert "row4" in response |
| 158 | + assert "row5" in response |
| 159 | + assert "row6" in response |
| 160 | + |
| 161 | + async def test_run_tool_param_auth_no_field(self, toolbox, auth_token1): |
| 162 | + """Tests running a tool with a param requiring auth, with insufficient auth.""" |
| 163 | + tool = await toolbox.load_tool( |
| 164 | + "get-row-by-content-auth", |
| 165 | + auth_token_getters={"my-test-auth": lambda: auth_token1}, |
| 166 | + ) |
| 167 | + with pytest.raises( |
| 168 | + Exception, |
| 169 | + match="no field named row_data in claims", |
| 170 | + ): |
| 171 | + await tool() |
0 commit comments