test(student-tests): lab2 consolidate negative, concurrency, auth and resilience checks in one commit

This commit is contained in:
Голиков Maximillian Антон
2026-10-02 16:32:10 +03:00
parent effb19289b
commit 9bba9c4b25
8 changed files with 337 additions and 7 deletions
+46
View File
@@ -0,0 +1,46 @@
#!/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)