Files
StarPunk/tests/test_articles.py
T
PhilandClaude Opus 5.5 ccb9f6dce2 fix: keep inline images in Micropub articles (v2.16.0)
- Content sent as both html and text is stored as the HTML; the text is
  usually a plain fallback, so images, links and headings were lost.
- Images embedded from other sites are copied into the media library
  (SSRF-checked fetcher, best effort, max 20) and the text points at the
  copies, on create and on content replace.
- HTML content is sanitized as is instead of going through Markdown, which
  added stray <br>s and empty paragraphs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 17:11:46 -07:00

196 lines
9.4 KiB
Python

"""
Articles: titles and summaries stored apart from the text (v2.16.0, ADR-076).
Feeds give only articles a title and never repeat it in the content; the
homepage and tag pages show an article as title, summary and "Read more"
without its images; the admin editor has Title and Summary fields.
"""
import json
import xml.etree.ElementTree as ET
import httpx
import mf2py
from starpunk.auth import create_session
from starpunk.notes import create_note, get_note
from tests.test_micropub_v240 import SITE, app, client, h, jpeg, source # noqa: F401
from tests.test_micropub_v265 import upload
ATOM = {"a": "http://www.w3.org/2005/Atom"}
def micropub(client, h, **props):
response = client.post("/micropub", json={"type": ["h-entry"], "properties": props}, headers=h)
assert response.status_code == 201, response.get_json()
return response.headers["Location"]
def make_both(app):
with app.app_context():
article = create_note(content="The **body**.", title="An Article", summary="The gist.",
published=True, tags=["essay"])
note = create_note(content="# Just a heading\n\nin a note", published=True, tags=["essay"])
return article, note
class TestFeeds:
def test_rss_title_only_for_articles(self, app, client):
article, note = make_both(app)
items = {i.find("link").text: i for i in ET.fromstring(client.get("/feed.rss").data).iter("item")}
a, n = items[f"{SITE}/note/{article.slug}"], items[f"{SITE}/note/{note.slug}"]
assert a.find("title").text == "An Article"
assert "An Article" not in a.find("description").text # not repeated in the body
assert n.find("title") is None
assert n.find("description") is not None
def test_atom_empty_title_and_summary(self, app, client):
article, note = make_both(app)
entries = {e.find("a:id", ATOM).text: e
for e in ET.fromstring(client.get("/feed.atom").data).findall("a:entry", ATOM)}
a, n = entries[f"{SITE}/note/{article.slug}"], entries[f"{SITE}/note/{note.slug}"]
assert a.find("a:title", ATOM).text == "An Article"
assert a.find("a:summary", ATOM).text == "The gist."
assert "An Article" not in a.find("a:content", ATOM).text
assert n.find("a:title", ATOM) is not None and not n.find("a:title", ATOM).text
assert n.find("a:summary", ATOM) is None
def test_json_feed(self, app, client):
article, note = make_both(app)
items = {i["url"]: i for i in json.loads(client.get("/feed.json").data)["items"]}
a, n = items[f"{SITE}/note/{article.slug}"], items[f"{SITE}/note/{note.slug}"]
assert (a["title"], a["summary"]) == ("An Article", "The gist.")
assert "An Article" not in a["content_html"]
assert "title" not in n and "summary" not in n
assert "<h1>Just a heading</h1>" in n["content_html"]
class TestPages:
def test_article_page(self, app, client):
article, _ = make_both(app)
(entry,) = [i for i in mf2py.parse(doc=client.get(f"/note/{article.slug}").data.decode(),
url=f"{SITE}/note/{article.slug}")["items"]
if "h-entry" in i["type"]]
props = entry["properties"]
assert props["name"] == ["An Article"]
assert props["summary"] == ["The gist."]
assert not props["content"][0]["value"].startswith("An Article")
def test_homepage_and_tag_page(self, app, client, h):
photo = upload(client, h)
url = micropub(client, h, name=["Trip"], content=["Day one of the trip."], photo=[photo],
category=["essay"])
micropub(client, h, content=["A photo note"], photo=[upload(client, h, "blue")], category=["essay"])
with app.app_context():
slug = url.rsplit("/", 1)[1]
article = get_note(slug=slug)
for path in ("/", "/tag/essay"):
html = client.get(path).data.decode()
start = html.index(f'href="{url}"')
section = html[html.rindex("<article", 0, start):html.index("</article>", start)]
assert "Trip" in section and "Day one of the trip." in section and "Read more" in section
assert "<img" not in section and "e-content" not in section
assert 'class="u-photo"' in section # parsers still see a photo post
assert html.count("<img") >= 1 # the photo note still shows its image
assert article.title == "Trip"
def test_preview_uses_summary_over_excerpt(self, app, client):
make_both(app)
html = client.get("/").data.decode()
assert "The gist." in html and "The <strong>body</strong>." not in html
class TestAdmin:
def login(self, app, client):
with app.test_request_context():
client.set_cookie("starpunk_session", create_session(app.config["ADMIN_ME"]))
def test_create_and_edit_article(self, app, client):
self.login(app, client)
response = client.post("/admin/new", data={
"title": "From the editor", "summary": "Short.", "content": "Body", "published": "on"})
assert response.status_code == 302
with app.app_context():
from starpunk.notes import list_notes
(note,) = list_notes()
assert (note.title, note.summary, note.content) == ("From the editor", "Short.", "Body")
form = client.get(f"/admin/edit/{note.id}").data.decode()
assert 'value="From the editor"' in form and ">Short.</textarea>" in form
client.post(f"/admin/edit/{note.id}", data={"title": "", "summary": "", "content": "Body",
"published": "on"})
with app.app_context():
note = get_note(id=note.id)
assert (note.title, note.summary) == (None, None)
def test_title_only_article(self, app, client):
self.login(app, client)
client.post("/admin/new", data={"title": "Only a title", "content": "", "published": "on"})
with app.app_context():
from starpunk.notes import list_notes
(note,) = list_notes()
assert note.title == "Only a title" and note.content == ""
class TestInlineImages:
"""Articles posted with images in their text (v2.16.0)"""
def remote_host(self, monkeypatch, ok=("/a.jpg",)):
fetched = []
def serve(request): # the fetcher connects to the pinned address, so match on path
fetched.append(request.url.path)
if request.url.path in ok:
return httpx.Response(200, content=jpeg("green"))
return httpx.Response(404)
monkeypatch.setattr("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(serve))
monkeypatch.setattr("starpunk.webmention_ssrf.socket.getaddrinfo",
lambda *a, **k: [(2, 1, 6, "", ("93.184.216.34", 0))])
return fetched
def test_html_is_kept_over_text_fallback(self, app, client, h, monkeypatch):
self.remote_host(monkeypatch)
html = '<p>Intro <a href="https://x.example">link</a></p><img src="https://cdn.example/a.jpg" alt="A">'
url = micropub(client, h, name=["T"], content=[{"html": html, "text": "Intro link"}])
(content,) = source(client, h, url)["properties"]["content"]
assert "html" in content and 'href="https://x.example"' in content["html"]
assert "<img" in content["html"]
def test_remote_images_are_copied(self, app, client, h, monkeypatch):
fetched = self.remote_host(monkeypatch)
url = micropub(client, h, name=["T"], content=[
"Intro\n\n![A](https://cdn.example/a.jpg)\n\n![B](https://cdn.example/gone.jpg)\n\n"
"Again ![A](https://cdn.example/a.jpg)"])
with app.app_context():
note = get_note(slug=url.rsplit("/", 1)[1])
assert fetched.count("/a.jpg") == 1 # each image once
assert "https://cdn.example/a.jpg" not in note.content
assert note.content.count(f"]({SITE}/media/") == 2
assert "https://cdn.example/gone.jpg" in note.content # couldn't fetch: left as is
assert client.get(note.content.split(f"]({SITE}")[1].split(")")[0]).status_code == 200
def test_html_images_copied_and_rendered_cleanly(self, app, client, h, monkeypatch):
self.remote_host(monkeypatch)
url = micropub(client, h, name=["T"], content=[{"html":
'<p>Intro</p><img src="https://cdn.example/a.jpg" alt="A"><p>More</p><script>x()</script>'}])
with app.app_context():
note = get_note(slug=url.rsplit("/", 1)[1])
assert note.content_format == "html"
assert note.html.startswith("<p>Intro</p><img") and f'src="{SITE}/media/' in note.html
assert "<br" not in note.html and "<p></p>" not in note.html and "script" not in note.html
def test_own_images_not_fetched(self, app, client, h, monkeypatch):
fetched = self.remote_host(monkeypatch)
micropub(client, h, content=[f"![A]({SITE}/media/2026/11/x.jpg)"])
assert fetched == []
def test_update_copies_new_images(self, app, client, h, monkeypatch):
self.remote_host(monkeypatch)
url = micropub(client, h, name=["T"], content=["Before"])
client.post("/micropub", json={"action": "update", "url": url,
"replace": {"content": ["![A](https://cdn.example/a.jpg)"]}}, headers=h)
with app.app_context():
assert f"]({SITE}/media/" in get_note(slug=url.rsplit("/", 1)[1]).content