forked from mtusi2026/mtusi2026lab2
35 lines
1.5 KiB
Python
35 lines
1.5 KiB
Python
"""Run against an isolated API started with SESSION_TTL_SECONDS=2.
|
|
|
|
BASE_URL defaults to http://localhost:8080; do not use a normal one-hour server.
|
|
"""
|
|
import base64
|
|
import time
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
|
|
from cache_consistency import http_json
|
|
|
|
|
|
def main():
|
|
username = "expiry_" + uuid.uuid4().hex[:16]
|
|
password = "expiry-password-123"
|
|
http_json("POST", "/api/v1/users", expected=201,
|
|
body={"username": username, "display_name": "Expiry", "password": password})
|
|
credentials = base64.b64encode(f"{username}:{password}".encode()).decode()
|
|
http_json("POST", "/api/v1/auth/sessions", expected=401,
|
|
headers={"Authorization": "Basic " + base64.b64encode(f"{username}:wrong-password".encode()).decode()})
|
|
result = http_json("POST", "/api/v1/auth/sessions", expected=201,
|
|
headers={"Authorization": "Basic " + credentials})
|
|
session = result["body"]
|
|
remaining = (datetime.fromisoformat(session["expires_at"].replace("Z", "+00:00")) - datetime.now(timezone.utc)).total_seconds()
|
|
assert 0 < remaining <= 3, "Use an isolated server with SESSION_TTL_SECONDS=2"
|
|
headers = {"Authorization": "Bearer " + session["token"]}
|
|
http_json("GET", "/api/v1/users/me", headers=headers)
|
|
time.sleep(max(0, remaining) + 0.2)
|
|
http_json("GET", "/api/v1/users/me", expected=401, headers=headers)
|
|
print("PASS: wrong password rejected; live session accepted; expired session rejected")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|