|
| 1 | +"""Every image ``src`` the builder reads goes through ``resolve_image_path``. |
| 2 | +
|
| 3 | +``resolve_image_path`` owns the rules for turning an image reference into a |
| 4 | +file: ``icons:``/``assets:`` lookups, ``~`` expansion, paths relative to the |
| 5 | +deck directory, and refusing ``..`` segments. These tests pin that the image |
| 6 | +element, injected group/shape images and video posters all follow it. |
| 7 | +""" |
| 8 | + |
| 9 | +import io |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import pytest |
| 13 | +from lxml import etree |
| 14 | +from PIL import Image |
| 15 | + |
| 16 | +from sdpm.utils.image import resolve_image_path |
| 17 | + |
| 18 | +TEMPLATE = Path(__file__).parent.parent / "sdpm" / "templates" / "blank-light.pptx" |
| 19 | + |
| 20 | + |
| 21 | +def _png(path: Path) -> Path: |
| 22 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 23 | + buf = io.BytesIO() |
| 24 | + Image.new("RGB", (4, 4), "red").save(buf, format="PNG") |
| 25 | + path.write_bytes(buf.getvalue()) |
| 26 | + return path |
| 27 | + |
| 28 | + |
| 29 | +def _builder(base_dir: Path): |
| 30 | + from sdpm.engine.builder import PPTXBuilder |
| 31 | + |
| 32 | + return PPTXBuilder( |
| 33 | + str(TEMPLATE), |
| 34 | + fonts={"fullwidth": "Meiryo", "halfwidth": "Arial"}, |
| 35 | + default_text_color="#000000", |
| 36 | + base_dir=base_dir, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def _pictures(builder) -> list: |
| 41 | + slide = builder.prs.slides[-1] |
| 42 | + return [s for s in slide.shapes if s.shape_type == 13] # MSO_SHAPE_TYPE.PICTURE |
| 43 | + |
| 44 | + |
| 45 | +def _add_image_slide(builder, src: str) -> None: |
| 46 | + builder.add_slide({"layout": "Blank", "elements": [ |
| 47 | + {"type": "image", "src": src, "x": 0, "y": 0, "width": 100, "height": 100}, |
| 48 | + ]}) |
| 49 | + |
| 50 | + |
| 51 | +def test_resolve_image_path_resolves_relative_to_base_dir(tmp_path): |
| 52 | + img = _png(tmp_path / "deck" / "images" / "a.png") |
| 53 | + assert resolve_image_path("images/a.png", base_dir=tmp_path / "deck") == img.resolve() |
| 54 | + |
| 55 | + |
| 56 | +def test_resolve_image_path_keeps_absolute_paths(tmp_path): |
| 57 | + img = _png(tmp_path / "a.png") |
| 58 | + assert resolve_image_path(str(img), base_dir=tmp_path / "elsewhere") == img.resolve() |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize("src", ["images/../a.png", "assets:aws/../a", "icons:../a"]) |
| 62 | +def test_resolve_image_path_rejects_parent_segments(tmp_path, src): |
| 63 | + _png(tmp_path / "a.png") |
| 64 | + with pytest.raises(ValueError): |
| 65 | + resolve_image_path(src, base_dir=tmp_path) |
| 66 | + |
| 67 | + |
| 68 | +def test_add_image_embeds_relative_src(tmp_path): |
| 69 | + _png(tmp_path / "images" / "a.png") |
| 70 | + b = _builder(tmp_path) |
| 71 | + _add_image_slide(b, "images/a.png") |
| 72 | + assert len(_pictures(b)) == 1 |
| 73 | + |
| 74 | + |
| 75 | +def test_add_image_uses_resolve_image_path_rules(tmp_path): |
| 76 | + deck = tmp_path / "deck" |
| 77 | + (deck / "images").mkdir(parents=True) |
| 78 | + _png(tmp_path / "a.png") |
| 79 | + b = _builder(deck) |
| 80 | + _add_image_slide(b, "images/../../a.png") |
| 81 | + assert _pictures(b) == [] |
| 82 | + |
| 83 | + |
| 84 | +def test_injected_images_use_resolve_image_path_rules(tmp_path): |
| 85 | + deck = tmp_path / "deck" |
| 86 | + deck.mkdir() |
| 87 | + _png(tmp_path / "a.png") |
| 88 | + _png(deck / "b.png") |
| 89 | + b = _builder(deck) |
| 90 | + b.add_slide({"layout": "Blank", "elements": []}) |
| 91 | + slide = b.prs.slides[-1] |
| 92 | + r_embed = "{http://schemas.openxmlformats.org/officeDocument/2006/relationships}embed" |
| 93 | + xml_el = etree.Element("root") |
| 94 | + etree.SubElement(xml_el, "blip").set(r_embed, "rIdOut") |
| 95 | + etree.SubElement(xml_el, "blip").set(r_embed, "rIdIn") |
| 96 | + b._reattach_xml_images(slide, xml_el, {"rIdOut": "../a.png", "rIdIn": "b.png"}) |
| 97 | + embeds = [el.get(r_embed) for el in xml_el.iter("blip")] |
| 98 | + assert embeds[0] == "rIdOut" # left unmapped |
| 99 | + assert embeds[1] != "rIdIn" # remapped to the added part |
| 100 | + |
| 101 | + |
| 102 | +# ``icons:``/``assets:`` references: the asset lookup itself refuses ``..``, so |
| 103 | +# every caller (PPTX builder, HTML assembly, the pre-build asset check) gets it. |
| 104 | + |
| 105 | + |
| 106 | +@pytest.fixture |
| 107 | +def outside_asset(tmp_path, monkeypatch): |
| 108 | + """An image one level above a stand-in assets directory, and the ref that names it.""" |
| 109 | + import sdpm.knowledge.assets as assets_mod |
| 110 | + |
| 111 | + assets_dir = tmp_path / "assets" |
| 112 | + (assets_dir / "aws").mkdir(parents=True) |
| 113 | + monkeypatch.setattr(assets_mod, "ASSETS_DIR", assets_dir) |
| 114 | + _png(tmp_path / "outside" / "evil.png") |
| 115 | + return "assets:../outside/evil" |
| 116 | + |
| 117 | + |
| 118 | +@pytest.mark.parametrize("ref", ["assets:../x/y", "assets:aws/../x", "icons:../x", "../x"]) |
| 119 | +def test_resolve_asset_path_rejects_parent_segments(ref): |
| 120 | + from sdpm.knowledge.assets import resolve_asset_path |
| 121 | + |
| 122 | + with pytest.raises(ValueError): |
| 123 | + resolve_asset_path(ref) |
| 124 | + |
| 125 | + |
| 126 | +def test_check_asset_exists_refuses_parent_segments(outside_asset): |
| 127 | + from sdpm.knowledge.assets import check_asset_exists |
| 128 | + |
| 129 | + assert check_asset_exists(outside_asset) is False |
| 130 | + |
| 131 | + |
| 132 | +def test_html_asset_ref_with_parent_segment_is_not_embedded(tmp_path, outside_asset): |
| 133 | + from sdpm.engine.html.assemble import _Resolver |
| 134 | + |
| 135 | + warnings: list[str] = [] |
| 136 | + resolver = _Resolver(tmp_path / "deck", "light", warnings.append) |
| 137 | + assert resolver.path(outside_asset, "slide 1") is None |
| 138 | + assert any("'..'" in w for w in warnings) |
| 139 | + |
| 140 | + |
| 141 | +def test_injected_image_with_parent_segment_warns(tmp_path, capsys): |
| 142 | + deck = tmp_path / "deck" |
| 143 | + deck.mkdir() |
| 144 | + _png(tmp_path / "a.png") |
| 145 | + b = _builder(deck) |
| 146 | + b.add_slide({"layout": "Blank", "elements": []}) |
| 147 | + b._reattach_xml_images(b.prs.slides[-1], etree.Element("root"), {"rId1": "../a.png"}) |
| 148 | + assert "image skipped" in capsys.readouterr().err |
0 commit comments