|
2 | 2 | import os |
3 | 3 | import unittest |
4 | 4 | from typing import List |
| 5 | +from unittest.mock import AsyncMock |
| 6 | +from unittest.mock import AsyncMock, MagicMock |
5 | 7 |
|
6 | 8 | from v2.nacos import ConfigParam |
7 | 9 | from v2.nacos.common.client_config import GRPCConfig |
|
12 | 14 | from v2.nacos.naming.nacos_naming_service import NacosNamingService |
13 | 15 | from v2.nacos.config.nacos_config_service import NacosConfigService |
14 | 16 | from v2.nacos.common.auth import CredentialsProvider, Credentials |
| 17 | +from v2.nacos.common.client_config import ClientConfig |
| 18 | +from v2.nacos.transport.auth_client import AuthClient |
15 | 19 |
|
16 | 20 | client_config = (ClientConfigBuilder() |
17 | 21 | .access_key(os.getenv('NACOS_ACCESS_KEY')) |
@@ -189,6 +193,90 @@ async def cb(instance_list: List[Instance]): |
189 | 193 |
|
190 | 194 | await client.shutdown() |
191 | 195 |
|
| 196 | + async def test_auth_login_url_with_root_context_path(self): |
| 197 | + """ |
| 198 | + [Issue #300] Verifies that when context_path is '/', the login URL |
| 199 | + is correctly formed as '/v1/auth/users/login' without double slashes. |
| 200 | + """ |
| 201 | + |
| 202 | + # 1. Setup config with root context path |
| 203 | + config = ClientConfig( |
| 204 | + server_addresses="http://127.0.0.1:8848", |
| 205 | + username="nacos", |
| 206 | + password="nacos", |
| 207 | + context_path="/" |
| 208 | + ) |
| 209 | + |
| 210 | + # 2. Create required mock objects |
| 211 | + mock_get_server_list = MagicMock(return_value=["http://127.0.0.1:8848"]) |
| 212 | + mock_http_agent = MagicMock() |
| 213 | + mock_logger = MagicMock() # Mock logger |
| 214 | + |
| 215 | + # 3. Initialize AuthClient with ALL required arguments |
| 216 | + client = AuthClient( |
| 217 | + client_config=config, |
| 218 | + get_server_list_func=mock_get_server_list, |
| 219 | + http_agent=mock_http_agent, |
| 220 | + logger=mock_logger |
| 221 | + ) |
| 222 | + |
| 223 | + # 4. Mock the HTTP request to capture the URL |
| 224 | + mock_response = (b'{"accessToken":"mock-token", "tokenTtl":18000}', None) |
| 225 | + mock_request = AsyncMock(return_value=mock_response) |
| 226 | + mock_http_agent.request = mock_request |
| 227 | + |
| 228 | + # 5. Execute login logic |
| 229 | + await client.get_access_token(force_refresh=True) |
| 230 | + |
| 231 | + # 6. Assert the generated URL |
| 232 | + called_url = mock_request.call_args[0][0] |
| 233 | + expected_url = "http://127.0.0.1:8848/v1/auth/users/login" |
| 234 | + |
| 235 | + self.assertEqual(called_url, expected_url, |
| 236 | + f"URL mismatch for root context_path. Expected '{expected_url}', but got '{called_url}'") |
| 237 | + |
| 238 | + async def test_auth_login_url_with_standard_context_path(self): |
| 239 | + """ |
| 240 | + [Regression Test] Verifies that when context_path is '/nacos', |
| 241 | + the login URL correctly includes the prefix. |
| 242 | + """ |
| 243 | + import logging |
| 244 | + |
| 245 | + # 1. Setup config with standard context path |
| 246 | + config = ClientConfig( |
| 247 | + server_addresses="http://127.0.0.1:8848", |
| 248 | + username="nacos", |
| 249 | + password="nacos", |
| 250 | + context_path="/nacos" |
| 251 | + ) |
| 252 | + |
| 253 | + # 2. Create required mock objects |
| 254 | + mock_get_server_list = MagicMock(return_value=["http://127.0.0.1:8848"]) |
| 255 | + mock_http_agent = MagicMock() |
| 256 | + mock_logger = MagicMock() # Mock logger |
| 257 | + |
| 258 | + # 3. Initialize AuthClient with ALL required arguments |
| 259 | + client = AuthClient( |
| 260 | + client_config=config, |
| 261 | + get_server_list_func=mock_get_server_list, |
| 262 | + http_agent=mock_http_agent, |
| 263 | + logger=mock_logger |
| 264 | + ) |
| 265 | + |
| 266 | + # 4. Mock the HTTP request |
| 267 | + mock_response = (b'{"accessToken":"mock-token", "tokenTtl":18000}', None) |
| 268 | + mock_request = AsyncMock(return_value=mock_response) |
| 269 | + mock_http_agent.request = mock_request |
| 270 | + |
| 271 | + # 5. Execute login logic |
| 272 | + await client.get_access_token(force_refresh=True) |
| 273 | + |
| 274 | + # 6. Assert the generated URL |
| 275 | + called_url = mock_request.call_args[0][0] |
| 276 | + expected_url = "http://127.0.0.1:8848/nacos/v1/auth/users/login" |
| 277 | + |
| 278 | + self.assertEqual(called_url, expected_url, |
| 279 | + f"URL mismatch for standard context_path. Expected '{expected_url}', but got '{called_url}'") |
192 | 280 |
|
193 | 281 | if __name__ == '__main__': |
194 | 282 | unittest.main() |
0 commit comments