Files
mtusi2026lab2/scripts/test_expiry.py
T
2026-10-29 17:16:55 +03:00

53 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Check real session expiry with TTL=2 and restore normal API configuration."""
import os
from pathlib import Path
import subprocess
import sys
import time
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT / 'tests'))
from client import Client, Failure, check
COMPOSE = ['docker','compose','--env-file','.env']
def compose(*args, env=None):
subprocess.run(COMPOSE+list(args),cwd=ROOT,env=env,check=True,
stdout=subprocess.DEVNULL)
def ready(c):
deadline=time.monotonic()+60
while time.monotonic()<deadline:
try:
c.call('GET','/health/ready')
return
except Failure:
time.sleep(0.5)
raise AssertionError('Readiness timeout')
def main():
c=Client()
ready(c)
try:
env=dict(os.environ,SESSION_TTL_SECONDS='2')
compose('up','-d','--no-deps','--force-recreate','api',env=env)
ready(c)
short=c.user('expiry')
c.call('GET','/api/v1/users/me',actor=short,schema='User')
time.sleep(2.3)
c.call('GET','/api/v1/users/me',401,actor=short)
c.call('DELETE','/api/v1/auth/sessions/current',401,actor=short)
print('PASS SESSION_TTL_SECONDS=2: session accepted before expiry and rejected afterwards',flush=True)
finally:
compose('up','-d','--no-deps','--force-recreate','api')
ready(c)
print('PASS expiry scenario; normal API configuration restored',flush=True)
if __name__=='__main__':
main()