Реализация лабораторной работы 4 на 4

This commit is contained in:
2026-11-06 19:23:06 +03:00
parent ec540a1a72
commit b72fa4b82d
134 changed files with 8344 additions and 20 deletions
+55
View File
@@ -0,0 +1,55 @@
"""
Истечение сессии при коротком SESSION_TTL_SECONDS.
Нужен отдельный API с SESSION_TTL_SECONDS=2: make test-expiry поднимает
api-short-ttl на :8081 поверх тех же PostgreSQL и Valkey.
"""
import os
import time
from datetime import UTC, datetime
import httpx
import pytest
from conftest import register_and_login
from helpers import assert_unauthorized, auth, basic
EXPIRY_BASE_URL = os.environ.get("EXPIRY_BASE_URL")
SHORT_TTL_SEC = 2
pytestmark = pytest.mark.skipif(
not EXPIRY_BASE_URL, reason="EXPIRY_BASE_URL не задан: запустите make test-expiry"
)
@pytest.fixture
def short_api() -> httpx.Client:
assert EXPIRY_BASE_URL is not None
with httpx.Client(base_url=EXPIRY_BASE_URL, timeout=10) as client:
yield client
def test_session_expires_after_short_ttl(short_api: httpx.Client) -> None:
alice = register_and_login(short_api, "ttl")
me = short_api.get("/api/v1/users/me", headers=auth(alice))
assert me.status_code == 200, me.text
time.sleep(SHORT_TTL_SEC + 1)
# Сессия уже лежала в кэше после первого запроса: истечь должна и там
assert_unauthorized(short_api.get("/api/v1/users/me", headers=auth(alice)))
assert_unauthorized(short_api.get("/api/v1/chats", headers=auth(alice)))
assert_unauthorized(
short_api.delete("/api/v1/auth/sessions/current", headers=auth(alice))
)
def test_expires_at_matches_short_ttl(short_api: httpx.Client) -> None:
before = datetime.now(UTC)
alice = register_and_login(short_api, "ttl")
session = short_api.post(
"/api/v1/auth/sessions", headers=basic(alice["username"], alice["password"])
).json()
expires_at = datetime.fromisoformat(session["expires_at"].replace("Z", "+00:00"))
lifetime = (expires_at - before).total_seconds()
assert 0 < lifetime <= SHORT_TTL_SEC + 1