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
+39 -6
View File
@@ -54,21 +54,50 @@ def test_enrolled_user_with_built_widget_returns_ready(client):
def test_settings_patch_enqueues_rebuild(client):
dbmod.enroll("alice", {"theme": "dark"})
r = client.patch("/api/alice/settings", json={"theme": "light"})
token = dbmod.enroll("alice", {"theme": "dark"})["edit_token"]
r = client.patch(
"/api/alice/settings",
json={"theme": "light"},
headers={"Authorization": f"Bearer {token}"},
)
assert r.status_code == 200
assert dbmod.get_settings("alice")["settings"]["theme"] == "light"
def test_refresh_is_one_shot(client):
def test_settings_patch_without_token_is_unauthorized(client):
dbmod.enroll("alice", {"theme": "dark"})
r = client.patch("/api/alice/settings", json={"theme": "light"})
assert r.status_code == 401
# Settings must not be mutated when the token check fails.
assert dbmod.get_settings("alice")["settings"]["theme"] == "dark"
def test_settings_patch_with_wrong_token_is_unauthorized(client):
dbmod.enroll("alice", {"theme": "dark"})
r = client.patch(
"/api/alice/settings",
json={"theme": "light"},
headers={"Authorization": "Bearer not-the-real-token"},
)
assert r.status_code == 401
def test_refresh_is_one_shot(client):
token = dbmod.enroll("alice", {"theme": "dark"})["edit_token"]
headers = {"Authorization": f"Bearer {token}"}
with patch("src.api.fetcher_client.force_fetch", return_value={"changed": True, "payload_hash": "x", "stored": True}):
r1 = client.post("/api/alice/refresh")
r1 = client.post("/api/alice/refresh", headers=headers)
assert r1.status_code == 200
r2 = client.post("/api/alice/refresh")
r2 = client.post("/api/alice/refresh", headers=headers)
assert r2.status_code == 409
def test_refresh_without_token_is_unauthorized(client):
dbmod.enroll("alice", {"theme": "dark"})
r = client.post("/api/alice/refresh")
assert r.status_code == 401
def test_not_found_status_header(client):
dbmod.enroll("ghost", {"theme": "dark"})
dbmod.put_widgets("ghost", "not_found", {"composite": "<svg>404</svg>"})
@@ -112,7 +141,11 @@ def test_generate_endpoint_rejects_unenrolled(client):
def test_data_endpoint_unknown_user_enrolls_and_returns_building(client):
r = client.get("/api/alice/data")
assert r.status_code == 202
assert r.get_json() == {"status": "building"}
body = r.get_json()
assert body["status"] == "building"
# Auto-enroll path surfaces the edit token so the browser can mutate
# the freshly-created profile without a separate claim flow.
assert "edit_token" in body and body["edit_token"]
assert dbmod.get_settings("alice") is not None # auto-enrolled
+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: