- A create that failed after the note was written (e.g. the same photo twice) answered 500 but left the post published without photos. Photos are now resolved first, and a failed note is removed. - Pooled connections roll back unfinished writes, so one failure no longer locks the database for the next ~10 s. - The same photo twice in one post is shown once. - Photo posts no longer need text (Micropub doesn't require content, and q=config already advertised a photo-only post type). Updates may clear the text while photos remain. Crossposts start with the link; the page is titled "Photo". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
4.8 KiB
Python
106 lines
4.8 KiB
Python
"""
|
|
Micropub media fixes (v2.6.5): photo posts without text, the same photo
|
|
twice, and failed creates that left a post (and a database lock) behind.
|
|
"""
|
|
|
|
import io
|
|
|
|
from starpunk.database.pool import ConnectionPool
|
|
from starpunk.notes import list_notes
|
|
from starpunk.syndication import compose_text
|
|
from tests.test_micropub_v240 import SITE, app, client, h, jpeg, note_at, source # noqa: F401
|
|
|
|
|
|
def upload(client, h, color="red"):
|
|
response = client.post("/micropub/media", data={"file": (io.BytesIO(jpeg(color)), "a.jpg")},
|
|
headers=h, content_type="multipart/form-data")
|
|
assert response.status_code == 201
|
|
return response.headers["Location"]
|
|
|
|
|
|
def post(client, h, props):
|
|
return client.post("/micropub", json={"type": ["h-entry"], "properties": props}, headers=h)
|
|
|
|
|
|
def update(client, h, url, **ops):
|
|
return client.post("/micropub", json={"action": "update", "url": url, **ops}, headers=h)
|
|
|
|
|
|
class TestPhotoWithoutText:
|
|
def test_json_photo_only(self, app, client, h):
|
|
photo = upload(client, h)
|
|
response = post(client, h, {"photo": [{"value": photo, "alt": "Red"}]})
|
|
assert response.status_code == 201, response.get_json()
|
|
url = response.headers["Location"]
|
|
assert note_at(app, url).content == ""
|
|
assert source(client, h, url, "photo")["properties"]["photo"] == [{"value": photo, "alt": "Red"}]
|
|
page = client.get(url.replace(SITE, ""))
|
|
assert page.status_code == 200 and b"<title>Photo \xc2\xb7 " in page.data
|
|
|
|
def test_multipart_photo_only(self, client, h):
|
|
response = client.post("/micropub", data={"h": "entry", "photo": (io.BytesIO(jpeg()), "p.jpg")},
|
|
headers=h, content_type="multipart/form-data")
|
|
assert response.status_code == 201, response.get_json()
|
|
|
|
def test_nothing_at_all_is_still_rejected(self, client, h):
|
|
assert post(client, h, {}).status_code == 400
|
|
# A photo that can't be found doesn't count
|
|
assert post(client, h, {"photo": [f"{SITE}/media/nope.jpg"]}).status_code == 400
|
|
|
|
def test_text_can_be_cleared_while_photos_remain(self, app, client, h):
|
|
photo = upload(client, h)
|
|
url = post(client, h, {"content": ["Hi"], "photo": [photo]}).headers["Location"]
|
|
assert update(client, h, url, replace={"content": [""]}).status_code == 204
|
|
assert note_at(app, url).content == ""
|
|
assert update(client, h, url, replace={"content": ["Back"]}).status_code == 204
|
|
assert update(client, h, url, delete=["content"]).status_code == 204
|
|
assert note_at(app, url).content == ""
|
|
# Without photos the text has to stay
|
|
response = update(client, h, url, delete=["content", "photo"])
|
|
assert response.status_code == 400
|
|
|
|
def test_text_is_required_without_photos(self, client, h):
|
|
url = post(client, h, {"content": ["Hi"]}).headers["Location"]
|
|
assert update(client, h, url, replace={"content": [""]}).status_code == 400
|
|
assert update(client, h, url, delete=["content"]).status_code == 400
|
|
|
|
def test_crosspost_text_starts_with_the_link(self, app, client, h):
|
|
url = post(client, h, {"photo": [upload(client, h)]}).headers["Location"]
|
|
with app.app_context():
|
|
note = note_at(app, url)
|
|
assert compose_text(note, url, ["cats"], 500) == f"{url}\n\n#cats"
|
|
|
|
|
|
class TestFailures:
|
|
def test_same_photo_twice_is_shown_once(self, client, h):
|
|
photo = upload(client, h)
|
|
response = post(client, h, {"content": ["Twice"], "photo": [photo, photo]})
|
|
assert response.status_code == 201, response.get_json()
|
|
assert source(client, h, response.headers["Location"], "photo")["properties"]["photo"] == [{"value": photo, "alt": ""}]
|
|
|
|
def test_failed_create_leaves_no_post_and_no_lock(self, app, client, h, monkeypatch):
|
|
import starpunk.syndication as syndication
|
|
|
|
def boom(*args, **kwargs):
|
|
raise RuntimeError("crosspost store down")
|
|
|
|
monkeypatch.setattr(syndication, "select_targets", boom)
|
|
response = post(client, h, {"content": ["Doomed"], "photo": [upload(client, h)]})
|
|
assert response.status_code == 500
|
|
with app.app_context():
|
|
assert list_notes(published_only=False) == []
|
|
monkeypatch.undo()
|
|
# The database is usable straight away
|
|
assert post(client, h, {"content": ["Next"]}).status_code == 201
|
|
|
|
def test_pool_rolls_back_open_transactions(self, tmp_path):
|
|
pool = ConnectionPool(str(tmp_path / "x.db"), pool_size=1, timeout=1)
|
|
conn = pool.get_connection()
|
|
conn.execute("CREATE TABLE t (x)")
|
|
conn.commit()
|
|
conn.execute("INSERT INTO t VALUES (1)")
|
|
assert conn.in_transaction
|
|
pool.return_connection(conn)
|
|
assert not conn.in_transaction
|
|
assert pool.get_connection().execute("SELECT count(*) FROM t").fetchone()[0] == 0
|