Articles as the IndieWeb defines them: a name separate from the content.
- Migration 023 adds notes.title and notes.summary. A one-time Python data
step (migrations.DATA_MIGRATIONS) moves each note's '# Heading' first line
into title, rewrites the file and hash, empties FTS for a rebuild, and puts
files back if the migration fails. Trashed notes included.
- Micropub: name -> title, summary stored when sent with content, summary
updatable, q=source returns both. MCP tools take a summary.
- Feeds: titles only for articles (RSS/JSON none, Atom empty); Atom and
JSON Feed summaries; the title is no longer repeated in the content.
- Pages: p-name/p-summary on the article page. Homepage and tag pages show
articles as title, summary and "Read more" without images (hidden u-photo
keeps the post type for parsers).
- Crossposts: articles go out as title, summary, link and hashtags, no
images; the Bluesky card is the article. Notes unchanged.
- Editor: Title and Summary fields; an article may have no body.
- Fix: a line starting with a hashtag ('#indieweb ...') was rendered as a
heading and crossposted without its '#' (HashtagLineExtension).
- Fix: excerpts showed raw Markdown; they are plain text now.
Back up the database and the data folder before deploying: the migration
rewrites note files.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
432 lines
17 KiB
Python
432 lines
17 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 TestArticles:
|
|
"""Articles are crossposted as title, teaser and link, without images (v2.16.0)"""
|
|
|
|
LINK = "https://blog.example.com/note/x"
|
|
|
|
def test_title_summary_and_link(self, app):
|
|
with app.app_context():
|
|
note = create_note(content="Long body " * 100, title="My Article",
|
|
summary="The gist.", published=True)
|
|
text = compose_text(note, self.LINK, ["IndieWeb"], 500)
|
|
assert text == f"My Article\n\nThe gist.\n\n{self.LINK}\n\n#IndieWeb"
|
|
|
|
def test_without_summary_the_start_of_the_text(self, app):
|
|
with app.app_context():
|
|
note = create_note(content="Some *markdown* " + "word " * 100, title="T", published=True)
|
|
title, teaser, link = compose_text(note, self.LINK, [], 5000).split("\n\n")
|
|
assert (title, link) == ("T", self.LINK)
|
|
assert teaser.startswith("Some markdown word") and teaser.endswith("…")
|
|
assert len(teaser) <= 201
|
|
|
|
def test_edit_follows_and_no_images(self, app, server, monkeypatch):
|
|
looked_up = []
|
|
monkeypatch.setattr(syndication, "_media_files", lambda *a, **k: looked_up.append(a) or [])
|
|
with app.test_request_context():
|
|
note = create_note(content="Body", title="Before", published=True)
|
|
select_targets(note.id, ["gotosocial"])
|
|
process_due()
|
|
update_note(id=note.id, title="After")
|
|
process_due()
|
|
assert server.statuses["1"]["status"].startswith("After\n\nBody")
|
|
assert not server.statuses["1"].get("media_ids")
|
|
assert looked_up == []
|
|
|
|
|
|
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_no_targets(self, app):
|
|
"""Absent mp-syndicate-to means no crossposting (v2.9.0)"""
|
|
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] == 0
|
|
|
|
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
|