Three-level end-to-end suite, all deterministic (no retries, no sleeps, no unmocked network): - tests/e2e/api (209 tests): every route in clipwright/api/app.py + free_tools — health/config, uploads, process submit matrix + worker/webhook/ auto-retry/resume, status/source/download-all, edit/effects, transcript/EDL/ rerender, scenes/reframe, subtitle, hook, Postiz, thumbnails, free tools, and the security/path guards. - tests/e2e/pipeline (136 tests): every stage with real ffmpeg and stubbed Gemini/yt-dlp/ASR/ML — download, transcribe, scene detect, selection, layout, reframe/render, subtitles, hooks, watermark, thumbnails, plus the real CLI full pipeline via stub_env. - dashboard/e2e (19 Playwright specs): route-interception state machine covering every dashboard flow; one real-backend spec left test.fixme. - Foundation: tests/e2e/conftest.py (state isolation, no-network guard, ffmpeg media factory, disk-seeded jobs), FakePopen worker harness, child-process stub_env, architecture-consistent fixtures. - CI: unit + backend-e2e + dashboard-e2e jobs in the python+node container; ffmpeg + Playwright installed in-job; no setup-node/cache (act runner). Verified: unit 790 passed; API E2E 209 passed; pipeline E2E 136 passed; Playwright 19 passed / 1 skipped.
423 lines
17 KiB
Python
423 lines
17 KiB
Python
"""L3 E2E: the Postiz integration routes.
|
|
|
|
Everything outbound is stubbed at the ``clipwright.integrations.postiz`` seam
|
|
(``app.py`` calls the module functions through ``asyncio.to_thread``), so the
|
|
tests exercise the real route logic: credential resolution, channel selection
|
|
by id vs the legacy ``platforms`` fallback, the per-provider ``settings``
|
|
mapping, the schedule window and the error translation.
|
|
|
|
The unit suite (``tests/test_postiz.py``) already pins the helper functions;
|
|
these are the ASGI-level contracts through the shared ``client`` fixture.
|
|
"""
|
|
|
|
import functools
|
|
from datetime import datetime, timedelta, timezone
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
pytestmark = pytest.mark.e2e
|
|
|
|
POSTIZ_HEADERS = {"X-Postiz-Url": "https://postiz.example", "X-Postiz-Key": "k"}
|
|
|
|
|
|
@pytest.fixture
|
|
def postiz_stub(monkeypatch):
|
|
"""Capture every Postiz call and return scripted responses.
|
|
|
|
Mutate ``stub.integrations`` / ``stub.*_result`` / ``stub.errors`` per test.
|
|
"""
|
|
from clipwright.integrations import postiz as postiz_mod
|
|
|
|
class Stub:
|
|
def __init__(self):
|
|
self.calls = {}
|
|
self.integrations = [
|
|
{"id": "yt-1", "name": "Main", "identifier": "youtube", "disabled": False},
|
|
{"id": "yt-2", "name": "Second", "identifier": "youtube", "disabled": False},
|
|
{"id": "ig-1", "name": "IG", "identifier": "instagram-standalone",
|
|
"disabled": False},
|
|
{"id": "tt-1", "name": "TT", "identifier": "tiktok-business",
|
|
"disabled": False},
|
|
]
|
|
self.upload_result = {"id": "media-1", "path": "media/path"}
|
|
self.create_result = [{"postId": "p1", "integration": "yt-1"}]
|
|
self.posts_result = [
|
|
{"id": "sched-1", "date": "2030-01-01T00:00:00.000Z",
|
|
"integration": {"id": "yt-1"}},
|
|
]
|
|
self.delete_result = None
|
|
self.analytics_result = {"views": 5, "likes": 1}
|
|
self.analytics_post_result = {"likes": 3}
|
|
self.errors = {}
|
|
|
|
def _record(self, name, *args, **kwargs):
|
|
self.calls.setdefault(name, []).append((args, kwargs))
|
|
if name in self.errors:
|
|
raise self.errors[name]
|
|
return {
|
|
"list_integrations": self.integrations,
|
|
"upload_media": self.upload_result,
|
|
"create_post": self.create_result,
|
|
"list_posts": self.posts_result,
|
|
"delete_post": self.delete_result,
|
|
"analytics": self.analytics_result,
|
|
"analytics_post": self.analytics_post_result,
|
|
}[name]
|
|
|
|
stub = Stub()
|
|
for name in ("list_integrations", "upload_media", "create_post",
|
|
"list_posts", "delete_post", "analytics", "analytics_post"):
|
|
monkeypatch.setattr(postiz_mod, name, functools.partial(stub._record, name))
|
|
return stub
|
|
|
|
|
|
def _payload(stub):
|
|
"""The payload handed to ``create_post`` (base, key, payload)."""
|
|
return stub.calls["create_post"][0][0][2]
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# POST /api/postiz/post
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
async def test_post_success_by_integration_id(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-post", base="pw", clips=1)
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0,
|
|
"integration_ids": ["ig-1", "tt-1"],
|
|
"title": "My Title", "description": "Watch this #fun #clips"},
|
|
)
|
|
assert resp.status_code == 200, resp.text
|
|
body = resp.json()
|
|
assert body["success"] is True
|
|
assert body["results"] == postiz_stub.create_result
|
|
assert [p["platform"] for p in body["posted"]] == ["instagram", "tiktok"]
|
|
assert [p["name"] for p in body["posted"]] == ["IG", "TT"]
|
|
|
|
payload = _payload(postiz_stub)
|
|
assert payload["type"] == "now"
|
|
assert payload["shortLink"] is False
|
|
posts = payload["posts"]
|
|
assert [p["integration"]["id"] for p in posts] == ["ig-1", "tt-1"]
|
|
# __type echoes the real identifier, not the mapped platform.
|
|
assert [p["settings"]["__type"] for p in posts] == \
|
|
["instagram-standalone", "tiktok-business"]
|
|
assert posts[0]["settings"] == {"__type": "instagram-standalone", "post_type": "post"}
|
|
assert posts[1]["settings"]["content_posting_method"] == "DIRECT_POST"
|
|
assert posts[0]["value"][0]["content"] == "Watch this #fun #clips"
|
|
assert posts[0]["value"][0]["image"][0] == {"id": "media-1", "path": "media/path"}
|
|
# The local clip was re-uploaded to Postiz first.
|
|
assert postiz_stub.calls["upload_media"][0][0][2].endswith("pw_clip_1.mp4")
|
|
|
|
|
|
async def test_post_legacy_platforms_use_the_identifier_map(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-legacy", base="pw")
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0,
|
|
"platforms": ["instagram", "tiktok"]},
|
|
)
|
|
assert resp.status_code == 200, resp.text
|
|
posts = _payload(postiz_stub)["posts"]
|
|
assert [p["integration"]["id"] for p in posts] == ["ig-1", "tt-1"]
|
|
assert [p["settings"]["__type"] for p in posts] == \
|
|
["instagram-standalone", "tiktok-business"]
|
|
assert [e["platform"] for e in resp.json()["posted"]] == ["instagram", "tiktok"]
|
|
|
|
|
|
async def test_post_future_schedule_sets_schedule_type(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-sched", base="pw")
|
|
future = datetime.now(timezone.utc) + timedelta(days=2)
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0, "integration_ids": ["yt-1"],
|
|
"scheduled_date": future.isoformat()},
|
|
)
|
|
assert resp.status_code == 200, resp.text
|
|
payload = _payload(postiz_stub)
|
|
assert payload["type"] == "schedule"
|
|
assert payload["date"] == future.astimezone(timezone.utc).strftime(
|
|
"%Y-%m-%dT%H:%M:%S.000Z")
|
|
|
|
|
|
async def test_post_schedule_accepts_an_iso_offset(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-tz", base="pw")
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0, "integration_ids": ["yt-1"],
|
|
"scheduled_date": "2030-06-01T12:00:00+02:00"},
|
|
)
|
|
assert resp.status_code == 200, resp.text
|
|
# The offset is normalised to UTC.
|
|
assert _payload(postiz_stub)["date"] == "2030-06-01T10:00:00.000Z"
|
|
|
|
|
|
async def test_post_past_schedule_is_400_before_upload(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-past", base="pw")
|
|
past = (datetime.now(timezone.utc) - timedelta(hours=1)).isoformat()
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0, "integration_ids": ["yt-1"],
|
|
"scheduled_date": past},
|
|
)
|
|
assert resp.status_code == 400
|
|
assert "past" in resp.json()["detail"].lower()
|
|
assert "upload_media" not in postiz_stub.calls
|
|
assert "create_post" not in postiz_stub.calls
|
|
|
|
|
|
async def test_post_unparseable_schedule_is_400(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-bad-date", base="pw")
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0, "integration_ids": ["yt-1"],
|
|
"scheduled_date": "tomorrow"},
|
|
)
|
|
assert resp.status_code == 400
|
|
|
|
|
|
async def test_post_unknown_integration_id_is_400(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-unknown", base="pw")
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0, "integration_ids": ["nope"]},
|
|
)
|
|
assert resp.status_code == 400
|
|
assert "nope" in resp.json()["detail"]
|
|
assert "upload_media" not in postiz_stub.calls
|
|
|
|
|
|
async def test_post_disabled_integration_id_is_400(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-disabled", base="pw")
|
|
postiz_stub.integrations = [
|
|
{"id": "ig-1", "name": "IG", "identifier": "instagram-standalone",
|
|
"disabled": True},
|
|
]
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0, "integration_ids": ["ig-1"]},
|
|
)
|
|
assert resp.status_code == 400
|
|
assert "Disabled" in resp.json()["detail"]
|
|
assert "upload_media" not in postiz_stub.calls
|
|
|
|
|
|
async def test_post_not_connected_platform_is_400(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-notconn", base="pw")
|
|
postiz_stub.integrations = [
|
|
{"id": "yt-1", "name": "Main", "identifier": "youtube", "disabled": False},
|
|
]
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0, "platforms": ["instagram"]},
|
|
)
|
|
assert resp.status_code == 400
|
|
assert "Not connected" in resp.json()["detail"]
|
|
assert "upload_media" not in postiz_stub.calls
|
|
|
|
|
|
async def test_post_missing_credentials_is_400(client, seeded_job, postiz_stub,
|
|
monkeypatch):
|
|
monkeypatch.delenv("POSTIZ_URL", raising=False)
|
|
monkeypatch.delenv("POSTIZ_API_KEY", raising=False)
|
|
job_id = seeded_job(job_id="job-creds", base="pw")
|
|
body = {"job_id": job_id, "clip_index": 0, "platforms": ["youtube"]}
|
|
|
|
r = await client.post("/api/postiz/post", headers={"X-Postiz-Key": "k"}, json=body)
|
|
assert r.status_code == 400 and "URL" in r.json()["detail"]
|
|
|
|
r = await client.post("/api/postiz/post",
|
|
headers={"X-Postiz-Url": "https://p.example"}, json=body)
|
|
assert r.status_code == 400 and "key" in r.json()["detail"].lower()
|
|
|
|
|
|
async def test_post_no_channels_is_400(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-nochan", base="pw")
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0},
|
|
)
|
|
assert resp.status_code == 400
|
|
assert "No channels" in resp.json()["detail"]
|
|
assert "upload_media" not in postiz_stub.calls
|
|
|
|
|
|
async def test_post_unknown_job_is_404(client, postiz_stub):
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": "nobody-home", "clip_index": 0, "integration_ids": ["yt-1"]},
|
|
)
|
|
assert resp.status_code == 404
|
|
assert resp.json()["detail"] == "Job not found"
|
|
assert postiz_stub.calls == {}
|
|
|
|
|
|
async def test_post_missing_clip_file_is_404(client, app_state, postiz_stub):
|
|
app_state.app.jobs["job-nofile"] = {
|
|
"result": {"clips": [{"video_url": "/videos/job-nofile/missing.mp4"}]},
|
|
}
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": "job-nofile", "clip_index": 0, "integration_ids": ["yt-1"]},
|
|
)
|
|
assert resp.status_code == 404
|
|
assert "not found" in resp.json()["detail"].lower()
|
|
assert "upload_media" not in postiz_stub.calls
|
|
|
|
|
|
async def test_post_clip_index_out_of_range_is_400(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-index", base="pw")
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 9, "integration_ids": ["yt-1"]},
|
|
)
|
|
assert resp.status_code == 400
|
|
assert "range" in resp.json()["detail"].lower()
|
|
|
|
|
|
async def test_post_upload_failure_is_502(client, seeded_job, postiz_stub):
|
|
job_id = seeded_job(job_id="job-upfail", base="pw")
|
|
postiz_stub.upload_result = {}
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0, "integration_ids": ["yt-1"]},
|
|
)
|
|
assert resp.status_code == 502
|
|
assert "upload failed" in resp.json()["detail"].lower()
|
|
assert "create_post" not in postiz_stub.calls
|
|
|
|
|
|
async def test_post_postiz_error_is_translated(client, seeded_job, postiz_stub):
|
|
from clipwright.integrations import postiz
|
|
|
|
job_id = seeded_job(job_id="job-err", base="pw")
|
|
postiz_stub.errors["create_post"] = postiz.PostizError(
|
|
429, "Postiz API Error: slow down")
|
|
resp = await client.post(
|
|
"/api/postiz/post",
|
|
headers=POSTIZ_HEADERS,
|
|
json={"job_id": job_id, "clip_index": 0, "integration_ids": ["yt-1"]},
|
|
)
|
|
assert resp.status_code == 429
|
|
assert resp.json()["detail"] == "Postiz API Error: slow down"
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# Other Postiz routes
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
async def test_integrations_route_normalises_disabled(client, postiz_stub):
|
|
postiz_stub.integrations = [
|
|
{"id": "a", "identifier": "youtube", "disabled": 1},
|
|
{"id": "b", "identifier": "youtube", "disabled": 0},
|
|
{"id": "c", "identifier": "youtube"},
|
|
]
|
|
resp = await client.get("/api/postiz/integrations", headers=POSTIZ_HEADERS)
|
|
assert resp.status_code == 200
|
|
integrations = resp.json()["integrations"]
|
|
assert [i["disabled"] for i in integrations] == [True, False, False]
|
|
|
|
|
|
async def test_integrations_route_translates_postiz_error(client, postiz_stub):
|
|
from clipwright.integrations import postiz
|
|
|
|
postiz_stub.errors["list_integrations"] = postiz.PostizError(
|
|
401, "Postiz API Error: bad key")
|
|
resp = await client.get("/api/postiz/integrations", headers=POSTIZ_HEADERS)
|
|
assert resp.status_code == 401
|
|
assert "bad key" in resp.json()["detail"]
|
|
|
|
|
|
async def test_scheduled_route_backfills_and_widens_window(client, postiz_stub):
|
|
resp = await client.get("/api/postiz/scheduled", headers=POSTIZ_HEADERS,
|
|
params={"days": 30})
|
|
assert resp.status_code == 200
|
|
posts = resp.json()["scheduled_posts"]
|
|
# Postiz calls it ``date``; the consumer reads ``scheduled_date``.
|
|
assert posts[0]["scheduled_date"] == "2030-01-01T00:00:00.000Z"
|
|
assert posts[0]["date"] == "2030-01-01T00:00:00.000Z"
|
|
|
|
args = postiz_stub.calls["list_posts"][0][0]
|
|
start = datetime.fromisoformat(args[2].replace("Z", "+00:00"))
|
|
end = datetime.fromisoformat(args[3].replace("Z", "+00:00"))
|
|
# The default window is widened to at least POSTIZ_SCHEDULED_MIN_DAYS (90).
|
|
assert (end - start) >= timedelta(days=89)
|
|
|
|
|
|
async def test_scheduled_route_forwards_explicit_dates(client, postiz_stub):
|
|
resp = await client.get(
|
|
"/api/postiz/scheduled", headers=POSTIZ_HEADERS,
|
|
params={"start_date": "2030-01-01T00:00:00.000Z",
|
|
"end_date": "2030-02-01T00:00:00.000Z"})
|
|
assert resp.status_code == 200
|
|
args = postiz_stub.calls["list_posts"][0][0]
|
|
assert args[2] == "2030-01-01T00:00:00.000Z"
|
|
assert args[3] == "2030-02-01T00:00:00.000Z"
|
|
|
|
|
|
async def test_scheduled_route_translates_postiz_error(client, postiz_stub):
|
|
from clipwright.integrations import postiz
|
|
|
|
postiz_stub.errors["list_posts"] = postiz.PostizError(500, "Postiz API Error: boom")
|
|
resp = await client.get("/api/postiz/scheduled", headers=POSTIZ_HEADERS)
|
|
assert resp.status_code == 500
|
|
assert "boom" in resp.json()["detail"]
|
|
|
|
|
|
async def test_cancel_scheduled_deletes_and_reports(client, postiz_stub):
|
|
resp = await client.delete("/api/postiz/scheduled/sched-9",
|
|
headers=POSTIZ_HEADERS)
|
|
assert resp.status_code == 200
|
|
assert resp.json() == {"success": True, "id": "sched-9"}
|
|
assert postiz_stub.calls["delete_post"][0][0][2] == "sched-9"
|
|
|
|
|
|
async def test_analytics_route_forwards_channel_and_days(client, postiz_stub):
|
|
resp = await client.get("/api/postiz/analytics", headers=POSTIZ_HEADERS,
|
|
params={"integration": "yt-1", "days": 7})
|
|
assert resp.status_code == 200
|
|
assert resp.json() == postiz_stub.analytics_result
|
|
args = postiz_stub.calls["analytics"][0][0]
|
|
assert args[2] == "yt-1" and args[3] == 7
|
|
|
|
|
|
async def test_analytics_posts_route_forwards_post_id(client, postiz_stub):
|
|
resp = await client.get("/api/postiz/analytics/posts", headers=POSTIZ_HEADERS,
|
|
params={"post_id": "post-9", "days": 3})
|
|
assert resp.status_code == 200
|
|
assert resp.json() == postiz_stub.analytics_post_result
|
|
args = postiz_stub.calls["analytics_post"][0][0]
|
|
assert args[2] == "post-9" and args[3] == 3
|
|
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
# The identifier -> platform map that drives settings.__type
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
def test_settings_echo_the_real_identifier(app_state):
|
|
app = app_state.app
|
|
assert app._identifier_platform("instagram-standalone") == "instagram"
|
|
assert app._identifier_platform("tiktok-business") == "tiktok"
|
|
assert app._identifier_platform("youtube") == "youtube"
|
|
|
|
ig = app._postiz_settings("instagram-standalone", "T", "caption", [])
|
|
assert ig == {"__type": "instagram-standalone", "post_type": "post"}
|
|
tt = app._postiz_settings("tiktok-business", "T", "caption", [])
|
|
assert tt["__type"] == "tiktok-business"
|
|
assert tt["content_posting_method"] == "DIRECT_POST"
|