Fix cached session identity and document PR 63 regression checks
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -8,6 +8,7 @@ import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
|
||||
BASE_URL = os.environ.get("BASE_URL", "http://localhost:8080")
|
||||
@@ -27,6 +28,8 @@ def http_json(method, path, *, expected=200, headers=None, body=None):
|
||||
payload = resp.read()
|
||||
if not payload:
|
||||
return {"status": resp.status, "headers": dict(resp.headers), "body": None}
|
||||
if resp.status not in (expected if isinstance(expected, (list, tuple, set)) else [expected]):
|
||||
raise AssertionError(f"Expected {expected}, got {resp.status}")
|
||||
return {"status": resp.status, "headers": dict(resp.headers), "body": json.loads(payload.decode("utf-8"))}
|
||||
except urllib.error.HTTPError as exc:
|
||||
payload = exc.read()
|
||||
@@ -91,6 +94,25 @@ def main():
|
||||
assert stale_result["status"] == 200, stale_result
|
||||
assert stale_result["body"]["username"] == username, stale_result
|
||||
|
||||
# An unexpired payload must not be able to substitute another identity.
|
||||
stale_payload["expires_at"] = "2100-01-01T00:00:00Z"
|
||||
r.setex(key, 120, json.dumps(stale_payload))
|
||||
forged = http_json("GET", "/api/v1/users/me", headers={"Authorization": "Bearer " + token})
|
||||
assert forged["body"]["id"] == session["body"]["user"]["id"], forged
|
||||
assert forged["body"]["username"] == username, forged
|
||||
|
||||
r.setex(key, 120, json.dumps(stale_payload))
|
||||
def read_identity(_):
|
||||
return http_json("GET", "/api/v1/users/me", headers={"Authorization": "Bearer " + token})["body"]["id"]
|
||||
with ThreadPoolExecutor(max_workers=8) as executor:
|
||||
identities = list(executor.map(read_identity, range(8)))
|
||||
assert identities == [session["body"]["user"]["id"]] * 8, identities
|
||||
|
||||
# Syntactically valid JSON with missing fields must be treated as a miss.
|
||||
r.setex(key, 120, "{}")
|
||||
malformed = http_json("GET", "/api/v1/users/me", headers={"Authorization": "Bearer " + token})
|
||||
assert malformed["body"]["username"] == username, malformed
|
||||
|
||||
r.setex(key, 1, json.dumps({
|
||||
"token": token,
|
||||
"user_id": str(uuid.uuid4()),
|
||||
@@ -110,11 +132,23 @@ def main():
|
||||
after_logout = http_json("GET", "/api/v1/users/me", expected=401, headers={"Authorization": "Bearer " + token})
|
||||
assert after_logout["status"] == 401, after_logout
|
||||
|
||||
# Simulate a writer repopulating the cache after logout's DELETE.
|
||||
stale_payload.update(session["body"])
|
||||
stale_payload["user_id"] = session["body"]["user"]["id"]
|
||||
stale_payload["revoked_at"] = None
|
||||
r.setex(key, 120, json.dumps(stale_payload))
|
||||
http_json("GET", "/api/v1/users/me", expected=401, headers={"Authorization": "Bearer " + token})
|
||||
assert not r.exists(key), "stale key after logout must be removed"
|
||||
|
||||
print("cache_key_exists=True")
|
||||
print(f"ttl_seconds={ttl}")
|
||||
print(f"avg_latency_ms={avg:.2f}")
|
||||
print(f"p95_latency_ms={p95:.2f}")
|
||||
print("stale_cache_rejected=True")
|
||||
print("forged_identity_rejected=True")
|
||||
print("concurrent_identity_checks=8/8")
|
||||
print("malformed_cache_recovers=True")
|
||||
print("repopulated_revoked_cache_rejected=True")
|
||||
print("short_ttl_recovers_from_db=True")
|
||||
print("logout_revokes_cache=True")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user