fix: eingefügte Bilder beim ersten Speichern dauerhaft ablegen

This commit is contained in:
2026-07-23 16:36:07 +02:00
parent 8d6b6a46db
commit b632a5bb04
6 changed files with 141 additions and 4 deletions
+48
View File
@@ -0,0 +1,48 @@
import base64
from io import BytesIO
import importlib.util
from pathlib import Path
import sys
from PIL import Image
spec = importlib.util.spec_from_file_location(
"documentation_embedded_images",
Path(__file__).parents[1] / "netbox_documentation" / "embedded_images.py",
)
embedded = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = embedded
spec.loader.exec_module(embedded)
def image_data_url():
stream = BytesIO()
Image.new("RGB", (2, 2), "red").save(stream, format="PNG")
return "data:image/png;base64," + base64.b64encode(stream.getvalue()).decode()
def test_embedded_image_is_replaced_with_stored_url():
html = f'<p><img alt="Test" src="{image_data_url()}"></p>'
stored = []
def store(mime, content):
stored.append((mime, content))
return "/media/documentation/bild.png"
result = embedded.store_embedded_images(html, 1024 * 1024, store)
assert 'src="/media/documentation/bild.png"' in result
assert "data:image/" not in result
assert stored[0][0] == "image/png"
assert stored[0][1]
def test_embedded_images_respect_total_size_limit():
html = f'<img src="{image_data_url()}">'
try:
embedded.validate_embedded_images(html, 1)
except embedded.EmbeddedImageError as exc:
assert "Gesamtgröße" in str(exc)
else:
raise AssertionError("Größenlimit wurde nicht angewendet")