#!/usr/bin/env python3 """Recreate the API with a two-second session TTL and prove expiry.""" import os import subprocess import sys import time from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / "tests")) from client import Client, Failure, check # noqa: E402 def compose(env, *args): subprocess.run(["docker", "compose", "--env-file", ".env", *args], cwd=ROOT, env=env, check=True, stdout=subprocess.DEVNULL) def wait_ready(timeout=60): client = Client(timeout=2) deadline = time.monotonic() + timeout while time.monotonic() < deadline: try: client.call("GET", "/health/ready", schema="Health") return except Failure: time.sleep(0.5) raise Failure("API did not become ready") def main(): normal_env = dict(os.environ) normal_env.pop("SESSION_TTL_SECONDS", None) short_env = dict(normal_env) short_env["SESSION_TTL_SECONDS"] = "2" try: compose(short_env, "up", "-d", "--force-recreate", "--no-deps", "--wait", "api1", "api2") compose(short_env, "exec", "-T", "nginx", "nginx", "-s", "reload") wait_ready() client = Client() actor = client.user("expiry") time.sleep(3) client.call("GET", "/api/v1/users/me", 401, actor=actor) print("PASS: two-second Redis session expired and was rejected") finally: compose(normal_env, "up", "-d", "--force-recreate", "--no-deps", "--wait", "api1", "api2") compose(normal_env, "exec", "-T", "nginx", "nginx", "-s", "reload") wait_ready() if __name__ == "__main__": try: main() except (Failure, OSError, subprocess.CalledProcessError) as exc: print("FAIL:", exc) raise SystemExit(1)