Files
mtusi2026lab2/student-tests/test_lab2.py
T

48 lines
2.5 KiB
Python

"""Негативные сценарии, курсоры и конкурентная запись через реальный HTTP API."""
import concurrent.futures
import sys
import uuid
from pathlib import Path
from urllib.parse import quote
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client, arguments, check, run
def main():
args=arguments('Собственные проверки лабораторной 2')
c=Client(args.base_url,args.ca_file)
alice,bob,eve,chat=c.bootstrap()
path='/api/v1/chats/'+chat['id']+'/messages'
c.call('POST','/api/v1/users',400,body={'username':'short','display_name':'Тест','password':'short'})
c.call('POST',path,400,actor=alice,body={'text':'hello','sender_id':bob['id']})
c.call('POST',path,400,actor=alice,body={'text':' '})
c.call('GET','/api/v1/chats/not-a-uuid',400,actor=alice)
# Сначала идентификация, затем валидация и доступ к ресурсу.
c.call('GET','/api/v1/chats/not-a-uuid',401)
c.call('POST',path,401,body={'text':''})
c.call('POST',path,400,actor=alice,raw=b'{',headers={'Content-Type':'application/json'})
c.call('POST',path,404,actor=eve,body={'text':'чужое'})
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool:
sent=list(pool.map(lambda i: c.message(alice,chat,'Параллельное '+str(i))['json'],range(100)))
expected={m['id'] for m in sent}
received=[]
cursor=None
while True:
url=path+'?limit=7'+('&cursor='+quote(cursor,safe='') if cursor else '')
page=c.call('GET',url,actor=bob,schema='MessagePage')['json']
received.extend(m['id'] for m in page['items'])
cursor=page['next_cursor']
if cursor is None:
break
check(len(received)==100 and set(received)==expected,'потеря или дубликат сообщения')
page=c.call('GET',path+'?limit=1',actor=alice,schema='MessagePage')['json']
other=c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Другой','member_ids':[alice['id']]},schema='Chat')['json']
c.call('GET','/api/v1/chats/'+other['id']+'/messages?cursor='+quote(page['next_cursor'],safe=''),400,actor=alice)
for bad in ['', 'not-base64', 'e30=']:
c.call('GET',path+'?cursor='+quote(bad,safe=''),400,actor=alice)
print('PASS: strict validation, access, cursor binding, 100 messages / 10 clients, complete pagination')
if __name__=='__main__':
run(main)