Skip to content

Commit 57bb133

Browse files
committed
fix
1 parent f5217de commit 57bb133

File tree

3 files changed

+155
-150
lines changed

3 files changed

+155
-150
lines changed

tests/test_ve_identity_function_tool.py

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
class TestVeIdentityFunctionToolInit:
2828
"""Tests for VeIdentityFunctionTool initialization."""
2929

30-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
30+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
3131
def test_init_with_api_key_auth(self, mock_identity_client):
3232
"""Test initializing with API key auth config."""
33+
3334
async def test_func(api_key: str):
3435
return f"Called with {api_key}"
3536

@@ -41,26 +42,24 @@ async def test_func(api_key: str):
4142
assert tool._auth_config == config
4243
assert tool._into == "api_key"
4344

44-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
45+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
4546
def test_init_with_oauth2_auth(self, mock_identity_client):
4647
"""Test initializing with OAuth2 auth config."""
48+
4749
async def test_func(access_token: str):
4850
return f"Called with {access_token}"
4951

50-
config = oauth2_auth(
51-
provider_name="github",
52-
scopes=["repo"],
53-
auth_flow="M2M"
54-
)
52+
config = oauth2_auth(provider_name="github", scopes=["repo"], auth_flow="M2M")
5553
tool = VeIdentityFunctionTool(func=test_func, auth_config=config)
5654

5755
assert tool.func == test_func
5856
assert tool._auth_config == config
5957
assert tool._into == "access_token"
6058

61-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
59+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
6260
def test_init_with_workload_auth(self, mock_identity_client):
6361
"""Test initializing with workload auth config."""
62+
6463
async def test_func(access_token: str):
6564
return f"Called with {access_token}"
6665

@@ -71,23 +70,23 @@ async def test_func(access_token: str):
7170
assert tool._auth_config == config
7271
assert tool._into == "access_token"
7372

74-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
73+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
7574
def test_init_with_custom_into_parameter(self, mock_identity_client):
7675
"""Test initializing with custom 'into' parameter."""
76+
7777
async def test_func(custom_token: str):
7878
return f"Called with {custom_token}"
7979

8080
config = api_key_auth("test-provider")
8181
tool = VeIdentityFunctionTool(
82-
func=test_func,
83-
auth_config=config,
84-
into="custom_token"
82+
func=test_func, auth_config=config, into="custom_token"
8583
)
8684

8785
assert tool._into == "custom_token"
8886

8987
def test_init_with_unsupported_auth_config(self):
9088
"""Test that unsupported auth config raises ValueError."""
89+
9190
async def test_func(token: str):
9291
return f"Called with {token}"
9392

@@ -103,9 +102,10 @@ class TestVeIdentityFunctionToolRunAsync:
103102
"""Tests for VeIdentityFunctionTool.run_async method."""
104103

105104
@pytest.mark.asyncio
106-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
105+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
107106
async def test_run_async_with_api_key(self, mock_identity_client):
108107
"""Test run_async with API key authentication."""
108+
109109
async def test_func(api_key: str):
110110
return f"Result: {api_key}"
111111

@@ -122,17 +122,14 @@ async def test_func(api_key: str):
122122
tool.run_with_identity_auth.assert_called_once()
123123

124124
@pytest.mark.asyncio
125-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
125+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
126126
async def test_run_async_with_oauth2(self, mock_identity_client):
127127
"""Test run_async with OAuth2 authentication."""
128+
128129
async def test_func(access_token: str):
129130
return f"Result: {access_token}"
130131

131-
config = oauth2_auth(
132-
provider_name="github",
133-
scopes=["repo"],
134-
auth_flow="M2M"
135-
)
132+
config = oauth2_auth(provider_name="github", scopes=["repo"], auth_flow="M2M")
136133
tool = VeIdentityFunctionTool(func=test_func, auth_config=config)
137134

138135
# Mock the run_with_identity_auth method
@@ -145,18 +142,18 @@ async def test_func(access_token: str):
145142
tool.run_with_identity_auth.assert_called_once()
146143

147144
@pytest.mark.asyncio
148-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
149-
async def test_run_async_handles_auth_required_exception(self, mock_identity_client):
145+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
146+
async def test_run_async_handles_auth_required_exception(
147+
self, mock_identity_client
148+
):
150149
"""Test that run_async handles AuthRequiredException."""
151150
from veadk.integrations.ve_identity.auth_mixins import AuthRequiredException
152151

153152
async def test_func(access_token: str):
154153
return f"Result: {access_token}"
155154

156155
config = oauth2_auth(
157-
provider_name="github",
158-
scopes=["repo"],
159-
auth_flow="USER_FEDERATION"
156+
provider_name="github", scopes=["repo"], auth_flow="USER_FEDERATION"
160157
)
161158
tool = VeIdentityFunctionTool(func=test_func, auth_config=config)
162159

@@ -174,9 +171,10 @@ class TestVeIdentityFunctionToolExecuteWithCredential:
174171
"""Tests for VeIdentityFunctionTool._execute_with_credential method."""
175172

176173
@pytest.mark.asyncio
177-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
174+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
178175
async def test_execute_with_credential_injects_api_key(self, mock_identity_client):
179176
"""Test that _execute_with_credential injects API key."""
177+
180178
async def test_func(api_key: str):
181179
return f"Result: {api_key}"
182180

@@ -188,32 +186,31 @@ async def test_func(api_key: str):
188186
credential.api_key = "test-api-key"
189187

190188
# Mock parent's run_async
191-
with patch.object(tool.__class__.__bases__[1], 'run_async', new_callable=AsyncMock) as mock_run:
189+
with patch.object(
190+
tool.__class__.__bases__[1], "run_async", new_callable=AsyncMock
191+
) as mock_run:
192192
mock_run.return_value = "Result: test-api-key"
193193

194194
tool_context = Mock()
195-
result = await tool._execute_with_credential(
196-
args={},
197-
tool_context=tool_context,
198-
credential=credential
195+
await tool._execute_with_credential(
196+
args={}, tool_context=tool_context, credential=credential
199197
)
200198

201199
# Verify that run_async was called with injected api_key
202200
call_args = mock_run.call_args
203-
assert call_args[1]['args']['api_key'] == "test-api-key"
201+
assert call_args[1]["args"]["api_key"] == "test-api-key"
204202

205203
@pytest.mark.asyncio
206-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
207-
async def test_execute_with_credential_injects_oauth2_token(self, mock_identity_client):
204+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
205+
async def test_execute_with_credential_injects_oauth2_token(
206+
self, mock_identity_client
207+
):
208208
"""Test that _execute_with_credential injects OAuth2 access token."""
209+
209210
async def test_func(access_token: str):
210211
return f"Result: {access_token}"
211212

212-
config = oauth2_auth(
213-
provider_name="github",
214-
scopes=["repo"],
215-
auth_flow="M2M"
216-
)
213+
config = oauth2_auth(provider_name="github", scopes=["repo"], auth_flow="M2M")
217214
tool = VeIdentityFunctionTool(func=test_func, auth_config=config)
218215

219216
# Mock credential
@@ -222,17 +219,16 @@ async def test_func(access_token: str):
222219
credential.oauth2.access_token = "test-oauth2-token"
223220

224221
# Mock parent's run_async
225-
with patch.object(tool.__class__.__bases__[1], 'run_async', new_callable=AsyncMock) as mock_run:
222+
with patch.object(
223+
tool.__class__.__bases__[1], "run_async", new_callable=AsyncMock
224+
) as mock_run:
226225
mock_run.return_value = "Result: test-oauth2-token"
227226

228227
tool_context = Mock()
229-
result = await tool._execute_with_credential(
230-
args={},
231-
tool_context=tool_context,
232-
credential=credential
228+
await tool._execute_with_credential(
229+
args={}, tool_context=tool_context, credential=credential
233230
)
234231

235232
# Verify that run_async was called with injected access_token
236233
call_args = mock_run.call_args
237-
assert call_args[1]['args']['access_token'] == "test-oauth2-token"
238-
234+
assert call_args[1]["args"]["access_token"] == "test-oauth2-token"

tests/test_ve_identity_mcp_tool.py

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
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+
116
"""Unit tests for VeIdentityMcpTool."""
217

318
import pytest
@@ -12,60 +27,56 @@
1227
class TestVeIdentityMcpToolInit:
1328
"""Tests for VeIdentityMcpTool initialization."""
1429

15-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
30+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
1631
def test_init_with_api_key_auth(self, mock_identity_client):
1732
"""Test initializing with API key auth config."""
1833
mcp_tool = Mock()
1934
mcp_tool.name = "test_tool"
2035
mcp_tool.description = "Test tool description"
21-
36+
2237
mcp_session_manager = Mock()
2338
config = api_key_auth("test-provider")
24-
39+
2540
tool = VeIdentityMcpTool(
2641
mcp_tool=mcp_tool,
2742
mcp_session_manager=mcp_session_manager,
28-
auth_config=config
43+
auth_config=config,
2944
)
30-
45+
3146
assert tool.name == "test_tool"
3247
assert tool.description == "Test tool description"
3348
assert tool._mcp_tool == mcp_tool
3449
assert tool._mcp_session_manager == mcp_session_manager
3550

36-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
51+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
3752
def test_init_with_oauth2_auth(self, mock_identity_client):
3853
"""Test initializing with OAuth2 auth config."""
3954
mcp_tool = Mock()
4055
mcp_tool.name = "github_tool"
4156
mcp_tool.description = "GitHub tool"
42-
57+
4358
mcp_session_manager = Mock()
44-
config = oauth2_auth(
45-
provider_name="github",
46-
scopes=["repo"],
47-
auth_flow="M2M"
48-
)
49-
59+
config = oauth2_auth(provider_name="github", scopes=["repo"], auth_flow="M2M")
60+
5061
tool = VeIdentityMcpTool(
5162
mcp_tool=mcp_tool,
5263
mcp_session_manager=mcp_session_manager,
53-
auth_config=config
64+
auth_config=config,
5465
)
55-
66+
5667
assert tool.name == "github_tool"
5768
assert tool._auth_config == config
5869

5970
def test_init_with_none_mcp_tool(self):
6071
"""Test that initialization fails with None mcp_tool."""
6172
mcp_session_manager = Mock()
6273
config = api_key_auth("test-provider")
63-
74+
6475
with pytest.raises(ValueError, match="mcp_tool cannot be None"):
6576
VeIdentityMcpTool(
6677
mcp_tool=None,
6778
mcp_session_manager=mcp_session_manager,
68-
auth_config=config
79+
auth_config=config,
6980
)
7081

7182
def test_init_with_none_mcp_session_manager(self):
@@ -74,71 +85,68 @@ def test_init_with_none_mcp_session_manager(self):
7485
mcp_tool.name = "test_tool"
7586
mcp_tool.description = "Test"
7687
config = api_key_auth("test-provider")
77-
88+
7889
with pytest.raises(ValueError, match="mcp_session_manager cannot be None"):
7990
VeIdentityMcpTool(
80-
mcp_tool=mcp_tool,
81-
mcp_session_manager=None,
82-
auth_config=config
91+
mcp_tool=mcp_tool, mcp_session_manager=None, auth_config=config
8392
)
8493

8594

8695
class TestVeIdentityMcpToolRunAsync:
8796
"""Tests for VeIdentityMcpTool.run_async method."""
8897

8998
@pytest.mark.asyncio
90-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
99+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
91100
async def test_run_async_with_api_key(self, mock_identity_client):
92101
"""Test run_async with API key authentication."""
93102
mcp_tool = Mock()
94103
mcp_tool.name = "test_tool"
95104
mcp_tool.description = "Test"
96-
105+
97106
mcp_session_manager = Mock()
98107
config = api_key_auth("test-provider")
99-
108+
100109
tool = VeIdentityMcpTool(
101110
mcp_tool=mcp_tool,
102111
mcp_session_manager=mcp_session_manager,
103-
auth_config=config
112+
auth_config=config,
104113
)
105-
114+
106115
# Mock the run_with_identity_auth method
107116
tool.run_with_identity_auth = AsyncMock(return_value="Result: test-key")
108-
117+
109118
tool_context = Mock()
110119
result = await tool.run_async(args={}, tool_context=tool_context)
111-
120+
112121
assert result == "Result: test-key"
113122
tool.run_with_identity_auth.assert_called_once()
114123

115124
@pytest.mark.asyncio
116-
@patch('veadk.integrations.ve_identity.auth_mixins.IdentityClient')
117-
async def test_run_async_handles_auth_required_exception(self, mock_identity_client):
125+
@patch("veadk.integrations.ve_identity.auth_mixins.IdentityClient")
126+
async def test_run_async_handles_auth_required_exception(
127+
self, mock_identity_client
128+
):
118129
"""Test that run_async handles AuthRequiredException."""
119130
mcp_tool = Mock()
120131
mcp_tool.name = "test_tool"
121132
mcp_tool.description = "Test"
122-
133+
123134
mcp_session_manager = Mock()
124135
config = oauth2_auth(
125-
provider_name="github",
126-
scopes=["repo"],
127-
auth_flow="USER_FEDERATION"
136+
provider_name="github", scopes=["repo"], auth_flow="USER_FEDERATION"
128137
)
129-
138+
130139
tool = VeIdentityMcpTool(
131140
mcp_tool=mcp_tool,
132141
mcp_session_manager=mcp_session_manager,
133-
auth_config=config
142+
auth_config=config,
134143
)
135-
144+
136145
# Mock the run_with_identity_auth to raise AuthRequiredException
137146
auth_exception = AuthRequiredException("Please authorize")
138147
tool.run_with_identity_auth = AsyncMock(side_effect=auth_exception)
139-
148+
140149
tool_context = Mock()
141150
result = await tool.run_async(args={}, tool_context=tool_context)
142-
143-
assert result == "Please authorize"
144151

152+
assert result == "Please authorize"

0 commit comments

Comments
 (0)