clipwright/tests/test_restoring_static.py
pedro-bento 1dec0dd1af
All checks were successful
CI / backend-tests (push) Successful in 28s
CI / frontend-build (push) Successful in 20s
chore/dead-code-removal (#4)
Reviewed-on: #4
Co-authored-by: pedro-bento <mail@pbento.pt>
Co-committed-by: pedro-bento <mail@pbento.pt>
2026-10-04 22:11:11 +01:00

43 lines
1.4 KiB
Python

"""/videos serving: StaticFiles that refuses non-deliverables via ``guard``,
keeping Range support. There is no restorer hook in this fork.
"""
import os
from starlette.applications import Starlette
from starlette.testclient import TestClient
from clipwright.api.restoring_static import RestoringStaticFiles
JOB = "cff3ad6c-c1e6-48e8-9a75-be74f39779c5"
def _app(tmp_path, guard=None):
app = Starlette()
app.mount("/videos", RestoringStaticFiles(directory=str(tmp_path), guard=guard))
return TestClient(app), tmp_path
def test_existing_file_is_served(tmp_path):
client, root = _app(tmp_path)
os.makedirs(root / JOB)
(root / JOB / "clip_1.mp4").write_bytes(b"x" * 100)
r = client.get(f"/videos/{JOB}/clip_1.mp4")
assert r.status_code == 200
def test_guard_refuses_a_non_deliverable(tmp_path):
def guard(path):
return not path.endswith(".json")
client, root = _app(tmp_path, guard)
os.makedirs(root / JOB)
(root / JOB / "clip_1.mp4").write_bytes(b"x" * 100)
(root / JOB / "x_metadata.json").write_bytes(b"{}")
assert client.get(f"/videos/{JOB}/clip_1.mp4").status_code == 200
# A refused path must look exactly like a missing one.
assert client.get(f"/videos/{JOB}/x_metadata.json").status_code == 404
def test_no_guard_behaves_like_staticfiles(tmp_path):
client, _root = _app(tmp_path, None)
assert client.get(f"/videos/{JOB}/clip_1.mp4").status_code == 404