Files
mtusi2026lab6/student-tests/corners.py
T

60 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Short live edge cases, no load or service restarts."""
import sys
import uuid
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from urllib.parse import quote
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'tests'))
from client import Client, check, ROOT
c=Client('http://localhost:18086')
a,b,e,chat=c.bootstrap()
p='/api/v1/chats/'+chat['id']
count=0
def case(name,method,path,status,**kw):
global count
r=c.call(method,path,status,**kw)
count+=1
print('PASS:',name)
return r
for limit in ['0','101','-1','abc']:
case('invalid limit '+limit,'GET',p+'/messages?limit='+limit,400,actor=a)
case('invalid cursor','GET',p+'/messages?cursor=broken',400,actor=a)
case('invalid UUID','GET','/api/v1/chats/not-a-uuid',400,actor=a)
case('unknown UUID','GET','/api/v1/chats/'+str(uuid.uuid4()),404,actor=a)
case('bad bearer','GET','/api/v1/users/me',401,headers={'Authorization':'Bearer invalid'})
case('broken JSON','POST',p+'/messages',400,actor=a,raw=b'{',headers={'Content-Type':'application/json','Idempotency-Key':uuid.uuid4().hex})
for text in [' \t\n','x'*4001]:
case('empty/oversized text','POST',p+'/messages',400,actor=a,body={'text':text},headers={'Idempotency-Key':uuid.uuid4().hex})
case('4000 Unicode characters','POST',p+'/messages',201,actor=a,body={'text':'🙂'*4000},headers={'Idempotency-Key':uuid.uuid4().hex},schema='Message')
for label,filename,mime,data,status in [
('path filename','../x.png','image/png',(ROOT/'tests/fixtures/sample.png').read_bytes(),400),
('corrupt PNG','x.png','image/png',b'not an image',415),
('unsupported MIME','x.txt','text/plain',b'hello',415),
('upload over 10 MiB','x.png','image/png',b'x'*(10485760+1),413)]:
case(label,'POST',p+'/attachments?filename='+quote(filename,safe=''),status,actor=a,raw=data,headers={'Content-Type':mime})
case('outsider upload','POST',p+'/attachments?filename=x.png',404,actor=e,raw=(ROOT/'tests/fixtures/sample.png').read_bytes(),headers={'Content-Type':'image/png'})
# Same idempotency key reaches two real API replicas concurrently.
key=uuid.uuid4().hex
with ThreadPoolExecutor(max_workers=8) as pool:
replies=list(pool.map(lambda _:Client('http://localhost:18086').message(a,chat,'same request',key=key),range(8)))
check(len({r['json']['id'] for r in replies})==1,'concurrent replay created duplicates')
check(len({r['headers']['X-Instance-Id'] for r in replies})==2,'did not reach both replicas')
print('PASS: 8 concurrent replays across both API replicas return one message')
count+=1
case('same key different body','POST',p+'/messages',409,actor=a,body={'text':'different'},headers={'Idempotency-Key':key})
att,_=c.attachment(a,chat)
case('duplicate attachment IDs','POST',p+'/messages',400,actor=a,body={'text':'','attachment_ids':[att['id'],att['id']]},headers={'Idempotency-Key':uuid.uuid4().hex})
other=c.call('POST','/api/v1/chats',201,actor=a,body={'title':'Other','member_ids':[a['id']]},schema='Chat')['json']
case('attachment from another chat','POST','/api/v1/chats/'+other['id']+'/messages',404,actor=a,body={'text':'','attachment_ids':[att['id']]},headers={'Idempotency-Key':uuid.uuid4().hex})
c.wait_ready(a,att)
case('unknown variant','GET','/api/v1/attachments/'+att['id']+'/content?variant=unknown',400,actor=a)
case('outsider thumbnail','GET','/api/v1/attachments/'+att['id']+'/content?variant=thumbnail',404,actor=e)
page=c.call('GET',p+'/messages?limit=1',actor=a,schema='MessagePage')['json']
check(bool(page['next_cursor']),'missing cursor')
case('cursor from another chat','GET','/api/v1/chats/'+other['id']+'/messages?cursor='+quote(page['next_cursor'],safe=''),400,actor=a)
print('PASS:',count,'live corner cases')