From d1e89fb1c3d062aad4991fabfc4897377e9bcdd3 Mon Sep 17 00:00:00 2001 From: shay Date: Fri, 12 Jun 2026 01:37:08 -0700 Subject: [PATCH] fix(api): normalize GitHub usernames to lowercase and update tests. --- generator/src/api.py | 36 ++++++++++++++++++++++ generator/tests/test_api.py | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/generator/src/api.py b/generator/src/api.py index 8db437b..48fa8cd 100644 --- a/generator/src/api.py +++ b/generator/src/api.py @@ -237,6 +237,17 @@ app.config["MAX_CONTENT_LENGTH"] = 128 * 1024 # We normalize to an explicit allow-list of fields and coerce/trim types. _ALLOWED_WIDGETS = {"name", "grade", "impact", "streaks", "collaborators", "focus", "languages", "achievements"} +# Singular / other-service aliases accepted by the widget-first compat route +# (`/api//`). Lets people paste e.g. `/api/streak/` straight from +# a streak-stats badge URL and still hit this fork's `streaks` widget. +_WIDGET_ALIASES = { + "streak": "streaks", + "lang": "languages", + "langs": "languages", + "top-langs": "languages", + "achievement": "achievements", + "collaborator": "collaborators", +} _ALLOWED_ICONS = {"trophy", "medal", "star", "hackathon"} _MAX_ACHIEVEMENTS = 10 _TITLE_MAX = 80 @@ -696,6 +707,31 @@ def get_widget_named(username: str, widget: str): return _serve(username, widget_name=widget) +@app.route("/api//", methods=["GET"]) +@track_request(lambda kw: _WIDGET_ALIASES.get(kw.get("widget"), kw.get("widget"))) +@rate_limited("read") +def get_widget_first(widget: str, username: str): + """Widget-first compat format: `/api//`. + + The native shape is `/api//.svg`, but people copy the + reversed `/api/streak/` form out of other badge services' READMEs. + Map the (aliased) widget name onto the same auto-enrolling `_serve` + path so a first hit for a never-seen user queues a build instead of + falling through to the catch-all 404. + + Routing precedence: Flask matches the static-suffixed + `/api//.svg` rule and the literal `/api//data` (and + /settings, /generate, /refresh) rules ahead of this fully-dynamic one, + so it only claims the two-segment space that previously 404'd. + """ + if not is_valid_username(username): + return jsonify({"error": "invalid_username"}), 400 + name = _WIDGET_ALIASES.get(widget, widget) + if name not in _ALLOWED_WIDGETS and name != "composite": + return jsonify({"error": "unknown_widget"}), 400 + return _serve(username, widget_name=name) + + @app.route("/api/top-langs", methods=["GET"]) @track_request("languages") @rate_limited("read") diff --git a/generator/tests/test_api.py b/generator/tests/test_api.py index 025b51e..ed6ee69 100644 --- a/generator/tests/test_api.py +++ b/generator/tests/test_api.py @@ -147,6 +147,67 @@ def test_settings_get_is_case_insensitive(client): assert r.get_json()["settings"]["theme"] == "dark" +# ---- widget-first compat format: /api// ---------------- +# Reversed shape (`/api/streak/`) that people copy from other badge +# services. Must resolve to the named widget and auto-enroll/build on miss. + + +def test_widget_first_streak_alias_serves_streaks_widget(client): + dbmod.enroll("alice", {"theme": "dark"}) + dbmod.put_widgets("alice", "h1", {"streaks": "streaksvg"}) + dbmod.point_current_widget("alice", "h1") + r = client.get("/api/streak/alice") + assert r.status_code == 200 + assert r.headers["X-Widget-Status"] == "ready" + assert b"streaksvg" in r.data + + +def test_widget_first_canonical_name_serves_widget(client): + dbmod.enroll("alice", {"theme": "dark"}) + dbmod.put_widgets("alice", "h1", {"grade": "gradesvg"}) + dbmod.point_current_widget("alice", "h1") + r = client.get("/api/grade/alice") + assert r.status_code == 200 + assert b"gradesvg" in r.data + + +def test_widget_first_unknown_user_auto_enrolls_and_builds(client): + """The whole point: a never-seen user in widget-first form must queue a + build instead of returning 404.""" + r = client.get("/api/streak/newcomer") + assert r.status_code == 200 + assert r.headers["X-Widget-Status"] == "building" + assert dbmod.get_settings("newcomer") is not None + + +def test_widget_first_is_case_insensitive(client): + r = client.get("/api/streak/Newcomer") + assert r.status_code == 200 + assert dbmod.get_settings("newcomer") is not None + + +def test_widget_first_unknown_widget_rejected(client): + r = client.get("/api/boguswidget/alice") + assert r.status_code == 400 + assert r.get_json()["error"] == "unknown_widget" + + +def test_widget_first_does_not_shadow_named_svg_route(client): + """The native `/api//.svg` route must still win for .svg URLs.""" + dbmod.enroll("alice", {"theme": "dark"}) + dbmod.put_widgets("alice", "h1", {"grade": "namedsvg"}) + dbmod.point_current_widget("alice", "h1") + r = client.get("/api/alice/grade.svg") + assert r.status_code == 200 + assert b"namedsvg" in r.data + + +def test_widget_first_does_not_shadow_data_route(client): + dbmod.enroll("alice", {"theme": "dark"}) + r = client.get("/api/alice/data") + assert r.is_json # data route returns JSON status, not an SVG + + def test_settings_patch_enqueues_rebuild(client): dbmod.enroll("alice", {"theme": "dark"}) with client.session_transaction() as s: