Skip to content

Commit 10158bc

Browse files
committed
Add optional end-to-end upload test harness
1 parent 072adad commit 10158bc

File tree

6 files changed

+106
-1
lines changed

6 files changed

+106
-1
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.jpg binary

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ Signature parity tests use `npx transloadit smart_sig` under the hood, matching
6464

6565
Pass `--python 3.12` (or set `PYTHON_VERSIONS`) to restrict the matrix, or append a custom command after `--`, for example `scripts/test-in-docker.sh -- pytest -k smartcdn`.
6666

67+
To exercise the optional end-to-end upload against a real Transloadit account, provide `TRANSLOADIT_KEY` and `TRANSLOADIT_SECRET` (via environment variables or `.env`) and set `PYTHON_SDK_E2E=1`:
68+
69+
```bash
70+
PYTHON_SDK_E2E=1 scripts/test-in-docker.sh --python 3.12 -- pytest tests/test_e2e_upload.py
71+
```
72+
73+
The test uploads `chameleon.jpg`, resizes it, and asserts on the live assembly results. It respects `TRANSLOADIT_HOST` and `TRANSLOADIT_REGION` overrides when present.
74+
6775
If you have a global installation of `poetry`, you can run the tests with:
6876

6977
```bash

chameleon.jpg

8.88 MB
Loading

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ build-backend = "poetry.core.masonry.api"
4848
[tool.pytest.ini_options]
4949
addopts = "--cov=transloadit --cov-report=term-missing"
5050
testpaths = ["tests"]
51+
markers = [
52+
"e2e: marks tests that hit the live Transloadit API"
53+
]
5154

5255
[tool.coverage.run]
5356
source = ["transloadit"]

scripts/test-in-docker.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ run_for_version() {
158158
docker_args+=(--env-file "$PWD/.env")
159159
fi
160160

161-
local -a passthrough_envs=(TRANSLOADIT_KEY TRANSLOADIT_SECRET TRANSLOADIT_HOST TRANSLOADIT_REGION TRANSLOADIT_TEMPLATE_ID)
161+
local -a passthrough_envs=(TRANSLOADIT_KEY TRANSLOADIT_SECRET TRANSLOADIT_HOST TRANSLOADIT_REGION TRANSLOADIT_TEMPLATE_ID PYTHON_SDK_E2E)
162162
for var in "${passthrough_envs[@]}"; do
163163
if [[ -n "${!var:-}" ]]; then
164164
docker_args+=(-e "$var=${!var}")

tests/test_e2e_upload.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)