fix(api): normalize GitHub usernames to lowercase and update tests.

This commit is contained in:
shay
2026-07-19 21:01:40 -07:00
parent 0586f83a33
commit fe4bb4e87e
4 changed files with 62 additions and 0 deletions
+4
View File
@@ -11,6 +11,10 @@ from flask_compress import Compress
from . import analytics, cache as cache_mod, config from . import analytics, cache as cache_mod, config
app = Flask(__name__) app = Flask(__name__)
# Tolerate stray trailing slashes on every route, matching the generator. A
# hand-copied embed URL like `/alice/` should resolve like `/alice` instead of
# Flask's default strict_slashes 404. Set before the route decorators run.
app.url_map.strict_slashes = False
Compress(app) Compress(app)
cache_ext = cache_mod.build_cache(app) cache_ext = cache_mod.build_cache(app)
log = logging.getLogger("edge") log = logging.getLogger("edge")
+11
View File
@@ -44,6 +44,17 @@ def test_origin_error_returns_503(client):
assert r.status_code == 503 assert r.status_code == 503
@responses.activate
def test_trailing_slash_tolerated(client):
"""A hand-copied `/alice/` must resolve like `/alice`, not 404."""
responses.add(responses.GET, "http://gen:5002/api/alice",
body=b"<svg>ready</svg>", status=200,
headers={"X-Widget-Status": "ready", "Content-Type": "image/svg+xml"})
r = client.get("/alice/")
assert r.status_code == 200
assert r.data == b"<svg>ready</svg>"
def test_health(client): def test_health(client):
r = client.get("/health") r = client.get("/health")
assert r.status_code == 200 assert r.status_code == 200
+8
View File
@@ -208,6 +208,14 @@ _STATIC_DIR = os.path.join(os.path.dirname(__file__), "static")
app = Flask(__name__, static_folder=None) app = Flask(__name__, static_folder=None)
# Tolerate stray trailing slashes on every route. README embeds are copied by
# hand from all over — upstream github-readme-stats even prints its canonical
# URL as `/api/top-langs/?username=X` (note the '/' before '?'). Flask's
# default strict_slashes turns any such URL into a hard 404 with no redirect,
# so a single misplaced slash silently breaks a user's widget. Set on the map
# before the route decorators run so every rule inherits the lenient default.
app.url_map.strict_slashes = False
if config.SECRET_KEY: if config.SECRET_KEY:
app.secret_key = config.SECRET_KEY app.secret_key = config.SECRET_KEY
else: else:
+39
View File
@@ -480,6 +480,45 @@ def test_top_langs_ignores_upstream_specific_query_params(client):
assert b"langs" in r.data assert b"langs" in r.data
# ---- Trailing-slash tolerance -----------------------------------------------
# Flask's default strict_slashes turns `/api/top-langs/?username=X` — the exact
# shape upstream github-readme-stats prints (note the '/' before '?') — into a
# hard 404 with no redirect. Every /api route had the same latent bug: any
# copied URL carrying a stray trailing slash 404'd. These pin the lenient
# behavior so both forms resolve.
def test_top_langs_trailing_slash_before_query(client):
"""Upstream's canonical URL is `/api/top-langs/?username=X`. The trailing
slash must not 404 — it's the single most common broken embed."""
dbmod.enroll("alice", {"theme": "dark"})
dbmod.put_widgets("alice", "h1", {"languages": "<svg>langs</svg>"})
dbmod.point_current_widget("alice", "h1")
r = client.get("/api/top-langs/?username=alice")
assert r.status_code == 200
assert b"langs" in r.data
def test_widget_first_trailing_slash(client):
"""`/api/streak/alice/` must resolve to the same widget as `/api/streak/alice`."""
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 b"streaksvg" in r.data
def test_composite_trailing_slash(client):
"""`/api/alice/` must resolve like `/api/alice`."""
dbmod.enroll("alice", {"theme": "dark"})
dbmod.put_widgets("alice", "h1", {"composite": "<svg>compositesvg</svg>"})
dbmod.point_current_widget("alice", "h1")
r = client.get("/api/alice/")
assert r.status_code == 200
assert b"compositesvg" in r.data
def test_enroll_endpoint_is_gone(client): def test_enroll_endpoint_is_gone(client):
r = client.post("/api/enroll", json={"username": "alice"}, r = client.post("/api/enroll", json={"username": "alice"},
headers={"Origin": "https://gh-stats.com"}) headers={"Origin": "https://gh-stats.com"})