mirror of
https://github.com/ShayManor/github-readme-stats.git
synced 2026-12-02 11:53:48 +05:30
db: enroll() accepts github profile; no longer returns edit_token
This commit is contained in:
+19
-17
@@ -152,29 +152,34 @@ def settings_hash(settings: dict) -> str:
|
|||||||
|
|
||||||
# ---- settings / enrollment ----
|
# ---- settings / enrollment ----
|
||||||
|
|
||||||
def enroll(username: str, defaults: dict) -> dict:
|
def enroll(username: str, defaults: dict,
|
||||||
"""Insert a settings row + increment daily counter + enqueue build.
|
github_id: int | None = None,
|
||||||
|
github_avatar_url: str | None = None) -> dict:
|
||||||
|
"""Insert a settings row if missing, bump the daily counter, enqueue a build.
|
||||||
|
|
||||||
Returns {"job_id", "edit_token"}. The edit_token is the bearer token the
|
Idempotent: calling again for an existing user refreshes github_avatar_url
|
||||||
caller must present on future settings mutations for this user. We only
|
(so login keeps the avatar fresh) and enqueues a rebuild. Always returns
|
||||||
store its SHA-256 hash, so the plaintext here is the one and only chance
|
{"job_id": int}. No token is returned — auth is handled by the signed
|
||||||
to surface it to the client. Subsequent enroll() calls for an existing
|
session cookie.
|
||||||
user return `edit_token=None` to avoid leaking tokens issued to earlier
|
|
||||||
registrants — settings cannot be taken over by re-enrolling.
|
|
||||||
"""
|
"""
|
||||||
sh = settings_hash(defaults)
|
sh = settings_hash(defaults)
|
||||||
now = _now()
|
now = _now()
|
||||||
token = secrets.token_urlsafe(32)
|
|
||||||
token_hash = _hash_edit_token(token)
|
|
||||||
with _settings_conn() as c:
|
with _settings_conn() as c:
|
||||||
cur_ins = c.execute(
|
cur_ins = c.execute(
|
||||||
"""INSERT INTO users(username, settings_json, settings_hash, enrolled_at,
|
"""INSERT INTO users(username, settings_json, settings_hash, enrolled_at,
|
||||||
last_requested_at, edit_token_hash)
|
last_requested_at, github_id, github_avatar_url)
|
||||||
VALUES (?, ?, ?, ?, ?, ?)
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||||
ON CONFLICT(username) DO NOTHING""",
|
ON CONFLICT(username) DO NOTHING""",
|
||||||
(username, json.dumps(defaults), sh, now, now, token_hash),
|
(username, json.dumps(defaults), sh, now, now, github_id, github_avatar_url),
|
||||||
)
|
)
|
||||||
inserted = cur_ins.rowcount == 1
|
inserted = cur_ins.rowcount == 1
|
||||||
|
if not inserted and (github_id is not None or github_avatar_url is not None):
|
||||||
|
c.execute(
|
||||||
|
"UPDATE users SET github_id=COALESCE(?, github_id), "
|
||||||
|
"github_avatar_url=COALESCE(?, github_avatar_url) WHERE username=?",
|
||||||
|
(github_id, github_avatar_url, username),
|
||||||
|
)
|
||||||
|
if inserted:
|
||||||
c.execute(
|
c.execute(
|
||||||
"""INSERT INTO enrollments_daily(day, count) VALUES (?, 1)
|
"""INSERT INTO enrollments_daily(day, count) VALUES (?, 1)
|
||||||
ON CONFLICT(day) DO UPDATE SET count = count + 1""",
|
ON CONFLICT(day) DO UPDATE SET count = count + 1""",
|
||||||
@@ -186,10 +191,7 @@ def enroll(username: str, defaults: dict) -> dict:
|
|||||||
(username, now, now),
|
(username, now, now),
|
||||||
)
|
)
|
||||||
c.commit()
|
c.commit()
|
||||||
return {
|
return {"job_id": cur.lastrowid}
|
||||||
"job_id": cur.lastrowid,
|
|
||||||
"edit_token": token if inserted else None,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def enrollments_today() -> int:
|
def enrollments_today() -> int:
|
||||||
|
|||||||
+14
-13
@@ -19,23 +19,24 @@ def test_settings_hash_is_deterministic():
|
|||||||
assert h1 == h2
|
assert h1 == h2
|
||||||
|
|
||||||
|
|
||||||
def test_enroll_and_get_settings(tmp_dbs):
|
def test_enroll_returns_job_id(tmp_dbs):
|
||||||
defaults = {"theme": "dark", "widgets": ["grade"]}
|
r = dbmod.enroll("alice", {"theme": "dark"})
|
||||||
result = dbmod.enroll("alice", defaults)
|
assert r["job_id"] > 0
|
||||||
assert result["job_id"] > 0
|
assert "edit_token" not in r
|
||||||
assert result["edit_token"] # first enrollment surfaces a token
|
|
||||||
s = dbmod.get_settings("alice")
|
|
||||||
assert s["settings"] == defaults
|
|
||||||
assert s["manual_refresh_used"] == 0
|
|
||||||
|
|
||||||
|
|
||||||
def test_enroll_twice_does_not_reissue_token(tmp_dbs):
|
def test_enroll_twice_noops(tmp_dbs):
|
||||||
r1 = dbmod.enroll("alice", {"theme": "dark"})
|
r1 = dbmod.enroll("alice", {"theme": "dark"})
|
||||||
r2 = dbmod.enroll("alice", {"theme": "dark"})
|
r2 = dbmod.enroll("alice", {"theme": "dark"})
|
||||||
assert r1["edit_token"]
|
assert r1["job_id"] > 0 and r2["job_id"] > 0
|
||||||
assert r2["edit_token"] is None
|
|
||||||
assert dbmod.verify_edit_token("alice", r1["edit_token"]) is True
|
|
||||||
assert dbmod.verify_edit_token("alice", "wrong") is False
|
def test_enroll_with_github_profile(tmp_dbs):
|
||||||
|
dbmod.enroll("alice", {"theme": "dark"}, github_id=1, github_avatar_url="https://x/a.png")
|
||||||
|
with dbmod._settings_conn() as c:
|
||||||
|
row = c.execute("SELECT github_id, github_avatar_url FROM users WHERE username='alice'").fetchone()
|
||||||
|
assert row["github_id"] == 1
|
||||||
|
assert row["github_avatar_url"] == "https://x/a.png"
|
||||||
|
|
||||||
|
|
||||||
def test_enrollments_daily_counter(tmp_dbs):
|
def test_enrollments_daily_counter(tmp_dbs):
|
||||||
|
|||||||
Reference in New Issue
Block a user