""" Micropub compliance fixes (v2.9.0) Section numbers refer to the W3C Micropub Recommendation. """ import io import sqlite3 import httpx import pytest from tests.test_micropub_v240 import SITE, app, auth, client, create, h, jpeg, note_at, source # noqa: F401 def syndications(app, url): note = note_at(app, url) db = sqlite3.connect(app.config["DATABASE_PATH"]) return sorted(r[0] for r in db.execute("SELECT target FROM syndications WHERE note_id = ?", (note.id,))) @pytest.fixture def targets(app): app.config.update(BLUESKY_HANDLE="me.example", BLUESKY_APP_PASSWORD="p") from starpunk import syndication syndication._client_cache.clear() with app.app_context(): return {t.uid: name for name, t in syndication.get_targets().items()} # Crossposting: mp-* in JSON properties; absent means none def test_mp_syndicate_to_inside_json_properties(app, client, h, targets): bsky_uid = next(uid for uid, name in targets.items() if name == "bluesky") url = create(client, h, **{"mp-syndicate-to": [bsky_uid]}) assert syndications(app, url) == ["bluesky"] @pytest.mark.parametrize("extra", [{}, {"mp-syndicate-to": []}]) def test_no_choice_means_no_crossposting(app, client, h, targets, extra): url = create(client, h, **extra) assert syndications(app, url) == [] def test_form_mp_syndicate_to(app, client, h, targets): bsky_uid = next(uid for uid, name in targets.items() if name == "bluesky") response = client.post("/micropub", data={"content": "x", "mp-syndicate-to[]": bsky_uid}, headers=h) assert syndications(app, response.headers["Location"]) == ["bluesky"] def test_mp_slug_inside_json_properties(app, client, h): url = create(client, h, **{"mp-slug": ["my-slug"]}) assert url.endswith("/note/my-slug") assert "mp-slug" not in source(client, h, url)["properties"] # Scopes def test_source_needs_a_posting_scope(app, client, h): create(client, h, **{"post-status": ["draft"]}) profile_only = auth(app, "profile") assert client.get("/micropub?q=source", headers=profile_only).status_code == 403 assert client.get("/micropub?q=category", headers=profile_only).status_code == 403 assert client.get("/micropub?q=config", headers=profile_only).status_code == 200 def test_draft_scope_only_creates_drafts(app, client): url = create(client, auth(app, "draft")) assert note_at(app, url).published is False # Visibility @pytest.mark.parametrize("visibility,published", [("public", True), ("unlisted", False), ("private", False)]) def test_visibility(app, client, h, visibility, published): url = create(client, h, visibility=[visibility]) assert note_at(app, url).published is published def test_unknown_visibility_rejected(client, h): body = {"type": ["h-entry"], "properties": {"content": ["x"], "visibility": ["friends"]}} assert client.post("/micropub", json=body, headers=h).status_code == 400 # Photos that can't be used fail the request def test_unreachable_photo_is_an_error(app, client, h, monkeypatch): monkeypatch.setattr("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(lambda r: httpx.Response(404))) monkeypatch.setattr("starpunk.webmention_ssrf.socket.getaddrinfo", lambda *a, **k: [(2, 1, 6, "", ("93.184.216.34", 0))]) body = {"type": ["h-entry"], "properties": {"content": ["x"], "photo": ["https://p.example/404.jpg"]}} assert client.post("/micropub", json=body, headers=h).status_code == 400 assert client.get("/micropub?q=source", headers=h).get_json()["items"] == [] def test_unknown_local_media_is_an_error_on_update(app, client, h): url = create(client, h) response = client.post("/micropub", json={"action": "update", "url": url, "replace": {"photo": [f"{SITE}/media/2026/11/nope.jpg"]}}, headers=h) assert response.status_code == 400 def test_redirect_to_private_address_is_not_fetched(app, client, h, monkeypatch): def resolve(host, *a, **k): return [(2, 1, 6, "", ("10.0.0.5" if host == "internal.example" else "93.184.216.34", 0))] def site(request): return httpx.Response(302, headers={"Location": "http://internal.example/secret.jpg"}) monkeypatch.setattr("starpunk.webmention_ssrf.socket.getaddrinfo", resolve) monkeypatch.setattr("starpunk.webmention_ssrf.TRANSPORT", httpx.MockTransport(site)) body = {"type": ["h-entry"], "properties": {"content": ["x"], "photo": ["https://p.example/a.jpg"]}} assert client.post("/micropub", json=body, headers=h).status_code == 400 # JSON values must be arrays (3.3.2) def test_scalar_json_values_are_not_split(app, client, h): body = {"type": ["h-entry"], "properties": {"content": "whole text", "category": "solo", "name": "Title"}} response = client.post("/micropub", json=body, headers=h) props = source(client, h, response.headers["Location"])["properties"] assert props["content"] == ["whole text"] assert props["category"] == ["solo"] assert props["name"] == ["Title"] def test_update_delete_values_must_be_arrays(app, client, h): url = create(client, h, category=["a", "b"]) response = client.post("/micropub", json={"action": "update", "url": url, "delete": {"category": "a"}}, headers=h) assert response.status_code == 400 def test_update_needs_an_operation(app, client, h): url = create(client, h) assert client.post("/micropub", json={"action": "update", "url": url}, headers=h).status_code == 400 def test_person_tag_categories_are_ignored(app, client, h): url = create(client, h, category=["kept", {"type": ["h-card"], "properties": {"url": ["https://a.example/"]}}]) assert source(client, h, url)["properties"]["category"] == ["kept"] # Properties that used to be dropped def test_bookmark_of(app, client, h): body = {"type": ["h-entry"], "properties": {"bookmark-of": ["https://read.example/post"]}} url = client.post("/micropub", json=body, headers=h).headers["Location"] note = note_at(app, url) assert note.kind == "bookmark" and note.bookmark_of == "https://read.example/post" props = source(client, h, url)["properties"] assert props["bookmark-of"] == ["https://read.example/post"] and "content" not in props page = client.get(url.replace(SITE, "")).data.decode() assert 'class="u-bookmark-of" href="https://read.example/post"' in page def test_summary_or_name_without_content(app, client, h): response = client.post("/micropub", json={"type": ["h-entry"], "properties": {"summary": ["Just a summary"]}}, headers=h) assert source(client, h, response.headers["Location"])["properties"]["content"] == ["Just a summary"] response = client.post("/micropub", json={"type": ["h-entry"], "properties": {"name": ["Only a title"]}}, headers=h) assert source(client, h, response.headers["Location"])["properties"]["name"] == ["Only a title"] @pytest.mark.parametrize("prop", ["video", "audio"]) def test_unsupported_media_is_refused(client, h, prop): body = {"type": ["h-entry"], "properties": {"content": ["x"], prop: ["https://m.example/clip"]}} assert client.post("/micropub", json=body, headers=h).status_code == 400 upload = {"content": "x", prop: (io.BytesIO(b"data"), "clip.mp4")} assert client.post("/micropub", data=upload, headers=h, content_type="multipart/form-data").status_code == 400 # HTML content (3.7.2) and sanitizing def test_html_content_comes_back_as_html(app, client, h): url = create(client, h, content=[{"html": "

Some bold text

"}]) assert source(client, h, url)["properties"]["content"] == [{"html": "

Some bold text

"}] url = create(client, h, content=["plain *markdown*"]) assert source(client, h, url)["properties"]["content"] == ["plain *markdown*"] def test_update_with_html_content(app, client, h): url = create(client, h) client.post("/micropub", json={"action": "update", "url": url, "replace": {"content": [{"html": "

new

"}]}}, headers=h) assert source(client, h, url)["properties"]["content"] == [{"html": "

new

"}] def test_html_is_sanitized_on_the_page(app, client, h): url = create(client, h, content=[{"html": '

hi' 'j

'}]) page = client.get(url.replace(SITE, "")).data.decode() for bad in ("