Files
kazakov_mtusi2026lab2/student-tests/test_expiry.py
T
2026-11-13 20:42:10 +03:00

27 lines
1003 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Истечение сессии: API перезапускается с SESSION_TTL_SECONDS=2, затем возвращается обычный TTL."""
import time
from common import *
TTL = 2
def main():
compose("up", "-d", "api", env={"SESSION_TTL_SECONDS": str(TTL)})
wait_ready()
try:
alice = new_user("exp")
session = login(alice)
token = session["token"]
check(call("GET", "/api/v1/users/me", headers=bearer(token))[0] == 200, "fresh token works (and is cached)")
time.sleep(TTL + 1.5)
st, _, h, _ = call("GET", "/api/v1/users/me", headers=bearer(token))
check(st == 401 and h.get("WWW-Authenticate", "").startswith("Bearer"), f"expired token must be 401, got {st}")
print(f"ok: TTL={TTL}s, after {TTL + 1.5}s the same token -> 401 (cache does not extend the session)")
finally:
compose("up", "-d", "api") # обычный TTL из .env
wait_ready()
print("PASS: test_expiry")
run(main)