forked from mtusi2026/mtusi2026lab2
46 lines
2.3 KiB
Python
46 lines
2.3 KiB
Python
"""Extra black-box cases. Uses public Client without modifying public tests."""
|
|
import base64
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import sys
|
|
import time
|
|
import uuid
|
|
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()
|
|
alice,bob,eve,chat = c.bootstrap()
|
|
body = {'username':'race_'+uuid.uuid4().hex[:16],'display_name':'Гонка','password':'Пароль:🙂'*3}
|
|
def register(_):
|
|
return Client().call('POST','/api/v1/users',[201,409],body=body)['status']
|
|
with ThreadPoolExecutor(max_workers=4) as pool:
|
|
statuses = list(pool.map(register,range(4)))
|
|
check(statuses.count(201)==1 and statuses.count(409)==3,'registration race failed')
|
|
print('PASS: concurrent registration: 1 x 201, 3 x 409')
|
|
credential = base64.b64encode((body['username']+':'+body['password']).encode()).decode()
|
|
c.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+credential},schema='Session')
|
|
print('PASS: Unicode password with colon')
|
|
c.call('POST','/api/v1/chats',400,actor=alice,body={'title':'rollback-'+uuid.uuid4().hex,'member_ids':[alice['id'],str(uuid.uuid4())]})
|
|
c.call('GET','/api/v1/chats/not-a-uuid',401)
|
|
c.call('GET','/api/v1/chats/not-a-uuid',400,actor=alice)
|
|
c.call('POST','/api/v1/users',400,body={**body,'extra':True})
|
|
for text in ('', ' ', 'x'*4001):
|
|
c.call('POST','/api/v1/chats/'+chat['id']+'/messages',400,actor=alice,body={'text':text})
|
|
for i in range(6):
|
|
c.message(alice,chat,'page '+str(i))
|
|
path='/api/v1/chats/'+chat['id']+'/messages'
|
|
page=c.call('GET',path+'?limit=2',actor=alice,schema='MessagePage')['json']
|
|
other=c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'other','member_ids':[alice['id']]},schema='Chat')['json']
|
|
from urllib.parse import quote
|
|
cursor=quote(page['next_cursor'],safe='')
|
|
c.call('GET','/api/v1/chats/'+other['id']+'/messages?cursor='+cursor,400,actor=alice)
|
|
c.call('GET',path+'?cursor=bad',400,actor=alice)
|
|
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=alice)
|
|
c.call('GET','/api/v1/users/me',401,actor=alice)
|
|
c.call('DELETE','/api/v1/auth/sessions/current',401,actor=alice)
|
|
print('PASS: validation, cursor isolation, logout')
|
|
|
|
if __name__=='__main__':
|
|
run(main)
|