feat: validate and unblock media downloads
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
from pathlib import Path
|
||||
|
||||
from rf4_research.media_assets import extract_media_candidates, merge_manifest, store_asset
|
||||
import io
|
||||
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from rf4_research.media_assets import extract_media_candidates, inspect_image, merge_manifest, store_asset
|
||||
|
||||
|
||||
def test_extracts_and_classifies_unique_https_media() -> None:
|
||||
@@ -25,6 +30,18 @@ def test_manifest_merges_and_binary_store_is_content_addressed(tmp_path: Path) -
|
||||
first = merge_manifest(tmp_path / "manifest.json", [item])
|
||||
second = merge_manifest(tmp_path / "manifest.json", [item])
|
||||
assert len(first["assets"]) == len(second["assets"]) == 1
|
||||
digest, relative = store_asset(tmp_path, b"image", content_type="image/png", source_url=item.asset_url)
|
||||
image = io.BytesIO()
|
||||
Image.new("RGB", (3, 2), "green").save(image, format="PNG")
|
||||
digest, relative, width, height, mime = store_asset(tmp_path, image.getvalue(), content_type="image/png", source_url=item.asset_url)
|
||||
assert len(digest) == 64
|
||||
assert (tmp_path / relative).read_bytes() == b"image"
|
||||
assert (tmp_path / relative).read_bytes() == image.getvalue()
|
||||
assert (width, height, mime) == (3, 2, "image/png")
|
||||
|
||||
|
||||
def test_image_inspection_rejects_invalid_body_and_mime_mismatch(tmp_path: Path) -> None:
|
||||
with pytest.raises(ValueError, match="valid raster"):
|
||||
inspect_image(b"not an image")
|
||||
image = io.BytesIO()
|
||||
Image.new("RGB", (1, 1)).save(image, format="PNG")
|
||||
with pytest.raises(ValueError, match="MIME mismatch"):
|
||||
store_asset(tmp_path, image.getvalue(), content_type="image/jpeg", source_url="https://example.test/a.jpg")
|
||||
|
||||
Reference in New Issue
Block a user