Files
mtusi2026lab2/student-tests/expiry.py
T
Чиняева df31f4a9fb Реализовать лабораторную 2 на оценку 4
PostgreSQL как источник истины, непрозрачные сессии в Redis с TTL и AOF, миграции под advisory lock, пул и таймауты, проверки гонки/отката/отказов и отчёт с evidence.
2026-11-06 22:37:31 +03:00

36 lines
1.2 KiB
Python

#!/usr/bin/env python3
"""Expiry check: run against an API with SESSION_TTL_SECONDS=2 (or pass --wait)."""
from __future__ import annotations
import argparse
import time
from common import HttpClient, assert_status, error_code
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("--base-url", default="http://localhost:8080")
parser.add_argument("--wait", type=float, default=3.0)
args = parser.parse_args()
client = HttpClient(args.base_url)
user, registration = client.register()
session = client.login(registration)
token = session["token"]
status, headers, payload, _ = client.call("GET", "/api/v1/users/me", token=token)
assert_status(status, 200, "fresh session")
assert payload["id"] == user["id"], "wrong current user"
time.sleep(args.wait)
status, headers, payload, _ = client.call("GET", "/api/v1/users/me", token=token)
assert_status(status, 401, "expired session")
assert error_code(payload) == "unauthorized", "expired session must be unauthorized"
assert headers.get("WWW-Authenticate", "").lower().startswith("bearer"), "missing Bearer challenge"
print("PASS: expired session is rejected with 401 and Bearer challenge")
if __name__ == "__main__":
main()