71 lines
4.3 KiB
Python
71 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
import sys,os,time,json,base64,uuid
|
|
from pathlib import Path
|
|
sys.path.insert(0,str(Path(__file__).resolve().parent))
|
|
from experiments import dc,save,restore,Client,Failure,check,command
|
|
results={}
|
|
# Retain previous lab's strict validation and Unicode password range.
|
|
results['contract_extra']=command([sys.executable,'scripts/contract_extra.py']).strip()
|
|
c=Client(); a,b,e,chat=c.bootstrap(); path='/api/v1/chats/'+chat['id']+'/messages'
|
|
for key in ('bad key','x'*129,'ü'):
|
|
# HTTP headers must be latin-1 encodable.
|
|
c.call('POST',path,400,actor=a,body={'text':'invalid key'},headers={'Idempotency-Key':key})
|
|
for payload in ({'text':'ok','sender_id':a['id']},{'text':True},{'text':'x','attachment_ids':False},{'text':'x','attachment_ids':{}}):
|
|
c.call('POST',path,400,actor=a,body=payload,headers={'Idempotency-Key':uuid.uuid4().hex})
|
|
results['strict_message_validation']='PASS'
|
|
# Same key belongs independently to each authenticated sender and chat.
|
|
key=uuid.uuid4().hex
|
|
m1=c.message(a,chat,'scoped',key)['json']; m2=c.message(b,chat,'scoped',key)['json']
|
|
chat2=c.call('POST','/api/v1/chats',201,actor=a,body={'title':'scope','member_ids':[a['id']]},schema='Chat')['json']
|
|
m3=c.message(a,chat2,'scoped',key)['json']
|
|
check(len({m['id'] for m in (m1,m2,m3)})==3,'key scope too broad')
|
|
results['key_scope']='PASS: 3 independent messages'
|
|
canonical=c.call('POST',path,201,actor=a,body={'attachment_ids':[],'text':'scoped'},headers={'Idempotency-Key':key},schema='Message')['json']
|
|
check(canonical==m1,'JSON key order changed replay')
|
|
results['canonical_json_order']='PASS: reversed fields and explicit [] replay the same message'
|
|
from urllib.parse import quote
|
|
page=c.call('GET',path+'?limit=1',actor=a,schema='MessagePage')['json']
|
|
check(page['next_cursor'] is not None,'cursor fixture missing')
|
|
c.call('GET','/api/v1/chats/'+chat2['id']+'/messages?cursor='+quote(page['next_cursor'],safe=''),400,actor=a)
|
|
results['cursor_scope']='PASS: cursor from another chat rejected with 400'
|
|
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=a)
|
|
for _ in range(16): c.call('POST',path,401,actor=a,body={'text':'scoped'},headers={'Idempotency-Key':key})
|
|
results['revoked_session_cannot_replay']='PASS'
|
|
# Explicit replay across a real process stop, with the original key and token.
|
|
key=uuid.uuid4().hex
|
|
first=c.message(b,chat,'replay after physical stop',key)
|
|
target=first['headers']['X-Instance-Id']; started=time.monotonic()
|
|
hook=command(['sh','scripts/stop-one.sh'],dict(os.environ,TARGET_INSTANCE_ID=target))
|
|
while True:
|
|
try:
|
|
replay=c.message(b,chat,'replay after physical stop',key)
|
|
break
|
|
except Failure:
|
|
check(time.monotonic()-started<10,'cross-failover replay did not recover')
|
|
time.sleep(.2)
|
|
check(first['json']==replay['json'],'replay changed after physical stop')
|
|
check(target!=replay['headers']['X-Instance-Id'],'stopped instance still serves replay')
|
|
save('idempotency-failover.json',{'original_instance':target,'replay_instance':replay['headers']['X-Instance-Id'],'same_message':True,'status':replay['status'],'seconds':round(time.monotonic()-started,3),'hook':hook})
|
|
results['same_key_after_physical_failover']='PASS: same Message and 201 via a different process with original token'
|
|
restore()
|
|
dc('stop','redis')
|
|
try:
|
|
c.call('GET','/api/v1/users/me',actor=b,schema='User'); c.call('GET','/health/ready',schema='Health')
|
|
results['redis_outage']='PASS: profile falls back to PostgreSQL, ready=200'
|
|
finally: dc('start','redis')
|
|
dc('stop','postgres')
|
|
try:
|
|
probe = "import urllib.request,urllib.error;\nfor path in ('live','ready'):\n try: r=urllib.request.urlopen('http://localhost:8080/health/'+path,timeout=8); print(path,r.status)\n except urllib.error.HTTPError as e: print(path,e.code)"
|
|
output=command(['docker','exec',dc('ps','-q','api').split()[0],'python','-c',probe])
|
|
check('live 200' in output and 'ready 503' in output,'health dependency semantics')
|
|
results['postgres_outage']=output.strip()+'; no DB failover claimed'
|
|
finally: restore()
|
|
env=dict(os.environ,SESSION_TTL_SECONDS='2')
|
|
dc('up','-d','--wait','--wait-timeout','90',env=env); time.sleep(3)
|
|
c=Client(); a=c.user('ttl'); time.sleep(2.2)
|
|
c.call('GET','/api/v1/users/me',401,actor=a)
|
|
results['expiry']='PASS: TTL=2 seconds, old token gives 401'
|
|
restore()
|
|
save('extra-checks.json',results)
|
|
print(json.dumps(results,ensure_ascii=False,indent=2))
|