|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from transloadit.client import Transloadit |
| 7 | + |
| 8 | + |
| 9 | +def _is_enabled(): |
| 10 | + flag = os.getenv("PYTHON_SDK_E2E", "") |
| 11 | + return flag.lower() in {"1", "true", "yes", "on"} |
| 12 | + |
| 13 | + |
| 14 | +pytestmark = [ |
| 15 | + pytest.mark.e2e, |
| 16 | + pytest.mark.skipif(not _is_enabled(), reason="Set PYTHON_SDK_E2E=1 to run E2E tests"), |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +def _build_service(): |
| 21 | + service = os.getenv("TRANSLOADIT_HOST") |
| 22 | + if service: |
| 23 | + return service |
| 24 | + |
| 25 | + region = os.getenv("TRANSLOADIT_REGION") |
| 26 | + if region: |
| 27 | + return f"https://api2-{region}.transloadit.com" |
| 28 | + |
| 29 | + return None |
| 30 | + |
| 31 | + |
| 32 | +def test_e2e_image_resize(tmp_path): |
| 33 | + key = os.getenv("TRANSLOADIT_KEY") |
| 34 | + secret = os.getenv("TRANSLOADIT_SECRET") |
| 35 | + |
| 36 | + if not key or not secret: |
| 37 | + pytest.skip("TRANSLOADIT_KEY and TRANSLOADIT_SECRET must be set to run E2E tests") |
| 38 | + |
| 39 | + fixture_path = Path(__file__).resolve().parents[1] / "chameleon.jpg" |
| 40 | + if not fixture_path.exists(): |
| 41 | + pytest.skip("chameleon.jpg fixture missing; run from repository root") |
| 42 | + |
| 43 | + service = _build_service() |
| 44 | + client_kwargs = {"service": service} if service else {} |
| 45 | + client = Transloadit(key, secret, **client_kwargs) |
| 46 | + |
| 47 | + assembly = client.new_assembly() |
| 48 | + |
| 49 | + with fixture_path.open("rb") as upload: |
| 50 | + assembly.add_file(upload) |
| 51 | + assembly.add_step( |
| 52 | + "resize", |
| 53 | + "/image/resize", |
| 54 | + { |
| 55 | + "use": ":original", |
| 56 | + "width": 128, |
| 57 | + "height": 128, |
| 58 | + "resize_strategy": "fit", |
| 59 | + "format": "png", |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + response = assembly.create(wait=True, resumable=False) |
| 64 | + |
| 65 | + data = response.data |
| 66 | + assert data.get("ok") == "ASSEMBLY_COMPLETED", data |
| 67 | + |
| 68 | + uploads = data.get("uploads") or [] |
| 69 | + assert uploads, f"Expected uploads in assembly response: {data}" |
| 70 | + upload_info = uploads[0] |
| 71 | + basename = upload_info.get("basename") |
| 72 | + if basename: |
| 73 | + assert basename == fixture_path.stem |
| 74 | + filename = upload_info.get("name") |
| 75 | + if filename: |
| 76 | + assert filename == fixture_path.name |
| 77 | + |
| 78 | + results = (data.get("results") or {}).get("resize") or [] |
| 79 | + assert results, f"Expected resize results in assembly response: {data}" |
| 80 | + first_result = results[0] |
| 81 | + |
| 82 | + ssl_url = first_result.get("ssl_url") |
| 83 | + assert ssl_url and ssl_url.startswith("https://"), f"Unexpected ssl_url: {ssl_url}" |
| 84 | + |
| 85 | + meta = first_result.get("meta") or {} |
| 86 | + width = meta.get("width") |
| 87 | + height = meta.get("height") |
| 88 | + if width is not None: |
| 89 | + width = int(width) |
| 90 | + if height is not None: |
| 91 | + height = int(height) |
| 92 | + assert width and height, f"Missing dimensions in result metadata: {meta}" |
| 93 | + assert 0 < width <= 128 and 0 < height <= 128 |
0 commit comments