feat(api): add widget_name parameter to render_composite_adhoc function.

This commit is contained in:
shay
2026-08-22 16:33:24 -07:00
parent 319f37761c
commit 584306beb0
4 changed files with 155 additions and 8 deletions
+108 -1
View File
@@ -654,9 +654,10 @@ def test_query_override_triggers_adhoc_render_without_persisting(client):
captured = {}
def fake_adhoc(username, overrides):
def fake_adhoc(username, overrides, widget_name="composite"):
captured["username"] = username
captured["overrides"] = overrides
captured["widget_name"] = widget_name
return "<svg>adhoc</svg>"
with patch("src.worker.render_composite_adhoc", side_effect=fake_adhoc):
@@ -703,6 +704,112 @@ def test_query_override_ignores_unknown_params(client):
assert b"<svg>cached</svg>" in r.data
# ---- ?theme= on the single-widget routes and the compat shims --------------
# READMEs migrating off upstream github-readme-stats carry `&theme=` in the
# URL. Dropping it silently renders the profile in the wrong colors, which
# reads as the product being broken.
_THEME_PAYLOAD = {"user": {"login": "alice"}, "repos": [], "events": [],
"commits": [], "total_commits": 0, "recent_commits": 0,
"total_prs": 0, "collaborators_data": [], "avatar_b64": ""}
def test_named_widget_honors_theme_query_param(client):
"""`/api/<u>/grade.svg?theme=X` must render in X, not in the owner's
stored theme, and must not persist anything."""
dbmod.enroll("alice", {"theme": "midnight", "enabled": ["grade"]})
dbmod.put_widget_data("alice", "h1", {"some": "data"})
dbmod.put_widgets("alice", "h1", {"grade": "<svg>cached</svg>"})
dbmod.point_current_widget("alice", "h1")
with patch("src.worker.fetcher_client.get_data",
return_value={"data": _THEME_PAYLOAD, "payload_hash": "h"}):
paper = client.get("/api/alice/grade.svg?theme=paper")
onyx = client.get("/api/alice/grade.svg?theme=onyx")
assert paper.status_code == 200 and onyx.status_code == 200
assert paper.data != onyx.data
assert b"#f5f0eb" in paper.data # paper card_bg
assert b"#18181b" in onyx.data # onyx card_bg
assert paper.headers["X-Widget-Status"] == "ready"
assert paper.headers["Cache-Control"] == "public, max-age=300"
assert dbmod.get_settings("alice")["settings"]["theme"] == "midnight"
assert dbmod.get_current_widget("alice", "grade") == "<svg>cached</svg>"
def test_named_widget_without_theme_serves_cached_svg(client):
"""No overrides → unchanged fast path: cached SVG, hour-long cache."""
dbmod.enroll("alice", {"theme": "midnight"})
dbmod.put_widgets("alice", "h1", {"grade": "<svg>cached</svg>"})
dbmod.point_current_widget("alice", "h1")
with patch("src.worker.render_composite_adhoc") as adhoc:
r = client.get("/api/alice/grade.svg")
assert adhoc.call_count == 0
assert b"<svg>cached</svg>" in r.data
assert r.headers["Cache-Control"] == "public, max-age=3600"
def test_compat_shims_honor_theme_query_param(client):
"""Both upstream-shaped shims route through the same override path."""
dbmod.enroll("alice", {"theme": "midnight"})
dbmod.put_widgets("alice", "h1", {"languages": "<svg>langs</svg>",
"streaks": "<svg>streak</svg>"})
dbmod.point_current_widget("alice", "h1")
calls = []
def fake_adhoc(username, overrides, widget_name="composite"):
calls.append((username, overrides, widget_name))
return f"<svg>{widget_name}-{overrides['theme']}</svg>"
with patch("src.worker.render_composite_adhoc", side_effect=fake_adhoc):
langs = client.get("/api/top-langs?username=alice&theme=paper")
streak = client.get("/api/streak/alice?theme=onyx")
assert b"languages-paper" in langs.data
assert b"streaks-onyx" in streak.data
assert [c[2] for c in calls] == ["languages", "streaks"]
def test_username_shim_honors_theme_query_param(client, monkeypatch):
"""`/api?username=X&theme=Y` renders the grade widget per request, so the
override applies directly to the theme it renders with."""
monkeypatch.setattr(
"src.api.fetcher_client.get_data",
lambda u: {"data": _THEME_PAYLOAD, "payload_hash": "h"},
)
monkeypatch.setattr("src.api.render_grade_widget",
lambda grade, theme, **kw: f"<svg>{theme}</svg>")
dbmod.enroll("alice", {"theme": "midnight"})
assert b"<svg>paper</svg>" in client.get("/api?username=alice&theme=paper").data
assert b"<svg>onyx</svg>" in client.get("/?username=alice&theme=onyx").data
# Unknown theme falls back to the stored one rather than erroring.
assert b"<svg>midnight</svg>" in client.get("/api?username=alice&theme=bogus").data
def test_invalid_theme_falls_back_to_cached_widget(client):
"""An unknown or malformed theme is dropped by sanitize_settings_query:
no ad-hoc render, no injection, still a 200 with the default rendering."""
dbmod.enroll("alice", {"theme": "midnight"})
dbmod.put_widgets("alice", "h1", {"grade": "<svg>cached</svg>",
"languages": "<svg>langs</svg>"})
dbmod.point_current_widget("alice", "h1")
with patch("src.worker.render_composite_adhoc") as adhoc:
r = client.get("/api/alice/grade.svg?theme=<script>evil</script>")
shim = client.get("/api/top-langs?username=alice&theme=../../etc/passwd")
assert adhoc.call_count == 0
assert r.status_code == 200
assert b"<svg>cached</svg>" in r.data
assert b"evil" not in r.data
assert shim.status_code == 200
assert b"<svg>langs</svg>" in shim.data
def test_per_login_mutate_rate_limit(client, monkeypatch):
"""Per-login rate limit on mutate routes allows up to MAX requests,
then rejects with 429."""
+27
View File
@@ -202,3 +202,30 @@ def test_process_one_records_render_event(monkeypatch, tmp_path):
with dbmod.analytics_conn() as c:
rows = c.execute("SELECT username, widget, kind FROM events").fetchall()
assert ("alice", "composite", "render") in [(r["username"], r["widget"], r["kind"]) for r in rows]
def test_render_adhoc_named_widget_renders_only_that_widget(tmp_dbs):
"""A themed `/api/<u>/<widget>.svg` must not pay to render every other
widget just to throw them away — the override narrows `enabled`."""
dbmod.enroll("alice", {"theme": "midnight",
"enabled": ["grade", "languages", "streaks"]})
fake_payload = {"user": {"login": "alice"}, "repos": [], "events": [],
"commits": [], "total_commits": 0, "recent_commits": 0,
"total_prs": 0, "collaborators_data": [], "avatar_b64": ""}
captured = {}
def fake_generate(payload, **kw):
captured.update(kw)
return {"grade": "<svg>g</svg>"}
with patch("src.worker.fetcher_client.get_data",
return_value={"data": fake_payload, "payload_hash": "h"}), \
patch("src.worker.processor.generate_widgets_from_github",
side_effect=fake_generate):
svg = worker.render_composite_adhoc("alice", {"theme": "paper"}, "grade")
assert svg == "<svg>g</svg>"
assert captured["theme"] == "paper"
assert captured["enabled"] == ["grade"]
# Stored settings untouched by the ad-hoc render.
assert dbmod.get_settings("alice")["settings"]["theme"] == "midnight"