#!/usr/bin/env python3 """Проверяет, что действующий Bearer-токен перестаёт работать по истечении TTL.""" import base64 from datetime import datetime, timezone 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 def main(): args = arguments("Check session expiry with SESSION_TTL_SECONDS=5") client = Client(args.base_url, args.ca_file) registration = client.user("expiry")["registration"] credentials = f"{registration['username']}:{registration['password']}" authorization = "Basic " + base64.b64encode(credentials.encode()).decode() session = client.call("POST", "/api/v1/auth/sessions", 201, headers={"Authorization": authorization}, schema="Session")["json"] actor = {"token": session["token"]} client.call("GET", "/api/v1/users/me", actor=actor, schema="User") remaining = (timestamp(session["expires_at"]) - datetime.now(timezone.utc)).total_seconds() check(0 < remaining <= 10, "set SESSION_TTL_SECONDS=5 on both API replicas") time.sleep(remaining + 1) client.call("GET", "/api/v1/users/me", 401, actor=actor) print("PASS: an active Bearer session returned 401 after its declared expiry") if __name__ == "__main__": run(main)