Support optional highlight names for uploaded photo batches

This commit is contained in:
2026-09-19 14:56:25 +03:30
parent 86dcfd71cb
commit f4037695a1
4 changed files with 65 additions and 9 deletions
+39 -1
View File
@@ -1,4 +1,5 @@
import io
import hashlib
import json
from pathlib import Path
import subprocess
@@ -28,9 +29,10 @@ def photo(color="red", format="JPEG", size=(40, 20), exif=None):
return output.getvalue()
def upload(client, images=None, caption="", headers=AUTH):
def upload(client, images=None, caption="", headers=AUTH, highlight=None):
return client.post("/api/photos", headers=headers, data={
"caption": caption,
**({"highlight": highlight} if highlight is not None else {}),
"photos": [(io.BytesIO(raw), "../../escape.jpg") for raw in (images or [photo()])],
})
@@ -96,6 +98,42 @@ def test_orientation_metadata_and_heic(service):
assert image.height > image.width
def test_optional_highlight_preserves_existing_upload_ids(service):
_, client, data = service
raw = photo()
digest = hashlib.sha256(b"Caption")
digest.update(hashlib.sha256(raw).digest())
response = upload(client, [raw], "Caption")
assert response.json["id"] == digest.hexdigest()
assert upload(client, [raw], "Caption", highlight=" ").json["id"] == digest.hexdigest()
entry = (data / "submissions" / digest.hexdigest() / "entry.md").read_text()
assert "anchor:" not in entry
assert entry.count(f'id="photo-{digest.hexdigest()}"') == 1
def test_highlight_album_and_safe_front_matter(service):
_, client, data = service
name = 'سفر: "Summer" & <friends>\n---\npublished: false'
images = [photo("red"), photo("blue")]
response = upload(client, images, "Album caption", highlight=name)
assert response.status_code == 202
job = response.json["id"]
folder = data / "submissions" / job
entry = (folder / "entry.md").read_text()
metadata, body = entry.split("---\n", 2)[1:]
anchor = next(line for line in metadata.splitlines() if line.startswith("anchor: "))
assert json.loads(anchor.removeprefix("anchor: ")) == name
assert "\npublished:" not in metadata
# The template emits the sole target at the start of this album, before 00.jpg.
assert f'title: "photo-{job}"' in metadata
assert f'id="photo-{job}"' not in body
assert body.index("00.jpg") < body.index("01.jpg")
assert json.loads((folder / "manifest.json").read_text())["highlight"] == name
assert upload(client, images, "Album caption", highlight=name).status_code == 200
assert upload(client, images, "Album caption", highlight="Other trip").json["id"] != job
assert upload(client, images, highlight="a" * 201).status_code == 400
def git(directory, *args):
return subprocess.check_output(["git", "-C", str(directory), *args], text=True).strip()