Some Micropub clients (OutPost) upload a photo to the media endpoint and then embed it in content instead of sending it as `photo`. Those posts have no attached media, so crossposts went out without the image. Images in the text that are in this site's media library are now sent like attached photos, with their alt text, and GoToSocial's text no longer carries the image Markdown. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
397 lines
15 KiB
Python
397 lines
15 KiB
Python
"""
|
|
Tests for crossposting to GoToSocial (starpunk/syndication.py, v2.1.0)
|
|
|
|
A fake Mastodon-API server (httpx.MockTransport) records what StarPunk sends.
|
|
"""
|
|
|
|
import io
|
|
import json
|
|
|
|
import httpx
|
|
import pytest
|
|
from PIL import Image
|
|
|
|
from starpunk import create_app, syndication
|
|
from starpunk.auth import create_session
|
|
from starpunk.indieauth import create_access_token
|
|
from starpunk.notes import create_note, delete_note, update_note
|
|
from starpunk.syndication import (
|
|
MastodonClient,
|
|
compose_text,
|
|
get_note_syndications,
|
|
note_changed,
|
|
process_due,
|
|
select_targets,
|
|
)
|
|
|
|
GTS = "https://social.example.com"
|
|
|
|
|
|
class FakeServer:
|
|
def __init__(self):
|
|
self.statuses = {}
|
|
self.calls = []
|
|
self.media = 0
|
|
self.uploads = [] # raw multipart bodies
|
|
self.fail_next = None # status code to return once
|
|
|
|
def __call__(self, request: httpx.Request) -> httpx.Response:
|
|
self.calls.append((request.method, request.url.path))
|
|
assert request.headers["Authorization"] == "Bearer secret-token"
|
|
if self.fail_next:
|
|
code, self.fail_next = self.fail_next, None
|
|
return httpx.Response(code, json={"error": "nope"})
|
|
path = request.url.path
|
|
if path == "/api/v2/media":
|
|
self.media += 1
|
|
self.uploads.append(request.read())
|
|
return httpx.Response(200, json={"id": f"m{self.media}"})
|
|
if path == "/api/v1/statuses" and request.method == "POST":
|
|
body = json.loads(request.content)
|
|
sid = str(len(self.statuses) + 1)
|
|
self.statuses[sid] = body
|
|
return httpx.Response(200, json={"id": sid, "url": f"{GTS}/@phil/statuses/{sid}"})
|
|
if path.startswith("/api/v1/statuses/"):
|
|
sid = path.rsplit("/", 1)[1]
|
|
if sid not in self.statuses:
|
|
return httpx.Response(404, json={"error": "not found"})
|
|
if request.method == "PUT":
|
|
self.statuses[sid] = json.loads(request.content)
|
|
return httpx.Response(200, json={"id": sid, "url": f"{GTS}/@phil/statuses/{sid}"})
|
|
if request.method == "DELETE":
|
|
del self.statuses[sid]
|
|
return httpx.Response(200, json={"id": sid})
|
|
return httpx.Response(500)
|
|
|
|
|
|
@pytest.fixture
|
|
def server():
|
|
return FakeServer()
|
|
|
|
|
|
@pytest.fixture
|
|
def app(tmp_path, server):
|
|
data = tmp_path / "data"
|
|
app = create_app(
|
|
{
|
|
"TESTING": True,
|
|
"SITE_URL": "https://blog.example.com",
|
|
"ADMIN_ME": "https://example.com",
|
|
"SESSION_SECRET": "test-secret",
|
|
"DATA_PATH": data,
|
|
"NOTES_PATH": data / "notes",
|
|
"DATABASE_PATH": data / "starpunk.db",
|
|
"GOTOSOCIAL_URL": GTS,
|
|
"GOTOSOCIAL_ACCESS_TOKEN": "secret-token",
|
|
}
|
|
)
|
|
syndication._client_cache[GTS] = MastodonClient(
|
|
GTS, "secret-token", http=httpx.Client(transport=httpx.MockTransport(server))
|
|
)
|
|
yield app
|
|
syndication._client_cache.clear()
|
|
|
|
|
|
def rows(app, note_id):
|
|
with app.app_context():
|
|
return get_note_syndications(note_id)
|
|
|
|
|
|
def publish(app, content="Hello fediverse", tags=None, targets=("gotosocial",)):
|
|
with app.test_request_context():
|
|
note = create_note(content=content, published=True, tags=tags)
|
|
select_targets(note.id, list(targets))
|
|
process_due()
|
|
return note
|
|
|
|
|
|
class TestCompose:
|
|
def test_full_note_with_link_and_hashtags(self, app):
|
|
with app.app_context():
|
|
note = create_note(content="# Title\n\nSome *markdown*", published=True)
|
|
text = compose_text(note, "https://blog.example.com/note/x", ["IndieWeb", "Self Hosting"], 5000)
|
|
assert text == "# Title\n\nSome *markdown*\n\nhttps://blog.example.com/note/x\n\n#IndieWeb #SelfHosting"
|
|
|
|
def test_long_note_is_shortened_but_keeps_link(self, app):
|
|
with app.app_context():
|
|
note = create_note(content="word " * 2000, published=True)
|
|
text = compose_text(note, "https://blog.example.com/note/x", [], 500)
|
|
assert len(text) <= 500
|
|
assert text.endswith("https://blog.example.com/note/x")
|
|
assert "…" in text
|
|
|
|
|
|
class TestLifecycle:
|
|
def test_publish_creates_status(self, app, server):
|
|
note = publish(app, tags=["Test"])
|
|
(row,) = rows(app, note.id)
|
|
assert row["state"] == "synced"
|
|
assert row["remote_url"] == f"{GTS}/@phil/statuses/1"
|
|
body = server.statuses["1"]
|
|
assert body["content_type"] == "text/markdown"
|
|
assert body["visibility"] == "public"
|
|
assert f"https://blog.example.com/note/{note.slug}" in body["status"]
|
|
assert "#Test" in body["status"]
|
|
|
|
def test_edit_updates_status(self, app, server):
|
|
note = publish(app)
|
|
with app.test_request_context():
|
|
update_note(id=note.id, content="Edited text")
|
|
process_due()
|
|
assert server.statuses["1"]["status"].startswith("Edited text")
|
|
assert ("PUT", "/api/v1/statuses/1") in server.calls
|
|
|
|
def test_delete_deletes_status(self, app, server):
|
|
note = publish(app)
|
|
with app.test_request_context():
|
|
delete_note(id=note.id)
|
|
process_due()
|
|
assert server.statuses == {}
|
|
(row,) = rows(app, note.id)
|
|
assert row["remote_id"] is None and row["state"] == "synced"
|
|
|
|
def test_hard_delete_deletes_status(self, app, server):
|
|
note = publish(app)
|
|
with app.test_request_context():
|
|
delete_note(id=note.id, soft=False)
|
|
process_due()
|
|
assert server.statuses == {}
|
|
|
|
def test_unpublish_then_republish(self, app, server):
|
|
note = publish(app)
|
|
with app.test_request_context():
|
|
update_note(id=note.id, published=False)
|
|
process_due()
|
|
assert server.statuses == {}
|
|
update_note(id=note.id, published=True)
|
|
process_due()
|
|
assert len(server.statuses) == 1
|
|
|
|
def test_opt_out_sends_nothing(self, app, server):
|
|
note = publish(app, targets=())
|
|
assert server.calls == []
|
|
assert rows(app, note.id) == []
|
|
|
|
def test_unticking_later_deletes_status(self, app, server):
|
|
note = publish(app)
|
|
with app.test_request_context():
|
|
select_targets(note.id, [])
|
|
process_due()
|
|
assert server.statuses == {}
|
|
|
|
def test_drafts_are_not_sent(self, app, server):
|
|
with app.test_request_context():
|
|
note = create_note(content="draft", published=False)
|
|
select_targets(note.id, ["gotosocial"])
|
|
process_due()
|
|
assert server.statuses == {}
|
|
|
|
def test_remote_deleted_by_hand_is_reposted_on_edit(self, app, server):
|
|
note = publish(app)
|
|
server.statuses.clear()
|
|
with app.test_request_context():
|
|
note_changed(note.id)
|
|
process_due()
|
|
assert len(server.statuses) == 1
|
|
|
|
def test_edit_during_send_is_not_lost(self, app, server):
|
|
with app.test_request_context():
|
|
note = create_note(content="v1", published=True)
|
|
select_targets(note.id, ["gotosocial"])
|
|
# Simulate an edit arriving while the first send is in flight
|
|
original = syndication._reconcile
|
|
|
|
def edit_mid_send(row, target):
|
|
result = original(row, target)
|
|
update_note(id=note.id, content="v2")
|
|
return result
|
|
|
|
syndication._reconcile = edit_mid_send
|
|
try:
|
|
process_due()
|
|
finally:
|
|
syndication._reconcile = original
|
|
assert rows(app, note.id)[0]["state"] == "pending"
|
|
process_due()
|
|
assert server.statuses["1"]["status"].startswith("v2")
|
|
|
|
|
|
class TestFailures:
|
|
def test_server_error_retries_later(self, app, server):
|
|
server.fail_next = 503
|
|
note = publish(app)
|
|
(row,) = rows(app, note.id)
|
|
assert row["state"] == "pending"
|
|
assert row["attempts"] == 1
|
|
assert "503" in row["last_error"]
|
|
|
|
def test_client_error_fails_permanently(self, app, server):
|
|
server.fail_next = 422
|
|
note = publish(app)
|
|
(row,) = rows(app, note.id)
|
|
assert row["state"] == "failed"
|
|
|
|
def test_retry_button(self, app, server):
|
|
server.fail_next = 422
|
|
note = publish(app)
|
|
(row,) = rows(app, note.id)
|
|
client = app.test_client()
|
|
with app.test_request_context():
|
|
client.set_cookie("starpunk_session", create_session("https://example.com"))
|
|
client.post(f"/admin/syndication/{row['id']}/retry")
|
|
with app.test_request_context():
|
|
process_due()
|
|
assert rows(app, note.id)[0]["state"] == "synced"
|
|
|
|
def test_no_targets_configured_does_nothing(self, tmp_path):
|
|
data = tmp_path / "d"
|
|
app = create_app({
|
|
"TESTING": True, "SITE_URL": "https://b.example", "ADMIN_ME": "https://e.example",
|
|
"SESSION_SECRET": "x", "DATA_PATH": data, "NOTES_PATH": data / "notes",
|
|
"DATABASE_PATH": data / "s.db",
|
|
})
|
|
with app.test_request_context():
|
|
note = create_note(content="x", published=True)
|
|
select_targets(note.id, ["gotosocial"])
|
|
assert process_due() == 0
|
|
assert get_note_syndications(note.id) == []
|
|
|
|
|
|
class TestMedia:
|
|
def test_images_are_uploaded_with_alt_text(self, app, server):
|
|
from starpunk.media import attach_media_to_note, save_media
|
|
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (800, 600), "red").save(buf, format="JPEG")
|
|
with app.test_request_context():
|
|
note = create_note(content="With a picture", published=True)
|
|
media = save_media(buf.getvalue(), "red.jpg")
|
|
attach_media_to_note(note.id, [media["id"]], ["A red square"])
|
|
select_targets(note.id, ["gotosocial"])
|
|
process_due()
|
|
assert ("POST", "/api/v2/media") in server.calls
|
|
assert server.statuses["1"]["media_ids"] == ["m1"]
|
|
|
|
|
|
class TestInlineImages:
|
|
"""Clients like OutPost upload a photo and embed it in the content (v2.6.6)"""
|
|
|
|
def _upload(self, app):
|
|
from starpunk.media import save_media
|
|
|
|
buf = io.BytesIO()
|
|
Image.new("RGB", (800, 600), "orange").save(buf, format="JPEG")
|
|
with app.test_request_context():
|
|
return save_media(buf.getvalue(), "cat.jpg")
|
|
|
|
def test_embedded_own_image_is_attached(self, app, server):
|
|
media = self._upload(app)
|
|
url = f"https://blog.example.com/media/{media['path']}"
|
|
publish(app, content=f"")
|
|
status = server.statuses["1"]
|
|
assert status["media_ids"] == ["m1"]
|
|
assert b"Tortie cat on a chair" in server.uploads[0]
|
|
assert "![" not in status["status"] and url not in status["status"]
|
|
assert status["status"].startswith("https://blog.example.com/note/")
|
|
|
|
def test_text_around_image_is_kept(self, app, server):
|
|
media = self._upload(app)
|
|
publish(app, content=f"Look\n\n<img src=\"/media/{media['path']}\" alt=\"cat\">\n\nNice")
|
|
status = server.statuses["1"]
|
|
assert status["media_ids"] == ["m1"]
|
|
assert status["status"].startswith("Look\n\nNice")
|
|
|
|
def test_image_both_attached_and_embedded_is_sent_once(self, app, server):
|
|
from starpunk.media import attach_media_to_note
|
|
|
|
media = self._upload(app)
|
|
with app.test_request_context():
|
|
note = create_note(content=f"", published=True)
|
|
attach_media_to_note(note.id, [media["id"]], ["caption"])
|
|
select_targets(note.id, ["gotosocial"])
|
|
process_due()
|
|
assert server.statuses["1"]["media_ids"] == ["m1"]
|
|
|
|
def test_other_sites_images_are_ignored(self, app, server):
|
|
publish(app, content=" hi")
|
|
assert server.statuses["1"]["media_ids"] == []
|
|
assert server.uploads == []
|
|
|
|
|
|
class TestEntryPoints:
|
|
@pytest.fixture
|
|
def admin(self, app):
|
|
client = app.test_client()
|
|
with app.test_request_context():
|
|
client.set_cookie("starpunk_session", create_session("https://example.com"))
|
|
return client
|
|
|
|
def test_new_note_form_ticks_target_by_default(self, admin):
|
|
html = admin.get("/admin/new").data.decode()
|
|
assert 'name="syndicate_to" value="gotosocial" checked' in html
|
|
|
|
def test_admin_create_queues(self, app, admin):
|
|
admin.post("/admin/new", data={
|
|
"content": "From the editor", "published": "on",
|
|
"syndication_form": "1", "syndicate_to": "gotosocial",
|
|
})
|
|
with app.app_context():
|
|
from starpunk.database import get_db
|
|
assert get_db(app).execute("SELECT COUNT(*) FROM syndications").fetchone()[0] == 1
|
|
|
|
def test_admin_create_opt_out(self, app, admin):
|
|
admin.post("/admin/new", data={"content": "Not this one", "published": "on", "syndication_form": "1"})
|
|
with app.app_context():
|
|
from starpunk.database import get_db
|
|
assert get_db(app).execute("SELECT COUNT(*) FROM syndications").fetchone()[0] == 0
|
|
|
|
def _token(self, app):
|
|
with app.test_request_context():
|
|
return create_access_token("https://quill.example/", "create")["access_token"]
|
|
|
|
def test_micropub_lists_target(self, app):
|
|
token = self._token(app)
|
|
data = app.test_client().get(
|
|
"/micropub?q=syndicate-to", headers={"Authorization": f"Bearer {token}"}
|
|
).get_json()
|
|
assert data["syndicate-to"] == [{"uid": f"{GTS}/", "name": "GoToSocial"}]
|
|
|
|
def test_micropub_default_is_all_targets(self, app):
|
|
token = self._token(app)
|
|
response = app.test_client().post(
|
|
"/micropub", data={"h": "entry", "content": "via quill"},
|
|
headers={"Authorization": f"Bearer {token}"},
|
|
)
|
|
assert response.status_code == 201
|
|
assert "/note/" in response.headers["Location"]
|
|
assert response.headers["Location"].startswith("https://blog.example.com/note/")
|
|
with app.app_context():
|
|
from starpunk.database import get_db
|
|
assert get_db(app).execute("SELECT COUNT(*) FROM syndications").fetchone()[0] == 1
|
|
|
|
def test_micropub_explicit_empty_choice(self, app):
|
|
token = self._token(app)
|
|
app.test_client().post(
|
|
"/micropub", json={"type": ["h-entry"], "properties": {"content": ["no crosspost"]},
|
|
"mp-syndicate-to": []},
|
|
headers={"Authorization": f"Bearer {token}"},
|
|
)
|
|
with app.app_context():
|
|
from starpunk.database import get_db
|
|
assert get_db(app).execute("SELECT COUNT(*) FROM syndications").fetchone()[0] == 0
|
|
|
|
def test_note_page_links_to_crosspost(self, app, server):
|
|
note = publish(app)
|
|
html = app.test_client().get(f"/note/{note.slug}").data.decode()
|
|
assert f'class="u-syndication" href="{GTS}/@phil/statuses/1"' in html
|
|
|
|
def test_edit_page_shows_status_and_separate_delete_form(self, app, admin, server):
|
|
note = publish(app)
|
|
html = admin.get(f"/admin/edit/{note.id}").data.decode()
|
|
assert 'value="gotosocial" checked' in html
|
|
assert f"{GTS}/@phil/statuses/1" in html
|
|
# Delete button belongs to its own form, not the edit form
|
|
assert 'form="delete-note-form"' in html
|
|
edit_form = html.split('class="note-form"', 1)[1].split("</form>", 1)[0]
|
|
assert "<form" not in edit_form
|