Files
2026-10-16 19:49:48 +03:00

37 lines
1.9 KiB
Python
Raw Permalink 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.
#!/usr/bin/env python3
"""Истечение сессии. Запускать против API, поднятого с коротким SESSION_TTL_SECONDS (например 2).
Проверяет и путь «через кэш» (токен успели использовать => он в Valkey), и путь «только PostgreSQL»."""
import sys
import time
import datetime as dt
from common import Failure, basic, check, client
from client import timestamp # доступен через sys.path, настроенный в common
def main():
ttl = float(sys.argv[1]) if len(sys.argv) > 1 else 2.0
c = client()
a = c.user("exp")
# Свежий вход: кэш прогрет логином; затем ещё читаем, чтобы запись точно была в Valkey.
sess = c.call("POST", "/api/v1/auth/sessions", 201, headers=basic(a["registration"]["username"], a["registration"]["password"]), schema="Session")["json"]
lifetime = (timestamp(sess["expires_at"]) - dt.datetime.now(dt.timezone.utc)).total_seconds()
check(lifetime <= ttl + 0.5, "expires_at (%.1f c) не соответствует короткому TTL=%s — сервер запущен с другим SESSION_TTL_SECONDS" % (lifetime, ttl))
h = {"Authorization": "Bearer " + sess["token"]}
c.call("GET", "/api/v1/users/me", 200, headers=h)
c.call("GET", "/api/v1/chats", 200, headers=h)
time.sleep(ttl + 1.5)
c.call("GET", "/api/v1/users/me", 401, headers=h)
c.call("GET", "/api/v1/chats", 401, headers=h)
c.call("DELETE", "/api/v1/auth/sessions/current", 401, headers=h)
print("PASS: сессия с TTL=%ss перестала действовать (401 на users/me, chats, logout) после ожидания %.1fs" % (ttl, ttl + 1.5))
if __name__ == "__main__":
try:
main()
except Failure as exc:
print("FAIL:", exc)
raise SystemExit(1)