clipwright/tests/test_source_access.py
pedro-bento edcfeaf9d5 refactor: remove cloud/ billing mode and flatten to self-hosted-only
- Delete the entire cloud/ package (Stripe billing, auth/OAuth, metering, API keys,
  account erasure, lifecycle/digest emails, R2 archive, autopilot, MCP OAuth,
  proxy ledger, managed keys) and all BILLING_ENABLED branches in app.py
- Keep self-host behavior via no-op shims; /api/config now returns
  {youtubeUrlEnabled, jobRetentionSeconds, localLlm}
- Remove cloud tests (31 files), alembic/, log_view.py, requirements-billing.txt,
  docker-compose.cloud.yml; update CLI to Postiz + /api/config; clean docs/skills
- Flatten dashboard: remove auth/billing/pricing/history chrome, inert config-only AuthContext
- Fix residual test drift; lint + build + compile all green
2026-10-04 17:02:55 +01:00

151 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("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,
"watermark": False,
}
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"