Files
Scrobbler/tests/test_federation_web.py
T
mattandClaude Opus 5.5 f312c4b577
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 14s
CI / test (push) Successful in 1m57s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
Inbox: intake and verified processing of follows (phase 2, step 4)
Intake (in the request, no network): size, content type and shape
checks, blocked domains refused (403), duplicates acknowledged once,
then stored and answered 202.

Processing (federation.inbox worker task):
- verify the HTTP signature with either scheme, as of when it arrived,
  using the cached key and refreshing the sender once on failure (key
  rotation); a deleted account's Delete still verifies from cache
- the signer must be the activity's actor and the activity id must be
  on the actor's host
- handlers: Follow (accept, or pending with manual approval; repeat
  follows re-accepted; blocked accounts rejected), Undo of a Follow
  (only by the same actor), Delete of an actor, Update of an actor;
  anything else recorded
- items being processed are marked, retried on transient failures,
  re-queued if a worker dies, and pruned after 30 days

Outgoing Accept/Reject/Block activities are stored with stable ids and
queued per inbox for the delivery task (next step). Public activities
are fetchable at /activities/<uuid>. Metrics for inbox activities and
signature checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-25 21:29:55 +01:00

116 lines
4.6 KiB
Python

import xml.etree.ElementTree as ET
from urllib.parse import urlsplit
import pytest
AP = {"Accept": "application/activity+json"}
BASE = "https://scrobble.test"
def webfinger(client, resource):
return client.get("/.well-known/webfinger", query_string={"resource": resource})
def test_webfinger_finds_sharing_users(fed_client, sharing_user, metric_delta):
found = metric_delta("scrobbler_federation_lookups_total", kind="webfinger", result="found")
response = webfinger(fed_client, "acct:ALICE@scrobble.test")
assert response.status_code == 200
assert response.content_type == "application/jrd+json"
assert response.headers["Access-Control-Allow-Origin"] == "*"
doc = response.get_json()
assert doc["subject"] == "acct:alice@scrobble.test"
assert {
"rel": "self",
"type": "application/activity+json",
"href": f"{BASE}/users/alice",
} in doc["links"]
assert found.delta == 1
def test_unknown_and_private_users_look_identical(fed_client, fed_ctx, make_user):
make_user(username="bob") # exists, never enabled sharing
unknown = webfinger(fed_client, "acct:nobody@scrobble.test")
private = webfinger(fed_client, "acct:bob@scrobble.test")
assert unknown.status_code == private.status_code == 404
assert unknown.data == private.data
ap_unknown = fed_client.get("/users/nobody", headers=AP)
ap_private = fed_client.get("/users/bob", headers=AP)
assert ap_unknown.status_code == ap_private.status_code == 404
assert ap_unknown.data == ap_private.data
def test_webfinger_rejects_other_domains(fed_client, sharing_user):
assert webfinger(fed_client, "acct:alice@elsewhere.example").status_code == 400
assert fed_client.get("/.well-known/webfinger").status_code == 400
def test_webfinger_instance_actor(fed_client, fed_ctx):
doc = webfinger(fed_client, "acct:scrobble.test@scrobble.test").get_json()
assert doc["links"][0]["href"] == f"{BASE}/actor"
def test_actor_document(fed_client, sharing_user):
response = fed_client.get("/users/alice", headers=AP)
assert response.status_code == 200
assert response.content_type == "application/activity+json"
assert response.headers["Vary"] == "Accept"
doc = response.get_json()
assert doc["id"] == f"{BASE}/users/alice"
assert (doc["type"], doc["preferredUsername"], doc["name"]) == ("Person", "alice", "Alice")
assert doc["summary"] == "<p>Listening.</p>"
assert doc["publicKey"]["id"] == f"{BASE}/users/alice#main-key"
assert doc["publicKey"]["publicKeyPem"].startswith("-----BEGIN PUBLIC KEY-----")
assert doc["manuallyApprovesFollowers"] is True # follows are requests until phase 2
assert (doc["discoverable"], doc["indexable"]) == (False, False)
assert doc["url"] == "http://localhost:8080/profile.html?u=alice"
def test_ids_come_from_config_not_the_request(fed_client, sharing_user):
doc = fed_client.get("/users/alice", headers={**AP, "Host": "evil.example"}).get_json()
assert urlsplit(doc["id"]).netloc == "scrobble.test"
@pytest.mark.parametrize("username", ["alice", "nobody"])
def test_browsers_are_sent_to_the_profile_page(fed_client, sharing_user, username):
response = fed_client.get(f"/users/{username}", headers={"Accept": "text/html"})
assert response.status_code == 302
assert response.headers["Location"] == f"http://localhost:8080/profile.html?u={username}"
def test_collections(fed_client, sharing_user):
outbox = fed_client.get("/users/alice/outbox", headers=AP).get_json()
assert (outbox["type"], outbox["totalItems"], outbox["orderedItems"]) == (
"OrderedCollection",
0,
[],
)
followers = fed_client.get("/users/alice/followers", headers=AP).get_json()
assert followers["totalItems"] == 0 and "orderedItems" not in followers
def test_nodeinfo(fed_client, sharing_user):
link = fed_client.get("/.well-known/nodeinfo").get_json()["links"][0]
assert link["href"] == f"{BASE}/nodeinfo/2.1"
doc = fed_client.get("/nodeinfo/2.1").get_json()
assert doc["software"]["name"] == "scrobbler"
assert doc["usage"]["users"]["total"] == 1
def test_host_meta(fed_client):
root = ET.fromstring(fed_client.get("/.well-known/host-meta").data)
assert "webfinger" in root[0].get("template")
def test_instance_actor(fed_client, fed_ctx):
doc = fed_client.get("/actor", headers=AP).get_json()
assert (doc["type"], doc["id"]) == ("Application", f"{BASE}/actor")
def test_disabled_app_has_none_of_these_routes(client):
for path in (
"/.well-known/webfinger?resource=acct:alice@scrobble.test",
"/users/alice",
"/nodeinfo/2.1",
):
assert client.get(path, headers=AP).status_code == 404