forked from mtusi2026/mtusi2026lab2
ЛР 2: PostgreSQL, сессии Basic/Bearer, Redis-кэш, тесты и отчёт
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Истечение сессии при коротком SESSION_TTL_SECONDS (=2).
|
||||
Запускается против отдельного экземпляра API с коротким TTL:
|
||||
docker compose --env-file .env --profile ttl up -d api-ttl
|
||||
python student-tests/expiry.py --base-url http://localhost:8081
|
||||
"""
|
||||
import argparse
|
||||
import time
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from common import Api, Report
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--base-url", default="http://localhost:8081")
|
||||
ap.add_argument("--ttl", type=float, default=2)
|
||||
args = ap.parse_args()
|
||||
api, r = Api(args.base_url), Report()
|
||||
|
||||
user = api.register("ttl")
|
||||
st, sess, _ = api.login(user["username"], user["password"])
|
||||
r.check(st == 201, "login -> 201")
|
||||
exp = datetime.fromisoformat(sess["expires_at"].replace("Z", "+00:00"))
|
||||
left = (exp - datetime.now(timezone.utc)).total_seconds()
|
||||
r.check(0 < left <= args.ttl + 1, f"expires_at is about {args.ttl}s ahead (got {left:.2f}s)")
|
||||
st, _, _ = api.call("GET", "/api/v1/users/me", token=sess["token"])
|
||||
r.check(st == 200, "token works before expiry") # заодно кладёт сессию в кэш Redis
|
||||
time.sleep(args.ttl + 1.5)
|
||||
st, _, rh = api.call("GET", "/api/v1/users/me", token=sess["token"])
|
||||
r.check(st == 401 and rh.get("WWW-Authenticate", "").startswith("Bearer"),
|
||||
"expired token -> 401 (cache does not extend the session)")
|
||||
r.finish()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user