Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | """Tests for auth configs management.""" |
| 2 | |
| 3 | import json |
| 4 | from unittest.mock import Mock |
| 5 | |
| 6 | import httpx |
| 7 | import pytest |
| 8 | from composio_client import ProjectAuth |
| 9 | from composio.client import HttpClient |
| 10 | from composio.client.types import ( |
| 11 | auth_config_create_response, |
| 12 | auth_config_delete_response, |
| 13 | auth_config_list_response, |
| 14 | auth_config_retrieve_response, |
| 15 | auth_config_update_response, |
| 16 | auth_config_update_status_response, |
| 17 | ) |
| 18 | from composio.core.models.auth_configs import AuthConfigs |
| 19 | from pydantic import BaseModel |
| 20 | |
| 21 | |
| 22 | class TestAuthConfigs: |
| 23 | """Test suite for AuthConfigs class.""" |
| 24 | |
| 25 | @pytest.fixture |
| 26 | def mock_client(self): |
| 27 | """Create a mock client for testing.""" |
| 28 | client = Mock() |
| 29 | client.auth_configs = Mock() |
| 30 | client.auth_configs.list = Mock() |
| 31 | client.auth_configs.create = Mock() |
| 32 | client.auth_configs.retrieve = Mock() |
| 33 | client.auth_configs.update = Mock() |
| 34 | client.auth_configs.delete = Mock() |
| 35 | client.auth_configs.update_status = Mock() |
| 36 | client.not_given = object() # Sentinel value for optional params |
| 37 | return client |
| 38 | |
| 39 | @pytest.fixture |
| 40 | def auth_configs(self, mock_client): |
| 41 | """Create an AuthConfigs instance with mock client.""" |
| 42 | return AuthConfigs(client=mock_client) |
| 43 | |
| 44 | @pytest.fixture |
| 45 | def mock_auth_config_response(self): |
| 46 | """Mock auth config retrieve response.""" |
| 47 | response = Mock(spec=auth_config_retrieve_response.AuthConfigRetrieveResponse) |
| 48 | response.id = "auth_12345" |
| 49 | response.name = "Test Auth Config" |
| 50 | response.no_of_connections = 5 |
| 51 | response.status = "ENABLED" |
| 52 | response.toolkit = Mock() |
| 53 | response.toolkit.logo = "https://example.com/logo.png" |
| 54 | response.toolkit.slug = "github" |
| 55 | response.uuid = "uuid-12345" |
| 56 | response.auth_scheme = "OAUTH2" |
| 57 | response.credentials = { |
| 58 | "client_id": "test_client_id", |
| 59 | "client_secret": "test_client_secret", |
| 60 | } |
| 61 | response.expected_input_fields = [ |
| 62 | {"name": "client_id", "type": "string"}, |
| 63 | {"name": "client_secret", "type": "string"}, |
| 64 | ] |
| 65 | response.is_composio_managed = True |
| 66 | response.created_by = "user_123" |
| 67 | response.created_at = "2023-01-01T00:00:00Z" |
| 68 | response.last_updated_at = "2023-01-01T00:00:00Z" |
| 69 | return response |
| 70 | |
| 71 | def test_constructor_creates_instance(self, auth_configs, mock_client): |
| 72 | """Test that AuthConfigs instance is created successfully.""" |
| 73 | assert isinstance(auth_configs, AuthConfigs) |
| 74 | assert auth_configs._client is mock_client |
| 75 | |
| 76 | # List tests |
| 77 | def test_list_without_params(self, auth_configs, mock_client): |
| 78 | """Test listing auth configs without query parameters.""" |
| 79 | mock_response = Mock(spec=auth_config_list_response.AuthConfigListResponse) |
| 80 | mock_response.items = [] |
| 81 | mock_response.next_cursor = None |
| 82 | mock_response.total_pages = 0 |
| 83 | mock_client.auth_configs.list.return_value = mock_response |
| 84 | |
| 85 | result = auth_configs.list() |
| 86 | |
| 87 | mock_client.auth_configs.list.assert_called_once_with() |
| 88 | assert result == mock_response |
| 89 | |
| 90 | def test_list_with_params(self, auth_configs, mock_client): |
| 91 | """Test listing auth configs with query parameters.""" |
| 92 | mock_response = Mock(spec=auth_config_list_response.AuthConfigListResponse) |
| 93 | mock_response.items = [] |
| 94 | mock_client.auth_configs.list.return_value = mock_response |
| 95 | |
| 96 | result = auth_configs.list( |
| 97 | cursor="cursor_123", |
| 98 | is_composio_managed=True, |
| 99 | limit=10, |
| 100 | toolkit_slug="github", |
| 101 | ) |
| 102 | |
| 103 | mock_client.auth_configs.list.assert_called_once_with( |
| 104 | cursor="cursor_123", |
| 105 | is_composio_managed=True, |
| 106 | limit=10, |
| 107 | toolkit_slug="github", |
| 108 | ) |
| 109 | assert result == mock_response |
| 110 | |
| 111 | # Create tests |
| 112 | def test_create_with_default_composio_managed_auth(self, auth_configs, mock_client): |
| 113 | """Test creating auth config with default Composio managed type.""" |
| 114 | mock_auth_config = Mock(spec=auth_config_create_response.AuthConfig) |
| 115 | mock_auth_config.id = "auth_12345" |
| 116 | mock_auth_config.auth_scheme = "OAUTH2" |
| 117 | mock_auth_config.is_composio_managed = True |
| 118 | |
| 119 | mock_response = Mock(spec=auth_config_create_response.AuthConfigCreateResponse) |
| 120 | mock_response.auth_config = mock_auth_config |
| 121 | |
| 122 | mock_client.auth_configs.create.return_value = mock_response |
| 123 | |
| 124 | options = { |
| 125 | "type": "use_composio_managed_auth", |
| 126 | "name": "My GitHub Config", |
| 127 | } |
| 128 | |
| 129 | result = auth_configs.create("github", options) |
| 130 | |
| 131 | mock_client.auth_configs.create.assert_called_once() |
| 132 | call_args = mock_client.auth_configs.create.call_args |
| 133 | assert call_args.kwargs["toolkit"] == {"slug": "github"} |
| 134 | assert call_args.kwargs["auth_config"] == options |
| 135 | assert result == mock_auth_config |
| 136 | |
| 137 | def test_create_with_custom_auth_and_credentials(self, auth_configs, mock_client): |
| 138 | """Test creating custom auth config with credentials.""" |
| 139 | mock_auth_config = Mock(spec=auth_config_create_response.AuthConfig) |
| 140 | mock_auth_config.id = "auth_12345" |
| 141 | mock_auth_config.auth_scheme = "OAUTH2" |
| 142 | mock_auth_config.is_composio_managed = False |
| 143 | |
| 144 | mock_response = Mock(spec=auth_config_create_response.AuthConfigCreateResponse) |
| 145 | mock_response.auth_config = mock_auth_config |
| 146 | |
| 147 | mock_client.auth_configs.create.return_value = mock_response |
| 148 | |
| 149 | options = { |
| 150 | "type": "use_custom_auth", |
| 151 | "name": "Custom GitHub Auth", |
| 152 | "auth_scheme": "OAUTH2", |
| 153 | "credentials": { |
| 154 | "client_id": "test_client_id", |
| 155 | "client_secret": "test_client_secret", |
| 156 | }, |
| 157 | } |
| 158 | |
| 159 | result = auth_configs.create("github", options) |
| 160 | |
| 161 | mock_client.auth_configs.create.assert_called_once() |
| 162 | call_args = mock_client.auth_configs.create.call_args |
| 163 | assert call_args.kwargs["toolkit"] == {"slug": "github"} |
| 164 | assert call_args.kwargs["auth_config"] == options |
| 165 | assert result.is_composio_managed is False |
| 166 | assert result.auth_scheme == "OAUTH2" |
| 167 | |
| 168 | def test_create_with_tool_access_config(self, auth_configs, mock_client): |
| 169 | """Test creating auth config with tool access configuration.""" |
| 170 | mock_auth_config = Mock(spec=auth_config_create_response.AuthConfig) |
| 171 | mock_auth_config.id = "auth_12345" |
| 172 | |
| 173 | mock_response = Mock(spec=auth_config_create_response.AuthConfigCreateResponse) |
| 174 | mock_response.auth_config = mock_auth_config |
| 175 | |
| 176 | mock_client.auth_configs.create.return_value = mock_response |
| 177 | |
| 178 | options = { |
| 179 | "type": "use_composio_managed_auth", |
| 180 | "name": "Config with Tool Access", |
| 181 | "tool_access_config": { |
| 182 | "tools_for_connected_account_creation": ["GITHUB_CREATE_ISSUE"] |
| 183 | }, |
| 184 | } |
| 185 | |
| 186 | result = auth_configs.create("github", options) |
| 187 | |
| 188 | mock_client.auth_configs.create.assert_called_once() |
| 189 | assert result == mock_auth_config |
| 190 | |
| 191 | # Get tests |
| 192 | def test_get_retrieves_auth_config_by_id( |
| 193 | self, auth_configs, mock_client, mock_auth_config_response |
| 194 | ): |
| 195 | """Test retrieving auth config by ID.""" |
| 196 | mock_client.auth_configs.retrieve.return_value = mock_auth_config_response |
| 197 | |
| 198 | result = auth_configs.get("auth_12345") |
| 199 | |
| 200 | mock_client.auth_configs.retrieve.assert_called_once_with("auth_12345") |
| 201 | assert result == mock_auth_config_response |
| 202 | assert result.id == "auth_12345" |
| 203 | assert result.name == "Test Auth Config" |
| 204 | |
| 205 | def test_get_handles_not_found_error(self, auth_configs, mock_client): |
| 206 | """Test get handles API error when auth config not found.""" |
| 207 | mock_client.auth_configs.retrieve.side_effect = Exception( |
| 208 | "Auth config not found" |
| 209 | ) |
| 210 | |
| 211 | with pytest.raises(Exception) as exc_info: |
| 212 | auth_configs.get("nonexistent_auth") |
| 213 | |
| 214 | assert "Auth config not found" in str(exc_info.value) |
| 215 | |
| 216 | # Update tests |
| 217 | def test_update_custom_auth_config_with_credentials( |
| 218 | self, auth_configs, mock_client |
| 219 | ): |
| 220 | """Test updating custom auth config with credentials.""" |
| 221 | mock_response = auth_config_update_response.AuthConfigUpdateResponse( |
| 222 | success=True, message="Successfully updated auth config" |
| 223 | ) |
| 224 | mock_client.auth_configs.update.return_value = mock_response |
| 225 | |
| 226 | options = { |
| 227 | "type": "custom", |
| 228 | "credentials": { |
| 229 | "client_id": "new_client_id", |
| 230 | "client_secret": "new_client_secret", |
| 231 | }, |
| 232 | } |
| 233 | |
| 234 | result = auth_configs.update("auth_12345", options=options) |
| 235 | |
| 236 | mock_client.auth_configs.update.assert_called_once() |
| 237 | call_args = mock_client.auth_configs.update.call_args |
| 238 | assert call_args.kwargs["nanoid"] == "auth_12345" |
| 239 | assert call_args.kwargs["type"] == "custom" |
| 240 | assert call_args.kwargs["credentials"] == options["credentials"] |
| 241 | assert result == mock_response |
| 242 | |
| 243 | def test_update_with_is_enabled_for_tool_router(self, auth_configs, mock_client): |
| 244 | """Test updating auth config with isEnabledForToolRouter.""" |
| 245 | mock_response = auth_config_update_response.AuthConfigUpdateResponse( |
| 246 | success=True, message="Successfully updated auth config" |
| 247 | ) |
| 248 | mock_client.auth_configs.update.return_value = mock_response |
| 249 | |
| 250 | options = { |
| 251 | "type": "custom", |
| 252 | "credentials": {"api_key": "new_key"}, |
| 253 | "is_enabled_for_tool_router": True, |
| 254 | } |
| 255 | |
| 256 | result = auth_configs.update("auth_12345", options=options) |
| 257 | |
| 258 | call_args = mock_client.auth_configs.update.call_args |
| 259 | assert call_args.kwargs["is_enabled_for_tool_router"] is True |
| 260 | assert result == mock_response |
| 261 | |
| 262 | def test_update_with_tool_access_config(self, auth_configs, mock_client): |
| 263 | """Test updating auth config with tool access configuration.""" |
| 264 | mock_response = auth_config_update_response.AuthConfigUpdateResponse( |
| 265 | success=True, message="Successfully updated auth config" |
| 266 | ) |
| 267 | mock_client.auth_configs.update.return_value = mock_response |
| 268 | |
| 269 | options = { |
| 270 | "type": "custom", |
| 271 | "credentials": {"api_key": "new_key"}, |
| 272 | "tool_access_config": { |
| 273 | "tools_for_connected_account_creation": ["GITHUB_CREATE_ISSUE"] |
| 274 | }, |
| 275 | } |
| 276 | |
| 277 | result = auth_configs.update("auth_12345", options=options) |
| 278 | |
| 279 | call_args = mock_client.auth_configs.update.call_args |
| 280 | assert call_args.kwargs["tool_access_config"] == options["tool_access_config"] |
| 281 | assert result == mock_response |
| 282 | |
| 283 | def test_update_handles_api_error(self, auth_configs, mock_client): |
| 284 | """Test update handles API errors.""" |
| 285 | mock_client.auth_configs.update.side_effect = Exception("Update failed") |
| 286 | |
| 287 | options = { |
| 288 | "type": "custom", |
| 289 | "credentials": {"api_key": "key"}, |
| 290 | } |
| 291 | |
| 292 | with pytest.raises(Exception) as exc_info: |
| 293 | auth_configs.update("auth_12345", options=options) |
| 294 | |
| 295 | assert "Update failed" in str(exc_info.value) |
| 296 | |
| 297 | # Delete tests |
| 298 | def test_delete_auth_config_by_id(self, auth_configs, mock_client): |
| 299 | """Test deleting auth config by ID.""" |
| 300 | mock_response = auth_config_delete_response.AuthConfigDeleteResponse( |
| 301 | success=True, message="Successfully deleted auth config" |
| 302 | ) |
| 303 | mock_client.auth_configs.delete.return_value = mock_response |
| 304 | |
| 305 | result = auth_configs.delete("auth_12345") |
| 306 | |
| 307 | mock_client.auth_configs.delete.assert_called_once_with("auth_12345") |
| 308 | assert result == mock_response |
| 309 | |
| 310 | def test_delete_handles_api_error(self, auth_configs, mock_client): |
| 311 | """Test delete handles API errors.""" |
| 312 | mock_client.auth_configs.delete.side_effect = Exception("Delete failed") |
| 313 | |
| 314 | with pytest.raises(Exception) as exc_info: |
| 315 | auth_configs.delete("auth_12345") |
| 316 | |
| 317 | assert "Delete failed" in str(exc_info.value) |
| 318 | |
| 319 | # Enable/Disable tests |
| 320 | def test_enable_auth_config(self, auth_configs, mock_client): |
| 321 | """Test enabling auth config.""" |
| 322 | mock_response = ( |
| 323 | auth_config_update_status_response.AuthConfigUpdateStatusResponse( |
| 324 | success=True, message="Successfully updated auth config status" |
| 325 | ) |
| 326 | ) |
| 327 | mock_client.auth_configs.update_status.return_value = mock_response |
| 328 | |
| 329 | result = auth_configs.enable("auth_12345") |
| 330 | |
| 331 | mock_client.auth_configs.update_status.assert_called_once_with( |
| 332 | "ENABLED", nanoid="auth_12345" |
| 333 | ) |
| 334 | assert result == mock_response |
| 335 | |
| 336 | def test_disable_auth_config(self, auth_configs, mock_client): |
| 337 | """Test disabling auth config.""" |
| 338 | mock_response = ( |
| 339 | auth_config_update_status_response.AuthConfigUpdateStatusResponse( |
| 340 | success=True, message="Successfully updated auth config status" |
| 341 | ) |
| 342 | ) |
| 343 | mock_client.auth_configs.update_status.return_value = mock_response |
| 344 | |
| 345 | result = auth_configs.disable("auth_12345") |
| 346 | |
| 347 | mock_client.auth_configs.update_status.assert_called_once_with( |
| 348 | "DISABLED", nanoid="auth_12345" |
| 349 | ) |
| 350 | assert result == mock_response |
| 351 | |
| 352 | def test_enable_handles_api_error(self, auth_configs, mock_client): |
| 353 | """Test enable handles API errors.""" |
| 354 | mock_client.auth_configs.update_status.side_effect = Exception("Enable failed") |
| 355 | |
| 356 | with pytest.raises(Exception) as exc_info: |
| 357 | auth_configs.enable("auth_12345") |
| 358 | |
| 359 | assert "Enable failed" in str(exc_info.value) |
| 360 | |
| 361 | def test_disable_handles_api_error(self, auth_configs, mock_client): |
| 362 | """Test disable handles API errors.""" |
| 363 | mock_client.auth_configs.update_status.side_effect = Exception("Disable failed") |
| 364 | |
| 365 | with pytest.raises(Exception) as exc_info: |
| 366 | auth_configs.disable("auth_12345") |
| 367 | |
| 368 | assert "Disable failed" in str(exc_info.value) |
| 369 | |
| 370 | # Edge cases |
| 371 | def test_update_with_missing_optional_fields(self, auth_configs, mock_client): |
| 372 | """Test update works with only required fields.""" |
| 373 | mock_response = auth_config_update_response.AuthConfigUpdateResponse( |
| 374 | success=True, message="Successfully updated auth config" |
| 375 | ) |
| 376 | mock_client.auth_configs.update.return_value = mock_response |
| 377 | |
| 378 | options = { |
| 379 | "type": "custom", |
| 380 | "credentials": {"api_key": "key"}, |
| 381 | } |
| 382 | |
| 383 | result = auth_configs.update("auth_12345", options=options) |
| 384 | |
| 385 | call_args = mock_client.auth_configs.update.call_args |
| 386 | # Verify that optional fields use the sentinel value |
| 387 | assert call_args.kwargs["is_enabled_for_tool_router"] == mock_client.not_given |
| 388 | assert call_args.kwargs["tool_access_config"] == mock_client.not_given |
| 389 | assert result == mock_response |
| 390 | |
| 391 | |
| 392 | @pytest.mark.parametrize( |
| 393 | ("operation", "method", "path", "response_type"), |
| 394 | [ |
| 395 | ( |
| 396 | "update_custom", |
| 397 | "PATCH", |
| 398 | "/api/v3.1/auth_configs/ac_test", |
| 399 | auth_config_update_response.AuthConfigUpdateResponse, |
| 400 | ), |
| 401 | ( |
| 402 | "update_default", |
| 403 | "PATCH", |
| 404 | "/api/v3.1/auth_configs/ac_test", |
| 405 | auth_config_update_response.AuthConfigUpdateResponse, |
| 406 | ), |
| 407 | ( |
| 408 | "delete", |
| 409 | "DELETE", |
| 410 | "/api/v3.1/auth_configs/ac_test", |
| 411 | auth_config_delete_response.AuthConfigDeleteResponse, |
| 412 | ), |
| 413 | ( |
| 414 | "enable", |
| 415 | "PATCH", |
| 416 | "/api/v3.1/auth_configs/ac_test/ENABLED", |
| 417 | auth_config_update_status_response.AuthConfigUpdateStatusResponse, |
| 418 | ), |
| 419 | ( |
| 420 | "disable", |
| 421 | "PATCH", |
| 422 | "/api/v3.1/auth_configs/ac_test/DISABLED", |
| 423 | auth_config_update_status_response.AuthConfigUpdateStatusResponse, |
| 424 | ), |
| 425 | ], |
| 426 | ) |
| 427 | def test_mutation_response_through_generated_client( |
| 428 | operation, method, path, response_type |
| 429 | ): |
| 430 | """Parse HTTP through the real client instead of mocking its return value.""" |
| 431 | payload = {"success": True, "message": "Successfully updated auth config"} |
| 432 | requests = [] |
| 433 | |
| 434 | def handler(request: httpx.Request) -> httpx.Response: |
| 435 | requests.append(request) |
| 436 | return httpx.Response(200, json=payload) |
| 437 | |
| 438 | with HttpClient( |
| 439 | provider="test", |
| 440 | auth=ProjectAuth("test"), |
| 441 | http_client=httpx.Client(transport=httpx.MockTransport(handler)), |
| 442 | ) as client: |
| 443 | auth_configs = AuthConfigs(client) |
| 444 | calls = { |
| 445 | "update_custom": lambda: auth_configs.update( |
| 446 | "ac_test", |
| 447 | options={"type": "custom", "credentials": {"scopes": "read:user"}}, |
| 448 | ), |
| 449 | "update_default": lambda: auth_configs.update( |
| 450 | "ac_test", |
| 451 | options={"type": "default", "is_enabled_for_tool_router": True}, |
| 452 | ), |
| 453 | "delete": lambda: auth_configs.delete("ac_test"), |
| 454 | "enable": lambda: auth_configs.enable("ac_test"), |
| 455 | "disable": lambda: auth_configs.disable("ac_test"), |
| 456 | } |
| 457 | result = calls[operation]() |
| 458 | |
| 459 | assert len(requests) == 1 |
| 460 | assert requests[0].method == method |
| 461 | assert requests[0].url.path == path |
| 462 | if operation == "update_default": |
| 463 | assert json.loads(requests[0].content)["type"] == "default" |
| 464 | elif operation == "update_custom": |
| 465 | assert json.loads(requests[0].content)["type"] == "custom" |
| 466 | assert isinstance(result, response_type) |
| 467 | assert isinstance(result, BaseModel) |
| 468 | assert result.success is True |
| 469 | assert result.message == payload["message"] |
| 470 | assert result.model_dump(exclude_unset=True) == payload |
| 471 | |
| 472 | |
| 473 | def test_create_on_named_managed_app_through_generated_client(): |
| 474 | """managed_app_id reaches the API and the response says which app was used.""" |
| 475 | requests = [] |
| 476 | |
| 477 | def handler(request: httpx.Request) -> httpx.Response: |
| 478 | requests.append(request) |
| 479 | return httpx.Response( |
| 480 | 201, |
| 481 | json={ |
| 482 | "toolkit": {"slug": "slack"}, |
| 483 | "auth_config": { |
| 484 | "id": "ac_test", |
| 485 | "auth_scheme": "OAUTH2", |
| 486 | "is_composio_managed": True, |
| 487 | "restrict_to_following_tools": [], |
| 488 | "managed_app_id": "ma_second00001", |
| 489 | }, |
| 490 | }, |
| 491 | ) |
| 492 | |
| 493 | with HttpClient( |
| 494 | provider="test", |
| 495 | auth=ProjectAuth("test"), |
| 496 | http_client=httpx.Client(transport=httpx.MockTransport(handler)), |
| 497 | ) as client: |
| 498 | result = AuthConfigs(client).create( |
| 499 | "slack", |
| 500 | {"type": "use_composio_managed_auth", "managed_app_id": "ma_second00001"}, |
| 501 | ) |
| 502 | |
| 503 | assert json.loads(requests[0].content)["auth_config"] == { |
| 504 | "type": "use_composio_managed_auth", |
| 505 | "managed_app_id": "ma_second00001", |
| 506 | } |
| 507 | assert result.id == "ac_test" |
| 508 | assert result.managed_app_id == "ma_second00001" |