clipwright/tests/test_source_access.py
pedro-bento 91a5465f81
All checks were successful
CI / unit (push) Successful in 45s
CI / backend-e2e (push) Successful in 1m16s
CI / dashboard-e2e (push) Successful in 2m29s
chore: drop watermark feature, CLI, docs, design.md; consolidate requirements (#6)
- Remove the watermark feature end to end (watermarked.py, apply_watermark/
  mark_delivery/watermark_filter, WATERMARK env, assets/) and its tests; self-host
  delivery is now unconditionally clean.
- Delete the unused cli/ package and .github/workflows/publish-cli.yml.
- Delete docs/ (compliance) and design.md; drop the dangling references in the
  legal page and the design-token/ui comments.
- Consolidate requirements: requirements.txt (runtime) + requirements-test.txt
  (unit + E2E); remove requirements-e2e.txt and requirements-ml-e2e.txt and
  point both CI jobs at the single test file. Add libgl1 to the unit job
  (scenedetect -> opencv-python links libGL).

Verified in the CI container: unit 701 passed; API E2E 209; pipeline E2E 127;
Playwright 19 passed / 1 skipped.

Reviewed-on: #6
Co-authored-by: pedro-bento <mail@pbento.pt>
Co-committed-by: pedro-bento <mail@pbento.pt>
2026-10-05 11:11:37 +01:00

150 lines
6.2 KiB
Python

"""Access control on the one endpoint that serves the untouched original.
/api/source streams the file we downloaded, not a derived clip. Self-host has
no owner to check, so the open path must keep working exactly as before. That
regression is most of what these tests are for.
"""
import asyncio
import json
import time
import httpx
import pytest
app_module = pytest.importorskip("clipwright.api.app")
JOB_ID = "source-access-test-job"
def _get(path):
async def _do():
transport = httpx.ASGITransport(app=app_module.app)
async with httpx.AsyncClient(transport=transport,
base_url="http://testserver") as client:
return await client.get(path)
return asyncio.run(_do())
@pytest.fixture()
def job(tmp_path, monkeypatch):
"""A URL-style job: the retained download sits in the job dir and is named
by ``source_video`` in the metadata, which is what _locate_source reads."""
out_root = tmp_path / "output"
job_dir = out_root / JOB_ID
job_dir.mkdir(parents=True)
(tmp_path / "uploads").mkdir()
monkeypatch.setattr(app_module, "OUTPUT_DIR", str(out_root))
monkeypatch.setattr(app_module, "UPLOAD_DIR", str(tmp_path / "uploads"))
(job_dir / "source.mp4").write_bytes(b"not really a video")
# One clip and a word timeline, so the EDL endpoint has something to answer
# with: the invariant test below is worthless if it skips.
(job_dir / "x_metadata.json").write_text(json.dumps({
"source_video": "source.mp4",
"transcript": {
"language": "en",
"segments": [{
"start": 0.0, "end": 30.0, "text": "hello world",
"words": [{"word": "hello", "start": 10.0, "end": 10.4},
{"word": "world", "start": 11.0, "end": 11.4}],
}],
},
"shorts": [{
"start": 10.0, "end": 20.0, "title": "clip", "score": 90,
"video_url": "/videos/x/clip_1.mp4",
}],
}))
# Also in memory: the EDL endpoint answers from the live job record, so a
# fixture that only writes disk makes this file order-dependent — the test
# below passed only because an earlier one had triggered a disk recovery.
app_module.jobs[JOB_ID] = {
"status": "completed",
"logs": [],
"result": {"clips": [{
"start": 10.0, "end": 20.0, "title": "clip", "score": 90,
"video_url": "/videos/x/clip_1.mp4",
}]},
"user_id": None,
}
try:
yield job_dir
finally:
app_module.jobs.pop(JOB_ID, None)
class TestSelfHostStaysOpen:
def test_source_is_served_without_a_token(self, job):
r = _get(f"/api/source/{JOB_ID}")
assert r.status_code == 200
assert r.content == b"not really a video"
def test_source_url_hands_back_the_plain_path(self, job):
r = _get(f"/api/source-url/{JOB_ID}")
assert r.status_code == 200
# Self-host: no capability URL to mint, just the plain path.
assert r.json()["url"] == f"/api/source/{JOB_ID}"
def test_missing_source_is_a_404_not_a_500(self, tmp_path, monkeypatch):
monkeypatch.setattr(app_module, "OUTPUT_DIR", str(tmp_path))
monkeypatch.setattr(app_module, "UPLOAD_DIR", str(tmp_path))
assert _get("/api/source/nobody-home").status_code == 404
class TestRetainedSourceSweep:
"""SOURCE_RETENTION_SECONDS drops the retained download ahead of the job."""
def test_is_a_no_op_at_the_default(self, job, monkeypatch):
# Default equals the job clock: the source lives and dies with its job,
# exactly as before the knob existed.
monkeypatch.setattr(app_module, "SOURCE_RETENTION_SECONDS",
app_module.JOB_RETENTION_SECONDS)
assert list(app_module._sweep_retained_sources()) == []
assert (job / "source.mp4").exists()
def test_drops_the_download_once_it_is_older_than_the_knob(self, job, monkeypatch):
monkeypatch.setattr(app_module, "SOURCE_RETENTION_SECONDS", 60)
monkeypatch.setattr(app_module, "JOB_RETENTION_SECONDS", 3600)
assert list(app_module._sweep_retained_sources(now=time.time() + 120)) == [JOB_ID]
assert not (job / "source.mp4").exists()
# The clips and the metadata are the deliverable and must survive.
assert (job / "x_metadata.json").exists()
def test_keeps_a_download_that_is_still_young(self, job, monkeypatch):
monkeypatch.setattr(app_module, "SOURCE_RETENTION_SECONDS", 3600)
monkeypatch.setattr(app_module, "JOB_RETENTION_SECONDS", 86400)
assert list(app_module._sweep_retained_sources()) == []
assert (job / "source.mp4").exists()
def test_survives_a_job_dir_with_no_metadata(self, tmp_path, monkeypatch):
monkeypatch.setattr(app_module, "OUTPUT_DIR", str(tmp_path))
monkeypatch.setattr(app_module, "SOURCE_RETENTION_SECONDS", 1)
monkeypatch.setattr(app_module, "JOB_RETENTION_SECONDS", 3600)
(tmp_path / "half-written-job").mkdir()
(tmp_path / "loose-file.txt").write_text("not a job dir")
assert list(app_module._sweep_retained_sources(now=time.time() + 120)) == []
def test_every_source_url_the_browser_gets_comes_from_one_place(job):
"""The clip editor renders edl.source.url straight into a <video src>.
Self-host serves every job's source from the one open path, so the EDL must
hand back that same path and not a derived or per-job spelling.
"""
r = _get(f"/api/clip/{JOB_ID}/0/edl")
assert r.status_code == 200, r.text
assert r.json()["source"]["available"] is True
assert r.json()["source"]["url"] == f"/api/source/{JOB_ID}"
class TestOpenSourceUrl:
"""The source-url endpoint answers for any job in self-host."""
def test_self_host_still_answers_for_an_unknown_job(self, tmp_path, monkeypatch):
# No owner and no secret: the preview has to keep working, so an
# unknown id is not something to refuse.
monkeypatch.setattr(app_module, "OUTPUT_DIR", str(tmp_path))
monkeypatch.setattr(app_module, "UPLOAD_DIR", str(tmp_path))
r = _get("/api/source-url/nobody-home")
assert r.status_code == 200
assert r.json()["url"] == "/api/source/nobody-home"