mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
fix(api): normalize GitHub usernames to lowercase and update tests.
This commit is contained in:
@@ -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/<widget>/<u>`). Lets people paste e.g. `/api/streak/<u>` 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/<widget>/<username>", 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/<widget>/<username>`.
|
||||
|
||||
The native shape is `/api/<u>/<widget>.svg`, but people copy the
|
||||
reversed `/api/streak/<u>` 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/<u>/<widget>.svg` rule and the literal `/api/<u>/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")
|
||||
|
||||
@@ -147,6 +147,67 @@ def test_settings_get_is_case_insensitive(client):
|
||||
assert r.get_json()["settings"]["theme"] == "dark"
|
||||
|
||||
|
||||
# ---- widget-first compat format: /api/<widget>/<username> ----------------
|
||||
# Reversed shape (`/api/streak/<u>`) 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": "<svg>streaksvg</svg>"})
|
||||
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": "<svg>gradesvg</svg>"})
|
||||
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/<u>/<widget>.svg` route must still win for .svg URLs."""
|
||||
dbmod.enroll("alice", {"theme": "dark"})
|
||||
dbmod.put_widgets("alice", "h1", {"grade": "<svg>namedsvg</svg>"})
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user