Files
mtusi2026lab2/student-tests/security.py
T
2026-11-05 01:43:30 +03:00

36 lines
2.0 KiB
Python

"""Additional API checks; tokens and passwords are never printed."""
import sys
import uuid
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client, check, run
def main():
c=Client()
a,b,e,room=c.bootstrap()
path='/api/v1/chats/'+room['id']
for data in [ {'text':' '}, {'text':'ok','sender_id':e['id']}, {'text':123}, {'text':'x'*4001} ]:
c.call('POST',path+'/messages',400,actor=a,body=data)
c.call('POST',path+'/messages',400,actor=a,raw=b'{broken',headers={'Content-Type':'application/json'})
c.call('GET',path+'/messages?cursor=broken',400,actor=a)
c.message(a,room,'page 1'); c.message(a,room,'page 2')
page=c.call('GET',path+'/messages?limit=1',actor=a,schema='MessagePage')['json']
other=c.call('POST','/api/v1/chats',201,actor=a,body={'title':'Other','member_ids':[a['id']]},schema='Chat')['json']
from urllib.parse import quote
c.call('GET','/api/v1/chats/'+other['id']+'/messages?cursor='+quote(page['next_cursor'],safe=''),400,actor=a)
before=c.call('GET','/api/v1/chats?limit=100',actor=a,schema='ChatPage')['json']
c.call('POST','/api/v1/chats',400,actor=a,body={'title':'must not exist','member_ids':[a['id'],str(uuid.uuid4())]})
after=c.call('GET','/api/v1/chats?limit=100',actor=a,schema='ChatPage')['json']
check(before==after,'invalid chat left state behind')
body={'username':'race_'+uuid.uuid4().hex[:12],'display_name':'Race','password':'Synthetic-password-123'}
def register(_):
return Client().call('POST','/api/v1/users',[201,409],body=body)['status']
with ThreadPoolExecutor(max_workers=4) as executor:
codes=sorted(executor.map(register,range(4)))
check(codes==[201,409,409,409],'registration race was not atomic')
print('PASS: malformed JSON, strict fields/types, whitespace, limits, cursor resource binding, invalid chat atomicity, concurrent username uniqueness')
if __name__=='__main__': run(main)