Files
mtusi2026lab2/student-tests/integration.py
T

133 lines
9.7 KiB
Python

#!/usr/bin/env python3
"""HTTP, SQL atomicity, dependency failures, persistence, and short TTL checks.
Only this lab's Compose services and synthetic users are affected.
"""
import argparse
import base64
from concurrent.futures import ThreadPoolExecutor
import json
import os
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
def compose(*args,env=None):
r=subprocess.run(['docker','compose','--env-file','.env',*args],cwd=ROOT,env=env,text=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
if r.returncode:raise RuntimeError('Compose command failed: '+args[0])
return r.stdout.strip()
def sql(q):return compose('exec','-T','postgres','psql','-X','-U','messenger','-d','messenger','-At','-v','ON_ERROR_STOP=1','-c',q)
def cache(*args):
# Password is read only inside container; never printed or put on host argv.
return compose('exec','-T','valkey','sh','-c','export REDISCLI_AUTH="$(sed -n \'s/^requirepass //p\' /run/secrets/valkey_config)"; exec valkey-cli "$@"','sh',*args)
def ready(c):
until=time.monotonic()+40
while time.monotonic()<until:
try:c.call('GET','/health/ready',schema='Health');return
except Exception:time.sleep(.2)
raise AssertionError('readiness timeout')
def containers():
return {s:json.loads(subprocess.check_output(['docker','inspect',compose('ps','-q',s)],text=True))[0]['State']['StartedAt'] for s in ['api','postgres','valkey']}
def main():
p=argparse.ArgumentParser();p.add_argument('--base-url',default='http://localhost:8082');args=p.parse_args()
c=Client(args.base_url,timeout=8);ready(c);evidence={'checks':{},'date_utc':time.strftime('%Y-%m-%dT%H:%M:%SZ',time.gmtime())}
def passed(name,value=True):evidence['checks'][name]=value;print('PASS: '+name,flush=True)
a,b,e,chat=c.bootstrap();path='/api/v1/chats/'+chat['id']+'/messages'
for body in [b'{"text":"a","text":"b"}',b'{"Text":"hi"}',b'{"text":null}',b'{"text":"a","sender_id":"x"}',b'{"text":"a"}{}',b'[]']:
c.call('POST',path,400,actor=a,raw=body,headers={'Content-Type':'application/json'})
c.call('POST',path,415,actor=a,raw=b'hi')
c.call('POST',path,413,actor=a,raw=b' '*(1024*1024+1),headers={'Content-Type':'application/json'})
c.call('POST',path,400,actor=a,body={'text':'🙂'*4001})
c.message(a,chat,'🙂'*4000)
# Full accepted password range reaches the HTTP KDF, including colons.
registration={'username':'unicode_'+uuid.uuid4().hex[:12],'display_name':'Unicode','password':'🙂'*127+':'}
c.call('POST','/api/v1/users',201,body=registration,schema='User')
credential=base64.b64encode((registration['username']+':'+registration['password']).encode()).decode()
c.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+credential},schema='Session')
passed('strict_json_unicode_body_limits')
# Real concurrent registration: database UNIQUE chooses exactly one winner.
reg={'username':'race_'+uuid.uuid4().hex[:16],'display_name':'Race','password':'Synthetic-Race-Password-123'}
def register(_):return Client(args.base_url).call('POST','/api/v1/users',[201,409],body=reg)['status']
with ThreadPoolExecutor(max_workers=4) as ex:statuses=list(ex.map(register,range(4)))
assert statuses.count(201)==1 and statuses.count(409)==3;passed('username_race',statuses)
rollback_title='Rollback_'+uuid.uuid4().hex
c.call('POST','/api/v1/chats',400,actor=a,body={'title':rollback_title,'member_ids':[a['id'],str(uuid.uuid4())]})
assert sql("SELECT count(*) FROM chats WHERE title='"+rollback_title+"'")=='0';passed('chat_rollback_after_fk_failure')
# 100 acknowledged concurrent messages, then keyset traversal.
def write(i):return Client(args.base_url).message(a,chat,'concurrent-'+str(i))['json']
with ThreadPoolExecutor(max_workers=10) as ex:sent=list(ex.map(write,range(100)))
items=[];url=path+'?limit=7'
while True:
page=c.call('GET',url,actor=b,schema='MessagePage')['json'];items.extend(page['items'])
if page['next_cursor'] is None:break
url=path+'?limit=11&cursor='+quote(page['next_cursor'],safe='')
assert len(items)==101 and len({m['id'] for m in items})==101
assert {m['id'] for m in sent}<={m['id'] for m in items}
assert items==sorted(items,key=lambda m:(m['created_at'],m['id']))
first=c.call('GET',path+'?limit=1',actor=a,schema='MessagePage')['json']
cursor=first['next_cursor'];other=c.call('POST','/api/v1/chats',201,actor=a,body={'title':'Other','member_ids':[a['id']]},schema='Chat')['json']
c.call('GET','/api/v1/chats/'+other['id']+'/messages?cursor='+quote(cursor,safe=''),400,actor=a)
c.call('GET',path+'?cursor=corrupt',400,actor=a)
passed('100_concurrent_messages_and_keyset_scope')
# Shared row clock remains monotonic even if its value lies in the future.
sql("UPDATE chats SET last_message_at=clock_timestamp()+interval '1 hour' WHERE id='"+chat['id']+"'")
future=c.message(a,chat,'future-clock-1')['json'];later=c.message(a,chat,'future-clock-2')['json']
assert later['created_at']>future['created_at'];passed('monotonic_chat_clock')
good=base64.b64encode((a['registration']['username']+':'+a['registration']['password']).encode()).decode()
bad=base64.b64encode((a['registration']['username']+':Synthetic-wrong-password').encode()).decode()
c.call('POST','/api/v1/auth/sessions',401,headers={'Authorization':'Basic '+bad})
c.call('GET','/api/v1/users/me',401,headers={'Authorization':'Basic '+good})
c.call('GET','/api/v1/users/me',401,headers={'X-User-Id':a['id']})
assert c.call('GET','/api/v1/users/me',actor=a,headers={'X-User-Id':e['id']},schema='User')['json']['id']==a['id']
c.call('GET',path,404,actor=e);passed('authentication_and_chat_authorization')
assert sql("SELECT bool_and(password_hash LIKE '$argon2id$%') FROM users")=='t'
assert sql('SELECT bool_and(octet_length(token_hash)=32) FROM sessions')=='t';passed('password_kdf_and_token_digest_storage')
c.call('GET','/api/v1/users/me',actor=a,schema='User')
ttl=int(cache('TTL','profile:v1:'+a['id']));assert 0<ttl<=300;passed('valkey_real_profile_cache_ttl',ttl)
cache('FLUSHDB');c.call('GET','/api/v1/users/me',actor=a,schema='User');assert int(cache('TTL','profile:v1:'+a['id']))>0
passed('cache_flush_repopulates_with_same_session')
compose('stop','valkey')
try:
c.call('GET','/health/ready',schema='Health');c.call('GET','/api/v1/users/me',actor=a,schema='User')
msg=c.message(a,chat,'cache-off-read-after-write')['json']
# Explicit small chat keeps complete post-write comparison unambiguous.
msg=c.message(a,other,'must-be-visible')['json'];assert msg in c.call('GET','/api/v1/chats/'+other['id']+'/messages',actor=a,schema='MessagePage')['json']['items']
c.call('GET','/api/v1/users/me',401,headers={'X-User-Id':a['id']})
passed('cache_down_fallback_and_read_after_write')
finally:compose('start','valkey')
compose('stop','postgres')
try:
live=c.call('GET','/health/live',schema='Health');ready_result=c.call('GET','/health/ready',503)
begin=time.monotonic();c.call('GET','/api/v1/users/me',503,actor=a);elapsed=time.monotonic()-begin
assert elapsed<4;passed('database_down_fails_closed',{'live':live['status'],'ready':ready_result['status'],'request_seconds':round(elapsed,3)})
finally:compose('start','postgres');ready(c)
# Revoke while profile remains cached, then retry with the exact token.
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=a)
c.call('GET','/api/v1/users/me',401,actor=a);c.call('DELETE','/api/v1/auth/sessions/current',401,actor=a)
passed('logout_with_warm_cache')
before_containers=containers();before_counts=sql('SELECT (SELECT count(*) FROM users),(SELECT count(*) FROM chats),(SELECT count(*) FROM messages),(SELECT count(*) FROM sessions),(SELECT count(*) FROM schema_migrations)')
subprocess.run(['make','test-persistence','ACTION_SCRIPT=scripts/restart.sh','BASE_URL='+args.base_url],cwd=ROOT,check=True)
after_containers=containers();assert all(before_containers[x]!=after_containers[x] for x in before_containers)
after_counts=sql('SELECT (SELECT count(*) FROM users),(SELECT count(*) FROM chats),(SELECT count(*) FROM messages),(SELECT count(*) FROM sessions),(SELECT count(*) FROM schema_migrations)')
assert sql('SELECT count(*) FROM schema_migrations')=='1'
c.call('GET','/api/v1/users/me',actor=b,schema='User')
# A cursor created before restart retains its HMAC key.
c.call('GET',path+'?limit=1&cursor='+quote(cursor,safe=''),actor=b,schema='MessagePage')
passed('full_stack_restart_and_cursor',{'before':before_containers,'after':after_containers,'counts_before':before_counts,'counts_after':after_counts})
# Separate short TTL configuration, no global DB edits and no token saved.
short_env=dict(os.environ,SESSION_TTL_SECONDS='2');compose('up','-d','--no-deps','--force-recreate','api',env=short_env);ready(c)
try:
short=c.user('expiry');c.call('GET','/api/v1/users/me',actor=short,schema='User');time.sleep(2.2)
c.call('GET','/api/v1/users/me',401,actor=short);passed('session_expiry_ttl_2_seconds')
# Existing long-lived session's expiry is not recomputed at API restart.
c.call('GET','/api/v1/users/me',actor=b,schema='User');passed('restart_does_not_shorten_existing_session')
finally:compose('up','-d','--no-deps','--force-recreate','api');ready(c)
evidence['result']='PASS';(ROOT/'evidence/integration.json').write_text(json.dumps(evidence,ensure_ascii=False,indent=2)+'\n')
print('PASS: all student integration checks')
if __name__=='__main__':main()