Files
StarPunk/tests/test_webmention_skip_hosts.py
PhilandClaude Opus 5.5 b9e5321df2 fix: don't send webmentions to this site or WEBMENTION_SKIP_HOSTS (v2.18.3)
Links to the site itself, and to self-hosted apps like Gitea, timed out
from inside the container and sat in the admin Log. They are now skipped
when queueing; already queued or failed ones are dropped at worker start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 13:40:27 -07:00

89 lines
3.8 KiB
Python

"""
Webmentions are never sent to this site or WEBMENTION_SKIP_HOSTS (v2.18.3)
From inside the container the site's own public address can time out, and
self-hosted apps like Gitea don't take webmentions, so these sends only
failed and stayed in the admin Log.
"""
import sqlite3
from starpunk.webmention_sender import (
SKIPPED_NOTE, is_skipped, queue_webmentions_for_note, skipped_hosts)
from starpunk.webmention_worker import WebmentionWorker
SKIP = "git.example.com"
def queued(db_path):
conn = sqlite3.connect(db_path)
rows = conn.execute("SELECT target_url, status, last_error FROM webmention_queue ORDER BY id").fetchall()
conn.close()
return rows
def test_hosts_include_this_site(app):
app.config["WEBMENTION_SKIP_HOSTS"] = [SKIP]
hosts = skipped_hosts(app.config)
assert hosts == {"localhost", SKIP}
assert is_skipped("https://GIT.example.com:443/phil/x", hosts)
assert not is_skipped("https://example.com/", hosts) # exact host only
def test_links_to_skipped_hosts_are_not_queued(app):
app.config["WEBMENTION_SKIP_HOSTS"] = [SKIP]
html = "".join(f'<p><a href="{u}">x</a></p>' for u in [
"http://localhost:5000/note/other", f"https://{SKIP}/phil/StarPunk"]
+ [f"https://site{i}.example/" for i in range(10)])
with app.app_context():
n = queue_webmentions_for_note(app.config["DATABASE_PATH"], 1, "http://localhost:5000/note/a",
html, in_reply_to="http://localhost:5000/note/b")
targets = [t for t, _, _ in queued(app.config["DATABASE_PATH"])]
assert n == 10 and targets == [f"https://site{i}.example/" for i in range(10)] # skipped ones don't use the cap
def test_worker_drops_queued_and_failed_sends(app):
app.config["WEBMENTION_SKIP_HOSTS"] = [SKIP]
db_path = app.config["DATABASE_PATH"]
conn = sqlite3.connect(db_path)
conn.executemany(
"INSERT INTO webmention_queue (source_url, target_url, status, last_error) VALUES (?, ?, ?, ?)",
[("http://localhost:5000/note/1", f"https://{SKIP}/phil/StarPunk", "failed", "timed out"),
("http://localhost:5000/note/1", "http://localhost:5000/", "pending", "timed out"),
("http://localhost:5000/note/1", "https://example.org/post", "failed", "timed out"),
("http://localhost:5000/note/2", "http://localhost:5000/x", "sent", None)])
conn.commit()
conn.close()
WebmentionWorker(app, db_path=db_path, site_url=app.config["SITE_URL"])._drop_skipped_targets()
assert [(s, e) for _, s, e in queued(db_path)] == [
("no_endpoint", SKIPPED_NOTE), ("no_endpoint", SKIPPED_NOTE),
("failed", "timed out"), # other sites are left alone
("sent", None)]
def test_worker_skips_without_fetching(app):
db_path = app.config["DATABASE_PATH"]
conn = sqlite3.connect(db_path)
conn.execute("INSERT INTO webmention_queue (source_url, target_url, status) VALUES (?, ?, 'pending')",
("http://localhost:5000/note/1", "http://localhost:5000/"))
conn.commit()
(queue_id,) = conn.execute("SELECT id FROM webmention_queue").fetchone()
conn.close()
worker = WebmentionWorker(app, db_path=db_path, site_url=app.config["SITE_URL"])
worker._discover_endpoint = lambda url: (_ for _ in ()).throw(AssertionError("fetched"))
worker._send_webmention(queue_id, "http://localhost:5000/note/1", "http://localhost:5000/", None, 0)
assert queued(db_path)[0][1:] == ("no_endpoint", SKIPPED_NOTE)
def test_settings_page_lists_skipped_hosts(app, client):
from starpunk.auth import create_session
app.config["WEBMENTION_SKIP_HOSTS"] = [SKIP]
with app.test_request_context():
client.set_cookie("starpunk_session", create_session(app.config["ADMIN_ME"]))
html = client.get("/admin/settings").data.decode()
assert f"<code>{SKIP}</code>" in html and "<code>localhost</code>" in html