""" 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 time from unittest.mock import MagicMock, patch from starpunk.webmention_worker import WebmentionWorker 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() page = MagicMock(status_code=200, url="https://example.org/post", 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.gethostbyname", return_value="93.184.216.34"), \ patch("starpunk.webmention_worker.httpx.get", return_value=page), \ 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() 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)