Files
Investidor-V2/outputs/investmoz-audit-current-20261012/consumer-lot-lib.py
T
nala 287e991486
tests / ci (push) Waiting to run
oportunidades 7
2026-10-13 11:40:29 +02:00

46 lines
3.4 KiB
Python

"""Authorized isolated QA transport; never logs bodies, cookies, MFA or signed query strings."""
from pathlib import Path
from urllib.parse import urlsplit,unquote
from urllib.request import Request
from urllib.error import HTTPError
import json,time,uuid,sys
sys.argv=['audit-http.py','promoter']
scope={'__file__':str(Path(__file__).with_name('audit-http.py'))}
exec(Path(scope['__file__']).read_text(encoding='utf-8-sig').split("proof={'at'")[0],scope)
Session=scope['Session'];actors=scope['actors'];page=scope['page'];otp=scope['otp'];BASE=scope['BASE'];TARGET=scope['TARGET'];OUT=scope['OUT'];PRIVATE=scope['PRIVATE']
bucket_file=OUT/'consumer-lot-auth-buckets.json'
events=[];buckets=json.loads(bucket_file.read_text(encoding='utf-8'))if bucket_file.exists()else {}
class Client(Session):
def call(self,method,path,data=None,expected=200,json_response=True,file=None):
assert path.startswith('/')and not path.startswith('//');headers={'Accept':'application/json'if json_response else'text/html','Accept-Encoding':'identity'}
if data is not None:headers['X-XSRF-TOKEN']=next((unquote(c.value)for c in self.cookies if c.name=='XSRF-TOKEN'),'')
if file is None:
headers['Content-Type']='application/json';payload=None if data is None else json.dumps(data).encode('utf-8')
else:
boundary='QA'+uuid.uuid4().hex;chunks=[]
for k,v in data.items():chunks.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{k}"\r\n\r\n{v}\r\n'.encode('utf-8'))
p=Path(file);assert p.name in ['qa-consumer-original-20261012.png','qa-franchise-license-20261012.pdf','qa-project-license-20261013.pdf'];mime='application/pdf'if p.suffix=='.pdf'else'image/png';chunks.append(f'--{boundary}\r\nContent-Disposition: form-data; name="file"; filename="{p.name}"\r\nContent-Type: {mime}\r\n\r\n'.encode()+p.read_bytes()+f'\r\n--{boundary}--\r\n'.encode());payload=b''.join(chunks);headers['Content-Type']='multipart/form-data; boundary='+boundary
try:r=self.client.open(Request(BASE+path,data=payload,headers=headers,method=method),timeout=40)
except HTTPError as e:r=e
with r:raw=r.read();status=r.code;meta=r.headers
assert meta.get('X-Investmoz-QA-Target')==TARGET,'Target header mismatch'
entry={'method':method,'route':urlsplit(path).path,'status':status,'expected':expected}
if status==422:
try:entry['error_fields']=list(json.loads(raw).get('errors',{}))
except ValueError:pass
events.append(entry)
if status!=expected:raise AssertionError('Unexpected response status')
if expected!=200 and expected!=201:return None
return json.loads(raw)if json_response else page(raw)['props']
def login(self,role):
st,_,body=self.request('GET','/_qa/runtime',check_target=False);assert st==200 and json.loads(body)['target']['database']=='stability_consumers_20261011_084534_test'
a=actors[role];self.call('GET','/login',json_response=False);self.call('POST','/login',{'email':a['email'],'password':a['password']},expected=200)
self.call('GET','/two-factor-challenge',json_response=False)
if buckets.get(role)==int(time.time())//30:time.sleep(31-time.time()%30)
elif time.time()%30>26:time.sleep(4)
buckets[role]=int(time.time())//30
bucket_file.write_text(json.dumps(buckets),encoding='utf-8')
self.call('POST','/two-factor-challenge',{'code':otp(a.get('totp_secret',a.get('secret')))},expected=204)
p=self.call('GET','/dashboard',json_response=False);assert p['auth']['user']['id']==a['id'];return self
def logout(self):self.call('POST','/logout',{},expected=204)