#!/usr/bin/env python3 """Use a stand started with SESSION_TTL_SECONDS between 3 and 15.""" import base64 import datetime as dt import sys import time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) from client import Client, arguments, check, run, timestamp # noqa: E402 def main(): args = arguments(__doc__) client = Client(args.base_url, args.ca_file) actor = client.user("expiry") registration = actor["registration"] credentials = base64.b64encode( f"{registration['username']}:{registration['password']}".encode() ).decode() session = client.call("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": "Basic " + credentials}, schema="Session")["json"] actor["token"] = session["token"] client.call("GET", "/api/v1/users/me", actor=actor, schema="User") remaining = (timestamp(session["expires_at"]) - dt.datetime.now(dt.timezone.utc)).total_seconds() check(0 < remaining <= 15, "Start rehearsal API with a short SESSION_TTL_SECONDS") time.sleep(remaining + 0.3) client.call("GET", "/api/v1/users/me", 401, actor=actor) print("PASS: valid session accepted before expiry and rejected after expiry") if __name__ == "__main__": run(main)