#!/usr/bin/env python3 """Own-project cache loss/restart, expiry while down, and log disclosure checks.""" import json, sys, time, uuid from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parents[1])) from grade4 import compose, wait_ready sys.path.insert(0,str(Path(__file__).resolve().parents[2]/'tests')) from client import Client,check def main(): c=Client('http://api:8080');short=Client('http://api-expiry:8080') a,b,eve,chat=c.bootstrap();expired=short.user('downexpiry');revoked=c.user('downrevoked') key='profile:'+b['id'];marker='g5_private_body_'+uuid.uuid4().hex first=c.message(a,chat,marker)['json'] response=c.call('POST','/api/v1/chats/'+chat['id']+'/messages',400,actor=a,body={'text':marker,'sender_id':eve['id']}) check(marker.encode() not in response['bytes'],'error echoes sensitive input') c.call('GET','/api/v1/users/me',actor=b,schema='User') check(compose('exec','-T','valkey','valkey-cli','DEL',key)=='1','own cache loss not applied') try: compose('stop','valkey');time.sleep(2.2) start=time.monotonic() c.call('GET','/api/v1/users/me',actor=b,schema='User') c.call('GET','/api/v1/users/me',401,actor=expired) c.call('DELETE','/api/v1/auth/sessions/current',204,actor=revoked) c.call('GET','/api/v1/users/me',401,actor=revoked) c.call('GET','/api/v1/users/me',401) second=c.message(b,chat,'synthetic during outage')['json'] page=c.call('GET','/api/v1/chats/'+chat['id']+'/messages',actor=a,schema='MessagePage')['json'] check(first in page['items'] and second in page['items'],'outage RAW') print('PASS: Valkey down: valid user/read-after-write work, expired/revoked/anonymous 401, errors omit input; %.3fs'%(time.monotonic()-start)) finally:compose('start','valkey') recovered=time.monotonic() for _ in range(40): if compose('exec','-T','valkey','valkey-cli','PING')=='PONG':break time.sleep(.1) check(compose('exec','-T','valkey','valkey-cli','EXISTS',key)=='0','deleted own key unexpectedly restored') time.sleep(5.1) # Existing bounded optional-call circuit opens for five seconds. c.call('GET','/api/v1/users/me',actor=b,schema='User') check(json.loads(compose('exec','-T','valkey','valkey-cli','GET',key))==b['user'],'no PG refill after restart/cooldown') check(0