forked from mtusi2026/mtusi2026lab2
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
import base64
|
|
import time
|
|
from datetime import datetime, timezone
|
|
from client import Client, Failure, arguments, check, run, timestamp
|
|
|
|
|
|
def main():
|
|
args = arguments("Short session expiry acceptance check")
|
|
client = Client(args.base_url, args.ca_file)
|
|
client.call("GET", "/health/ready", schema="Health")
|
|
username = "expiry_" + __import__("uuid").uuid4().hex[:16]
|
|
password = "Expiry-test-" + __import__("uuid").uuid4().hex
|
|
user = client.call(
|
|
"POST",
|
|
"/api/v1/users",
|
|
201,
|
|
body={
|
|
"username": username,
|
|
"display_name": "Expiry test",
|
|
"password": password,
|
|
},
|
|
schema="User",
|
|
)["json"]
|
|
basic = base64.b64encode((username + ":" + password).encode()).decode()
|
|
session = client.call(
|
|
"POST",
|
|
"/api/v1/auth/sessions",
|
|
201,
|
|
headers={"Authorization": "Basic " + basic},
|
|
schema="Session",
|
|
)["json"]
|
|
expires_at = timestamp(session["expires_at"])
|
|
remaining = (expires_at - datetime.now(timezone.utc)).total_seconds()
|
|
check(0 < remaining <= 5, "start API with SESSION_TTL_SECONDS=2 for this check")
|
|
client.call("GET", "/api/v1/users/me", actor={"id": user["id"], "token": session["token"]}, schema="User")
|
|
time.sleep(max(0, remaining) + 0.2)
|
|
client.call(
|
|
"GET",
|
|
"/api/v1/users/me",
|
|
expected=401,
|
|
actor={"id": user["id"], "token": session["token"]},
|
|
)
|
|
print("PASS: session expired at its configured deadline")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run(main)
|