feat(api): implement per-user edit token for settings mutations

This commit is contained in:
shay
2026-04-19 09:34:26 -04:00
parent 3ecb6ad67b
commit 016583c9f3
16 changed files with 774 additions and 116 deletions
+15 -5
View File
@@ -21,13 +21,23 @@ def test_settings_hash_is_deterministic():
def test_enroll_and_get_settings(tmp_dbs):
defaults = {"theme": "dark", "widgets": ["grade"]}
job_id = dbmod.enroll("alice", defaults)
assert job_id > 0
result = dbmod.enroll("alice", defaults)
assert result["job_id"] > 0
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):
r1 = dbmod.enroll("alice", {"theme": "dark"})
r2 = dbmod.enroll("alice", {"theme": "dark"})
assert r1["edit_token"]
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_enrollments_daily_counter(tmp_dbs):
dbmod.enroll("alice", {"theme": "dark"})
dbmod.enroll("bob", {"theme": "dark"})
@@ -43,7 +53,7 @@ def test_update_settings_changes_hash(tmp_dbs):
def test_claim_job_marks_running(tmp_dbs):
job_id = dbmod.enroll("alice", {"theme": "dark"})
job_id = dbmod.enroll("alice", {"theme": "dark"})["job_id"]
job = dbmod.claim_next_job()
assert job["id"] == job_id
assert job["status"] == "running"
@@ -51,7 +61,7 @@ def test_claim_job_marks_running(tmp_dbs):
def test_complete_job(tmp_dbs):
jid = dbmod.enroll("alice", {"theme": "dark"})
jid = dbmod.enroll("alice", {"theme": "dark"})["job_id"]
dbmod.claim_next_job()
dbmod.complete_job(jid)
with dbmod._settings_conn() as c:
@@ -81,7 +91,7 @@ def test_lru_trim_keeps_n_newest(tmp_dbs):
def test_reclaim_stuck_running_jobs(tmp_dbs):
jid = dbmod.enroll("alice", {"theme": "dark"})
jid = dbmod.enroll("alice", {"theme": "dark"})["job_id"]
dbmod.claim_next_job()
# Backdate updated_at to simulate dead worker
with dbmod._settings_conn() as c: