Files
mtusi2026lab3-fork/student-tests/test_expiry.py
T

46 lines
1.7 KiB
Python
Raw 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
import base64
import time
import uuid
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tests'))
from client import Client, arguments, check, run # noqa: E402
def main():
args = arguments('Истечение сессии. Требует SESSION_TTL_SECONDS=2')
c = Client(args.base_url, args.ca_file)
username = 'exp_' + uuid.uuid4().hex[:10]
password = 'T3st-' + uuid.uuid4().hex
c.call('POST', '/api/v1/users', 201, body={
'username': username, 'display_name': 'Exp', 'password': password})
cred = base64.b64encode(f'{username}:{password}'.encode()).decode()
s = c.call('POST', '/api/v1/auth/sessions', 201,
headers={'Authorization': 'Basic ' + cred}, schema='Session')['json']
actor = {'id': s['user']['id'], 'token': s['token']}
# сразу после логина — работает
c.call('GET', '/api/v1/users/me', actor=actor, schema='User')
# ждём больше TTL. Если TTL не 2, тест упадёт с понятным сообщением
# (см. проверку ниже по факту отказа).
time.sleep(3.5)
# ожидаем 401 с Bearer challenge
r = c.call('GET', '/api/v1/users/me', 401, actor=actor)
challenge = r['headers'].get('WWW-Authenticate', '')
check(challenge.lower().startswith('bearer'),
f'ожидался WWW-Authenticate: Bearer, получено {challenge!r}')
# и сразу проверяем, что до истечения он работал — уже сделано выше
print('PASS: сессия истекла после TTL и вернула 401 Bearer')
if __name__ == '__main__':
run(main)