"""Level 4 integration evidence; only synthetic data in this Compose project.""" import base64 from concurrent.futures import ThreadPoolExecutor import json from pathlib import Path import subprocess import sys import time import uuid from urllib.parse import quote ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests')) from client import Client, check def compose(*args, input=None): return subprocess.check_output(['docker','compose','--env-file','.env',*args], cwd=ROOT, input=input, text=True, stderr=subprocess.STDOUT) def sql(statement): return compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-At','-v','ON_ERROR_STOP=1','-c',statement).strip() def wait_ready(client): start=time.monotonic() while time.monotonic()-start < 60: try: client.call('GET','/health/ready',schema='Health') return round(time.monotonic()-start,3) except AssertionError: time.sleep(0.25) raise AssertionError('Recovery timeout') def main(): results={} c=Client() alice,bob,eve,chat=c.bootstrap() path='/api/v1/chats/'+chat['id'] # Registration race must be resolved by a database unique constraint. body={'username':'race_'+uuid.uuid4().hex[:20], 'display_name':'Гонка', 'password':'тестовый:пароль🔒'} with ThreadPoolExecutor(max_workers=8) as pool: statuses=list(pool.map(lambda _: Client().call('POST','/api/v1/users',[201,409],body=body)['status'],range(8))) check(statuses.count(201)==1 and statuses.count(409)==7,'Registration race') results['registration_race']={'201':1,'409':7} credential=base64.b64encode((body['username']+':'+body['password']).encode()).decode() c.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+credential},schema='Session') results['unicode_password_with_colon']='PASS' digest=sql("SELECT password_hash FROM users WHERE username='"+body['username']+"'") check(digest.startswith('$argon2id$') and body['password'] not in digest,'Password KDF') results['password_storage']='Argon2id; no plaintext' before=sql('SELECT count(*) FROM chats') c.call('POST','/api/v1/chats',400,actor=alice,body={'title':'Rollback','member_ids':[alice['id'],str(uuid.uuid4())]}) check(sql('SELECT count(*) FROM chats')==before,'Chat transaction was not rolled back') check(sql('SELECT count(*) FROM members m LEFT JOIN chats c ON m.chat_id=c.id WHERE c.id IS NULL')=='0','Orphan members') results['rollback']='PASS: chat count unchanged after failed member FK' for body in ({'text':' '},{'text':42},{'text':'x','sender_id':eve['id']}): c.call('POST',path+'/messages',400,actor=alice,body=body) c.call('POST',path+'/messages',400,actor=alice,raw=b'{',headers={'Content-Type':'application/json'}) c.call('POST',path+'/messages',415,actor=alice,raw=b'text') c.call('GET',path,401) c.call('GET',path,404,actor=eve) results['validation_and_access']='PASS' with ThreadPoolExecutor(max_workers=8) as pool: messages=list(pool.map(lambda i: Client().message(alice,chat,'Сообщение '+str(i))['json'],range(100))) found=[]; cursor=None while True: suffix='?limit=7'+('&cursor='+quote(cursor,safe='') if cursor else '') page=c.call('GET',path+'/messages'+suffix,actor=bob,schema='MessagePage')['json'] found.extend(page['items']); cursor=page['next_cursor'] if not cursor: break check(len(found)==100 and {m['id'] for m in found}=={m['id'] for m in messages},'Concurrent pagination') first=c.call('GET',path+'/messages?limit=1',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'] c.call('GET','/api/v1/chats/'+other['id']+'/messages?cursor='+quote(first['next_cursor']),400,actor=alice) results['concurrent_messages']='100/100; pages of 7; no loss or duplicates; cross-chat cursor rejected' # Real cache use, expiry and fallback; cached profiles never authenticate tokens. c.call('GET','/api/v1/users/me',actor=alice,schema='User') cache_ttl=int(compose('exec','-T','valkey','valkey-cli','TTL','user:'+alice['id']).strip()) check(0 < cache_ttl <= 60,'Profile cache not used') results['cache_ttl_seconds']=cache_ttl try: compose('stop','valkey') response=c.call('GET','/api/v1/users/me',actor=alice,schema='User') c.message(alice,chat,'Cache unavailable') c.call('GET','/api/v1/users/me',401) c.call('GET','/health/ready',schema='Health') results['valkey_outage']={'fallback_status':response['status'],'latency_seconds':round(response['elapsed'],4),'anonymous':401,'ready':200} finally: compose('start','valkey') c.call('GET','/api/v1/users/me',actor=alice,schema='User') before_msg=c.message(alice,chat,'Before DB outage')['json'] try: compose('stop','postgres') response=c.call('GET','/api/v1/users/me',503,actor=alice) check(response['elapsed']<8,'DB outage not bounded') c.call('GET','/health/ready',503) c.call('GET','/health/live',schema='Health') c.call('GET','/api/v1/users/me',401) results['database_outage']={'authenticated':503,'latency_seconds':round(response['elapsed'],3),'anonymous':401,'live':200,'ready':503} finally: compose('start','postgres') results['database_recovery_seconds']=wait_ready(c) history=c.call('GET',path+'/messages?limit=100',actor=alice,schema='MessagePage')['json'] while history['next_cursor']: history=c.call('GET',path+'/messages?limit=100&cursor='+quote(history['next_cursor']),actor=alice,schema='MessagePage')['json'] check(before_msg in history['items'],'Committed message lost after outage') results['committed_message_after_outage']='PASS' 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) results['logout']='PASS even with cached profile' # Independent API, same database, SESSION_TTL_SECONDS=2. compose('--profile','test','up','-d','expiry-api') try: short=Client('http://localhost:8082') wait_ready(short) actor=short.user('ttl') short.call('GET','/api/v1/users/me',actor=actor,schema='User') time.sleep(2.2) short.call('GET','/api/v1/users/me',401,actor=actor) results['expiry']='PASS: SESSION_TTL_SECONDS=2' finally: compose('--profile','test','stop','expiry-api') counts=sql('SELECT (SELECT count(*) FROM users),(SELECT count(*) FROM chats),(SELECT count(*) FROM messages),(SELECT count(*) FROM sessions)') compose('run','--rm','migrate') compose('run','--rm','migrate') check(counts==sql('SELECT (SELECT count(*) FROM users),(SELECT count(*) FROM chats),(SELECT count(*) FROM messages),(SELECT count(*) FROM sessions)'),'Migrations duplicated data') results['migration_reapply']='PASS twice; all entity counts unchanged' (ROOT/'evidence/student-tests.json').write_text(json.dumps(results,ensure_ascii=False,indent=2)+'\n') print(json.dumps(results,ensure_ascii=False,indent=2)) print('PASS: all level 4 integration checks') if __name__=='__main__': main()