""" Webmention worker regression tests (v2.6.8) The worker thread used Flask's current_app (via the SSRF checks) without an app context, so every send and validation failed. It was also stopped whenever any app context ended. """ import sqlite3 import httpx import time from unittest.mock import MagicMock, patch from starpunk.webmention_worker import WebmentionWorker PUBLIC = [(2, 1, 6, '', ('93.184.216.34', 0))] def _wait_for(predicate, timeout=5): deadline = time.time() + timeout while time.time() < deadline: if predicate(): return True time.sleep(0.05) return False def test_worker_sends_queued_webmention(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", "https://example.org/post"), ) conn.commit() conn.close() def target_site(request): return httpx.Response(200, headers={"Link": '; rel="webmention"'}) accepted = MagicMock(status_code=202, text="") worker = WebmentionWorker(app, db_path=db_path, site_url=app.config["SITE_URL"], poll_interval=0.05) with patch("starpunk.webmention_ssrf.socket.getaddrinfo", return_value=PUBLIC), \ patch("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(target_site)), \ patch("starpunk.webmention_worker.httpx.post", return_value=accepted) as post: worker.start() try: def status(): c = sqlite3.connect(db_path) row = c.execute("SELECT status, last_error FROM webmention_queue").fetchone() c.close() return row assert _wait_for(lambda: status()[0] != "pending"), status() assert status() == ("sent", None) post.assert_called_once() assert post.call_args.args[0] == "https://example.org/wm" finally: worker.stop() worker.join(timeout=2) def test_worker_survives_app_context_teardown(app): worker = WebmentionWorker(app, db_path=app.config["DATABASE_PATH"], site_url=app.config["SITE_URL"], poll_interval=0.05) worker.start() try: with app.app_context(): pass app.test_client().get("/") time.sleep(0.2) assert worker.is_alive() finally: worker.stop() worker.join(timeout=2) def test_only_one_worker_processes_queues(app): """Each gunicorn process starts a worker; only one may hold the queue lock""" kwargs = dict(db_path=app.config["DATABASE_PATH"], site_url=app.config["SITE_URL"]) first = WebmentionWorker(app, **kwargs) second = WebmentionWorker(app, **kwargs) try: assert first._acquire_lock() assert not second._acquire_lock() first._release_lock() assert second._acquire_lock() finally: first._release_lock() second._release_lock() def test_author_photo_with_alt_text_is_a_string(): """mf2py returns u-photo with alt as a dict; the database needs a URL string""" import mf2py from starpunk.webmention_processor import extract_author html = """
""" author = extract_author(mf2py.parse(doc=html, url="https://src.example/"), "https://src.example/") assert author == { "author_name": "A Person", "author_url": "https://a.example/", "author_photo": "https://a.example/me.jpg", } def test_author_urls_must_be_http(): """A javascript: author URL would be rendered as a clickable link (XSS)""" import mf2py from starpunk.webmention_processor import extract_author html = """