Skip to content

Commit 39c37fe

Browse files
committed
Fix: Restore accidentally deleted ve_identity test files
- Restore tests/test_ve_identity_auth_config.py - Restore tests/test_ve_identity_function_tool.py - Restore tests/test_ve_identity_mcp_tool.py - Restore tests/test_ve_identity_mcp_toolset.py - These files were accidentally deleted in previous commits and are essential for identity service testing
1 parent 5bd49b4 commit 39c37fe

File tree

4 files changed

+777
-0
lines changed

4 files changed

+777
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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+
"""Unit tests for ve_identity auth_config module."""
16+
17+
import pytest
18+
from veadk.integrations.ve_identity import (
19+
api_key_auth,
20+
oauth2_auth,
21+
workload_auth,
22+
ApiKeyAuthConfig,
23+
OAuth2AuthConfig,
24+
WorkloadAuthConfig,
25+
)
26+
27+
28+
class TestApiKeyAuth:
29+
"""Tests for api_key_auth factory function."""
30+
31+
def test_api_key_auth_basic(self):
32+
"""Test creating basic API key auth config."""
33+
config = api_key_auth("test-provider")
34+
35+
assert isinstance(config, ApiKeyAuthConfig)
36+
assert config.provider_name == "test-provider"
37+
assert config.auth_type == "api_key"
38+
assert config.region == "cn-beijing"
39+
assert config.identity_client is None
40+
41+
def test_api_key_auth_with_region(self):
42+
"""Test creating API key auth config with custom region."""
43+
config = api_key_auth("test-provider", region="us-east-1")
44+
45+
assert config.provider_name == "test-provider"
46+
assert config.region == "us-east-1"
47+
assert config.auth_type == "api_key"
48+
49+
def test_api_key_auth_empty_provider_name(self):
50+
"""Test that empty provider_name raises ValueError."""
51+
with pytest.raises(ValueError, match="provider_name cannot be empty"):
52+
api_key_auth("")
53+
54+
def test_api_key_auth_whitespace_provider_name(self):
55+
"""Test that whitespace-only provider_name raises ValueError."""
56+
with pytest.raises(ValueError, match="provider_name cannot be empty"):
57+
api_key_auth(" ")
58+
59+
60+
class TestOAuth2Auth:
61+
"""Tests for oauth2_auth factory function."""
62+
63+
def test_oauth2_auth_basic(self):
64+
"""Test creating basic OAuth2 auth config."""
65+
config = oauth2_auth(
66+
provider_name="github", scopes=["repo", "user"], auth_flow="M2M"
67+
)
68+
69+
assert isinstance(config, OAuth2AuthConfig)
70+
assert config.provider_name == "github"
71+
assert config.scopes == ["repo", "user"]
72+
assert config.auth_flow == "M2M"
73+
assert config.auth_type == "oauth2"
74+
assert config.force_authentication is False
75+
assert config.callback_url is None
76+
77+
def test_oauth2_auth_with_all_params(self):
78+
"""Test creating OAuth2 auth config with all parameters."""
79+
80+
def on_auth_url_callback(url: str):
81+
pass
82+
83+
config = oauth2_auth(
84+
provider_name="github",
85+
scopes=["repo", "user"],
86+
auth_flow="USER_FEDERATION",
87+
callback_url="https://example.com/callback",
88+
force_authentication=True,
89+
response_for_auth_required="Please authorize",
90+
on_auth_url=on_auth_url_callback,
91+
region="us-west-2",
92+
)
93+
94+
assert config.provider_name == "github"
95+
assert config.scopes == ["repo", "user"]
96+
assert config.auth_flow == "USER_FEDERATION"
97+
assert config.callback_url == "https://example.com/callback"
98+
assert config.force_authentication is True
99+
assert config.response_for_auth_required == "Please authorize"
100+
assert config.on_auth_url == on_auth_url_callback
101+
assert config.region == "us-west-2"
102+
103+
def test_oauth2_auth_empty_scopes(self):
104+
"""Test that empty scopes raises ValueError."""
105+
with pytest.raises(ValueError, match="scopes cannot be an empty list"):
106+
oauth2_auth(provider_name="github", scopes=[], auth_flow="M2M")
107+
108+
def test_oauth2_auth_empty_scope_value(self):
109+
"""Test that empty scope value raises ValueError."""
110+
with pytest.raises(ValueError, match="scope values cannot be empty"):
111+
oauth2_auth(provider_name="github", scopes=["repo", ""], auth_flow="M2M")
112+
113+
def test_oauth2_auth_duplicate_scopes_removed(self):
114+
"""Test that duplicate scopes are removed."""
115+
config = oauth2_auth(
116+
provider_name="github",
117+
scopes=["repo", "user", "repo", "user"],
118+
auth_flow="M2M",
119+
)
120+
121+
assert config.scopes == ["repo", "user"]
122+
123+
def test_oauth2_auth_invalid_callback_url(self):
124+
"""Test that invalid callback URL raises ValueError."""
125+
with pytest.raises(
126+
ValueError, match="callback_url must be a valid HTTP/HTTPS URL"
127+
):
128+
oauth2_auth(
129+
provider_name="github",
130+
scopes=["repo"],
131+
auth_flow="M2M",
132+
callback_url="invalid-url",
133+
)
134+
135+
def test_oauth2_auth_valid_https_callback_url(self):
136+
"""Test that valid HTTPS callback URL is accepted."""
137+
config = oauth2_auth(
138+
provider_name="github",
139+
scopes=["repo"],
140+
auth_flow="M2M",
141+
callback_url="https://example.com/callback",
142+
)
143+
144+
assert config.callback_url == "https://example.com/callback"
145+
146+
def test_oauth2_auth_valid_http_callback_url(self):
147+
"""Test that valid HTTP callback URL is accepted."""
148+
config = oauth2_auth(
149+
provider_name="github",
150+
scopes=["repo"],
151+
auth_flow="M2M",
152+
callback_url="http://localhost:8080/callback",
153+
)
154+
155+
assert config.callback_url == "http://localhost:8080/callback"
156+
157+
158+
class TestWorkloadAuth:
159+
"""Tests for workload_auth factory function."""
160+
161+
def test_workload_auth_basic(self):
162+
"""Test creating basic workload auth config."""
163+
config = workload_auth("test-provider")
164+
165+
assert isinstance(config, WorkloadAuthConfig)
166+
assert config.provider_name == "test-provider"
167+
assert config.auth_type == "workload"
168+
assert config.region == "cn-beijing"
169+
assert config.identity_client is None
170+
171+
def test_workload_auth_with_region(self):
172+
"""Test creating workload auth config with custom region."""
173+
config = workload_auth("test-provider", region="eu-west-1")
174+
175+
assert config.provider_name == "test-provider"
176+
assert config.region == "eu-west-1"
177+
assert config.auth_type == "workload"
178+
179+
def test_workload_auth_empty_provider_name(self):
180+
"""Test that empty provider_name raises ValueError."""
181+
with pytest.raises(ValueError, match="provider_name cannot be empty"):
182+
workload_auth("")

0 commit comments

Comments
 (0)