Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """Tests for file helper functionality in composio/core/models/_files.py. |
| 2 | |
| 3 | These tests ensure that the FileHelper class correctly handles JSON schemas |
| 4 | that use anyOf, oneOf, allOf, or $ref instead of direct 'type' properties. |
| 5 | """ |
| 6 | |
| 7 | import tempfile |
| 8 | from pathlib import Path |
| 9 | from unittest.mock import Mock, patch, MagicMock |
| 10 | |
| 11 | import pytest |
| 12 | import requests |
| 13 | |
| 14 | from composio.client.types import Tool, tool_list_response |
| 15 | from composio.core.models._files import ( |
| 16 | FileDownloadable, |
| 17 | FileHelper, |
| 18 | FileUploadable, |
| 19 | upload, |
| 20 | _is_url, |
| 21 | _get_extension_from_mimetype, |
| 22 | _generate_timestamped_filename, |
| 23 | _truncate_filename, |
| 24 | _fetch_file_from_url, |
| 25 | _upload_bytes_to_s3, |
| 26 | _sanitize_url_for_logging, |
| 27 | _MAX_FILENAME_LENGTH, |
| 28 | ) |
| 29 | from composio.core.models.base import allow_tracking |
| 30 | from composio.exceptions import ( |
| 31 | BlockedInternalUrlError, |
| 32 | ErrorDownloadingFile, |
| 33 | ErrorUploadingFile, |
| 34 | ResponseTooLargeError, |
| 35 | SensitiveFilePathBlockedError, |
| 36 | UnsafePathComponentError, |
| 37 | ) |
| 38 | |
| 39 | |
| 40 | @pytest.fixture(autouse=True) |
| 41 | def disable_telemetry(): |
| 42 | """Disable telemetry for all tests to prevent thread issues.""" |
| 43 | token = allow_tracking.set(False) |
| 44 | yield |
| 45 | allow_tracking.reset(token) |
| 46 | |
| 47 | |
| 48 | @pytest.fixture |
| 49 | def mock_client(): |
| 50 | """Create a mock HTTP client.""" |
| 51 | return Mock() |
| 52 | |
| 53 | |
| 54 | @pytest.fixture |
| 55 | def file_helper(mock_client, monkeypatch, tmp_path): |
| 56 | """Create a FileHelper instance with a mock client. |
| 57 | |
| 58 | Sandboxes ``COMPOSIO_CACHE_DIR`` so the default-outdir download path |
| 59 | (``ensure_cache_directory()``) never touches the real home directory, |
| 60 | even though ``FileDownloadable.download`` itself is mocked in most of |
| 61 | these tests. |
| 62 | """ |
| 63 | monkeypatch.setenv("COMPOSIO_CACHE_DIR", str(tmp_path / ".composio")) |
| 64 | return FileHelper(client=mock_client) |
| 65 | |
| 66 | |
| 67 | @pytest.fixture |
| 68 | def mock_tool(): |
| 69 | """Create a mock tool for testing.""" |
| 70 | return Tool( |
| 71 | name="Test Tool", |
| 72 | slug="TEST_TOOL", |
| 73 | description="Test tool", |
| 74 | input_parameters={ |
| 75 | "properties": { |
| 76 | "query": {"type": "string"}, |
| 77 | }, |
| 78 | "type": "object", |
| 79 | }, |
| 80 | output_parameters={ |
| 81 | "properties": { |
| 82 | "data": {"type": "object", "properties": {}}, |
| 83 | }, |
| 84 | "type": "object", |
| 85 | }, |
| 86 | available_versions=["v1.0.0"], |
| 87 | version="v1.0.0", |
| 88 | scopes=[], |
| 89 | toolkit=tool_list_response.ItemToolkit( |
| 90 | name="Test Toolkit", slug="test_toolkit", logo="" |
| 91 | ), |
| 92 | deprecated=tool_list_response.ItemDeprecated( |
| 93 | available_versions=["v1.0.0"], |
| 94 | displayName="Test Tool", |
| 95 | version="v1.0.0", |
| 96 | toolkit=tool_list_response.ItemDeprecatedToolkit(logo=""), |
| 97 | is_deprecated=False, |
| 98 | ), |
| 99 | is_deprecated=False, |
| 100 | no_auth=False, |
| 101 | tags=[], |
| 102 | ) |
| 103 | |
| 104 | |
| 105 | class TestFileHelperSchemaHandling: |
| 106 | """Test cases for handling schemas without direct 'type' property. |
| 107 | |
| 108 | Regression tests for PLEN-766: KeyError: 'type' when schemas use |
| 109 | anyOf, oneOf, allOf, or $ref instead of direct type properties. |
| 110 | """ |
| 111 | |
| 112 | def test_substitute_file_uploads_with_oneof_schema(self, file_helper, mock_tool): |
| 113 | """Test that oneOf schemas don't cause KeyError in upload path.""" |
| 114 | schema_with_oneof = { |
| 115 | "properties": { |
| 116 | "input": { |
| 117 | "oneOf": [ |
| 118 | {"type": "string"}, |
| 119 | {"type": "object", "properties": {"file": {"type": "string"}}}, |
| 120 | ] |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | request = {"input": {"file": "test.txt"}} |
| 125 | |
| 126 | # Should not raise KeyError |
| 127 | result = file_helper._substitute_file_uploads_recursively( |
| 128 | tool=mock_tool, |
| 129 | schema=schema_with_oneof, |
| 130 | request=request.copy(), |
| 131 | ) |
| 132 | assert result == {"input": {"file": "test.txt"}} |
| 133 | |
| 134 | def test_substitute_file_uploads_with_anyof_schema(self, file_helper, mock_tool): |
| 135 | """Test that anyOf schemas don't cause KeyError in upload path.""" |
| 136 | schema_with_anyof = { |
| 137 | "properties": { |
| 138 | "data": { |
| 139 | "anyOf": [ |
| 140 | {"type": "string"}, |
| 141 | {"type": "object", "properties": {"value": {"type": "string"}}}, |
| 142 | ] |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | request = {"data": {"value": "test"}} |
| 147 | |
| 148 | # Should not raise KeyError |
| 149 | result = file_helper._substitute_file_uploads_recursively( |
| 150 | tool=mock_tool, |
| 151 | schema=schema_with_anyof, |
| 152 | request=request.copy(), |
| 153 | ) |
| 154 | assert result == {"data": {"value": "test"}} |
| 155 | |
| 156 | def test_substitute_file_uploads_with_allof_schema(self, file_helper, mock_tool): |
| 157 | """Test that allOf schemas don't cause KeyError in upload path.""" |
| 158 | schema_with_allof = { |
| 159 | "properties": { |
| 160 | "config": { |
| 161 | "allOf": [ |
| 162 | {"properties": {"name": {"type": "string"}}}, |
| 163 | {"properties": {"value": {"type": "string"}}}, |
| 164 | ] |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | request = {"config": {"name": "test", "value": "123"}} |
| 169 | |
| 170 | # Should not raise KeyError |
| 171 | result = file_helper._substitute_file_uploads_recursively( |
| 172 | tool=mock_tool, |
| 173 | schema=schema_with_allof, |
| 174 | request=request.copy(), |
| 175 | ) |
| 176 | assert result == {"config": {"name": "test", "value": "123"}} |
| 177 | |
| 178 | def test_substitute_file_uploads_with_ref_schema(self, file_helper, mock_tool): |
| 179 | """Test that $ref schemas don't cause KeyError in upload path.""" |
| 180 | schema_with_ref = { |
| 181 | "properties": {"reference": {"$ref": "#/definitions/SomeType"}} |
| 182 | } |
| 183 | request = {"reference": {"nested": "value"}} |
| 184 | |
| 185 | # Should not raise KeyError |
| 186 | result = file_helper._substitute_file_uploads_recursively( |
| 187 | tool=mock_tool, |
| 188 | schema=schema_with_ref, |
| 189 | request=request.copy(), |
| 190 | ) |
| 191 | assert result == {"reference": {"nested": "value"}} |
| 192 | |
| 193 | def test_substitute_file_downloads_with_oneof_schema(self, file_helper, mock_tool): |
| 194 | """Test that oneOf schemas don't cause KeyError in download path.""" |
| 195 | schema_with_oneof = { |
| 196 | "properties": { |
| 197 | "result": { |
| 198 | "oneOf": [ |
| 199 | {"type": "string"}, |
| 200 | {"type": "object", "properties": {"data": {"type": "string"}}}, |
| 201 | ] |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | response = {"result": {"data": "some value"}} |
| 206 | |
| 207 | # Should not raise KeyError |
| 208 | result = file_helper._substitute_file_downloads_recursively( |
| 209 | tool=mock_tool, |
| 210 | schema=schema_with_oneof, |
| 211 | request=response.copy(), |
| 212 | ) |
| 213 | assert result == {"result": {"data": "some value"}} |
| 214 | |
| 215 | def test_substitute_file_downloads_with_anyof_schema(self, file_helper, mock_tool): |
| 216 | """Test that anyOf schemas don't cause KeyError in download path.""" |
| 217 | schema_with_anyof = { |
| 218 | "properties": { |
| 219 | "output": { |
| 220 | "anyOf": [ |
| 221 | {"type": "string"}, |
| 222 | { |
| 223 | "type": "object", |
| 224 | "properties": {"nested": {"type": "string"}}, |
| 225 | }, |
| 226 | ] |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | response = {"output": {"nested": "value"}} |
| 231 | |
| 232 | # Should not raise KeyError |
| 233 | result = file_helper._substitute_file_downloads_recursively( |
| 234 | tool=mock_tool, |
| 235 | schema=schema_with_anyof, |
| 236 | request=response.copy(), |
| 237 | ) |
| 238 | assert result == {"output": {"nested": "value"}} |
| 239 | |
| 240 | def test_substitute_file_downloads_with_allof_schema(self, file_helper, mock_tool): |
| 241 | """Test that allOf schemas don't cause KeyError in download path.""" |
| 242 | schema_with_allof = { |
| 243 | "properties": { |
| 244 | "response": { |
| 245 | "allOf": [ |
| 246 | {"properties": {"status": {"type": "string"}}}, |
| 247 | {"properties": {"message": {"type": "string"}}}, |
| 248 | ] |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | response = {"response": {"status": "ok", "message": "success"}} |
| 253 | |
| 254 | # Should not raise KeyError |
| 255 | result = file_helper._substitute_file_downloads_recursively( |
| 256 | tool=mock_tool, |
| 257 | schema=schema_with_allof, |
| 258 | request=response.copy(), |
| 259 | ) |
| 260 | assert result == {"response": {"status": "ok", "message": "success"}} |
| 261 | |
| 262 | def test_substitute_file_downloads_with_ref_schema(self, file_helper, mock_tool): |
| 263 | """Test that $ref schemas don't cause KeyError in download path.""" |
| 264 | schema_with_ref = { |
| 265 | "properties": {"data": {"$ref": "#/definitions/ResponseType"}} |
| 266 | } |
| 267 | response = {"data": {"field": "value"}} |
| 268 | |
| 269 | # Should not raise KeyError |
| 270 | result = file_helper._substitute_file_downloads_recursively( |
| 271 | tool=mock_tool, |
| 272 | schema=schema_with_ref, |
| 273 | request=response.copy(), |
| 274 | ) |
| 275 | assert result == {"data": {"field": "value"}} |
| 276 | |
| 277 | def test_substitute_file_uploads_with_normal_type_still_works( |
| 278 | self, file_helper, mock_tool |
| 279 | ): |
| 280 | """Test that normal schemas with 'type' property still work correctly.""" |
| 281 | schema_with_type = { |
| 282 | "properties": { |
| 283 | "nested": { |
| 284 | "type": "object", |
| 285 | "properties": { |
| 286 | "value": {"type": "string"}, |
| 287 | }, |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | request = {"nested": {"value": "test"}} |
| 292 | |
| 293 | # Should process normally and recurse into nested object |
| 294 | result = file_helper._substitute_file_uploads_recursively( |
| 295 | tool=mock_tool, |
| 296 | schema=schema_with_type, |
| 297 | request=request.copy(), |
| 298 | ) |
| 299 | assert result == {"nested": {"value": "test"}} |
| 300 | |
| 301 | def test_substitute_file_downloads_with_normal_type_still_works( |
| 302 | self, file_helper, mock_tool |
| 303 | ): |
| 304 | """Test that normal schemas with 'type' property still work correctly.""" |
| 305 | schema_with_type = { |
| 306 | "properties": { |
| 307 | "data": { |
| 308 | "type": "object", |
| 309 | "properties": { |
| 310 | "result": {"type": "string"}, |
| 311 | }, |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | response = {"data": {"result": "success"}} |
| 316 | |
| 317 | # Should process normally and recurse into nested object |
| 318 | result = file_helper._substitute_file_downloads_recursively( |
| 319 | tool=mock_tool, |
| 320 | schema=schema_with_type, |
| 321 | request=response.copy(), |
| 322 | ) |
| 323 | assert result == {"data": {"result": "success"}} |
| 324 | |
| 325 | def test_substitute_with_empty_properties(self, file_helper, mock_tool): |
| 326 | """Test handling of schemas with empty properties.""" |
| 327 | schema_empty = {"properties": {}} |
| 328 | request = {"unknown": {"nested": "value"}} |
| 329 | |
| 330 | # Should not raise any errors |
| 331 | result = file_helper._substitute_file_uploads_recursively( |
| 332 | tool=mock_tool, |
| 333 | schema=schema_empty, |
| 334 | request=request.copy(), |
| 335 | ) |
| 336 | assert result == {"unknown": {"nested": "value"}} |
| 337 | |
| 338 | def test_substitute_with_no_properties_key(self, file_helper, mock_tool): |
| 339 | """Test handling of schemas without properties key.""" |
| 340 | schema_no_props = {"type": "object"} |
| 341 | request = {"data": {"value": "test"}} |
| 342 | |
| 343 | # Should return request unchanged |
| 344 | result = file_helper._substitute_file_uploads_recursively( |
| 345 | tool=mock_tool, |
| 346 | schema=schema_no_props, |
| 347 | request=request.copy(), |
| 348 | ) |
| 349 | assert result == {"data": {"value": "test"}} |
| 350 | |
| 351 | |
| 352 | class TestFileHelperRefDefsResolution: |
| 353 | """File flags reachable only through a $ref/$defs indirection. |
| 354 | |
| 355 | Regression coverage for |
| 356 | https://github.com/ComposioHQ/composio/issues/3506. References are inlined |
| 357 | once at the public-method boundary (``dereference_json_schema``), so the |
| 358 | walkers themselves stay reference-agnostic. |
| 359 | """ |
| 360 | |
| 361 | @patch("composio.core.models._files.FileDownloadable.download") |
| 362 | def test_download_resolves_ref_defs(self, mock_download, file_helper, mock_tool): |
| 363 | """GMAIL_GET_ATTACHMENT shape: file_downloadable two $ref hops deep.""" |
| 364 | mock_download.return_value = "/tmp/invoice.pdf" |
| 365 | mock_tool.output_parameters = { |
| 366 | "type": "object", |
| 367 | "$defs": { |
| 368 | "FileDownloadable": { |
| 369 | "type": "object", |
| 370 | "properties": { |
| 371 | "name": {"type": "string"}, |
| 372 | "mimetype": {"type": "string"}, |
| 373 | "s3url": {"type": "string"}, |
| 374 | }, |
| 375 | "required": ["name", "mimetype", "s3url"], |
| 376 | "file_downloadable": True, |
| 377 | }, |
| 378 | "GetAttachmentResponse": { |
| 379 | "type": "object", |
| 380 | "properties": { |
| 381 | "file": {"$ref": "#/$defs/FileDownloadable"}, |
| 382 | "display_url": {"type": "string"}, |
| 383 | }, |
| 384 | "required": ["file"], |
| 385 | }, |
| 386 | }, |
| 387 | "properties": { |
| 388 | "data": {"$ref": "#/$defs/GetAttachmentResponse"}, |
| 389 | "successful": {"type": "boolean"}, |
| 390 | }, |
| 391 | "required": ["data", "successful"], |
| 392 | } |
| 393 | response = { |
| 394 | "data": { |
| 395 | "file": { |
| 396 | "name": "invoice.pdf", |
| 397 | "mimetype": "application/pdf", |
| 398 | "s3url": "https://s3.example.com/invoice.pdf", |
| 399 | }, |
| 400 | "display_url": "https://mail.google.com/...", |
| 401 | }, |
| 402 | "successful": True, |
| 403 | } |
| 404 | |
| 405 | result = file_helper.substitute_file_downloads( |
| 406 | tool=mock_tool, |
| 407 | response=response, |
| 408 | ) |
| 409 | |
| 410 | assert result["data"]["file"] == "/tmp/invoice.pdf" |
| 411 | assert result["data"]["display_url"] == "https://mail.google.com/..." |
| 412 | assert result["successful"] is True |
| 413 | mock_download.assert_called_once() |
| 414 | |
| 415 | @patch("composio.core.models._files.FileUploadable.from_path") |
| 416 | def test_upload_resolves_ref_defs(self, mock_from_path, file_helper, mock_tool): |
| 417 | """$ref/$defs input schema exposes a nested file_uploadable leaf.""" |
| 418 | upload = MagicMock() |
| 419 | upload.model_dump.return_value = { |
| 420 | "name": "invoice.pdf", |
| 421 | "mimetype": "application/pdf", |
| 422 | "s3key": "uploads/invoice.pdf", |
| 423 | } |
| 424 | mock_from_path.return_value = upload |
| 425 | mock_tool.input_parameters = { |
| 426 | "type": "object", |
| 427 | "$defs": { |
| 428 | "FileUploadable": {"type": "string", "file_uploadable": True}, |
| 429 | "AttachmentInput": { |
| 430 | "type": "object", |
| 431 | "properties": { |
| 432 | "file": {"$ref": "#/$defs/FileUploadable"}, |
| 433 | "name": {"type": "string"}, |
| 434 | }, |
| 435 | "required": ["file"], |
| 436 | }, |
| 437 | }, |
| 438 | "properties": {"attachment": {"$ref": "#/$defs/AttachmentInput"}}, |
| 439 | "required": ["attachment"], |
| 440 | } |
| 441 | request = {"attachment": {"file": "/local/invoice.pdf", "name": "invoice.pdf"}} |
| 442 | |
| 443 | result = file_helper.substitute_file_uploads( |
| 444 | tool=mock_tool, |
| 445 | request=request, |
| 446 | ) |
| 447 | |
| 448 | assert result["attachment"]["file"] == { |
| 449 | "name": "invoice.pdf", |
| 450 | "mimetype": "application/pdf", |
| 451 | "s3key": "uploads/invoice.pdf", |
| 452 | } |
| 453 | assert result["attachment"]["name"] == "invoice.pdf" |
| 454 | mock_from_path.assert_called_once() |
| 455 | |
| 456 | def test_process_file_uploadable_schema_resolves_ref_defs(self, file_helper): |
| 457 | """The LLM-facing input transform inlines $ref and rewrites the leaf.""" |
| 458 | schema = { |
| 459 | "type": "object", |
| 460 | "$defs": { |
| 461 | "FileUploadable": {"type": "string", "file_uploadable": True}, |
| 462 | "AttachmentInput": { |
| 463 | "type": "object", |
| 464 | "properties": {"file": {"$ref": "#/$defs/FileUploadable"}}, |
| 465 | }, |
| 466 | }, |
| 467 | "properties": {"attachment": {"$ref": "#/$defs/AttachmentInput"}}, |
| 468 | } |
| 469 | |
| 470 | result = file_helper.process_file_uploadable_schema(schema) |
| 471 | |
| 472 | leaf = result["properties"]["attachment"]["properties"]["file"] |
| 473 | assert leaf["type"] == "string" |
| 474 | assert leaf["format"] == "path" |
| 475 | assert leaf["file_uploadable"] is True |
| 476 | # $defs is inlined away; no dangling references remain. |
| 477 | assert "$defs" not in result |
| 478 | assert "$ref" not in result["properties"]["attachment"] |
| 479 | |
| 480 | @patch("composio.core.models._files.FileDownloadable.download") |
| 481 | def test_dangling_ref_download_degrades_gracefully( |
| 482 | self, mock_download, file_helper, mock_tool |
| 483 | ): |
| 484 | """A $ref with no $defs block (issue #3307) must not raise (sentinel).""" |
| 485 | mock_tool.output_parameters = { |
| 486 | "type": "object", |
| 487 | "properties": { |
| 488 | "data": {"$ref": "#/$defs/Missing"}, |
| 489 | "successful": {"type": "boolean"}, |
| 490 | }, |
| 491 | } |
| 492 | response = {"data": {"value": "passthrough"}, "successful": True} |
| 493 | |
| 494 | # No $defs block at all: strict resolution would raise and abort the |
| 495 | # tool call. Sentinel mode keeps execution working — the unresolved |
| 496 | # branch is simply not treated as a file. |
| 497 | result = file_helper.substitute_file_downloads( |
| 498 | tool=mock_tool, |
| 499 | response=response, |
| 500 | ) |
| 501 | |
| 502 | assert result == {"data": {"value": "passthrough"}, "successful": True} |
| 503 | mock_download.assert_not_called() |
| 504 | |
| 505 | @patch("composio.core.models._files.FileDownloadable.download") |
| 506 | def test_recursive_ref_schema_does_not_recurse_infinitely( |
| 507 | self, mock_download, file_helper, mock_tool |
| 508 | ): |
| 509 | """A self-referential $ref schema must not blow the stack. |
| 510 | |
| 511 | A per-node lazy resolver without threaded cycle tracking infinite-loops |
| 512 | here; the centralized walker breaks the cycle with a sentinel. |
| 513 | """ |
| 514 | mock_download.return_value = "/tmp/node.bin" |
| 515 | mock_tool.output_parameters = { |
| 516 | "type": "object", |
| 517 | "$defs": { |
| 518 | "Node": { |
| 519 | "type": "object", |
| 520 | "properties": { |
| 521 | "file": { |
| 522 | "type": "object", |
| 523 | "properties": {"s3url": {"type": "string"}}, |
| 524 | "file_downloadable": True, |
| 525 | }, |
| 526 | "child": {"$ref": "#/$defs/Node"}, |
| 527 | }, |
| 528 | }, |
| 529 | }, |
| 530 | "properties": {"root": {"$ref": "#/$defs/Node"}}, |
| 531 | } |
| 532 | response = { |
| 533 | "root": { |
| 534 | "file": { |
| 535 | "name": "n.bin", |
| 536 | "mimetype": "application/octet-stream", |
| 537 | "s3url": "https://s3/x", |
| 538 | }, |
| 539 | "child": {"value": "leaf"}, |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | # Must complete without RecursionError. |
| 544 | result = file_helper.substitute_file_downloads( |
| 545 | tool=mock_tool, |
| 546 | response=response, |
| 547 | ) |
| 548 | |
| 549 | assert result["root"]["file"] == "/tmp/node.bin" |
| 550 | assert result["root"]["child"] == {"value": "leaf"} |
| 551 | |
| 552 | |
| 553 | class TestFileUploadableInUnionTypes: |
| 554 | """Test cases for file_uploadable detection in anyOf, oneOf, and allOf schemas.""" |
| 555 | |
| 556 | def test_has_file_property_direct(self, file_helper): |
| 557 | """Test _has_file_property detects direct file_uploadable.""" |
| 558 | schema = {"type": "string", "file_uploadable": True} |
| 559 | assert file_helper._has_file_property(schema, "file_uploadable") is True |
| 560 | |
| 561 | def test_has_file_property_in_anyof(self, file_helper): |
| 562 | """Test _has_file_property detects file_uploadable in anyOf.""" |
| 563 | schema = { |
| 564 | "anyOf": [ |
| 565 | {"type": "string", "file_uploadable": True}, |
| 566 | {"type": "null"}, |
| 567 | ] |
| 568 | } |
| 569 | assert file_helper._has_file_property(schema, "file_uploadable") is True |
| 570 | |
| 571 | def test_has_file_property_in_oneof(self, file_helper): |
| 572 | """Test _has_file_property detects file_uploadable in oneOf.""" |
| 573 | schema = { |
| 574 | "oneOf": [ |
| 575 | {"type": "string", "file_uploadable": True}, |
| 576 | {"type": "string", "description": "URL"}, |
| 577 | ] |
| 578 | } |
| 579 | assert file_helper._has_file_property(schema, "file_uploadable") is True |
| 580 | |
| 581 | def test_has_file_property_in_allof(self, file_helper): |
| 582 | """Test _has_file_property detects file_uploadable in allOf.""" |
| 583 | schema = { |
| 584 | "allOf": [ |
| 585 | {"type": "string", "file_uploadable": True}, |
| 586 | {"minLength": 1}, |
| 587 | ] |
| 588 | } |
| 589 | assert file_helper._has_file_property(schema, "file_uploadable") is True |
| 590 | |
| 591 | def test_has_file_property_nested_in_anyof(self, file_helper): |
| 592 | """Test _has_file_property detects file_uploadable nested in anyOf object.""" |
| 593 | schema = { |
| 594 | "anyOf": [ |
| 595 | { |
| 596 | "type": "object", |
| 597 | "properties": { |
| 598 | "attachment": {"type": "string", "file_uploadable": True} |
| 599 | }, |
| 600 | }, |
| 601 | {"type": "string"}, |
| 602 | ] |
| 603 | } |
| 604 | assert file_helper._has_file_property(schema, "file_uploadable") is True |
| 605 | |
| 606 | def test_has_file_property_not_present(self, file_helper): |
| 607 | """Test _has_file_property returns False when not present.""" |
| 608 | schema = { |
| 609 | "anyOf": [ |
| 610 | {"type": "string"}, |
| 611 | {"type": "null"}, |
| 612 | ] |
| 613 | } |
| 614 | assert file_helper._has_file_property(schema, "file_uploadable") is False |
| 615 | |
| 616 | def test_has_file_downloadable_in_anyof(self, file_helper): |
| 617 | """Test _has_file_property detects file_downloadable in anyOf.""" |
| 618 | schema = { |
| 619 | "anyOf": [ |
| 620 | {"type": "object", "file_downloadable": True}, |
| 621 | {"type": "null"}, |
| 622 | ] |
| 623 | } |
| 624 | assert file_helper._has_file_property(schema, "file_downloadable") is True |
| 625 | |
| 626 | def test_transform_schema_direct_file_uploadable(self, file_helper): |
| 627 | """Test _transform_schema_for_file_upload transforms direct file_uploadable.""" |
| 628 | schema = { |
| 629 | "type": "string", |
| 630 | "file_uploadable": True, |
| 631 | "description": "Upload a file", |
| 632 | } |
| 633 | result = file_helper._transform_schema_for_file_upload(schema) |
| 634 | assert result["format"] == "path" |
| 635 | assert result["type"] == "string" |
| 636 | assert result["file_uploadable"] is True |
| 637 | |
| 638 | def test_transform_schema_file_uploadable_in_anyof(self, file_helper): |
| 639 | """Test _transform_schema_for_file_upload transforms file_uploadable in anyOf.""" |
| 640 | schema = { |
| 641 | "anyOf": [ |
| 642 | {"type": "string", "file_uploadable": True, "description": "File path"}, |
| 643 | {"type": "null"}, |
| 644 | ] |
| 645 | } |
| 646 | result = file_helper._transform_schema_for_file_upload(schema) |
| 647 | assert result["anyOf"][0]["format"] == "path" |
| 648 | assert result["anyOf"][0]["file_uploadable"] is True |
| 649 | assert "format" not in result["anyOf"][1] |
| 650 | |
| 651 | def test_transform_schema_file_uploadable_in_oneof(self, file_helper): |
| 652 | """Test _transform_schema_for_file_upload transforms file_uploadable in oneOf.""" |
| 653 | schema = { |
| 654 | "oneOf": [ |
| 655 | {"type": "string", "file_uploadable": True}, |
| 656 | {"type": "string", "description": "URL reference"}, |
| 657 | ] |
| 658 | } |
| 659 | result = file_helper._transform_schema_for_file_upload(schema) |
| 660 | assert result["oneOf"][0]["format"] == "path" |
| 661 | assert result["oneOf"][0]["file_uploadable"] is True |
| 662 | assert "format" not in result["oneOf"][1] |
| 663 | |
| 664 | def test_transform_schema_file_uploadable_in_allof(self, file_helper): |
| 665 | """Test _transform_schema_for_file_upload transforms file_uploadable in allOf.""" |
| 666 | schema = { |
| 667 | "allOf": [ |
| 668 | {"type": "string", "file_uploadable": True}, |
| 669 | {"minLength": 1}, |
| 670 | ] |
| 671 | } |
| 672 | result = file_helper._transform_schema_for_file_upload(schema) |
| 673 | assert result["allOf"][0]["format"] == "path" |
| 674 | assert result["allOf"][0]["file_uploadable"] is True |
| 675 | |
| 676 | def test_transform_schema_nested_file_uploadable_in_anyof(self, file_helper): |
| 677 | """Test transform handles nested file_uploadable inside anyOf objects.""" |
| 678 | schema = { |
| 679 | "anyOf": [ |
| 680 | { |
| 681 | "type": "object", |
| 682 | "properties": { |
| 683 | "attachment": {"type": "string", "file_uploadable": True} |
| 684 | }, |
| 685 | }, |
| 686 | {"type": "string"}, |
| 687 | ] |
| 688 | } |
| 689 | result = file_helper._transform_schema_for_file_upload(schema) |
| 690 | assert result["anyOf"][0]["properties"]["attachment"]["format"] == "path" |
| 691 | assert result["anyOf"][0]["properties"]["attachment"]["file_uploadable"] is True |
| 692 | |
| 693 | def test_transform_schema_array_items_with_anyof(self, file_helper): |
| 694 | """Test transform handles array items with anyOf containing file_uploadable.""" |
| 695 | schema = { |
| 696 | "type": "array", |
| 697 | "items": { |
| 698 | "anyOf": [ |
| 699 | {"type": "string", "file_uploadable": True}, |
| 700 | {"type": "null"}, |
| 701 | ] |
| 702 | }, |
| 703 | } |
| 704 | result = file_helper._transform_schema_for_file_upload(schema) |
| 705 | assert result["items"]["anyOf"][0]["format"] == "path" |
| 706 | assert result["items"]["anyOf"][0]["file_uploadable"] is True |
| 707 | |
| 708 | def test_process_file_uploadable_schema_with_anyof(self, file_helper): |
| 709 | """Test process_file_uploadable_schema handles anyOf properties.""" |
| 710 | schema = { |
| 711 | "type": "object", |
| 712 | "properties": { |
| 713 | "fileInput": { |
| 714 | "anyOf": [ |
| 715 | {"type": "string", "file_uploadable": True}, |
| 716 | {"type": "null"}, |
| 717 | ] |
| 718 | }, |
| 719 | "text": {"type": "string"}, |
| 720 | }, |
| 721 | } |
| 722 | result = file_helper.process_file_uploadable_schema(schema) |
| 723 | assert result["properties"]["fileInput"]["anyOf"][0]["format"] == "path" |
| 724 | assert "format" not in result["properties"]["text"] |
| 725 | |
| 726 | |
| 727 | class TestFileUploadSubstitutionWithUnionTypes: |
| 728 | """Test cases for file upload substitution with anyOf, oneOf, and allOf schemas.""" |
| 729 | |
| 730 | def test_substitute_upload_with_file_uploadable_in_anyof( |
| 731 | self, file_helper, mock_tool, mock_client |
| 732 | ): |
| 733 | """Test that file_uploadable in anyOf triggers file upload.""" |
| 734 | mock_tool.input_parameters = { |
| 735 | "type": "object", |
| 736 | "properties": { |
| 737 | "fileInput": { |
| 738 | "anyOf": [ |
| 739 | {"type": "string", "file_uploadable": True}, |
| 740 | {"type": "null"}, |
| 741 | ] |
| 742 | } |
| 743 | }, |
| 744 | } |
| 745 | |
| 746 | # Mock the file upload |
| 747 | mock_client.files.create_presigned_url.return_value = Mock( |
| 748 | key="s3key/file.txt", new_presigned_url="https://s3.example.com/upload" |
| 749 | ) |
| 750 | |
| 751 | with ( |
| 752 | pytest.raises(Exception), |
| 753 | ): # Will fail because file doesn't exist, but proves detection works |
| 754 | file_helper._substitute_file_uploads_recursively( |
| 755 | tool=mock_tool, |
| 756 | schema=mock_tool.input_parameters, |
| 757 | request={"fileInput": "/path/to/file.txt"}, |
| 758 | ) |
| 759 | |
| 760 | def test_substitute_upload_null_value_in_anyof(self, file_helper, mock_tool): |
| 761 | """Test that null values in anyOf with file_uploadable are handled.""" |
| 762 | mock_tool.input_parameters = { |
| 763 | "type": "object", |
| 764 | "properties": { |
| 765 | "fileInput": { |
| 766 | "anyOf": [ |
| 767 | {"type": "string", "file_uploadable": True}, |
| 768 | {"type": "null"}, |
| 769 | ] |
| 770 | } |
| 771 | }, |
| 772 | } |
| 773 | |
| 774 | result = file_helper._substitute_file_uploads_recursively( |
| 775 | tool=mock_tool, |
| 776 | schema=mock_tool.input_parameters, |
| 777 | request={"fileInput": None}, |
| 778 | ) |
| 779 | # None/empty values should be removed |
| 780 | assert "fileInput" not in result |
| 781 | |
| 782 | def test_substitute_upload_preserves_null_optional_object_with_nested_file( |
| 783 | self, file_helper, mock_tool |
| 784 | ): |
| 785 | """A null container is not itself a file-uploadable leaf.""" |
| 786 | mock_tool.input_parameters = { |
| 787 | "type": "object", |
| 788 | "properties": { |
| 789 | "options": { |
| 790 | "type": "object", |
| 791 | "properties": { |
| 792 | "attachment": { |
| 793 | "type": "object", |
| 794 | "file_uploadable": True, |
| 795 | } |
| 796 | }, |
| 797 | } |
| 798 | }, |
| 799 | } |
| 800 | request = {"options": None} |
| 801 | |
| 802 | with patch.object(FileUploadable, "from_path") as from_path: |
| 803 | result = file_helper.substitute_file_uploads( |
| 804 | tool=mock_tool, |
| 805 | request=request, |
| 806 | ) |
| 807 | |
| 808 | assert result is request |
| 809 | assert result == {"options": None} |
| 810 | from_path.assert_not_called() |
| 811 | |
| 812 | def test_drop_empty_file_uploads_omits_empty_strings_without_uploading( |
| 813 | self, file_helper, mock_tool |
| 814 | ): |
| 815 | """Disabled auto-upload omits empty strings but preserves explicit nulls.""" |
| 816 | file_uploadable = { |
| 817 | "type": "object", |
| 818 | "file_uploadable": True, |
| 819 | "title": "FileUploadable", |
| 820 | "properties": { |
| 821 | "name": {"type": "string"}, |
| 822 | "mimetype": {"type": "string"}, |
| 823 | "s3key": {"type": "string"}, |
| 824 | }, |
| 825 | "required": ["name", "mimetype", "s3key"], |
| 826 | } |
| 827 | mock_tool.input_parameters = { |
| 828 | "type": "object", |
| 829 | "properties": { |
| 830 | "subject": {"type": "string"}, |
| 831 | "attachment": { |
| 832 | "anyOf": [ |
| 833 | file_uploadable, |
| 834 | {"type": "array", "items": file_uploadable}, |
| 835 | {"type": "null"}, |
| 836 | ], |
| 837 | "default": None, |
| 838 | }, |
| 839 | "extra": { |
| 840 | "anyOf": [ |
| 841 | {"type": "array", "items": file_uploadable}, |
| 842 | {"type": "null"}, |
| 843 | ] |
| 844 | }, |
| 845 | "nested": { |
| 846 | "type": "object", |
| 847 | "properties": {"file": {"$ref": "#/$defs/F"}}, |
| 848 | }, |
| 849 | "opaque": {"type": "object", "additionalProperties": True}, |
| 850 | "thread_id": {"type": "string"}, |
| 851 | }, |
| 852 | "$defs": {"F": file_uploadable}, |
| 853 | } |
| 854 | staged = {"name": "a.txt", "mimetype": "text/plain", "s3key": "k"} |
| 855 | request = { |
| 856 | "subject": "Test", |
| 857 | "attachment": "", |
| 858 | "extra": [None, "", staged, "/tmp/keep.txt"], |
| 859 | "nested": {"file": None}, |
| 860 | "opaque": {"preserve_identity": True}, |
| 861 | "thread_id": "", |
| 862 | } |
| 863 | original_request = { |
| 864 | "subject": "Test", |
| 865 | "attachment": "", |
| 866 | "extra": [None, "", staged, "/tmp/keep.txt"], |
| 867 | "nested": {"file": None}, |
| 868 | "opaque": {"preserve_identity": True}, |
| 869 | "thread_id": "", |
| 870 | } |
| 871 | |
| 872 | with patch.object(FileUploadable, "from_path") as from_path: |
| 873 | result = file_helper.drop_empty_file_uploads( |
| 874 | tool=mock_tool, request=request |
| 875 | ) |
| 876 | |
| 877 | from_path.assert_not_called() |
| 878 | assert result is not request |
| 879 | assert result == { |
| 880 | "subject": "Test", |
| 881 | "extra": [None, staged, "/tmp/keep.txt"], |
| 882 | "nested": {"file": None}, |
| 883 | "opaque": {"preserve_identity": True}, |
| 884 | # non-file empty strings are not the walker's business |
| 885 | "thread_id": "", |
| 886 | } |
| 887 | assert result["extra"] is not request["extra"] |
| 888 | assert result["extra"][1] is request["extra"][2] |
| 889 | assert result["nested"] is not request["nested"] |
| 890 | assert result["opaque"] is request["opaque"] |
| 891 | assert request == original_request |
| 892 | |
| 893 | def test_drop_empty_file_uploads_skips_dereference_for_non_file_schema( |
| 894 | self, file_helper, mock_tool |
| 895 | ): |
| 896 | """Default execution does not dereference schemas without file inputs.""" |
| 897 | mock_tool.input_parameters = { |
| 898 | "type": "object", |
| 899 | "properties": { |
| 900 | "filters": { |
| 901 | "type": "object", |
| 902 | "properties": {"query": {"type": "string"}}, |
| 903 | } |
| 904 | }, |
| 905 | } |
| 906 | request = {"filters": {"query": "open"}} |
| 907 | |
| 908 | with patch( |
| 909 | "composio.core.models._files.dereference_json_schema" |
| 910 | ) as dereference: |
| 911 | result = file_helper.drop_empty_file_uploads( |
| 912 | tool=mock_tool, request=request |
| 913 | ) |
| 914 | |
| 915 | assert result is request |
| 916 | dereference.assert_not_called() |
| 917 | |
| 918 | @pytest.mark.parametrize( |
| 919 | ("definitions_key", "ref"), |
| 920 | [ |
| 921 | ("$defs", "#/$defs/FileUploadable"), |
| 922 | ("definitions", "#/definitions/FileUploadable"), |
| 923 | ], |
| 924 | ) |
| 925 | def test_drop_empty_file_uploads_resolves_referenced_file_schema( |
| 926 | self, file_helper, mock_tool, definitions_key, ref |
| 927 | ): |
| 928 | """The raw cheap gate still admits modern and legacy referenced schemas.""" |
| 929 | mock_tool.input_parameters = { |
| 930 | "type": "object", |
| 931 | "properties": {"attachment": {"$ref": ref}}, |
| 932 | definitions_key: { |
| 933 | "FileUploadable": { |
| 934 | "type": "object", |
| 935 | "file_uploadable": True, |
| 936 | } |
| 937 | }, |
| 938 | } |
| 939 | |
| 940 | result = file_helper.drop_empty_file_uploads( |
| 941 | tool=mock_tool, request={"attachment": ""} |
| 942 | ) |
| 943 | |
| 944 | assert result == {} |
| 945 | |
| 946 | @pytest.mark.parametrize( |
| 947 | "attachment_schema", |
| 948 | [ |
| 949 | { |
| 950 | "type": "array", |
| 951 | "items": {"type": "object", "file_uploadable": True}, |
| 952 | }, |
| 953 | { |
| 954 | "items": {"type": "object", "file_uploadable": True}, |
| 955 | }, |
| 956 | { |
| 957 | "anyOf": [ |
| 958 | { |
| 959 | "type": "array", |
| 960 | "items": {"type": "object", "file_uploadable": True}, |
| 961 | }, |
| 962 | {"type": "null"}, |
| 963 | ] |
| 964 | }, |
| 965 | { |
| 966 | "anyOf": [ |
| 967 | { |
| 968 | "items": {"type": "object", "file_uploadable": True}, |
| 969 | }, |
| 970 | {"type": "null"}, |
| 971 | ] |
| 972 | }, |
| 973 | ], |
| 974 | ) |
| 975 | @pytest.mark.parametrize( |
| 976 | ("value", "expected_without_upload"), |
| 977 | [("", {}), (None, {"attachment": None})], |
| 978 | ) |
| 979 | def test_empty_values_follow_mode_for_array_only_file_schema( |
| 980 | self, file_helper, mock_tool, attachment_schema, value, expected_without_upload |
| 981 | ): |
| 982 | """Array-only file inputs omit empty strings and upload-mode nulls.""" |
| 983 | mock_tool.input_parameters = { |
| 984 | "type": "object", |
| 985 | "properties": {"attachment": attachment_schema}, |
| 986 | } |
| 987 | |
| 988 | dropped = file_helper.drop_empty_file_uploads( |
| 989 | tool=mock_tool, request={"attachment": value} |
| 990 | ) |
| 991 | with patch.object(FileUploadable, "from_path") as from_path: |
| 992 | uploaded = file_helper.substitute_file_uploads( |
| 993 | tool=mock_tool, request={"attachment": value} |
| 994 | ) |
| 995 | |
| 996 | assert dropped == expected_without_upload |
| 997 | assert uploaded == {} |
| 998 | from_path.assert_not_called() |
| 999 | |
| 1000 | @pytest.mark.parametrize("array_type", ["explicit", "inferred"]) |
| 1001 | def test_empty_string_is_preserved_when_non_file_string_variant_matches( |
| 1002 | self, file_helper, mock_tool, array_type |
| 1003 | ): |
| 1004 | """A composed string branch takes precedence over array-file cleanup.""" |
| 1005 | array_file_schema = { |
| 1006 | "items": { |
| 1007 | "type": "object", |
| 1008 | "file_uploadable": True, |
| 1009 | }, |
| 1010 | } |
| 1011 | if array_type == "explicit": |
| 1012 | array_file_schema["type"] = "array" |
| 1013 | |
| 1014 | mock_tool.input_parameters = { |
| 1015 | "type": "object", |
| 1016 | "properties": { |
| 1017 | "attachment": { |
| 1018 | "anyOf": [ |
| 1019 | array_file_schema, |
| 1020 | {"type": "string"}, |
| 1021 | ] |
| 1022 | } |
| 1023 | }, |
| 1024 | } |
| 1025 | |
| 1026 | dropped = file_helper.drop_empty_file_uploads( |
| 1027 | tool=mock_tool, request={"attachment": ""} |
| 1028 | ) |
| 1029 | with patch.object(FileUploadable, "from_path") as from_path: |
| 1030 | uploaded = file_helper.substitute_file_uploads( |
| 1031 | tool=mock_tool, request={"attachment": ""} |
| 1032 | ) |
| 1033 | |
| 1034 | assert dropped == {"attachment": ""} |
| 1035 | assert uploaded == {"attachment": ""} |
| 1036 | from_path.assert_not_called() |
| 1037 | |
| 1038 | def test_substitute_upload_empty_string_in_anyof(self, file_helper, mock_tool): |
| 1039 | """Test that empty string values in anyOf with file_uploadable are handled.""" |
| 1040 | mock_tool.input_parameters = { |
| 1041 | "type": "object", |
| 1042 | "properties": { |
| 1043 | "fileInput": { |
| 1044 | "anyOf": [ |
| 1045 | {"type": "string", "file_uploadable": True}, |
| 1046 | {"type": "null"}, |
| 1047 | ] |
| 1048 | } |
| 1049 | }, |
| 1050 | } |
| 1051 | |
| 1052 | result = file_helper._substitute_file_uploads_recursively( |
| 1053 | tool=mock_tool, |
| 1054 | schema=mock_tool.input_parameters, |
| 1055 | request={"fileInput": ""}, |
| 1056 | ) |
| 1057 | # Empty values should be removed |
| 1058 | assert "fileInput" not in result |
| 1059 | |
| 1060 | def test_substitute_upload_nested_in_anyof_object(self, file_helper, mock_tool): |
| 1061 | """Test file upload for nested file_uploadable inside anyOf object.""" |
| 1062 | mock_tool.input_parameters = { |
| 1063 | "type": "object", |
| 1064 | "properties": { |
| 1065 | "content": { |
| 1066 | "anyOf": [ |
| 1067 | { |
| 1068 | "type": "object", |
| 1069 | "properties": { |
| 1070 | "attachment": { |
| 1071 | "type": "string", |
| 1072 | "file_uploadable": True, |
| 1073 | } |
| 1074 | }, |
| 1075 | }, |
| 1076 | {"type": "string"}, |
| 1077 | ] |
| 1078 | } |
| 1079 | }, |
| 1080 | } |
| 1081 | |
| 1082 | # Should try to process the nested file_uploadable |
| 1083 | with pytest.raises(Exception): # Will fail because file doesn't exist |
| 1084 | file_helper._substitute_file_uploads_recursively( |
| 1085 | tool=mock_tool, |
| 1086 | schema=mock_tool.input_parameters, |
| 1087 | request={"content": {"attachment": "/path/to/file.pdf"}}, |
| 1088 | ) |
| 1089 | |
| 1090 | @patch("composio.core.models._files.FileUploadable.from_path") |
| 1091 | def test_substitute_upload_anyof_single_or_array_prefers_array_for_list_value( |
| 1092 | self, mock_from_path, file_helper, mock_tool |
| 1093 | ): |
| 1094 | """List runtime values should use the array branch and upload each item.""" |
| 1095 | mock_tool.input_parameters = { |
| 1096 | "type": "object", |
| 1097 | "properties": { |
| 1098 | "attachment": { |
| 1099 | "anyOf": [ |
| 1100 | {"type": "string", "file_uploadable": True}, |
| 1101 | { |
| 1102 | "type": "array", |
| 1103 | "items": {"type": "string", "file_uploadable": True}, |
| 1104 | }, |
| 1105 | ] |
| 1106 | } |
| 1107 | }, |
| 1108 | } |
| 1109 | |
| 1110 | upload_1 = MagicMock() |
| 1111 | upload_1.model_dump.return_value = { |
| 1112 | "name": "a.txt", |
| 1113 | "mimetype": "text/plain", |
| 1114 | "s3key": "key-a", |
| 1115 | } |
| 1116 | upload_2 = MagicMock() |
| 1117 | upload_2.model_dump.return_value = { |
| 1118 | "name": "b.txt", |
| 1119 | "mimetype": "text/plain", |
| 1120 | "s3key": "key-b", |
| 1121 | } |
| 1122 | mock_from_path.side_effect = [upload_1, upload_2] |
| 1123 | |
| 1124 | request = {"attachment": ["/tmp/a.txt", "/tmp/b.txt"]} |
| 1125 | result = file_helper._substitute_file_uploads_recursively( |
| 1126 | tool=mock_tool, |
| 1127 | schema=mock_tool.input_parameters, |
| 1128 | request=request, |
| 1129 | ) |
| 1130 | |
| 1131 | assert result is request |
| 1132 | assert result["attachment"] == [ |
| 1133 | {"name": "a.txt", "mimetype": "text/plain", "s3key": "key-a"}, |
| 1134 | {"name": "b.txt", "mimetype": "text/plain", "s3key": "key-b"}, |
| 1135 | ] |
| 1136 | assert [call.kwargs["file"] for call in mock_from_path.call_args_list] == [ |
| 1137 | "/tmp/a.txt", |
| 1138 | "/tmp/b.txt", |
| 1139 | ] |
| 1140 | |
| 1141 | @patch("composio.core.models._files.FileUploadable.from_path") |
| 1142 | def test_substitute_upload_anyof_single_or_array_keeps_string_behavior( |
| 1143 | self, mock_from_path, file_helper, mock_tool |
| 1144 | ): |
| 1145 | """String runtime values should still use the single-file branch.""" |
| 1146 | mock_tool.input_parameters = { |
| 1147 | "type": "object", |
| 1148 | "properties": { |
| 1149 | "attachment": { |
| 1150 | "anyOf": [ |
| 1151 | {"type": "string", "file_uploadable": True}, |
| 1152 | { |
| 1153 | "type": "array", |
| 1154 | "items": {"type": "string", "file_uploadable": True}, |
| 1155 | }, |
| 1156 | ] |
| 1157 | } |
| 1158 | }, |
| 1159 | } |
| 1160 | |
| 1161 | upload = MagicMock() |
| 1162 | upload.model_dump.return_value = { |
| 1163 | "name": "a.txt", |
| 1164 | "mimetype": "text/plain", |
| 1165 | "s3key": "key-a", |
| 1166 | } |
| 1167 | mock_from_path.return_value = upload |
| 1168 | |
| 1169 | result = file_helper._substitute_file_uploads_recursively( |
| 1170 | tool=mock_tool, |
| 1171 | schema=mock_tool.input_parameters, |
| 1172 | request={"attachment": "/tmp/a.txt"}, |
| 1173 | ) |
| 1174 | |
| 1175 | assert result["attachment"] == { |
| 1176 | "name": "a.txt", |
| 1177 | "mimetype": "text/plain", |
| 1178 | "s3key": "key-a", |
| 1179 | } |
| 1180 | mock_from_path.assert_called_once() |
| 1181 | assert mock_from_path.call_args.kwargs["file"] == "/tmp/a.txt" |
| 1182 | |
| 1183 | @patch("composio.core.models._files.FileUploadable.from_path") |
| 1184 | def test_substitute_upload_array_items_with_anyof_file_branch( |
| 1185 | self, mock_from_path, file_helper, mock_tool |
| 1186 | ): |
| 1187 | """Array item schemas can contain file-uploadable union branches.""" |
| 1188 | mock_tool.input_parameters = { |
| 1189 | "type": "object", |
| 1190 | "properties": { |
| 1191 | "attachments": { |
| 1192 | "type": "array", |
| 1193 | "items": { |
| 1194 | "anyOf": [ |
| 1195 | {"type": "string", "file_uploadable": True}, |
| 1196 | {"type": "null"}, |
| 1197 | ] |
| 1198 | }, |
| 1199 | } |
| 1200 | }, |
| 1201 | } |
| 1202 | |
| 1203 | upload_1 = MagicMock() |
| 1204 | upload_1.model_dump.return_value = {"s3key": "key-a"} |
| 1205 | upload_2 = MagicMock() |
| 1206 | upload_2.model_dump.return_value = {"s3key": "key-b"} |
| 1207 | mock_from_path.side_effect = [upload_1, upload_2] |
| 1208 | |
| 1209 | result = file_helper._substitute_file_uploads_recursively( |
| 1210 | tool=mock_tool, |
| 1211 | schema=mock_tool.input_parameters, |
| 1212 | request={"attachments": ["/tmp/a.txt", None, "", "/tmp/b.txt"]}, |
| 1213 | ) |
| 1214 | |
| 1215 | assert result["attachments"] == [{"s3key": "key-a"}, {"s3key": "key-b"}] |
| 1216 | assert [call.kwargs["file"] for call in mock_from_path.call_args_list] == [ |
| 1217 | "/tmp/a.txt", |
| 1218 | "/tmp/b.txt", |
| 1219 | ] |
| 1220 | |
| 1221 | @patch("composio.core.models._files.FileUploadable.from_path") |
| 1222 | def test_substitute_upload_array_drops_all_null_and_empty_items( |
| 1223 | self, mock_from_path, file_helper, mock_tool |
| 1224 | ): |
| 1225 | """Array of file-uploadable items collapses to [] when every item is null/empty.""" |
| 1226 | mock_tool.input_parameters = { |
| 1227 | "type": "object", |
| 1228 | "properties": { |
| 1229 | "attachments": { |
| 1230 | "type": "array", |
| 1231 | "items": {"type": "string", "file_uploadable": True}, |
| 1232 | } |
| 1233 | }, |
| 1234 | } |
| 1235 | |
| 1236 | result = file_helper._substitute_file_uploads_recursively( |
| 1237 | tool=mock_tool, |
| 1238 | schema=mock_tool.input_parameters, |
| 1239 | request={"attachments": [None, "", None]}, |
| 1240 | ) |
| 1241 | |
| 1242 | assert result["attachments"] == [] |
| 1243 | mock_from_path.assert_not_called() |
| 1244 | |
| 1245 | |
| 1246 | class TestFileDownloadSubstitutionWithUnionTypes: |
| 1247 | """Test cases for file download substitution with anyOf, oneOf, and allOf schemas.""" |
| 1248 | |
| 1249 | def test_substitute_download_with_file_downloadable_in_anyof( |
| 1250 | self, file_helper, mock_tool |
| 1251 | ): |
| 1252 | """Test that file_downloadable in anyOf triggers file download detection.""" |
| 1253 | mock_tool.output_parameters = { |
| 1254 | "type": "object", |
| 1255 | "properties": { |
| 1256 | "fileOutput": { |
| 1257 | "anyOf": [ |
| 1258 | { |
| 1259 | "type": "object", |
| 1260 | "file_downloadable": True, |
| 1261 | "properties": { |
| 1262 | "s3url": {"type": "string"}, |
| 1263 | "mimetype": {"type": "string"}, |
| 1264 | "name": {"type": "string"}, |
| 1265 | }, |
| 1266 | }, |
| 1267 | {"type": "null"}, |
| 1268 | ] |
| 1269 | } |
| 1270 | }, |
| 1271 | } |
| 1272 | |
| 1273 | # Should try to download the file (will fail but proves detection) |
| 1274 | with pytest.raises(Exception): |
| 1275 | file_helper._substitute_file_downloads_recursively( |
| 1276 | tool=mock_tool, |
| 1277 | schema=mock_tool.output_parameters, |
| 1278 | request={ |
| 1279 | "fileOutput": { |
| 1280 | "s3url": "https://s3.example.com/file.txt", |
| 1281 | "mimetype": "text/plain", |
| 1282 | "name": "file.txt", |
| 1283 | } |
| 1284 | }, |
| 1285 | ) |
| 1286 | |
| 1287 | def test_substitute_download_null_value_in_anyof(self, file_helper, mock_tool): |
| 1288 | """Test that null values in anyOf with file_downloadable are handled.""" |
| 1289 | mock_tool.output_parameters = { |
| 1290 | "type": "object", |
| 1291 | "properties": { |
| 1292 | "fileOutput": { |
| 1293 | "anyOf": [ |
| 1294 | {"type": "object", "file_downloadable": True}, |
| 1295 | {"type": "null"}, |
| 1296 | ] |
| 1297 | } |
| 1298 | }, |
| 1299 | } |
| 1300 | |
| 1301 | result = file_helper._substitute_file_downloads_recursively( |
| 1302 | tool=mock_tool, |
| 1303 | schema=mock_tool.output_parameters, |
| 1304 | request={"fileOutput": None}, |
| 1305 | ) |
| 1306 | assert result["fileOutput"] is None |
| 1307 | |
| 1308 | def test_substitute_download_nested_in_anyof_object(self, file_helper, mock_tool): |
| 1309 | """Test file download for nested file_downloadable inside anyOf object.""" |
| 1310 | mock_tool.output_parameters = { |
| 1311 | "type": "object", |
| 1312 | "properties": { |
| 1313 | "response": { |
| 1314 | "anyOf": [ |
| 1315 | { |
| 1316 | "type": "object", |
| 1317 | "properties": { |
| 1318 | "attachment": { |
| 1319 | "type": "object", |
| 1320 | "file_downloadable": True, |
| 1321 | "properties": { |
| 1322 | "s3url": {"type": "string"}, |
| 1323 | "mimetype": {"type": "string"}, |
| 1324 | "name": {"type": "string"}, |
| 1325 | }, |
| 1326 | } |
| 1327 | }, |
| 1328 | }, |
| 1329 | {"type": "string"}, |
| 1330 | ] |
| 1331 | } |
| 1332 | }, |
| 1333 | } |
| 1334 | |
| 1335 | # Should try to download the nested file |
| 1336 | with pytest.raises(Exception): |
| 1337 | file_helper._substitute_file_downloads_recursively( |
| 1338 | tool=mock_tool, |
| 1339 | schema=mock_tool.output_parameters, |
| 1340 | request={ |
| 1341 | "response": { |
| 1342 | "attachment": { |
| 1343 | "s3url": "https://s3.example.com/doc.pdf", |
| 1344 | "mimetype": "application/pdf", |
| 1345 | "name": "document.pdf", |
| 1346 | } |
| 1347 | } |
| 1348 | }, |
| 1349 | ) |
| 1350 | |
| 1351 | def test_substitute_download_with_file_downloadable_in_oneof( |
| 1352 | self, file_helper, mock_tool |
| 1353 | ): |
| 1354 | """Test that file_downloadable in oneOf triggers file download detection.""" |
| 1355 | mock_tool.output_parameters = { |
| 1356 | "type": "object", |
| 1357 | "properties": { |
| 1358 | "result": { |
| 1359 | "oneOf": [ |
| 1360 | { |
| 1361 | "type": "object", |
| 1362 | "file_downloadable": True, |
| 1363 | "properties": { |
| 1364 | "s3url": {"type": "string"}, |
| 1365 | "mimetype": {"type": "string"}, |
| 1366 | "name": {"type": "string"}, |
| 1367 | }, |
| 1368 | }, |
| 1369 | {"type": "string"}, |
| 1370 | ] |
| 1371 | } |
| 1372 | }, |
| 1373 | } |
| 1374 | |
| 1375 | with pytest.raises(Exception): |
| 1376 | file_helper._substitute_file_downloads_recursively( |
| 1377 | tool=mock_tool, |
| 1378 | schema=mock_tool.output_parameters, |
| 1379 | request={ |
| 1380 | "result": { |
| 1381 | "s3url": "https://s3.example.com/file.txt", |
| 1382 | "mimetype": "text/plain", |
| 1383 | "name": "file.txt", |
| 1384 | } |
| 1385 | }, |
| 1386 | ) |
| 1387 | |
| 1388 | def test_substitute_download_with_file_downloadable_in_allof( |
| 1389 | self, file_helper, mock_tool |
| 1390 | ): |
| 1391 | """Test that file_downloadable in allOf triggers file download detection.""" |
| 1392 | mock_tool.output_parameters = { |
| 1393 | "type": "object", |
| 1394 | "properties": { |
| 1395 | "image": { |
| 1396 | "allOf": [ |
| 1397 | { |
| 1398 | "type": "object", |
| 1399 | "file_downloadable": True, |
| 1400 | "properties": { |
| 1401 | "s3url": {"type": "string"}, |
| 1402 | "mimetype": {"type": "string"}, |
| 1403 | "name": {"type": "string"}, |
| 1404 | }, |
| 1405 | }, |
| 1406 | {"required": ["s3url"]}, |
| 1407 | ] |
| 1408 | } |
| 1409 | }, |
| 1410 | } |
| 1411 | |
| 1412 | with pytest.raises(Exception): |
| 1413 | file_helper._substitute_file_downloads_recursively( |
| 1414 | tool=mock_tool, |
| 1415 | schema=mock_tool.output_parameters, |
| 1416 | request={ |
| 1417 | "image": { |
| 1418 | "s3url": "https://s3.example.com/image.png", |
| 1419 | "mimetype": "image/png", |
| 1420 | "name": "image.png", |
| 1421 | } |
| 1422 | }, |
| 1423 | ) |
| 1424 | |
| 1425 | @patch("composio.core.models._files.FileDownloadable.download") |
| 1426 | def test_substitute_download_anyof_single_or_array_prefers_array_for_list_value( |
| 1427 | self, mock_download, file_helper, mock_tool |
| 1428 | ): |
| 1429 | """List runtime values should use the array branch and download each item.""" |
| 1430 | mock_tool.output_parameters = { |
| 1431 | "type": "object", |
| 1432 | "properties": { |
| 1433 | "attachment": { |
| 1434 | "anyOf": [ |
| 1435 | {"type": "object", "file_downloadable": True}, |
| 1436 | { |
| 1437 | "type": "array", |
| 1438 | "items": {"type": "object", "file_downloadable": True}, |
| 1439 | }, |
| 1440 | ] |
| 1441 | } |
| 1442 | }, |
| 1443 | } |
| 1444 | mock_download.side_effect = ["/tmp/a.txt", "/tmp/b.txt"] |
| 1445 | |
| 1446 | request = { |
| 1447 | "attachment": [ |
| 1448 | { |
| 1449 | "s3url": "https://s3.example.com/a.txt", |
| 1450 | "mimetype": "text/plain", |
| 1451 | "name": "a.txt", |
| 1452 | }, |
| 1453 | { |
| 1454 | "s3url": "https://s3.example.com/b.txt", |
| 1455 | "mimetype": "text/plain", |
| 1456 | "name": "b.txt", |
| 1457 | }, |
| 1458 | ] |
| 1459 | } |
| 1460 | result = file_helper._substitute_file_downloads_recursively( |
| 1461 | tool=mock_tool, |
| 1462 | schema=mock_tool.output_parameters, |
| 1463 | request=request, |
| 1464 | ) |
| 1465 | |
| 1466 | assert result is request |
| 1467 | assert result["attachment"] == ["/tmp/a.txt", "/tmp/b.txt"] |
| 1468 | assert mock_download.call_count == 2 |
| 1469 | |
| 1470 | @patch("composio.core.models._files.FileDownloadable.download") |
| 1471 | def test_substitute_download_array_items_with_anyof_file_branch( |
| 1472 | self, mock_download, file_helper, mock_tool |
| 1473 | ): |
| 1474 | """Array item schemas can contain file-downloadable union branches.""" |
| 1475 | mock_tool.output_parameters = { |
| 1476 | "type": "object", |
| 1477 | "properties": { |
| 1478 | "attachments": { |
| 1479 | "type": "array", |
| 1480 | "items": { |
| 1481 | "anyOf": [ |
| 1482 | {"type": "object", "file_downloadable": True}, |
| 1483 | {"type": "null"}, |
| 1484 | ] |
| 1485 | }, |
| 1486 | } |
| 1487 | }, |
| 1488 | } |
| 1489 | mock_download.side_effect = ["/tmp/a.txt", "/tmp/b.txt"] |
| 1490 | |
| 1491 | result = file_helper._substitute_file_downloads_recursively( |
| 1492 | tool=mock_tool, |
| 1493 | schema=mock_tool.output_parameters, |
| 1494 | request={ |
| 1495 | "attachments": [ |
| 1496 | { |
| 1497 | "s3url": "https://s3.example.com/a.txt", |
| 1498 | "mimetype": "text/plain", |
| 1499 | "name": "a.txt", |
| 1500 | }, |
| 1501 | None, |
| 1502 | { |
| 1503 | "s3url": "https://s3.example.com/b.txt", |
| 1504 | "mimetype": "text/plain", |
| 1505 | "name": "b.txt", |
| 1506 | }, |
| 1507 | ] |
| 1508 | }, |
| 1509 | ) |
| 1510 | |
| 1511 | assert result["attachments"] == ["/tmp/a.txt", None, "/tmp/b.txt"] |
| 1512 | assert mock_download.call_count == 2 |
| 1513 | |
| 1514 | |
| 1515 | class TestFileHelperFindVariantMethods: |
| 1516 | """Test cases for _find_uploadable_schema_variant and _find_downloadable_schema_variant.""" |
| 1517 | |
| 1518 | def test_find_uploadable_variant_in_anyof(self, file_helper): |
| 1519 | """Test finding uploadable variant in anyOf.""" |
| 1520 | schema = { |
| 1521 | "anyOf": [ |
| 1522 | {"type": "string", "file_uploadable": True}, |
| 1523 | {"type": "null"}, |
| 1524 | ] |
| 1525 | } |
| 1526 | result = file_helper._find_uploadable_schema_variant(schema) |
| 1527 | assert result is not None |
| 1528 | assert result.get("file_uploadable") is True |
| 1529 | |
| 1530 | def test_find_uploadable_variant_in_oneof(self, file_helper): |
| 1531 | """Test finding uploadable variant in oneOf.""" |
| 1532 | schema = { |
| 1533 | "oneOf": [ |
| 1534 | {"type": "string"}, |
| 1535 | {"type": "string", "file_uploadable": True}, |
| 1536 | ] |
| 1537 | } |
| 1538 | result = file_helper._find_uploadable_schema_variant(schema) |
| 1539 | assert result is not None |
| 1540 | assert result.get("file_uploadable") is True |
| 1541 | |
| 1542 | def test_find_uploadable_variant_in_allof(self, file_helper): |
| 1543 | """Test finding uploadable variant in allOf.""" |
| 1544 | schema = { |
| 1545 | "allOf": [ |
| 1546 | {"type": "string", "file_uploadable": True}, |
| 1547 | {"minLength": 1}, |
| 1548 | ] |
| 1549 | } |
| 1550 | result = file_helper._find_uploadable_schema_variant(schema) |
| 1551 | assert result is not None |
| 1552 | assert result.get("file_uploadable") is True |
| 1553 | |
| 1554 | def test_find_uploadable_variant_not_found(self, file_helper): |
| 1555 | """Test that None is returned when no uploadable variant exists.""" |
| 1556 | schema = { |
| 1557 | "anyOf": [ |
| 1558 | {"type": "string"}, |
| 1559 | {"type": "null"}, |
| 1560 | ] |
| 1561 | } |
| 1562 | result = file_helper._find_uploadable_schema_variant(schema) |
| 1563 | assert result is None |
| 1564 | |
| 1565 | def test_find_uploadable_variant_prefers_runtime_value_shape(self, file_helper): |
| 1566 | """Runtime values should select the matching file-bearing schema shape.""" |
| 1567 | schema = { |
| 1568 | "anyOf": [ |
| 1569 | {"type": "string", "file_uploadable": True}, |
| 1570 | { |
| 1571 | "type": "array", |
| 1572 | "items": {"type": "string", "file_uploadable": True}, |
| 1573 | }, |
| 1574 | ] |
| 1575 | } |
| 1576 | |
| 1577 | result = file_helper._find_uploadable_schema_variant( |
| 1578 | schema, |
| 1579 | value=["/tmp/a.txt", "/tmp/b.txt"], |
| 1580 | ) |
| 1581 | |
| 1582 | assert result is not None |
| 1583 | assert result["type"] == "array" |
| 1584 | |
| 1585 | def test_find_uploadable_variant_falls_back_to_first_file_bearing_variant( |
| 1586 | self, file_helper |
| 1587 | ): |
| 1588 | """No runtime shape match should preserve historical first-match behavior.""" |
| 1589 | schema = { |
| 1590 | "anyOf": [ |
| 1591 | {"type": "string", "file_uploadable": True}, |
| 1592 | { |
| 1593 | "type": "array", |
| 1594 | "items": {"type": "string", "file_uploadable": True}, |
| 1595 | }, |
| 1596 | ] |
| 1597 | } |
| 1598 | |
| 1599 | result = file_helper._find_uploadable_schema_variant(schema, value=123) |
| 1600 | |
| 1601 | assert result is not None |
| 1602 | assert result["type"] == "string" |
| 1603 | |
| 1604 | def test_find_downloadable_variant_in_anyof(self, file_helper): |
| 1605 | """Test finding downloadable variant in anyOf.""" |
| 1606 | schema = { |
| 1607 | "anyOf": [ |
| 1608 | {"type": "object", "file_downloadable": True}, |
| 1609 | {"type": "null"}, |
| 1610 | ] |
| 1611 | } |
| 1612 | result = file_helper._find_downloadable_schema_variant(schema) |
| 1613 | assert result is not None |
| 1614 | assert result.get("file_downloadable") is True |
| 1615 | |
| 1616 | def test_find_downloadable_variant_in_oneof(self, file_helper): |
| 1617 | """Test finding downloadable variant in oneOf.""" |
| 1618 | schema = { |
| 1619 | "oneOf": [ |
| 1620 | {"type": "string"}, |
| 1621 | {"type": "object", "file_downloadable": True}, |
| 1622 | ] |
| 1623 | } |
| 1624 | result = file_helper._find_downloadable_schema_variant(schema) |
| 1625 | assert result is not None |
| 1626 | assert result.get("file_downloadable") is True |
| 1627 | |
| 1628 | def test_find_downloadable_variant_nested(self, file_helper): |
| 1629 | """Test finding downloadable variant with nested file_downloadable.""" |
| 1630 | schema = { |
| 1631 | "anyOf": [ |
| 1632 | { |
| 1633 | "type": "object", |
| 1634 | "properties": { |
| 1635 | "file": {"type": "object", "file_downloadable": True} |
| 1636 | }, |
| 1637 | }, |
| 1638 | {"type": "null"}, |
| 1639 | ] |
| 1640 | } |
| 1641 | result = file_helper._find_downloadable_schema_variant(schema) |
| 1642 | assert result is not None |
| 1643 | assert result.get("type") == "object" |
| 1644 | |
| 1645 | |
| 1646 | class TestFileUploadWithMixedSchemas: |
| 1647 | """Test cases for schemas with anyOf/oneOf/allOf alongside properties with file_uploadable.""" |
| 1648 | |
| 1649 | def test_upload_from_base_properties_when_anyof_has_no_file_uploadable( |
| 1650 | self, file_helper, mock_tool |
| 1651 | ): |
| 1652 | """Test that file_uploadable in base properties works when sibling has anyOf without file_uploadable.""" |
| 1653 | mock_tool.input_parameters = { |
| 1654 | "type": "object", |
| 1655 | "properties": { |
| 1656 | "metadata": { |
| 1657 | "anyOf": [ |
| 1658 | {"type": "string"}, |
| 1659 | {"type": "null"}, |
| 1660 | ] |
| 1661 | }, |
| 1662 | "file": { |
| 1663 | "type": "string", |
| 1664 | "file_uploadable": True, |
| 1665 | }, |
| 1666 | }, |
| 1667 | } |
| 1668 | |
| 1669 | # Should try to upload the file (will fail because file doesn't exist) |
| 1670 | with pytest.raises(Exception): |
| 1671 | file_helper._substitute_file_uploads_recursively( |
| 1672 | tool=mock_tool, |
| 1673 | schema=mock_tool.input_parameters, |
| 1674 | request={"metadata": "some data", "file": "/path/to/file.txt"}, |
| 1675 | ) |
| 1676 | |
| 1677 | def test_upload_when_root_has_anyof_without_file_uploadable_and_properties_with_file_uploadable( |
| 1678 | self, file_helper, mock_tool |
| 1679 | ): |
| 1680 | """Test upload when root schema has anyOf (no file_uploadable) and properties (with file_uploadable).""" |
| 1681 | mock_tool.input_parameters = { |
| 1682 | "type": "object", |
| 1683 | "anyOf": [ |
| 1684 | {"required": ["text"]}, |
| 1685 | {"required": ["file"]}, |
| 1686 | ], |
| 1687 | "properties": { |
| 1688 | "text": {"type": "string"}, |
| 1689 | "file": {"type": "string", "file_uploadable": True}, |
| 1690 | }, |
| 1691 | } |
| 1692 | |
| 1693 | # Should try to upload the file |
| 1694 | with pytest.raises(Exception): |
| 1695 | file_helper._substitute_file_uploads_recursively( |
| 1696 | tool=mock_tool, |
| 1697 | schema=mock_tool.input_parameters, |
| 1698 | request={"file": "/path/to/document.pdf"}, |
| 1699 | ) |
| 1700 | |
| 1701 | def test_metadata_preserved_when_file_upload_with_mixed_schema( |
| 1702 | self, file_helper, mock_tool |
| 1703 | ): |
| 1704 | """Test that non-file properties are preserved when processing mixed schemas.""" |
| 1705 | mock_tool.input_parameters = { |
| 1706 | "type": "object", |
| 1707 | "properties": { |
| 1708 | "metadata": { |
| 1709 | "anyOf": [ |
| 1710 | {"type": "string"}, |
| 1711 | {"type": "null"}, |
| 1712 | ] |
| 1713 | }, |
| 1714 | "count": {"type": "integer"}, |
| 1715 | }, |
| 1716 | } |
| 1717 | |
| 1718 | result = file_helper._substitute_file_uploads_recursively( |
| 1719 | tool=mock_tool, |
| 1720 | schema=mock_tool.input_parameters, |
| 1721 | request={"metadata": "test data", "count": 42}, |
| 1722 | ) |
| 1723 | |
| 1724 | # Both values should be preserved unchanged |
| 1725 | assert result["metadata"] == "test data" |
| 1726 | assert result["count"] == 42 |
| 1727 | |
| 1728 | |
| 1729 | class TestUrlHelperFunctions: |
| 1730 | """Test cases for URL-related helper functions.""" |
| 1731 | |
| 1732 | def test_is_url_with_http(self): |
| 1733 | """Test _is_url correctly identifies HTTP URLs.""" |
| 1734 | assert _is_url("http://example.com/file.jpg") is True |
| 1735 | assert _is_url("http://localhost:8080/api/file") is True |
| 1736 | |
| 1737 | def test_is_url_with_https(self): |
| 1738 | """Test _is_url correctly identifies HTTPS URLs.""" |
| 1739 | assert _is_url("https://example.com/file.jpg") is True |
| 1740 | assert _is_url("https://images.pexels.com/photos/123.jpg") is True |
| 1741 | |
| 1742 | def test_is_url_with_long_real_world_urls(self): |
| 1743 | """Test _is_url handles long real-world URLs with query parameters.""" |
| 1744 | assert ( |
| 1745 | _is_url( |
| 1746 | "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQq0powzLhfi1bakZ0eNSIA_aJ_5UlPsCte1g&s" |
| 1747 | ) |
| 1748 | is True |
| 1749 | ) |
| 1750 | assert ( |
| 1751 | _is_url( |
| 1752 | "https://images.unsplash.com/photo-1503023345310-bd7c1de61c7d?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8bWFjYm9vfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=800&q=60" |
| 1753 | ) |
| 1754 | is True |
| 1755 | ) |
| 1756 | |
| 1757 | def test_is_url_with_local_paths(self): |
| 1758 | """Test _is_url correctly rejects local file paths.""" |
| 1759 | assert _is_url("/path/to/file.jpg") is False |
| 1760 | assert _is_url("./relative/path.txt") is False |
| 1761 | assert _is_url("file.txt") is False |
| 1762 | assert _is_url("C:\\Windows\\file.txt") is False |
| 1763 | |
| 1764 | def test_is_url_with_other_schemes(self): |
| 1765 | """Test _is_url rejects non-HTTP schemes.""" |
| 1766 | assert _is_url("ftp://ftp.example.com/file.txt") is False |
| 1767 | assert _is_url("file:///local/file.txt") is False |
| 1768 | assert _is_url("mailto:test@example.com") is False |
| 1769 | |
| 1770 | def test_is_url_with_invalid_inputs(self): |
| 1771 | """Test _is_url handles invalid inputs gracefully.""" |
| 1772 | assert _is_url("") is False |
| 1773 | assert _is_url("not a url") is False |
| 1774 | assert _is_url("http://") is False |
| 1775 | |
| 1776 | def test_get_extension_from_mimetype_common_types(self): |
| 1777 | """Test _get_extension_from_mimetype for common types.""" |
| 1778 | assert _get_extension_from_mimetype("image/jpeg") == ".jpg" |
| 1779 | assert _get_extension_from_mimetype("image/png") == ".png" |
| 1780 | assert _get_extension_from_mimetype("application/pdf") == ".pdf" |
| 1781 | assert _get_extension_from_mimetype("text/plain") == ".txt" |
| 1782 | assert _get_extension_from_mimetype("application/json") == ".json" |
| 1783 | |
| 1784 | @pytest.mark.parametrize( |
| 1785 | ("mimetype", "expected"), |
| 1786 | [ |
| 1787 | ( |
| 1788 | "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
| 1789 | ".xlsx", |
| 1790 | ), |
| 1791 | ( |
| 1792 | "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 1793 | ".docx", |
| 1794 | ), |
| 1795 | ( |
| 1796 | "application/vnd.openxmlformats-officedocument.presentationml.presentation", |
| 1797 | ".pptx", |
| 1798 | ), |
| 1799 | ("application/vnd.ms-excel", ".xls"), |
| 1800 | ("application/msword", ".doc"), |
| 1801 | ("application/vnd.ms-powerpoint", ".ppt"), |
| 1802 | ("text/csv", ".csv"), |
| 1803 | ("application/x-tar", ".tar"), |
| 1804 | ("video/quicktime", ".mov"), |
| 1805 | ("audio/ogg", ".ogg"), |
| 1806 | ], |
| 1807 | ) |
| 1808 | def test_get_extension_from_mimetype_uses_shared_table(self, mimetype, expected): |
| 1809 | """Types the shared MIME table knows keep their extension (issue #5105).""" |
| 1810 | assert _get_extension_from_mimetype(mimetype) == expected |
| 1811 | |
| 1812 | def test_get_extension_from_mimetype_unknown_type(self): |
| 1813 | """Test _get_extension_from_mimetype returns empty for unknown types.""" |
| 1814 | assert _get_extension_from_mimetype("application/unknown") == "" |
| 1815 | assert _get_extension_from_mimetype("custom/type") == "" |
| 1816 | |
| 1817 | def test_get_extension_from_mimetype_generic_binary_type(self): |
| 1818 | """A generic binary type does not name a format, so no extension is added.""" |
| 1819 | assert _get_extension_from_mimetype("application/octet-stream") == "" |
| 1820 | |
| 1821 | def test_get_extension_from_mimetype_case_insensitive(self): |
| 1822 | """Test _get_extension_from_mimetype handles case-insensitive mimetypes (RFC 2045).""" |
| 1823 | # Uppercase variations |
| 1824 | assert _get_extension_from_mimetype("IMAGE/JPEG") == ".jpg" |
| 1825 | assert _get_extension_from_mimetype("APPLICATION/PDF") == ".pdf" |
| 1826 | # Mixed case variations |
| 1827 | assert _get_extension_from_mimetype("Image/Jpeg") == ".jpg" |
| 1828 | assert _get_extension_from_mimetype("Application/Pdf") == ".pdf" |
| 1829 | assert _get_extension_from_mimetype("Text/Plain") == ".txt" |
| 1830 | # Edge cases |
| 1831 | assert _get_extension_from_mimetype("IMAGE/png") == ".png" |
| 1832 | assert _get_extension_from_mimetype("video/MP4") == ".mp4" |
| 1833 | |
| 1834 | def test_generate_timestamped_filename(self): |
| 1835 | """Test _generate_timestamped_filename generates valid filenames.""" |
| 1836 | filename = _generate_timestamped_filename(".jpg") |
| 1837 | assert filename.startswith("file_") |
| 1838 | assert filename.endswith(".jpg") |
| 1839 | assert len(filename) > 15 # Should have timestamp and unique ID |
| 1840 | |
| 1841 | def test_generate_timestamped_filename_no_extension(self): |
| 1842 | """Test _generate_timestamped_filename works without extension.""" |
| 1843 | filename = _generate_timestamped_filename("") |
| 1844 | assert filename.startswith("file_") |
| 1845 | assert not filename.endswith(".") |
| 1846 | |
| 1847 | |
| 1848 | class TestFetchFileFromUrl: |
| 1849 | """Test cases for _fetch_file_from_url function.""" |
| 1850 | |
| 1851 | @patch("composio.core.models._files.safe_get") |
| 1852 | def test_fetch_file_from_url_success(self, mock_get): |
| 1853 | """Test successful file fetch from URL.""" |
| 1854 | mock_response = MagicMock() |
| 1855 | mock_response.ok = True |
| 1856 | mock_response.status_code = 200 |
| 1857 | mock_response.headers = {"content-type": "image/jpeg"} |
| 1858 | mock_response.iter_content.return_value = [b"test file content"] |
| 1859 | mock_response.close = MagicMock() |
| 1860 | mock_get.return_value = mock_response |
| 1861 | |
| 1862 | filename, content, mimetype = _fetch_file_from_url( |
| 1863 | "https://example.com/image.jpg" |
| 1864 | ) |
| 1865 | |
| 1866 | assert content == b"test file content" |
| 1867 | assert mimetype == "image/jpeg" |
| 1868 | assert filename == "image.jpg" |
| 1869 | mock_get.assert_called_once_with( |
| 1870 | "https://example.com/image.jpg", |
| 1871 | stream=True, |
| 1872 | timeout=(5, 60), |
| 1873 | ) |
| 1874 | |
| 1875 | @patch("composio.core.models._files.safe_get") |
| 1876 | def test_fetch_file_from_url_with_charset_in_content_type(self, mock_get): |
| 1877 | """Test that charset is stripped from content-type.""" |
| 1878 | mock_response = MagicMock() |
| 1879 | mock_response.ok = True |
| 1880 | mock_response.status_code = 200 |
| 1881 | mock_response.headers = {"content-type": "text/html; charset=utf-8"} |
| 1882 | mock_response.iter_content.return_value = [b"<html></html>"] |
| 1883 | mock_response.close = MagicMock() |
| 1884 | mock_get.return_value = mock_response |
| 1885 | |
| 1886 | filename, content, mimetype = _fetch_file_from_url( |
| 1887 | "https://example.com/page.html" |
| 1888 | ) |
| 1889 | |
| 1890 | assert mimetype == "text/html" |
| 1891 | |
| 1892 | @patch("composio.core.models._files.safe_get") |
| 1893 | def test_fetch_file_from_url_generates_filename_when_missing(self, mock_get): |
| 1894 | """Test filename generation when URL has no filename.""" |
| 1895 | mock_response = MagicMock() |
| 1896 | mock_response.ok = True |
| 1897 | mock_response.status_code = 200 |
| 1898 | mock_response.headers = {"content-type": "image/png"} |
| 1899 | mock_response.iter_content.return_value = [b"image data"] |
| 1900 | mock_response.close = MagicMock() |
| 1901 | mock_get.return_value = mock_response |
| 1902 | |
| 1903 | filename, content, mimetype = _fetch_file_from_url("https://example.com/") |
| 1904 | |
| 1905 | assert filename.startswith("file_") |
| 1906 | assert filename.endswith(".png") |
| 1907 | |
| 1908 | @patch("composio.core.models._files.safe_get") |
| 1909 | def test_fetch_file_from_url_generates_filename_when_no_extension(self, mock_get): |
| 1910 | """Test filename generation when URL filename has no extension.""" |
| 1911 | mock_response = MagicMock() |
| 1912 | mock_response.ok = True |
| 1913 | mock_response.status_code = 200 |
| 1914 | mock_response.headers = {"content-type": "application/pdf"} |
| 1915 | mock_response.iter_content.return_value = [b"pdf data"] |
| 1916 | mock_response.close = MagicMock() |
| 1917 | mock_get.return_value = mock_response |
| 1918 | |
| 1919 | filename, content, mimetype = _fetch_file_from_url( |
| 1920 | "https://example.com/document" |
| 1921 | ) |
| 1922 | |
| 1923 | assert filename.startswith("file_") |
| 1924 | assert filename.endswith(".pdf") |
| 1925 | |
| 1926 | @patch("composio.core.models._files.safe_get") |
| 1927 | def test_fetch_file_from_url_keeps_office_extension_when_url_has_none( |
| 1928 | self, mock_get |
| 1929 | ): |
| 1930 | """An export URL without an extension keeps the xlsx extension (issue #5105).""" |
| 1931 | xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" |
| 1932 | mock_response = MagicMock() |
| 1933 | mock_response.ok = True |
| 1934 | mock_response.status_code = 200 |
| 1935 | mock_response.headers = {"content-type": xlsx} |
| 1936 | mock_response.iter_content.return_value = [b"xlsx data"] |
| 1937 | mock_response.close = MagicMock() |
| 1938 | mock_get.return_value = mock_response |
| 1939 | |
| 1940 | filename, content, mimetype = _fetch_file_from_url( |
| 1941 | "https://docs.google.com/spreadsheets/d/abc123/export?format=xlsx" |
| 1942 | ) |
| 1943 | |
| 1944 | assert filename.startswith("file_") |
| 1945 | assert filename.endswith(".xlsx") |
| 1946 | assert mimetype == xlsx |
| 1947 | |
| 1948 | @patch("composio.core.models._files.safe_get") |
| 1949 | def test_fetch_file_from_url_octet_stream_without_extension(self, mock_get): |
| 1950 | """A generic binary response from an extensionless URL stays extensionless.""" |
| 1951 | mock_response = MagicMock() |
| 1952 | mock_response.ok = True |
| 1953 | mock_response.status_code = 200 |
| 1954 | mock_response.headers = {"content-type": "application/octet-stream"} |
| 1955 | mock_response.iter_content.return_value = [b"binary data"] |
| 1956 | mock_response.close = MagicMock() |
| 1957 | mock_get.return_value = mock_response |
| 1958 | |
| 1959 | filename, _, _ = _fetch_file_from_url("https://example.com/download") |
| 1960 | |
| 1961 | assert filename.startswith("file_") |
| 1962 | assert "." not in filename |
| 1963 | |
| 1964 | @patch("composio.core.models._files.safe_get") |
| 1965 | def test_fetch_file_from_url_failure(self, mock_get): |
| 1966 | """Test error handling when URL fetch fails.""" |
| 1967 | mock_response = MagicMock() |
| 1968 | mock_response.ok = False |
| 1969 | mock_response.status_code = 404 |
| 1970 | mock_response.close = MagicMock() |
| 1971 | mock_get.return_value = mock_response |
| 1972 | |
| 1973 | with pytest.raises(ErrorUploadingFile) as exc_info: |
| 1974 | _fetch_file_from_url("https://example.com/notfound.jpg") |
| 1975 | |
| 1976 | assert "Failed to fetch file from URL" in str(exc_info.value) |
| 1977 | assert "404" in str(exc_info.value) |
| 1978 | |
| 1979 | @patch("composio.core.models._files.safe_get") |
| 1980 | def test_fetch_file_from_url_maps_midstream_failure(self, mock_get): |
| 1981 | def failing_stream(chunk_size=None): |
| 1982 | yield b"partial" |
| 1983 | raise requests.exceptions.ConnectionError("peer reset mid-stream") |
| 1984 | |
| 1985 | mock_response = MagicMock() |
| 1986 | mock_response.ok = True |
| 1987 | mock_response.status_code = 200 |
| 1988 | mock_response.headers = {"content-type": "text/plain"} |
| 1989 | mock_response.iter_content.side_effect = failing_stream |
| 1990 | mock_get.return_value = mock_response |
| 1991 | |
| 1992 | with pytest.raises(ErrorUploadingFile, match="peer reset mid-stream"): |
| 1993 | _fetch_file_from_url("https://example.com/file.txt") |
| 1994 | |
| 1995 | mock_response.close.assert_called_once() |
| 1996 | |
| 1997 | @patch("composio.core.models._files.safe_get") |
| 1998 | def test_fetch_file_from_url_decodes_percent_encoded_filename(self, mock_get): |
| 1999 | """Test that percent-encoded characters in URL filenames are decoded.""" |
| 2000 | mock_response = MagicMock() |
| 2001 | mock_response.ok = True |
| 2002 | mock_response.status_code = 200 |
| 2003 | mock_response.headers = {"content-type": "application/pdf"} |
| 2004 | mock_response.iter_content.return_value = [b"document content"] |
| 2005 | mock_response.close = MagicMock() |
| 2006 | mock_get.return_value = mock_response |
| 2007 | |
| 2008 | # URL with percent-encoded spaces and special characters |
| 2009 | filename, content, mimetype = _fetch_file_from_url( |
| 2010 | "https://example.com/My%20Document%20%282024%29.pdf" |
| 2011 | ) |
| 2012 | |
| 2013 | # Filename should be decoded |
| 2014 | assert filename == "My Document (2024).pdf" |
| 2015 | assert content == b"document content" |
| 2016 | assert mimetype == "application/pdf" |
| 2017 | |
| 2018 | @patch("composio.core.models._files.safe_get") |
| 2019 | def test_fetch_file_from_url_decodes_unicode_filename(self, mock_get): |
| 2020 | """Test that percent-encoded unicode characters in URL filenames are decoded.""" |
| 2021 | mock_response = MagicMock() |
| 2022 | mock_response.ok = True |
| 2023 | mock_response.status_code = 200 |
| 2024 | mock_response.headers = {"content-type": "image/jpeg"} |
| 2025 | mock_response.iter_content.return_value = [b"image data"] |
| 2026 | mock_response.close = MagicMock() |
| 2027 | mock_get.return_value = mock_response |
| 2028 | |
| 2029 | # URL with percent-encoded unicode (e.g., Japanese characters) |
| 2030 | filename, content, mimetype = _fetch_file_from_url( |
| 2031 | "https://example.com/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.jpg" |
| 2032 | ) |
| 2033 | |
| 2034 | # Filename should be decoded to unicode |
| 2035 | assert filename == "ファイル.jpg" |
| 2036 | |
| 2037 | @patch("composio.core.models._files.safe_get") |
| 2038 | def test_fetch_file_from_url_handles_plus_sign_in_filename(self, mock_get): |
| 2039 | """Test that plus signs in URL paths are preserved (not converted to spaces).""" |
| 2040 | mock_response = MagicMock() |
| 2041 | mock_response.ok = True |
| 2042 | mock_response.status_code = 200 |
| 2043 | mock_response.headers = {"content-type": "text/plain"} |
| 2044 | mock_response.iter_content.return_value = [b"file content"] |
| 2045 | mock_response.close = MagicMock() |
| 2046 | mock_get.return_value = mock_response |
| 2047 | |
| 2048 | # Plus signs in path should remain as plus signs (unquote doesn't convert + to space) |
| 2049 | filename, content, mimetype = _fetch_file_from_url( |
| 2050 | "https://example.com/file+name.txt" |
| 2051 | ) |
| 2052 | |
| 2053 | assert filename == "file+name.txt" |
| 2054 | |
| 2055 | |
| 2056 | class TestUploadBytesToS3: |
| 2057 | """Test cases for _upload_bytes_to_s3 function.""" |
| 2058 | |
| 2059 | @patch("composio.core.models._files.safe_request") |
| 2060 | def test_upload_bytes_to_s3_success(self, mock_safe_request): |
| 2061 | """Test successful upload to S3.""" |
| 2062 | mock_client = MagicMock() |
| 2063 | mock_s3_response = MagicMock() |
| 2064 | mock_s3_response.key = "s3-key-123" |
| 2065 | mock_s3_response.new_presigned_url = "https://s3.example.com/upload" |
| 2066 | mock_client.files.create_presigned_url.return_value = mock_s3_response |
| 2067 | |
| 2068 | mock_put_response = MagicMock() |
| 2069 | mock_put_response.status_code = 200 |
| 2070 | mock_safe_request.return_value = mock_put_response |
| 2071 | |
| 2072 | result = _upload_bytes_to_s3( |
| 2073 | client=mock_client, |
| 2074 | filename="test.jpg", |
| 2075 | content=b"file content", |
| 2076 | mimetype="image/jpeg", |
| 2077 | tool="TEST_TOOL", |
| 2078 | toolkit="test_toolkit", |
| 2079 | ) |
| 2080 | |
| 2081 | assert result == "s3-key-123" |
| 2082 | mock_client.files.create_presigned_url.assert_called_once() |
| 2083 | mock_safe_request.assert_called_once_with( |
| 2084 | "PUT", |
| 2085 | "https://s3.example.com/upload", |
| 2086 | data=b"file content", |
| 2087 | headers={"Content-Type": "image/jpeg"}, |
| 2088 | timeout=(5, 60), |
| 2089 | ) |
| 2090 | |
| 2091 | @patch("composio.core.models._files.safe_request") |
| 2092 | def test_upload_bytes_to_s3_failure(self, mock_safe_request): |
| 2093 | """Test error handling when S3 upload fails.""" |
| 2094 | mock_client = MagicMock() |
| 2095 | mock_s3_response = MagicMock() |
| 2096 | mock_s3_response.key = "s3-key-123" |
| 2097 | mock_s3_response.new_presigned_url = "https://s3.example.com/upload" |
| 2098 | mock_client.files.create_presigned_url.return_value = mock_s3_response |
| 2099 | |
| 2100 | mock_put_response = MagicMock() |
| 2101 | mock_put_response.status_code = 500 |
| 2102 | mock_safe_request.return_value = mock_put_response |
| 2103 | |
| 2104 | with pytest.raises(ErrorUploadingFile) as exc_info: |
| 2105 | _upload_bytes_to_s3( |
| 2106 | client=mock_client, |
| 2107 | filename="test.jpg", |
| 2108 | content=b"file content", |
| 2109 | mimetype="image/jpeg", |
| 2110 | tool="TEST_TOOL", |
| 2111 | toolkit="test_toolkit", |
| 2112 | ) |
| 2113 | |
| 2114 | assert "Failed to upload to S3" in str(exc_info.value) |
| 2115 | |
| 2116 | @patch("composio.core.models._files.safe_request") |
| 2117 | def test_upload_bytes_to_s3_timeout(self, mock_safe_request): |
| 2118 | """Test request timeouts are reported as upload errors.""" |
| 2119 | mock_client = MagicMock() |
| 2120 | mock_s3_response = MagicMock() |
| 2121 | mock_s3_response.key = "s3-key-123" |
| 2122 | mock_s3_response.new_presigned_url = "https://s3.example.com/upload?token=abc" |
| 2123 | mock_client.files.create_presigned_url.return_value = mock_s3_response |
| 2124 | # The exception text itself carries the presigned URL (incl. token), as |
| 2125 | # real urllib3 errors do — the SDK must not surface it in the message. |
| 2126 | mock_safe_request.side_effect = requests.exceptions.Timeout( |
| 2127 | "HTTPSConnectionPool(host='s3.example.com', port=443): " |
| 2128 | "Max retries exceeded with url: /upload?token=abc" |
| 2129 | ) |
| 2130 | |
| 2131 | with pytest.raises(ErrorUploadingFile) as exc_info: |
| 2132 | _upload_bytes_to_s3( |
| 2133 | client=mock_client, |
| 2134 | filename="test.jpg", |
| 2135 | content=b"file content", |
| 2136 | mimetype="image/jpeg", |
| 2137 | tool="TEST_TOOL", |
| 2138 | toolkit="test_toolkit", |
| 2139 | ) |
| 2140 | |
| 2141 | assert "Failed to upload to S3" in str(exc_info.value) |
| 2142 | assert "token=abc" not in str(exc_info.value) |
| 2143 | |
| 2144 | |
| 2145 | class TestFileUploadableFromUrl: |
| 2146 | """Test cases for FileUploadable.from_url and from_path with URLs.""" |
| 2147 | |
| 2148 | @patch("composio.core.models._files._upload_bytes_to_s3") |
| 2149 | @patch("composio.core.models._files._fetch_file_from_url") |
| 2150 | def test_from_url_success(self, mock_fetch, mock_upload): |
| 2151 | """Test successful FileUploadable creation from URL.""" |
| 2152 | mock_fetch.return_value = ("image.jpg", b"image data", "image/jpeg") |
| 2153 | mock_upload.return_value = "s3-key-abc" |
| 2154 | mock_client = MagicMock() |
| 2155 | |
| 2156 | result = FileUploadable.from_url( |
| 2157 | client=mock_client, |
| 2158 | url="https://example.com/image.jpg", |
| 2159 | tool="TEST_TOOL", |
| 2160 | toolkit="test_toolkit", |
| 2161 | ) |
| 2162 | |
| 2163 | assert result.name == "image.jpg" |
| 2164 | assert result.mimetype == "image/jpeg" |
| 2165 | assert result.s3key == "s3-key-abc" |
| 2166 | mock_fetch.assert_called_once_with("https://example.com/image.jpg") |
| 2167 | mock_upload.assert_called_once() |
| 2168 | |
| 2169 | @patch("composio.core.models._files._upload_bytes_to_s3") |
| 2170 | @patch("composio.core.models._files._fetch_file_from_url") |
| 2171 | def test_from_path_detects_url(self, mock_fetch, mock_upload): |
| 2172 | """Test that from_path correctly detects and handles URLs.""" |
| 2173 | mock_fetch.return_value = ("photo.png", b"photo data", "image/png") |
| 2174 | mock_upload.return_value = "s3-key-xyz" |
| 2175 | mock_client = MagicMock() |
| 2176 | |
| 2177 | result = FileUploadable.from_path( |
| 2178 | client=mock_client, |
| 2179 | file="https://images.example.com/photo.png", |
| 2180 | tool="SEND_EMAIL", |
| 2181 | toolkit="gmail", |
| 2182 | ) |
| 2183 | |
| 2184 | assert result.name == "photo.png" |
| 2185 | assert result.mimetype == "image/png" |
| 2186 | assert result.s3key == "s3-key-xyz" |
| 2187 | mock_fetch.assert_called_once_with("https://images.example.com/photo.png") |
| 2188 | |
| 2189 | @patch("composio.core.models._files._fetch_file_from_url") |
| 2190 | def test_from_url_propagates_fetch_error(self, mock_fetch): |
| 2191 | """Test that fetch errors are propagated correctly.""" |
| 2192 | mock_fetch.side_effect = ErrorUploadingFile("Fetch failed") |
| 2193 | mock_client = MagicMock() |
| 2194 | |
| 2195 | with pytest.raises(ErrorUploadingFile) as exc_info: |
| 2196 | FileUploadable.from_url( |
| 2197 | client=mock_client, |
| 2198 | url="https://example.com/missing.jpg", |
| 2199 | tool="TEST_TOOL", |
| 2200 | toolkit="test_toolkit", |
| 2201 | ) |
| 2202 | |
| 2203 | assert "Fetch failed" in str(exc_info.value) |
| 2204 | |
| 2205 | def test_before_file_upload_hook_receives_source_url(self): |
| 2206 | """from_path emits ``source="url"`` to the hook for http(s) inputs. |
| 2207 | |
| 2208 | We abort from the hook to avoid the downstream network path; the |
| 2209 | pre-abort capture is what we're asserting on. |
| 2210 | """ |
| 2211 | from composio.exceptions import FileUploadAbortedError |
| 2212 | |
| 2213 | mock_client = MagicMock() |
| 2214 | seen = {} |
| 2215 | |
| 2216 | def hook(ctx): |
| 2217 | seen.update(ctx) |
| 2218 | return False |
| 2219 | |
| 2220 | with pytest.raises(FileUploadAbortedError): |
| 2221 | FileUploadable.from_path( |
| 2222 | client=mock_client, |
| 2223 | file="https://example.com/photo.png", |
| 2224 | tool="SEND_EMAIL", |
| 2225 | toolkit="gmail", |
| 2226 | before_file_upload=hook, |
| 2227 | ) |
| 2228 | |
| 2229 | assert seen == { |
| 2230 | "path": "https://example.com/photo.png", |
| 2231 | "source": "url", |
| 2232 | "tool": "SEND_EMAIL", |
| 2233 | "toolkit": "gmail", |
| 2234 | } |
| 2235 | |
| 2236 | def test_before_file_upload_hook_receives_source_path(self, tmp_path): |
| 2237 | """from_path emits ``source="path"`` to the hook for local inputs.""" |
| 2238 | from composio.exceptions import FileUploadAbortedError |
| 2239 | |
| 2240 | f = tmp_path / "doc.txt" |
| 2241 | f.write_text("hello") |
| 2242 | mock_client = MagicMock() |
| 2243 | seen = {} |
| 2244 | |
| 2245 | def hook(ctx): |
| 2246 | seen.update(ctx) |
| 2247 | return False |
| 2248 | |
| 2249 | with pytest.raises(FileUploadAbortedError): |
| 2250 | FileUploadable.from_path( |
| 2251 | client=mock_client, |
| 2252 | file=str(f), |
| 2253 | tool="MY_TOOL", |
| 2254 | toolkit="my_toolkit", |
| 2255 | before_file_upload=hook, |
| 2256 | ) |
| 2257 | |
| 2258 | assert seen == { |
| 2259 | "path": str(f), |
| 2260 | "source": "path", |
| 2261 | "tool": "MY_TOOL", |
| 2262 | "toolkit": "my_toolkit", |
| 2263 | } |
| 2264 | |
| 2265 | def test_url_hook_returning_local_path_routes_through_path_branch(self, tmp_path): |
| 2266 | """A hook that rewrites a URL into a local path must NOT be fed to |
| 2267 | ``from_url``. It has to route back into the local-file branch so the |
| 2268 | allowlist / denylist / existence checks all run.""" |
| 2269 | from composio.exceptions import SDKFileNotFoundError |
| 2270 | |
| 2271 | # A path that's syntactically a path but does not exist — if routing |
| 2272 | # is correct, we'll get SDKFileNotFoundError from the local branch. |
| 2273 | # If the bug is still there, we'd hit `from_url` and the URL fetch |
| 2274 | # would explode (or worse, succeed) instead. |
| 2275 | rewritten = str(tmp_path / "does-not-exist.txt") |
| 2276 | |
| 2277 | def hook(ctx): |
| 2278 | assert ctx["source"] == "url" |
| 2279 | return rewritten |
| 2280 | |
| 2281 | mock_client = MagicMock() |
| 2282 | |
| 2283 | with pytest.raises(SDKFileNotFoundError): |
| 2284 | FileUploadable.from_path( |
| 2285 | client=mock_client, |
| 2286 | file="https://example.com/photo.png", |
| 2287 | tool="T", |
| 2288 | toolkit="tk", |
| 2289 | before_file_upload=hook, |
| 2290 | ) |
| 2291 | |
| 2292 | @patch("composio.core.models._files._fetch_file_from_url") |
| 2293 | @patch("composio.core.models._files._upload_bytes_to_s3") |
| 2294 | def test_path_hook_returning_url_routes_through_url_branch( |
| 2295 | self, mock_upload, mock_fetch, tmp_path |
| 2296 | ): |
| 2297 | """Inverse of the above: a hook on a local path that returns a URL |
| 2298 | must route through ``from_url``, not stat the URL string as a file.""" |
| 2299 | f = tmp_path / "local.txt" |
| 2300 | f.write_text("hi") |
| 2301 | |
| 2302 | mock_fetch.return_value = ("photo.png", b"x", "image/png") |
| 2303 | mock_upload.return_value = "s3-key" |
| 2304 | |
| 2305 | def hook(ctx): |
| 2306 | assert ctx["source"] == "path" |
| 2307 | return "https://example.com/photo.png" |
| 2308 | |
| 2309 | mock_client = MagicMock() |
| 2310 | result = FileUploadable.from_path( |
| 2311 | client=mock_client, |
| 2312 | file=str(f), |
| 2313 | tool="T", |
| 2314 | toolkit="tk", |
| 2315 | before_file_upload=hook, |
| 2316 | ) |
| 2317 | |
| 2318 | # If routing worked, the URL fetch path was taken. |
| 2319 | mock_fetch.assert_called_once_with("https://example.com/photo.png") |
| 2320 | assert result.s3key == "s3-key" |
| 2321 | |
| 2322 | |
| 2323 | class TestFileHelperWithUrls: |
| 2324 | """Test cases for FileHelper handling URLs in file uploads.""" |
| 2325 | |
| 2326 | @patch("composio.core.models._files.FileUploadable.from_path") |
| 2327 | def test_substitute_file_uploads_with_url( |
| 2328 | self, mock_from_path, file_helper, mock_tool |
| 2329 | ): |
| 2330 | """Test that URLs are correctly processed in substitute_file_uploads.""" |
| 2331 | mock_tool.input_parameters = { |
| 2332 | "type": "object", |
| 2333 | "properties": { |
| 2334 | "attachment": {"type": "string", "file_uploadable": True}, |
| 2335 | }, |
| 2336 | } |
| 2337 | |
| 2338 | mock_uploadable = MagicMock() |
| 2339 | mock_uploadable.model_dump.return_value = { |
| 2340 | "name": "image.jpg", |
| 2341 | "mimetype": "image/jpeg", |
| 2342 | "s3key": "s3-key-123", |
| 2343 | } |
| 2344 | mock_from_path.return_value = mock_uploadable |
| 2345 | |
| 2346 | result = file_helper._substitute_file_uploads_recursively( |
| 2347 | tool=mock_tool, |
| 2348 | schema=mock_tool.input_parameters, |
| 2349 | request={"attachment": "https://example.com/image.jpg"}, |
| 2350 | ) |
| 2351 | |
| 2352 | assert result["attachment"] == { |
| 2353 | "name": "image.jpg", |
| 2354 | "mimetype": "image/jpeg", |
| 2355 | "s3key": "s3-key-123", |
| 2356 | } |
| 2357 | mock_from_path.assert_called_once() |
| 2358 | |
| 2359 | @patch("composio.core.models._files.FileUploadable.from_path") |
| 2360 | def test_substitute_file_uploads_with_url_in_anyof( |
| 2361 | self, mock_from_path, file_helper, mock_tool |
| 2362 | ): |
| 2363 | """Test URL handling in anyOf schema variants.""" |
| 2364 | mock_tool.input_parameters = { |
| 2365 | "type": "object", |
| 2366 | "properties": { |
| 2367 | "file": { |
| 2368 | "anyOf": [ |
| 2369 | {"type": "string", "file_uploadable": True}, |
| 2370 | {"type": "null"}, |
| 2371 | ] |
| 2372 | }, |
| 2373 | }, |
| 2374 | } |
| 2375 | |
| 2376 | mock_uploadable = MagicMock() |
| 2377 | mock_uploadable.model_dump.return_value = { |
| 2378 | "name": "doc.pdf", |
| 2379 | "mimetype": "application/pdf", |
| 2380 | "s3key": "s3-key-456", |
| 2381 | } |
| 2382 | mock_from_path.return_value = mock_uploadable |
| 2383 | |
| 2384 | result = file_helper._substitute_file_uploads_recursively( |
| 2385 | tool=mock_tool, |
| 2386 | schema=mock_tool.input_parameters, |
| 2387 | request={"file": "https://docs.example.com/doc.pdf"}, |
| 2388 | ) |
| 2389 | |
| 2390 | assert result["file"] == { |
| 2391 | "name": "doc.pdf", |
| 2392 | "mimetype": "application/pdf", |
| 2393 | "s3key": "s3-key-456", |
| 2394 | } |
| 2395 | |
| 2396 | @patch("composio.core.models._files.FileUploadable.from_path") |
| 2397 | def test_substitute_file_uploads_array_with_urls( |
| 2398 | self, mock_from_path, file_helper, mock_tool |
| 2399 | ): |
| 2400 | """Test URL handling in arrays with file_uploadable items.""" |
| 2401 | mock_tool.input_parameters = { |
| 2402 | "type": "object", |
| 2403 | "properties": { |
| 2404 | "attachments": { |
| 2405 | "type": "array", |
| 2406 | "items": {"type": "string", "file_uploadable": True}, |
| 2407 | }, |
| 2408 | }, |
| 2409 | } |
| 2410 | |
| 2411 | mock_uploadable1 = MagicMock() |
| 2412 | mock_uploadable1.model_dump.return_value = { |
| 2413 | "name": "file1.jpg", |
| 2414 | "mimetype": "image/jpeg", |
| 2415 | "s3key": "key1", |
| 2416 | } |
| 2417 | mock_uploadable2 = MagicMock() |
| 2418 | mock_uploadable2.model_dump.return_value = { |
| 2419 | "name": "file2.png", |
| 2420 | "mimetype": "image/png", |
| 2421 | "s3key": "key2", |
| 2422 | } |
| 2423 | mock_from_path.side_effect = [mock_uploadable1, mock_uploadable2] |
| 2424 | |
| 2425 | result = file_helper._substitute_file_uploads_recursively( |
| 2426 | tool=mock_tool, |
| 2427 | schema=mock_tool.input_parameters, |
| 2428 | request={ |
| 2429 | "attachments": [ |
| 2430 | "https://example.com/file1.jpg", |
| 2431 | "https://example.com/file2.png", |
| 2432 | ] |
| 2433 | }, |
| 2434 | ) |
| 2435 | |
| 2436 | assert len(result["attachments"]) == 2 |
| 2437 | assert result["attachments"][0]["s3key"] == "key1" |
| 2438 | assert result["attachments"][1]["s3key"] == "key2" |
| 2439 | |
| 2440 | |
| 2441 | class TestTruncateFilename: |
| 2442 | """Test cases for _truncate_filename function. |
| 2443 | |
| 2444 | Long filenames are common with public bucket URLs containing hashes or UUIDs. |
| 2445 | These can cause issues, so they are replaced with timestamped filenames. |
| 2446 | """ |
| 2447 | |
| 2448 | def test_truncate_filename_short_unchanged(self): |
| 2449 | """Short filenames should not be modified.""" |
| 2450 | assert _truncate_filename("document.pdf") == "document.pdf" |
| 2451 | assert _truncate_filename("image.jpg") == "image.jpg" |
| 2452 | assert _truncate_filename("file.txt") == "file.txt" |
| 2453 | |
| 2454 | def test_truncate_filename_at_limit_unchanged(self): |
| 2455 | """Filenames exactly at the limit should not be modified.""" |
| 2456 | # Create a filename exactly at the limit (100 chars by default) |
| 2457 | name = "a" * 95 + ".pdf" # 95 + 4 = 99 chars |
| 2458 | assert _truncate_filename(name) == name |
| 2459 | |
| 2460 | name_at_limit = "a" * 96 + ".pdf" # 96 + 4 = 100 chars |
| 2461 | assert _truncate_filename(name_at_limit) == name_at_limit |
| 2462 | |
| 2463 | def test_truncate_filename_long_generates_timestamped(self): |
| 2464 | """Long filenames should be replaced with timestamped filename.""" |
| 2465 | long_name = "a" * 150 + ".pdf" |
| 2466 | result = _truncate_filename(long_name) |
| 2467 | |
| 2468 | assert len(result) < _MAX_FILENAME_LENGTH |
| 2469 | assert result.startswith("file_") |
| 2470 | assert result.endswith(".pdf") |
| 2471 | |
| 2472 | def test_truncate_filename_preserves_extension(self): |
| 2473 | """Extension should be preserved when generating timestamped filename.""" |
| 2474 | test_cases = [ |
| 2475 | ("very_long_" * 20 + ".docx", ".docx"), |
| 2476 | ("hash_" * 30 + ".jpg", ".jpg"), |
| 2477 | ("uuid_" * 25 + ".png", ".png"), |
| 2478 | ("data_" * 40 + ".json", ".json"), |
| 2479 | ] |
| 2480 | |
| 2481 | for long_name, expected_ext in test_cases: |
| 2482 | result = _truncate_filename(long_name) |
| 2483 | assert result.endswith(expected_ext), ( |
| 2484 | f"Expected {result} to end with {expected_ext}" |
| 2485 | ) |
| 2486 | |
| 2487 | def test_truncate_filename_long_hash_url(self): |
| 2488 | """Hash-based filenames from URLs should be truncated.""" |
| 2489 | # Typical long hash filename from public bucket URLs (needs to be >100 chars) |
| 2490 | hash_filename = "8f14e45fceea167a5a36dedd4bea2543_5d41402abc4b2a76b9719d911017c592_extra_hash_data_to_exceed_limit_download.jpg" |
| 2491 | assert len(hash_filename) > _MAX_FILENAME_LENGTH # Verify test setup |
| 2492 | |
| 2493 | result = _truncate_filename(hash_filename) |
| 2494 | |
| 2495 | assert len(result) <= _MAX_FILENAME_LENGTH |
| 2496 | assert result.startswith("file_") |
| 2497 | assert result.endswith(".jpg") |
| 2498 | |
| 2499 | def test_truncate_filename_multiple_hashes(self): |
| 2500 | """Multiple concatenated hashes should be truncated.""" |
| 2501 | # 32 char hash * 5 = 160 chars + extension |
| 2502 | multi_hash = "8f14e45fceea167a5a36dedd4bea2543" * 5 + ".pdf" |
| 2503 | result = _truncate_filename(multi_hash) |
| 2504 | |
| 2505 | assert len(result) <= _MAX_FILENAME_LENGTH |
| 2506 | assert result.endswith(".pdf") |
| 2507 | |
| 2508 | def test_truncate_filename_no_extension(self): |
| 2509 | """Long filenames without extension should still be truncated.""" |
| 2510 | long_name = "a" * 150 |
| 2511 | result = _truncate_filename(long_name) |
| 2512 | |
| 2513 | assert len(result) <= _MAX_FILENAME_LENGTH |
| 2514 | assert result.startswith("file_") |
| 2515 | # Should not end with a dot |
| 2516 | assert not result.endswith(".") |
| 2517 | |
| 2518 | def test_truncate_filename_custom_max_length(self): |
| 2519 | """Custom max_length parameter should be respected.""" |
| 2520 | name = "a" * 60 + ".txt" # 64 chars total |
| 2521 | |
| 2522 | # With default limit (100), should be unchanged |
| 2523 | assert _truncate_filename(name) == name |
| 2524 | |
| 2525 | # With custom limit (50), should be truncated |
| 2526 | result = _truncate_filename(name, max_length=50) |
| 2527 | assert len(result) <= 50 |
| 2528 | assert result.startswith("file_") |
| 2529 | assert result.endswith(".txt") |
| 2530 | |
| 2531 | def test_truncate_filename_edge_case_one_over_limit(self): |
| 2532 | """Filename one character over limit should be truncated.""" |
| 2533 | # Create filename exactly one char over the limit |
| 2534 | name = "a" * 97 + ".pdf" # 97 + 4 = 101 chars (one over default 100) |
| 2535 | result = _truncate_filename(name) |
| 2536 | |
| 2537 | assert len(result) < _MAX_FILENAME_LENGTH |
| 2538 | assert result.startswith("file_") |
| 2539 | assert result.endswith(".pdf") |
| 2540 | |
| 2541 | def test_truncate_filename_long_extension(self): |
| 2542 | """Long extensions should be preserved when truncating.""" |
| 2543 | long_name = "file_" * 30 + ".dockerfile" |
| 2544 | result = _truncate_filename(long_name) |
| 2545 | |
| 2546 | assert result.endswith(".dockerfile") |
| 2547 | assert result.startswith("file_") |
| 2548 | |
| 2549 | def test_truncate_filename_multiple_dots_preserves_last_extension(self): |
| 2550 | """Filename with multiple dots should preserve only the last extension.""" |
| 2551 | long_name = "archive" * 20 + ".backup.tar.gz" |
| 2552 | result = _truncate_filename(long_name) |
| 2553 | |
| 2554 | # rsplit(".", 1) takes the part after the last dot |
| 2555 | assert result.endswith(".gz") |
| 2556 | |
| 2557 | |
| 2558 | class TestFetchFileFromUrlWithTruncation: |
| 2559 | """Test cases for _fetch_file_from_url with filename truncation.""" |
| 2560 | |
| 2561 | @patch("composio.core.models._files.safe_get") |
| 2562 | def test_fetch_truncates_long_filename(self, mock_get): |
| 2563 | """Long filenames from URLs should be truncated.""" |
| 2564 | mock_response = MagicMock() |
| 2565 | mock_response.ok = True |
| 2566 | mock_response.status_code = 200 |
| 2567 | mock_response.headers = {"content-type": "application/pdf"} |
| 2568 | mock_response.iter_content.return_value = [b"test content"] |
| 2569 | mock_response.close = MagicMock() |
| 2570 | mock_get.return_value = mock_response |
| 2571 | |
| 2572 | # Create a very long filename (hash-based, common in public buckets) |
| 2573 | long_filename = "8f14e45fceea167a5a36dedd4bea2543" * 5 + ".pdf" |
| 2574 | |
| 2575 | filename, content, mimetype = _fetch_file_from_url( |
| 2576 | f"https://bucket.example.com/files/{long_filename}" |
| 2577 | ) |
| 2578 | |
| 2579 | assert len(filename) <= _MAX_FILENAME_LENGTH |
| 2580 | assert filename.startswith("file_") |
| 2581 | assert filename.endswith(".pdf") |
| 2582 | assert content == b"test content" |
| 2583 | |
| 2584 | @patch("composio.core.models._files.safe_get") |
| 2585 | def test_fetch_preserves_short_filename(self, mock_get): |
| 2586 | """Short filenames should be preserved unchanged.""" |
| 2587 | mock_response = MagicMock() |
| 2588 | mock_response.ok = True |
| 2589 | mock_response.status_code = 200 |
| 2590 | mock_response.headers = {"content-type": "image/jpeg"} |
| 2591 | mock_response.iter_content.return_value = [b"image data"] |
| 2592 | mock_response.close = MagicMock() |
| 2593 | mock_get.return_value = mock_response |
| 2594 | |
| 2595 | filename, content, mimetype = _fetch_file_from_url( |
| 2596 | "https://example.com/photo.jpg" |
| 2597 | ) |
| 2598 | |
| 2599 | assert filename == "photo.jpg" |
| 2600 | |
| 2601 | @patch("composio.core.models._files.safe_get") |
| 2602 | def test_fetch_truncates_after_adding_extension(self, mock_get): |
| 2603 | """Truncation should happen after extension is appended.""" |
| 2604 | mock_response = MagicMock() |
| 2605 | mock_response.ok = True |
| 2606 | mock_response.status_code = 200 |
| 2607 | mock_response.headers = {"content-type": "application/pdf"} |
| 2608 | mock_response.iter_content.return_value = [b"pdf content"] |
| 2609 | mock_response.close = MagicMock() |
| 2610 | mock_get.return_value = mock_response |
| 2611 | |
| 2612 | # URL without extension - extension will be added from mimetype |
| 2613 | long_name_no_ext = "8f14e45fceea167a5a36dedd4bea2543" * 4 |
| 2614 | filename, content, mimetype = _fetch_file_from_url( |
| 2615 | f"https://bucket.example.com/{long_name_no_ext}" |
| 2616 | ) |
| 2617 | |
| 2618 | # Should be truncated and have .pdf extension |
| 2619 | assert len(filename) <= _MAX_FILENAME_LENGTH |
| 2620 | assert filename.endswith(".pdf") |
| 2621 | |
| 2622 | @patch("composio.core.models._files.safe_get") |
| 2623 | def test_fetch_generated_filename_not_truncated(self, mock_get): |
| 2624 | """Generated timestamped filenames (when URL has no filename) should be short enough.""" |
| 2625 | mock_response = MagicMock() |
| 2626 | mock_response.ok = True |
| 2627 | mock_response.status_code = 200 |
| 2628 | mock_response.headers = {"content-type": "image/png"} |
| 2629 | mock_response.iter_content.return_value = [b"data"] |
| 2630 | mock_response.close = MagicMock() |
| 2631 | mock_get.return_value = mock_response |
| 2632 | |
| 2633 | # URL with no filename - will generate a timestamped one |
| 2634 | filename, content, mimetype = _fetch_file_from_url("https://example.com/") |
| 2635 | |
| 2636 | # Generated filename should be naturally short |
| 2637 | assert filename.startswith("file_") |
| 2638 | assert filename.endswith(".png") |
| 2639 | assert len(filename) < 50 # Timestamped names are short |
| 2640 | |
| 2641 | @patch("composio.core.models._files.safe_get") |
| 2642 | def test_fetch_long_real_world_url(self, mock_get): |
| 2643 | """Long real-world URLs should be handled correctly.""" |
| 2644 | mock_response = MagicMock() |
| 2645 | mock_response.ok = True |
| 2646 | mock_response.status_code = 200 |
| 2647 | mock_response.headers = {"content-type": "application/pdf"} |
| 2648 | mock_response.iter_content.return_value = [b"test content"] |
| 2649 | mock_response.close = MagicMock() |
| 2650 | mock_get.return_value = mock_response |
| 2651 | |
| 2652 | # Real-world long URL example (no extension, relies on mimetype) |
| 2653 | long_url = ( |
| 2654 | "https://encrypted-tbn0.gstatic.com/images?q=" |
| 2655 | "tbn:ANd9GcQq0powzLhfi1bakZ0eNSIA_aJ_5UlPsCte1g&s" |
| 2656 | ) |
| 2657 | filename, content, mimetype = _fetch_file_from_url(long_url) |
| 2658 | |
| 2659 | assert len(filename) <= _MAX_FILENAME_LENGTH |
| 2660 | assert filename.startswith("file_") |
| 2661 | assert filename.endswith(".pdf") |
| 2662 | assert content == b"test content" |
| 2663 | |
| 2664 | |
| 2665 | class TestResponseSizeLimit: |
| 2666 | """Test response size limiting.""" |
| 2667 | |
| 2668 | @patch("composio.core.models._files.safe_get") |
| 2669 | def test_rejects_oversized_content_length(self, mock_get): |
| 2670 | """Files with Content-Length > max_size should be rejected early.""" |
| 2671 | mock_response = MagicMock() |
| 2672 | mock_response.ok = True |
| 2673 | mock_response.status_code = 200 |
| 2674 | mock_response.headers = {"Content-Length": "200000000"} # 200MB |
| 2675 | mock_response.close = MagicMock() |
| 2676 | mock_get.return_value = mock_response |
| 2677 | |
| 2678 | with pytest.raises(ResponseTooLargeError): |
| 2679 | _fetch_file_from_url( |
| 2680 | "https://example.com/large.zip", max_size=100 * 1024 * 1024 |
| 2681 | ) |
| 2682 | |
| 2683 | @patch("composio.core.models._files.safe_get") |
| 2684 | def test_rejects_oversized_during_streaming(self, mock_get): |
| 2685 | """Files that exceed max_size during download should be rejected.""" |
| 2686 | mock_response = MagicMock() |
| 2687 | mock_response.ok = True |
| 2688 | mock_response.status_code = 200 |
| 2689 | mock_response.headers = {} # No Content-Length |
| 2690 | # Return 20MB of data in chunks |
| 2691 | mock_response.iter_content.return_value = [ |
| 2692 | b"x" * 1024 * 1024 for _ in range(20) |
| 2693 | ] |
| 2694 | mock_response.close = MagicMock() |
| 2695 | mock_get.return_value = mock_response |
| 2696 | |
| 2697 | with pytest.raises(ResponseTooLargeError): |
| 2698 | _fetch_file_from_url( |
| 2699 | "https://example.com/large.zip", max_size=10 * 1024 * 1024 |
| 2700 | ) |
| 2701 | |
| 2702 | @patch("composio.core.models._files.safe_get") |
| 2703 | def test_accepts_file_within_limit(self, mock_get): |
| 2704 | """Files within size limit should be accepted.""" |
| 2705 | mock_response = MagicMock() |
| 2706 | mock_response.ok = True |
| 2707 | mock_response.status_code = 200 |
| 2708 | mock_response.headers = {"content-type": "image/jpeg", "Content-Length": "1000"} |
| 2709 | mock_response.iter_content.return_value = [b"x" * 1000] |
| 2710 | mock_response.close = MagicMock() |
| 2711 | mock_get.return_value = mock_response |
| 2712 | |
| 2713 | filename, content, mimetype = _fetch_file_from_url( |
| 2714 | "https://example.com/image.jpg", max_size=10 * 1024 * 1024 |
| 2715 | ) |
| 2716 | |
| 2717 | assert len(content) == 1000 |
| 2718 | assert mimetype == "image/jpeg" |
| 2719 | |
| 2720 | |
| 2721 | class TestDownloadSizeLimit: |
| 2722 | """``FileDownloadable.download`` streams an untrusted body to disk.""" |
| 2723 | |
| 2724 | @staticmethod |
| 2725 | def _downloadable() -> FileDownloadable: |
| 2726 | return FileDownloadable( |
| 2727 | name="report.bin", |
| 2728 | mimetype="application/octet-stream", |
| 2729 | s3url="https://example.com/report.bin", |
| 2730 | ) |
| 2731 | |
| 2732 | @patch("composio.core.models._files.safe_request") |
| 2733 | def test_download_rejects_oversized_content_length(self, mock_get, tmp_path): |
| 2734 | """A self-declared oversized body is rejected before any bytes are read.""" |
| 2735 | mock_response = MagicMock() |
| 2736 | mock_response.status_code = 200 |
| 2737 | mock_response.headers = {"Content-Length": "200000000"} |
| 2738 | mock_response.close = MagicMock() |
| 2739 | mock_get.return_value = mock_response |
| 2740 | |
| 2741 | with pytest.raises(ResponseTooLargeError): |
| 2742 | self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024) |
| 2743 | |
| 2744 | mock_response.iter_content.assert_not_called() |
| 2745 | |
| 2746 | @patch("composio.core.models._files.safe_request") |
| 2747 | def test_download_rejects_oversized_during_streaming(self, mock_get, tmp_path): |
| 2748 | """A dishonest (here, absent) Content-Length cannot bypass the cap.""" |
| 2749 | mock_response = MagicMock() |
| 2750 | mock_response.status_code = 200 |
| 2751 | mock_response.headers = {} |
| 2752 | mock_response.iter_content.return_value = [b"x" * 512 for _ in range(4)] |
| 2753 | mock_response.close = MagicMock() |
| 2754 | mock_get.return_value = mock_response |
| 2755 | |
| 2756 | with pytest.raises(ResponseTooLargeError): |
| 2757 | self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024) |
| 2758 | |
| 2759 | @pytest.mark.parametrize("existing", [False, True]) |
| 2760 | @patch("composio.core.models._files.safe_request") |
| 2761 | def test_download_removes_partial_file_on_failure( |
| 2762 | self, mock_get, tmp_path, existing |
| 2763 | ): |
| 2764 | """A truncated download must not be left behind as if it succeeded.""" |
| 2765 | mock_response = MagicMock() |
| 2766 | mock_response.status_code = 200 |
| 2767 | mock_response.headers = {} |
| 2768 | mock_response.iter_content.return_value = [b"x" * 512 for _ in range(4)] |
| 2769 | mock_response.close = MagicMock() |
| 2770 | mock_get.return_value = mock_response |
| 2771 | |
| 2772 | if existing: |
| 2773 | (tmp_path / "report.bin").write_bytes(b"original") |
| 2774 | |
| 2775 | with pytest.raises(ResponseTooLargeError): |
| 2776 | self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024) |
| 2777 | |
| 2778 | if existing: |
| 2779 | assert list(tmp_path.iterdir()) == [tmp_path / "report.bin"] |
| 2780 | assert (tmp_path / "report.bin").read_bytes() == b"original" |
| 2781 | else: |
| 2782 | assert list(tmp_path.iterdir()) == [] |
| 2783 | |
| 2784 | @patch("composio.core.models._files.safe_request") |
| 2785 | def test_download_accepts_file_within_limit(self, mock_get, tmp_path): |
| 2786 | """A body under the cap is written through unchanged.""" |
| 2787 | mock_response = MagicMock() |
| 2788 | mock_response.status_code = 200 |
| 2789 | mock_response.headers = {} |
| 2790 | mock_response.iter_content.return_value = [b"x" * 256, b"y" * 256] |
| 2791 | mock_response.close = MagicMock() |
| 2792 | mock_get.return_value = mock_response |
| 2793 | |
| 2794 | outfile = self._downloadable().download( |
| 2795 | outdir=tmp_path, root=tmp_path, max_size=1024 |
| 2796 | ) |
| 2797 | |
| 2798 | assert outfile.exists() |
| 2799 | assert outfile.read_bytes() == b"x" * 256 + b"y" * 256 |
| 2800 | |
| 2801 | @patch("composio.core.models._files.safe_request") |
| 2802 | def test_download_wraps_stream_failure_and_removes_partial_file( |
| 2803 | self, mock_get, tmp_path |
| 2804 | ): |
| 2805 | """A transport failure mid-stream keeps the documented error contract.""" |
| 2806 | |
| 2807 | def failing_stream(chunk_size=None): |
| 2808 | yield b"x" * 256 |
| 2809 | raise requests.exceptions.ConnectionError("connection reset") |
| 2810 | |
| 2811 | mock_response = MagicMock() |
| 2812 | mock_response.status_code = 200 |
| 2813 | mock_response.headers = {} |
| 2814 | mock_response.iter_content.side_effect = failing_stream |
| 2815 | mock_response.close = MagicMock() |
| 2816 | mock_get.return_value = mock_response |
| 2817 | |
| 2818 | with pytest.raises(ErrorDownloadingFile): |
| 2819 | self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024) |
| 2820 | |
| 2821 | assert list(tmp_path.iterdir()) == [] |
| 2822 | |
| 2823 | @patch("composio.core.models._files.safe_request") |
| 2824 | def test_download_wraps_write_failure_and_removes_partial_file( |
| 2825 | self, mock_get, tmp_path |
| 2826 | ): |
| 2827 | """A disk failure while writing is an `ErrorDownloadingFile`, not a raw OSError.""" |
| 2828 | |
| 2829 | def failing_stream(chunk_size=None): |
| 2830 | yield b"x" * 256 |
| 2831 | raise OSError(28, "No space left on device") |
| 2832 | |
| 2833 | mock_response = MagicMock() |
| 2834 | mock_response.status_code = 200 |
| 2835 | mock_response.headers = {} |
| 2836 | mock_response.iter_content.side_effect = failing_stream |
| 2837 | mock_response.close = MagicMock() |
| 2838 | mock_get.return_value = mock_response |
| 2839 | |
| 2840 | with pytest.raises(ErrorDownloadingFile): |
| 2841 | self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024) |
| 2842 | |
| 2843 | assert list(tmp_path.iterdir()) == [] |
| 2844 | |
| 2845 | |
| 2846 | class TestRedirectHandling: |
| 2847 | """Test redirect handling (redirects should be rejected).""" |
| 2848 | |
| 2849 | @patch("composio.core.models._files.safe_get") |
| 2850 | def test_rejects_redirect_302(self, mock_get): |
| 2851 | """302 redirects should be rejected with clear error message.""" |
| 2852 | mock_response = MagicMock() |
| 2853 | mock_response.status_code = 302 |
| 2854 | mock_response.headers = {"Location": "https://example.com/final.jpg"} |
| 2855 | mock_response.close = MagicMock() |
| 2856 | mock_get.return_value = mock_response |
| 2857 | |
| 2858 | with pytest.raises(ErrorUploadingFile, match="redirect"): |
| 2859 | _fetch_file_from_url("https://example.com/redirect") |
| 2860 | |
| 2861 | @patch("composio.core.models._files.safe_get") |
| 2862 | def test_rejects_redirect_301(self, mock_get): |
| 2863 | """301 redirects should be rejected.""" |
| 2864 | mock_response = MagicMock() |
| 2865 | mock_response.status_code = 301 |
| 2866 | mock_response.headers = {"Location": "https://example.com/"} |
| 2867 | mock_response.close = MagicMock() |
| 2868 | mock_get.return_value = mock_response |
| 2869 | |
| 2870 | with pytest.raises(ErrorUploadingFile, match="redirect"): |
| 2871 | _fetch_file_from_url("https://example.com/test") |
| 2872 | |
| 2873 | @patch("composio.core.models._files.safe_get") |
| 2874 | def test_rejects_redirect_307(self, mock_get): |
| 2875 | """307 redirects should be rejected.""" |
| 2876 | mock_response = MagicMock() |
| 2877 | mock_response.status_code = 307 |
| 2878 | mock_response.headers = {"Location": "https://example.com/"} |
| 2879 | mock_response.close = MagicMock() |
| 2880 | mock_get.return_value = mock_response |
| 2881 | |
| 2882 | with pytest.raises(ErrorUploadingFile, match="redirect"): |
| 2883 | _fetch_file_from_url("https://example.com/test") |
| 2884 | |
| 2885 | @patch("composio.core.models._files.safe_get") |
| 2886 | def test_rejects_redirect_308(self, mock_get): |
| 2887 | """308 redirects should be rejected.""" |
| 2888 | mock_response = MagicMock() |
| 2889 | mock_response.status_code = 308 |
| 2890 | mock_response.headers = {"Location": "https://example.com/"} |
| 2891 | mock_response.close = MagicMock() |
| 2892 | mock_get.return_value = mock_response |
| 2893 | |
| 2894 | with pytest.raises(ErrorUploadingFile, match="redirect"): |
| 2895 | _fetch_file_from_url("https://example.com/test") |
| 2896 | |
| 2897 | |
| 2898 | class TestS3UploadErrorHandling: |
| 2899 | """Test S3 upload error handling.""" |
| 2900 | |
| 2901 | @patch("composio.core.models._files.safe_request") |
| 2902 | def test_403_is_treated_as_error(self, mock_safe_request): |
| 2903 | """HTTP 403 should be treated as upload failure.""" |
| 2904 | mock_client = MagicMock() |
| 2905 | mock_s3_response = MagicMock() |
| 2906 | mock_s3_response.key = "s3-key" |
| 2907 | mock_s3_response.new_presigned_url = "https://s3.example.com/upload" |
| 2908 | mock_client.files.create_presigned_url.return_value = mock_s3_response |
| 2909 | |
| 2910 | mock_put_response = MagicMock() |
| 2911 | mock_put_response.status_code = 403 |
| 2912 | mock_safe_request.return_value = mock_put_response |
| 2913 | |
| 2914 | with pytest.raises(ErrorUploadingFile, match="403"): |
| 2915 | _upload_bytes_to_s3( |
| 2916 | client=mock_client, |
| 2917 | filename="test.jpg", |
| 2918 | content=b"data", |
| 2919 | mimetype="image/jpeg", |
| 2920 | tool="TEST", |
| 2921 | toolkit="test", |
| 2922 | ) |
| 2923 | |
| 2924 | @patch("composio.core.models._files.safe_request") |
| 2925 | def test_200_is_success(self, mock_safe_request): |
| 2926 | """HTTP 200 should be treated as success.""" |
| 2927 | mock_client = MagicMock() |
| 2928 | mock_s3_response = MagicMock() |
| 2929 | mock_s3_response.key = "s3-key" |
| 2930 | mock_s3_response.new_presigned_url = "https://s3.example.com/upload" |
| 2931 | mock_client.files.create_presigned_url.return_value = mock_s3_response |
| 2932 | |
| 2933 | mock_put_response = MagicMock() |
| 2934 | mock_put_response.status_code = 200 |
| 2935 | mock_safe_request.return_value = mock_put_response |
| 2936 | |
| 2937 | result = _upload_bytes_to_s3( |
| 2938 | client=mock_client, |
| 2939 | filename="test.jpg", |
| 2940 | content=b"data", |
| 2941 | mimetype="image/jpeg", |
| 2942 | tool="TEST", |
| 2943 | toolkit="test", |
| 2944 | ) |
| 2945 | assert result == "s3-key" |
| 2946 | |
| 2947 | @patch("composio.core.models._files.safe_request") |
| 2948 | def test_500_is_treated_as_error(self, mock_safe_request): |
| 2949 | """HTTP 500 should be treated as upload failure.""" |
| 2950 | mock_client = MagicMock() |
| 2951 | mock_s3_response = MagicMock() |
| 2952 | mock_s3_response.key = "s3-key" |
| 2953 | mock_s3_response.new_presigned_url = "https://s3.example.com/upload" |
| 2954 | mock_client.files.create_presigned_url.return_value = mock_s3_response |
| 2955 | |
| 2956 | mock_put_response = MagicMock() |
| 2957 | mock_put_response.status_code = 500 |
| 2958 | mock_safe_request.return_value = mock_put_response |
| 2959 | |
| 2960 | with pytest.raises(ErrorUploadingFile, match="500"): |
| 2961 | _upload_bytes_to_s3( |
| 2962 | client=mock_client, |
| 2963 | filename="test.jpg", |
| 2964 | content=b"data", |
| 2965 | mimetype="image/jpeg", |
| 2966 | tool="TEST", |
| 2967 | toolkit="test", |
| 2968 | ) |
| 2969 | |
| 2970 | |
| 2971 | class TestUrlSanitization: |
| 2972 | """Test URL sanitization for logging.""" |
| 2973 | |
| 2974 | def test_sanitizes_query_params(self): |
| 2975 | """Query parameters should be redacted in logs.""" |
| 2976 | url = "https://example.com/file?token=secret123&key=abc" |
| 2977 | sanitized = _sanitize_url_for_logging(url) |
| 2978 | assert "secret123" not in sanitized |
| 2979 | assert "abc" not in sanitized |
| 2980 | assert "[REDACTED]" in sanitized |
| 2981 | |
| 2982 | def test_preserves_url_without_query(self): |
| 2983 | """URLs without query params should be unchanged.""" |
| 2984 | url = "https://example.com/path/to/file.jpg" |
| 2985 | sanitized = _sanitize_url_for_logging(url) |
| 2986 | assert sanitized == url |
| 2987 | |
| 2988 | def test_preserves_path(self): |
| 2989 | """Path should be preserved when redacting query params.""" |
| 2990 | url = "https://example.com/path/to/file.jpg?token=secret" |
| 2991 | sanitized = _sanitize_url_for_logging(url) |
| 2992 | assert "/path/to/file.jpg" in sanitized |
| 2993 | assert "example.com" in sanitized |
| 2994 | |
| 2995 | def test_handles_empty_query(self): |
| 2996 | """URLs with empty query string should not have [REDACTED].""" |
| 2997 | url = "https://example.com/file.jpg" |
| 2998 | sanitized = _sanitize_url_for_logging(url) |
| 2999 | assert "[REDACTED]" not in sanitized |
| 3000 | |
| 3001 | |
| 3002 | class TestFileDownloadablePathTraversal: |
| 3003 | """SEC-316: server-controlled `name` must not escape the output dir.""" |
| 3004 | |
| 3005 | @pytest.fixture(autouse=True) |
| 3006 | def _allow_fetch_target(self): |
| 3007 | yield |
| 3008 | |
| 3009 | def _mock_response(self, content: bytes = b"data") -> MagicMock: |
| 3010 | response = MagicMock() |
| 3011 | response.status_code = 200 |
| 3012 | response.iter_content = lambda chunk_size: [content] |
| 3013 | response.close = MagicMock() |
| 3014 | return response |
| 3015 | |
| 3016 | def test_relative_traversal_is_neutralized(self, tmp_path): |
| 3017 | outdir = tmp_path / "safe" |
| 3018 | # outdir does not need to exist yet — download() creates it. |
| 3019 | f = FileDownloadable( |
| 3020 | name="../../../PWNED.sh", |
| 3021 | mimetype="application/octet-stream", |
| 3022 | s3url="https://example.com/file", |
| 3023 | ) |
| 3024 | with patch( |
| 3025 | "composio.core.models._files.safe_request", |
| 3026 | return_value=self._mock_response(b"#!/bin/sh\n"), |
| 3027 | ): |
| 3028 | written = f.download(outdir, root=outdir) |
| 3029 | |
| 3030 | # Traversal sequence collapsed to basename and stayed inside outdir. |
| 3031 | assert written == outdir / "PWNED.sh" |
| 3032 | assert written.resolve().is_relative_to(outdir.resolve()) |
| 3033 | assert not (tmp_path / "PWNED.sh").exists() |
| 3034 | assert written.read_bytes() == b"#!/bin/sh\n" |
| 3035 | |
| 3036 | def test_absolute_path_is_neutralized(self, tmp_path): |
| 3037 | outdir = tmp_path / "safe" |
| 3038 | f = FileDownloadable( |
| 3039 | name="/etc/passwd", |
| 3040 | mimetype="application/octet-stream", |
| 3041 | s3url="https://example.com/file", |
| 3042 | ) |
| 3043 | with patch( |
| 3044 | "composio.core.models._files.safe_request", |
| 3045 | return_value=self._mock_response(b"x"), |
| 3046 | ): |
| 3047 | written = f.download(outdir, root=outdir) |
| 3048 | |
| 3049 | # `Path('/etc/passwd').name == 'passwd'` — absolute path is stripped. |
| 3050 | assert written == outdir / "passwd" |
| 3051 | assert written.resolve().is_relative_to(outdir.resolve()) |
| 3052 | |
| 3053 | def test_dotdot_only_name_is_rejected(self, tmp_path): |
| 3054 | """`Path('..').name == '..'`. Basename strip alone wouldn't catch it, |
| 3055 | but the second-line `is_relative_to()` check does.""" |
| 3056 | from composio.exceptions import ErrorDownloadingFile |
| 3057 | |
| 3058 | outdir = tmp_path / "safe" |
| 3059 | f = FileDownloadable( |
| 3060 | name="..", |
| 3061 | mimetype="application/octet-stream", |
| 3062 | s3url="https://example.com/file", |
| 3063 | ) |
| 3064 | with patch( |
| 3065 | "composio.core.models._files.safe_request", |
| 3066 | return_value=self._mock_response(), |
| 3067 | ): |
| 3068 | with pytest.raises(ErrorDownloadingFile, match="Path traversal detected"): |
| 3069 | f.download(outdir, root=outdir) |
| 3070 | # No file was written under the parent. |
| 3071 | assert not (tmp_path / "x").exists() |
| 3072 | |
| 3073 | def test_safe_filename_passes_through(self, tmp_path): |
| 3074 | outdir = tmp_path / "safe" |
| 3075 | f = FileDownloadable( |
| 3076 | name="report.pdf", |
| 3077 | mimetype="application/pdf", |
| 3078 | s3url="https://example.com/file", |
| 3079 | ) |
| 3080 | with patch( |
| 3081 | "composio.core.models._files.safe_request", |
| 3082 | return_value=self._mock_response(b"%PDF-1.4"), |
| 3083 | ): |
| 3084 | written = f.download(outdir, root=outdir) |
| 3085 | |
| 3086 | assert written == outdir / "report.pdf" |
| 3087 | assert written.read_bytes() == b"%PDF-1.4" |
| 3088 | |
| 3089 | def test_download_uses_timeout(self, tmp_path): |
| 3090 | outdir = tmp_path / "safe" |
| 3091 | f = FileDownloadable( |
| 3092 | name="report.pdf", |
| 3093 | mimetype="application/pdf", |
| 3094 | s3url="https://example.com/file", |
| 3095 | ) |
| 3096 | with patch( |
| 3097 | "composio.core.models._files.safe_request", |
| 3098 | return_value=self._mock_response(b"%PDF-1.4"), |
| 3099 | ) as mock_get: |
| 3100 | f.download(outdir, root=outdir) |
| 3101 | |
| 3102 | mock_get.assert_called_once_with( |
| 3103 | "GET", |
| 3104 | "https://example.com/file", |
| 3105 | stream=True, |
| 3106 | timeout=(5, 60), |
| 3107 | ) |
| 3108 | |
| 3109 | def test_download_timeout_raises_error(self, tmp_path): |
| 3110 | outdir = tmp_path / "safe" |
| 3111 | f = FileDownloadable( |
| 3112 | name="report.pdf", |
| 3113 | mimetype="application/pdf", |
| 3114 | s3url="https://example.com/file?token=abc", |
| 3115 | ) |
| 3116 | with patch( |
| 3117 | "composio.core.models._files.safe_request", |
| 3118 | side_effect=requests.exceptions.Timeout( |
| 3119 | "Max retries exceeded with url: /file?token=abc" |
| 3120 | ), |
| 3121 | ): |
| 3122 | with pytest.raises(ErrorDownloadingFile) as exc_info: |
| 3123 | f.download(outdir, root=outdir) |
| 3124 | |
| 3125 | assert "Error downloading file" in str(exc_info.value) |
| 3126 | assert "token=abc" not in str(exc_info.value) |
| 3127 | |
| 3128 | def test_download_stream_timeout_raises_error(self, tmp_path): |
| 3129 | outdir = tmp_path / "safe" |
| 3130 | response = self._mock_response() |
| 3131 | response.iter_content = MagicMock( |
| 3132 | side_effect=requests.exceptions.ReadTimeout( |
| 3133 | "Max retries exceeded with url: /file?token=abc" |
| 3134 | ) |
| 3135 | ) |
| 3136 | f = FileDownloadable( |
| 3137 | name="report.pdf", |
| 3138 | mimetype="application/pdf", |
| 3139 | s3url="https://example.com/file?token=abc", |
| 3140 | ) |
| 3141 | with patch( |
| 3142 | "composio.core.models._files.safe_request", |
| 3143 | return_value=response, |
| 3144 | ): |
| 3145 | with pytest.raises(ErrorDownloadingFile) as exc_info: |
| 3146 | f.download(outdir, root=outdir) |
| 3147 | |
| 3148 | assert "Error downloading file" in str(exc_info.value) |
| 3149 | assert "token=abc" not in str(exc_info.value) |
| 3150 | response.close.assert_called_once() |
| 3151 | |
| 3152 | def test_download_non_200_redacts_url_and_closes(self, tmp_path): |
| 3153 | outdir = tmp_path / "safe" |
| 3154 | response = self._mock_response() |
| 3155 | response.status_code = 403 |
| 3156 | f = FileDownloadable( |
| 3157 | name="report.pdf", |
| 3158 | mimetype="application/pdf", |
| 3159 | s3url="https://example.com/file?token=abc", |
| 3160 | ) |
| 3161 | with patch( |
| 3162 | "composio.core.models._files.safe_request", |
| 3163 | return_value=response, |
| 3164 | ): |
| 3165 | with pytest.raises(ErrorDownloadingFile) as exc_info: |
| 3166 | f.download(outdir, root=outdir) |
| 3167 | |
| 3168 | assert "token=abc" not in str(exc_info.value) |
| 3169 | response.close.assert_called_once() |
| 3170 | |
| 3171 | def test_basename_collapse_through_subdir_is_rejected(self, tmp_path): |
| 3172 | """`Path('foo/..').name == '..'` — the basename strip of a name that |
| 3173 | traverses *through* a subdir collapses to `..`, then the |
| 3174 | `is_relative_to()` check rejects it. Explicit coverage of the |
| 3175 | residual-`..` path through basename normalization (the plain `..` |
| 3176 | test covers the no-subdir case).""" |
| 3177 | from composio.exceptions import ErrorDownloadingFile |
| 3178 | |
| 3179 | outdir = tmp_path / "safe" |
| 3180 | f = FileDownloadable( |
| 3181 | name="foo/..", |
| 3182 | mimetype="application/octet-stream", |
| 3183 | s3url="https://example.com/file", |
| 3184 | ) |
| 3185 | with patch( |
| 3186 | "composio.core.models._files.safe_request", |
| 3187 | return_value=self._mock_response(), |
| 3188 | ): |
| 3189 | with pytest.raises(ErrorDownloadingFile, match="Path traversal detected"): |
| 3190 | f.download(outdir, root=outdir) |
| 3191 | # SEC-316 P3.1: check runs before mkdir, so outdir is not created |
| 3192 | # as a side effect of a rejected payload. |
| 3193 | assert not outdir.exists() |
| 3194 | |
| 3195 | @pytest.mark.parametrize("name", ["", "."]) |
| 3196 | def test_name_without_usable_basename_is_rejected(self, name, tmp_path): |
| 3197 | """`Path('').name` and `Path('.').name` are both `''`, so `outdir / ''` |
| 3198 | used to resolve to `outdir` itself, pass the containment check (a path |
| 3199 | is relative to itself), and only fail at write time with a raw |
| 3200 | `IsADirectoryError` after the directory had been created. |
| 3201 | |
| 3202 | These are now refused up front. Documents the fail-closed behavior so a |
| 3203 | future change cannot silently weaken it without breaking this test.""" |
| 3204 | from composio.exceptions import ErrorDownloadingFile |
| 3205 | |
| 3206 | outdir = tmp_path / "safe" |
| 3207 | f = FileDownloadable( |
| 3208 | name=name, |
| 3209 | mimetype="application/octet-stream", |
| 3210 | s3url="https://example.com/file", |
| 3211 | ) |
| 3212 | with patch( |
| 3213 | "composio.core.models._files.safe_request", |
| 3214 | return_value=self._mock_response(b"x"), |
| 3215 | ): |
| 3216 | with pytest.raises(ErrorDownloadingFile, match="no usable basename"): |
| 3217 | f.download(outdir, root=outdir) |
| 3218 | |
| 3219 | # Rejected before `mkdir`, so nothing was created on disk at all. |
| 3220 | assert not outdir.exists() |
| 3221 | |
| 3222 | def test_nul_byte_filename_is_rejected(self, tmp_path): |
| 3223 | """`self.name` is untrusted by the same rule as the slugs. Without this |
| 3224 | check a NUL byte escapes as a raw ValueError from `resolve()`.""" |
| 3225 | from composio.exceptions import ErrorDownloadingFile |
| 3226 | |
| 3227 | outdir = tmp_path / "safe" |
| 3228 | f = FileDownloadable( |
| 3229 | name="a\x00b", |
| 3230 | mimetype="application/octet-stream", |
| 3231 | s3url="https://example.com/file", |
| 3232 | ) |
| 3233 | with patch( |
| 3234 | "composio.core.models._files.safe_request", |
| 3235 | return_value=self._mock_response(b"x"), |
| 3236 | ): |
| 3237 | with pytest.raises(ErrorDownloadingFile, match="NUL byte"): |
| 3238 | f.download(outdir, root=outdir) |
| 3239 | assert not outdir.exists() |
| 3240 | |
| 3241 | @pytest.mark.parametrize( |
| 3242 | "name,expected", |
| 3243 | [ |
| 3244 | ( |
| 3245 | "report_2026-09-29T10:30:00.csv", |
| 3246 | "report_2026-09-29T10_30_00-d7211bb25cb815fe.csv", |
| 3247 | ), |
| 3248 | ("What is this?.png", "What is this_-9c68adf2da8b6e8d.png"), |
| 3249 | ('invoice "final".pdf', "invoice _final_-ec493385353bf798.pdf"), |
| 3250 | ("NUL", "_NUL-369eba19c29ffe00"), |
| 3251 | ("NUL.tar.gz", "_NUL.tar-c5d0283bb0ecb474.gz"), |
| 3252 | ("x" * 300 + ".pdf", "x" * 107 + "-b8191dd974368539.pdf"), |
| 3253 | ("請" * 70 + ".pdf", "請" * 35 + "-a7cbc40614b84819.pdf"), |
| 3254 | ], |
| 3255 | ) |
| 3256 | def test_unportable_filenames_are_made_portable(self, name, expected, tmp_path): |
| 3257 | """Ordinary names with a `:` or `?`, reserved device names, and names over |
| 3258 | the byte limit are written under a portable name rather than failing |
| 3259 | the download after the tool has already run. The name is tagged with a |
| 3260 | digest of the original, so names that normalize alike stay apart.""" |
| 3261 | outdir = tmp_path / "safe" |
| 3262 | f = FileDownloadable( |
| 3263 | name=name, |
| 3264 | mimetype="application/octet-stream", |
| 3265 | s3url="https://example.com/file", |
| 3266 | ) |
| 3267 | with patch( |
| 3268 | "composio.core.models._files.safe_request", |
| 3269 | return_value=self._mock_response(b"x"), |
| 3270 | ): |
| 3271 | outfile = f.download(outdir, root=outdir) |
| 3272 | assert outfile == (outdir / expected).resolve() |
| 3273 | assert outfile.read_bytes() == b"x" |
| 3274 | |
| 3275 | def test_names_that_normalize_alike_do_not_overwrite_each_other(self, tmp_path): |
| 3276 | outdir = tmp_path / "safe" |
| 3277 | names = ["report?.png", "report*.png", "report_.png"] |
| 3278 | outfiles = [] |
| 3279 | for index, name in enumerate(names): |
| 3280 | f = FileDownloadable( |
| 3281 | name=name, |
| 3282 | mimetype="image/png", |
| 3283 | s3url="https://example.com/file", |
| 3284 | ) |
| 3285 | with patch( |
| 3286 | "composio.core.models._files.safe_request", |
| 3287 | return_value=self._mock_response(bytes([index])), |
| 3288 | ): |
| 3289 | outfiles.append(f.download(outdir, root=outdir)) |
| 3290 | |
| 3291 | assert len(set(outfiles)) == len(names) |
| 3292 | assert [outfile.read_bytes() for outfile in outfiles] == [ |
| 3293 | b"\x00", |
| 3294 | b"\x01", |
| 3295 | b"\x02", |
| 3296 | ] |
| 3297 | |
| 3298 | @pytest.mark.parametrize( |
| 3299 | "names", |
| 3300 | [ |
| 3301 | ["report?.png", "report_-05fcb95aa5b918e9.png"], |
| 3302 | ["report_-05fcb95aa5b918e9.png", "report?.png"], |
| 3303 | ["請" * 41 + ".pdf"] * 3, |
| 3304 | ], |
| 3305 | ) |
| 3306 | def test_literal_tag_name_does_not_overwrite_download(self, names, tmp_path): |
| 3307 | outdir = tmp_path / "safe" |
| 3308 | outfiles = [] |
| 3309 | for index, name in enumerate(names): |
| 3310 | file = FileDownloadable( |
| 3311 | name=name, |
| 3312 | mimetype="image/png", |
| 3313 | s3url="https://example.com/file", |
| 3314 | ) |
| 3315 | with patch( |
| 3316 | "composio.core.models._files.safe_request", |
| 3317 | return_value=self._mock_response(bytes([index])), |
| 3318 | ): |
| 3319 | outfiles.append(file.download(outdir, root=outdir)) |
| 3320 | |
| 3321 | assert len(set(outfiles)) == len(names) |
| 3322 | assert [outfile.read_bytes() for outfile in outfiles] == [ |
| 3323 | bytes([i]) for i in range(len(names)) |
| 3324 | ] |
| 3325 | assert all(len(outfile.name.encode()) <= 128 for outfile in outfiles) |
| 3326 | assert all(outfile.suffix == Path(names[0]).suffix for outfile in outfiles) |
| 3327 | |
| 3328 | |
| 3329 | class TestDownloadDirSlugTraversal: |
| 3330 | """Server-controlled tool/toolkit slugs must not relocate the download |
| 3331 | directory. |
| 3332 | |
| 3333 | The directory is built from API response fields, so containment has to be |
| 3334 | checked against the locally configured root rather than against the built |
| 3335 | directory itself — the latter is a reference those fields can move. These |
| 3336 | tests pin the anchor to the configured root. |
| 3337 | """ |
| 3338 | |
| 3339 | TRAVERSALS = [ |
| 3340 | "../../../../../etc/escaped", |
| 3341 | "..", |
| 3342 | ".", |
| 3343 | "", |
| 3344 | "/etc", |
| 3345 | "..\\..\\evil", |
| 3346 | "a/b", |
| 3347 | "CON", |
| 3348 | "x" * 200, |
| 3349 | "a\x00b", |
| 3350 | ] |
| 3351 | |
| 3352 | def _tool(self, tool_slug="GMAIL_GET_ATTACHMENT", toolkit_slug="GMAIL"): |
| 3353 | tool = MagicMock() |
| 3354 | tool.slug = tool_slug |
| 3355 | tool.toolkit.slug = toolkit_slug |
| 3356 | tool.output_parameters = { |
| 3357 | "type": "object", |
| 3358 | "properties": {"attachment": {"file_downloadable": True}}, |
| 3359 | } |
| 3360 | return tool |
| 3361 | |
| 3362 | def _response(self): |
| 3363 | return { |
| 3364 | "attachment": { |
| 3365 | "name": "composio", |
| 3366 | "mimetype": "text/plain", |
| 3367 | "s3url": "https://example.com/file", |
| 3368 | } |
| 3369 | } |
| 3370 | |
| 3371 | def _mock_get(self, content: bytes = b"payload"): |
| 3372 | response = MagicMock() |
| 3373 | response.status_code = 200 |
| 3374 | response.iter_content = lambda chunk_size: [content] |
| 3375 | response.close = MagicMock() |
| 3376 | return patch("composio.core.models._files.safe_request", return_value=response) |
| 3377 | |
| 3378 | @pytest.mark.parametrize("slug", TRAVERSALS) |
| 3379 | def test_hostile_tool_slug_is_rejected(self, slug, tmp_path): |
| 3380 | outdir = tmp_path / "files" |
| 3381 | outdir.mkdir() |
| 3382 | helper = FileHelper(client=None, outdir=str(outdir)) |
| 3383 | with self._mock_get(): |
| 3384 | with pytest.raises(UnsafePathComponentError): |
| 3385 | helper.substitute_file_downloads( |
| 3386 | tool=self._tool(tool_slug=slug), response=self._response() |
| 3387 | ) |
| 3388 | # The write is refused before any directory is created. |
| 3389 | assert list(outdir.iterdir()) == [] |
| 3390 | |
| 3391 | @pytest.mark.parametrize("slug", TRAVERSALS) |
| 3392 | def test_hostile_toolkit_slug_is_rejected(self, slug, tmp_path): |
| 3393 | outdir = tmp_path / "files" |
| 3394 | outdir.mkdir() |
| 3395 | helper = FileHelper(client=None, outdir=str(outdir)) |
| 3396 | with self._mock_get(): |
| 3397 | with pytest.raises(UnsafePathComponentError): |
| 3398 | helper.substitute_file_downloads( |
| 3399 | tool=self._tool(toolkit_slug=slug), response=self._response() |
| 3400 | ) |
| 3401 | assert list(outdir.iterdir()) == [] |
| 3402 | |
| 3403 | def test_traversal_does_not_escape_the_configured_root(self, tmp_path): |
| 3404 | """A deep `../` chain in `tool.slug` must not land the payload in a |
| 3405 | sibling of the configured directory.""" |
| 3406 | outdir = tmp_path / "home" / "app" / ".composio" / "files" |
| 3407 | outdir.mkdir(parents=True) |
| 3408 | helper = FileHelper(client=None, outdir=str(outdir)) |
| 3409 | depth = len(outdir.resolve().parts) - len(tmp_path.resolve().parts) + 1 |
| 3410 | traversal = "/".join([".."] * depth) + "/etc/escaped" |
| 3411 | |
| 3412 | with self._mock_get(b"payload-that-must-not-be-written"): |
| 3413 | with pytest.raises(UnsafePathComponentError): |
| 3414 | helper.substitute_file_downloads( |
| 3415 | tool=self._tool(tool_slug=traversal), response=self._response() |
| 3416 | ) |
| 3417 | |
| 3418 | assert not (tmp_path / "etc").exists() |
| 3419 | assert list(outdir.iterdir()) == [] |
| 3420 | |
| 3421 | def test_legitimate_slugs_still_nest_under_the_root(self, tmp_path): |
| 3422 | outdir = tmp_path / "files" |
| 3423 | outdir.mkdir() |
| 3424 | helper = FileHelper(client=None, outdir=str(outdir)) |
| 3425 | with self._mock_get(b"payload"): |
| 3426 | result = helper.substitute_file_downloads( |
| 3427 | tool=self._tool(), response=self._response() |
| 3428 | ) |
| 3429 | |
| 3430 | written = Path(result["attachment"]) |
| 3431 | assert ( |
| 3432 | written.resolve() |
| 3433 | == (outdir / "GMAIL" / "GMAIL_GET_ATTACHMENT" / "composio").resolve() |
| 3434 | ) |
| 3435 | assert written.read_bytes() == b"payload" |
| 3436 | |
| 3437 | def test_tilde_download_dir_still_works(self, tmp_path, monkeypatch): |
| 3438 | """`secure_join` expands `~` in the root; the containment check in |
| 3439 | `download()` must expand it too. When only one side did, every download |
| 3440 | under `file_download_dir='~/...'` failed as a path traversal.""" |
| 3441 | monkeypatch.setenv("HOME", str(tmp_path)) |
| 3442 | helper = FileHelper(client=None, outdir="~/downloads") |
| 3443 | with self._mock_get(b"payload"): |
| 3444 | result = helper.substitute_file_downloads( |
| 3445 | tool=self._tool(), response=self._response() |
| 3446 | ) |
| 3447 | |
| 3448 | written = Path(result["attachment"]) |
| 3449 | assert ( |
| 3450 | written.resolve() |
| 3451 | == ( |
| 3452 | tmp_path / "downloads" / "GMAIL" / "GMAIL_GET_ATTACHMENT" / "composio" |
| 3453 | ).resolve() |
| 3454 | ) |
| 3455 | assert written.read_bytes() == b"payload" |
| 3456 | |
| 3457 | def test_symlink_inside_root_cannot_be_used_to_escape(self, tmp_path): |
| 3458 | """Per-component validation cannot see a symlink; the post-resolve |
| 3459 | containment check in `secure_join` is what catches this.""" |
| 3460 | outdir = tmp_path / "files" |
| 3461 | outdir.mkdir() |
| 3462 | outside = tmp_path / "outside" |
| 3463 | outside.mkdir() |
| 3464 | (outdir / "GMAIL").symlink_to(outside, target_is_directory=True) |
| 3465 | |
| 3466 | helper = FileHelper(client=None, outdir=str(outdir)) |
| 3467 | with self._mock_get(): |
| 3468 | with pytest.raises(UnsafePathComponentError, match="outside"): |
| 3469 | helper.substitute_file_downloads( |
| 3470 | tool=self._tool(), response=self._response() |
| 3471 | ) |
| 3472 | assert list(outside.iterdir()) == [] |
| 3473 | |
| 3474 | |
| 3475 | class TestEnhanceSchemaDescriptionsEmptySchema: |
| 3476 | """Regression tests for the empty-input-parameters crash. |
| 3477 | |
| 3478 | MCP-backed toolkits can return `input_parameters: {}` for tools with no |
| 3479 | required arguments (the same convention the API uses for missing output |
| 3480 | schemas). Before the fix, `FileHelper.enhance_schema_descriptions` — |
| 3481 | called unconditionally on every fetched tool's input_parameters from |
| 3482 | `Tools._get()` — raised `KeyError: 'properties'` on that shape, |
| 3483 | crashing the public `Composio.tools.get(...)` call. |
| 3484 | |
| 3485 | The sibling `FileHelper.process_file_uploadable_schema` already guarded |
| 3486 | this with `if "properties" not in schema: return schema`. This adds the |
| 3487 | same guard to `enhance_schema_descriptions`. |
| 3488 | |
| 3489 | See https://github.com/ComposioHQ/composio/issues/3354 for the related |
| 3490 | TS-side issue. |
| 3491 | """ |
| 3492 | |
| 3493 | def test_empty_schema_does_not_raise(self, file_helper): |
| 3494 | """schema={} must not raise — it means 'no declared schema'.""" |
| 3495 | # Pre-fix: KeyError: 'properties' |
| 3496 | result = file_helper.enhance_schema_descriptions(schema={}) |
| 3497 | assert result == {} |
| 3498 | |
| 3499 | def test_schema_without_properties_returns_unchanged(self, file_helper): |
| 3500 | """A schema with metadata but no `properties` key is returned as-is. |
| 3501 | |
| 3502 | Mirrors how `process_file_uploadable_schema` treats the same input. |
| 3503 | """ |
| 3504 | schema = {"type": "object", "additionalProperties": False} |
| 3505 | result = file_helper.enhance_schema_descriptions(schema=schema) |
| 3506 | assert result == {"type": "object", "additionalProperties": False} |
| 3507 | |
| 3508 | def test_schema_with_empty_properties_dict_is_handled(self, file_helper): |
| 3509 | """`properties: {}` is valid JSON Schema (declares no fields). |
| 3510 | |
| 3511 | The loop iterates zero times and the schema is returned unchanged. |
| 3512 | """ |
| 3513 | schema = {"type": "object", "properties": {}} |
| 3514 | result = file_helper.enhance_schema_descriptions(schema=schema) |
| 3515 | assert result == {"type": "object", "properties": {}} |
| 3516 | |
| 3517 | def test_populated_schema_still_enhanced(self, file_helper): |
| 3518 | """Non-empty schemas continue to receive type-hint enhancements. |
| 3519 | |
| 3520 | Guard against regressing the enhancement behavior while fixing |
| 3521 | the empty-schema crash. |
| 3522 | """ |
| 3523 | schema = { |
| 3524 | "type": "object", |
| 3525 | "properties": { |
| 3526 | "query": {"type": "string", "description": "Search term"}, |
| 3527 | }, |
| 3528 | "required": ["query"], |
| 3529 | } |
| 3530 | result = file_helper.enhance_schema_descriptions(schema=schema) |
| 3531 | description = result["properties"]["query"]["description"] |
| 3532 | # The description must have been mutated from the input and must |
| 3533 | # mention the type and the required-marker. Assertions are token- |
| 3534 | # based to avoid coupling to the exact phrasing in |
| 3535 | # enhance_schema_descriptions. |
| 3536 | assert description != "Search term" |
| 3537 | assert "string" in description |
| 3538 | assert "required" in description |
| 3539 | |
| 3540 | def test_boolean_property_schemas_are_left_unchanged(self, file_helper): |
| 3541 | schema = { |
| 3542 | "type": "object", |
| 3543 | "properties": { |
| 3544 | "anything": True, |
| 3545 | "never": False, |
| 3546 | "query": {"type": "string"}, |
| 3547 | }, |
| 3548 | "required": ["anything", "query"], |
| 3549 | } |
| 3550 | |
| 3551 | result = file_helper.enhance_schema_descriptions(schema) |
| 3552 | |
| 3553 | assert result["properties"]["anything"] is True |
| 3554 | assert result["properties"]["never"] is False |
| 3555 | assert "string" in result["properties"]["query"]["description"] |
| 3556 | assert "required" in result["properties"]["query"]["description"] |
| 3557 | |
| 3558 | |
| 3559 | class TestFromPathSensitiveGuard: |
| 3560 | """`FileUploadable.from_path` is the single upload primitive in the Python |
| 3561 | SDK; the sensitive-path denylist must fire there before any file read or |
| 3562 | network round-trip (parity with the TS core SDK and the CLI fix for |
| 3563 | issue #3746 / GHSA-hp3h-89pf-5q58).""" |
| 3564 | |
| 3565 | def test_from_path_blocks_ssh_private_key(self, mock_client): |
| 3566 | p = Path.home() / ".ssh" / "id_rsa" |
| 3567 | with pytest.raises(SensitiveFilePathBlockedError): |
| 3568 | FileUploadable.from_path( |
| 3569 | client=mock_client, |
| 3570 | file=str(p), |
| 3571 | tool="GMAIL_SEND_EMAIL", |
| 3572 | toolkit="gmail", |
| 3573 | ) |
| 3574 | # The guard runs before the SDK contacts the API for a presigned URL. |
| 3575 | mock_client.files.create_presigned_url.assert_not_called() |
| 3576 | |
| 3577 | def test_from_path_blocks_dotenv_basename(self, mock_client): |
| 3578 | p = Path(tempfile.gettempdir()) / ".env" |
| 3579 | with pytest.raises(SensitiveFilePathBlockedError): |
| 3580 | FileUploadable.from_path( |
| 3581 | client=mock_client, |
| 3582 | file=str(p), |
| 3583 | tool="GMAIL_SEND_EMAIL", |
| 3584 | toolkit="gmail", |
| 3585 | ) |
| 3586 | mock_client.files.create_presigned_url.assert_not_called() |
| 3587 | |
| 3588 | def test_from_path_opt_out_disables_guard(self, mock_client): |
| 3589 | p = Path.home() / ".ssh" / "composio-does-not-exist-guard-test" |
| 3590 | with pytest.raises(Exception) as exc_info: |
| 3591 | FileUploadable.from_path( |
| 3592 | client=mock_client, |
| 3593 | file=str(p), |
| 3594 | tool="GMAIL_SEND_EMAIL", |
| 3595 | toolkit="gmail", |
| 3596 | sensitive_file_upload_protection=False, |
| 3597 | ) |
| 3598 | assert not isinstance(exc_info.value, SensitiveFilePathBlockedError) |
| 3599 | mock_client.files.create_presigned_url.assert_not_called() |
| 3600 | |
| 3601 | |
| 3602 | class TestResponseDerivedUrlsAreGuarded: |
| 3603 | def _download_response(self) -> MagicMock: |
| 3604 | response = MagicMock() |
| 3605 | response.status_code = 200 |
| 3606 | response.iter_content = lambda chunk_size: [b"%PDF-1.4"] |
| 3607 | response.close = MagicMock() |
| 3608 | return response |
| 3609 | |
| 3610 | def _s3_client(self, presigned_url: str) -> MagicMock: |
| 3611 | client = MagicMock() |
| 3612 | s3meta = MagicMock() |
| 3613 | s3meta.key = "s3-key" |
| 3614 | s3meta.new_presigned_url = presigned_url |
| 3615 | client.files.create_presigned_url.return_value = s3meta |
| 3616 | return client |
| 3617 | |
| 3618 | def test_download_validates_s3url(self, tmp_path): |
| 3619 | f = FileDownloadable( |
| 3620 | name="report.pdf", |
| 3621 | mimetype="application/pdf", |
| 3622 | s3url="https://s3.example.com/file", |
| 3623 | ) |
| 3624 | with patch( |
| 3625 | "composio.core.models._files.safe_request", |
| 3626 | return_value=self._download_response(), |
| 3627 | ) as mock_get: |
| 3628 | f.download(tmp_path / "out", root=tmp_path / "out") |
| 3629 | |
| 3630 | # `safe_request` is the guard: it validates each hop and connects to |
| 3631 | # the address it validated, rather than re-resolving the hostname. |
| 3632 | assert mock_get.call_args.args == ("GET", "https://s3.example.com/file") |
| 3633 | |
| 3634 | def test_download_blocked_url_never_reaches_the_network(self, tmp_path): |
| 3635 | outdir = tmp_path / "out" |
| 3636 | f = FileDownloadable( |
| 3637 | name="report.pdf", |
| 3638 | mimetype="application/pdf", |
| 3639 | s3url="http://169.254.169.254/latest/meta-data", |
| 3640 | ) |
| 3641 | with patch( |
| 3642 | "composio.utils.url_safety.assert_safe_fetch_target", |
| 3643 | side_effect=BlockedInternalUrlError("blocked"), |
| 3644 | ): |
| 3645 | with patch( |
| 3646 | "composio.utils.url_safety.requests.Session.request" |
| 3647 | ) as mock_send: |
| 3648 | with pytest.raises(BlockedInternalUrlError): |
| 3649 | f.download(outdir, root=outdir) |
| 3650 | |
| 3651 | mock_send.assert_not_called() |
| 3652 | assert not outdir.exists() |
| 3653 | |
| 3654 | def test_download_follows_a_validated_redirect(self, tmp_path): |
| 3655 | """A presigned URL may answer with a redirect (an S3 region redirect, |
| 3656 | say). The download follows it, and every hop goes through the pinned, |
| 3657 | validated request rather than a plain ``requests`` redirect.""" |
| 3658 | redirect = MagicMock() |
| 3659 | redirect.status_code = 307 |
| 3660 | redirect.headers = {"Location": "https://s3.eu.example.com/file"} |
| 3661 | f = FileDownloadable( |
| 3662 | name="report.pdf", |
| 3663 | mimetype="application/pdf", |
| 3664 | s3url="https://s3.example.com/file", |
| 3665 | ) |
| 3666 | with patch( |
| 3667 | "composio.utils.url_safety._pinned_request", |
| 3668 | side_effect=[redirect, self._download_response()], |
| 3669 | ) as mock_pinned: |
| 3670 | outfile = f.download(tmp_path / "out", root=tmp_path / "out") |
| 3671 | |
| 3672 | assert outfile.read_bytes() == b"%PDF-1.4" |
| 3673 | assert [call.args[:2] for call in mock_pinned.call_args_list] == [ |
| 3674 | ("GET", "https://s3.example.com/file"), |
| 3675 | ("GET", "https://s3.eu.example.com/file"), |
| 3676 | ] |
| 3677 | |
| 3678 | def test_upload_bytes_to_s3_goes_through_safe_request(self): |
| 3679 | client = self._s3_client("https://s3.example.com/upload") |
| 3680 | with patch("composio.core.models._files.safe_request") as mock_safe_request: |
| 3681 | mock_safe_request.return_value.status_code = 200 |
| 3682 | _upload_bytes_to_s3( |
| 3683 | client=client, |
| 3684 | filename="test.jpg", |
| 3685 | content=b"data", |
| 3686 | mimetype="image/jpeg", |
| 3687 | tool="TEST", |
| 3688 | toolkit="test", |
| 3689 | ) |
| 3690 | |
| 3691 | assert mock_safe_request.call_args.args == ( |
| 3692 | "PUT", |
| 3693 | "https://s3.example.com/upload", |
| 3694 | ) |
| 3695 | |
| 3696 | def test_upload_bytes_to_s3_blocked_url_sends_nothing(self): |
| 3697 | client = self._s3_client("http://169.254.169.254/upload") |
| 3698 | with patch( |
| 3699 | "composio.utils.url_safety.assert_safe_fetch_target", |
| 3700 | side_effect=BlockedInternalUrlError("blocked"), |
| 3701 | ): |
| 3702 | with patch( |
| 3703 | "composio.utils.url_safety.requests.Session.request" |
| 3704 | ) as mock_request: |
| 3705 | with pytest.raises(BlockedInternalUrlError): |
| 3706 | _upload_bytes_to_s3( |
| 3707 | client=client, |
| 3708 | filename="test.jpg", |
| 3709 | content=b"data", |
| 3710 | mimetype="image/jpeg", |
| 3711 | tool="TEST", |
| 3712 | toolkit="test", |
| 3713 | ) |
| 3714 | |
| 3715 | mock_request.assert_not_called() |
| 3716 | |
| 3717 | def test_file_upload_goes_through_safe_request(self, tmp_path): |
| 3718 | source = tmp_path / "report.pdf" |
| 3719 | source.write_bytes(b"%PDF-1.4") |
| 3720 | |
| 3721 | with patch("composio.core.models._files.safe_request") as mock_safe_request: |
| 3722 | mock_safe_request.return_value.status_code = 200 |
| 3723 | assert upload(url="https://s3.example.com/upload", file=source) is True |
| 3724 | |
| 3725 | assert mock_safe_request.call_args.args == ( |
| 3726 | "PUT", |
| 3727 | "https://s3.example.com/upload", |
| 3728 | ) |
| 3729 | |
| 3730 | def test_file_upload_blocked_url_sends_nothing(self, tmp_path): |
| 3731 | source = tmp_path / "report.pdf" |
| 3732 | source.write_bytes(b"%PDF-1.4") |
| 3733 | |
| 3734 | with patch( |
| 3735 | "composio.utils.url_safety.assert_safe_fetch_target", |
| 3736 | side_effect=BlockedInternalUrlError("blocked"), |
| 3737 | ): |
| 3738 | with patch( |
| 3739 | "composio.utils.url_safety.requests.Session.request" |
| 3740 | ) as mock_request: |
| 3741 | with pytest.raises(BlockedInternalUrlError): |
| 3742 | upload(url="http://127.0.0.1:9000/upload", file=source) |
| 3743 | |
| 3744 | mock_request.assert_not_called() |