Sharing settings API, public profiles and ActivityPub docs (phase 1, step 4)
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 12s
CI / test (push) Successful in 1m5s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s

- /api/v1/federation/settings (GET, PATCH): sharing on/off, visibility
  (followers-only default), follow approval, discoverable/indexable,
  display name and bio, plus the read-only handle and URLs
- /api/v1/federation/profiles/<name>: public profile for the UI page,
  only for users sharing
- ActivityPub endpoints documented under an ActivityPub tag
- docs/openapi.json is now exported with every optional module on
  (python -m scrobbler.openapi.export); the drift test and route checks
  cover both a federation-off and a federation-on app
- Boundary: federation may use scrobbler.api.get_api to register its
  blueprint; the checker proves other scrobbler.api modules stay off
  limits

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Opus 5.5 committed 2026-09-25 20:26:33 +01:00
1 parent ce39e8f865
commit 0b10469514
11 files changed
+1175 -11

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
import pytest
@pytest.fixture
def fed_auth(fed_ctx, user):
from scrobbler.services.accounts import issue_ui_token
return {"Authorization": f"Bearer {issue_ui_token(user)}"}
def test_settings_start_private(fed_client, fed_auth):
body = fed_client.get("/api/v1/federation/settings", headers=fed_auth).get_json()
assert body["enabled"] is False
assert body["visibility"] == "followers"
assert (body["discoverable"], body["indexable"], body["manually_approves_followers"]) == (
False,
False,
False,
)
assert body["handle"] == "@alice@scrobble.test"
assert body["actor_url"] == "https://scrobble.test/users/alice"
assert body["profile_url"] == "http://localhost:8080/profile.html?u=alice"
def test_enable_and_change_settings(fed_client, fed_auth, metric_delta):
enabled = metric_delta("scrobbler_federation_sharing_changes_total", change="enabled")
response = fed_client.patch(
"/api/v1/federation/settings",
json={"enabled": True, "visibility": "unlisted", "display_name": "Alice", "bio": "Hi"},
headers=fed_auth,
)
assert response.status_code == 200
body = response.get_json()
assert (body["enabled"], body["visibility"], body["display_name"]) == (
True,
"unlisted",
"Alice",
)
assert enabled.delta == 1
# Now findable
doc = fed_client.get("/users/alice", headers={"Accept": "application/activity+json"}).get_json()
assert doc["name"] == "Alice"
# A partial update leaves the rest alone
body = fed_client.patch(
"/api/v1/federation/settings", json={"bio": None}, headers=fed_auth
).get_json()
assert (body["visibility"], body["bio"]) == ("unlisted", None)
@pytest.mark.parametrize(
"changes",
[
{"visibility": "everyone"},
{"handle": "@x@y"},
{"display_name": "x" * 101},
{"enabled": "maybe"},
],
)
def test_invalid_settings_are_rejected(fed_client, fed_auth, changes):
response = fed_client.patch("/api/v1/federation/settings", json=changes, headers=fed_auth)
assert response.status_code == 422
def test_settings_need_auth(fed_client):
assert fed_client.get("/api/v1/federation/settings").status_code == 401
def test_public_profile(fed_client, sharing_user, make_user):
body = fed_client.get("/api/v1/federation/profiles/Alice").get_json()
assert body == {
"username": "alice",
"display_name": "Alice",
"bio": "Listening.",
"handle": "@alice@scrobble.test",
"actor_url": "https://scrobble.test/users/alice",
}
make_user(username="bob")
assert fed_client.get("/api/v1/federation/profiles/bob").status_code == 404
def test_disabled_server_has_no_federation_api(client, auth):
assert client.get("/api/v1/federation/settings", headers=auth).status_code == 404
+5 -1
View File
@@ -36,6 +36,7 @@ FRAMEWORKS = (
)
CORE_PUBLIC_SURFACE = (
"scrobbler.__version__",
"scrobbler.api.get_api",
"scrobbler.events",
"scrobbler.extensions",
"scrobbler.worker",
@@ -85,7 +86,8 @@ def violations(module: str, imports: set[str]) -> list[str]:
problems.append(f"{module} (protocol) imports {name}")
elif within(module, FEDERATION):
for name in imports:
if not within(name, "scrobbler") or name == "scrobbler":
# The packages themselves (for their public names listed above) are fine.
if not within(name, "scrobbler") or name in ("scrobbler", "scrobbler.api"):
continue
if within(name, FEDERATION) or any(within(name, ok) for ok in CORE_PUBLIC_SURFACE):
continue
@@ -126,6 +128,7 @@ def test_federation_package_is_checked():
(PROTOCOL + ".vocab", "from ..config import FederationConfig", "imports"),
(FEDERATION + ".inbox", "from scrobbler.models import User", "core internals"),
(FEDERATION + ".inbox", "from scrobbler import metrics", "core internals"),
(FEDERATION + ".api", "from scrobbler.api import apps", "core internals"),
],
)
def test_the_checker_catches_violations(module, source, expected):
@@ -143,6 +146,7 @@ def test_the_checker_catches_violations(module, source, expected):
"from scrobbler.services import stats\nfrom scrobbler import events",
),
(FEDERATION + ".inbox", "from .protocol import vocab\nfrom flask import Blueprint"),
(FEDERATION, "from scrobbler.api import get_api"),
(PROTOCOL + ".signatures.rfc9421", "from ..vocab import note\nimport hashlib"),
],
)
+38 -7
View File
@@ -8,7 +8,17 @@ from openapi_spec_validator import validate
from scrobbler.lastfm.registry import METHODS
COMMITTED_SPEC = Path(__file__).resolve().parents[1] / "docs" / "openapi.json"
DOCUMENTED_PREFIXES = ("/api/v1/", "/2.0/", "/api/auth/", "/healthz")
DOCUMENTED_PREFIXES = (
"/api/v1/",
"/2.0/",
"/api/auth/",
"/healthz",
"/.well-known/",
"/nodeinfo/",
"/users/",
"/inbox",
"/actor",
)
HTTP_VERBS = {"get", "put", "post", "delete", "patch", "head", "options"}
@@ -25,7 +35,21 @@ def test_spec_is_valid_openapi(spec):
validate(spec)
def test_every_api_route_is_documented(app, spec):
@pytest.fixture(scope="module")
def fed_spec(fed_app):
return fed_app.test_client().get("/api/openapi.json").get_json()
def test_every_api_route_is_documented(app, spec, fed_app, fed_spec):
_check_documented(app, spec)
_check_documented(fed_app, fed_spec) # with federation's routes too
def test_federation_off_documents_no_federation_paths(spec):
assert not [p for p in spec["paths"] if p.startswith(("/api/v1/federation", "/users/"))]
def _check_documented(app, spec):
for rule in app.url_map.iter_rules():
if not rule.rule.startswith(DOCUMENTED_PREFIXES):
continue
@@ -47,8 +71,13 @@ def test_every_lastfm_method_is_documented(spec):
assert len(request_schemas) == len(METHODS)
def test_ui_routes_declare_bearer_auth(spec):
public = {("/api/v1/auth/login", "post"), ("/api/v1/auth/register", "post")}
def test_ui_routes_declare_bearer_auth(fed_spec):
spec = fed_spec # includes the federation API
public = {
("/api/v1/auth/login", "post"),
("/api/v1/auth/register", "post"),
("/api/v1/federation/profiles/{username}", "get"),
}
for path, operations in spec["paths"].items():
if not path.startswith("/api/v1/"):
continue
@@ -67,9 +96,11 @@ def test_docs_pages_are_served(client):
assert client.get("/api/redoc").status_code == 200
def test_committed_spec_is_up_to_date(spec):
def test_committed_spec_is_up_to_date():
from scrobbler.openapi.export import build_spec
committed = json.loads(COMMITTED_SPEC.read_text())
assert committed == spec, (
assert committed == build_spec(), (
"docs/openapi.json is stale. Regenerate it with: "
"uv run flask --app scrobbler openapi write --format=json docs/openapi.json"
"uv run python -m scrobbler.openapi.export docs/openapi.json"
)