59 lines
4.5 KiB
Python
59 lines
4.5 KiB
Python
from pathlib import Path
|
|
import json,sys,urllib.request,urllib.error,urllib.parse,http.cookiejar,hmac,hashlib,base64,struct,time
|
|
from html.parser import HTMLParser
|
|
out=Path(__file__).resolve().parent;f=json.loads(Path('C:/Users/macbook/.config/investmoz/promotor-contexto-QA-20261015/fixture-private.json').read_text('utf-8'))
|
|
phase=sys.argv[1];assert phase in ['before','after'] and f['url']=='http://127.0.0.1:18520'
|
|
checks={}
|
|
def check(name,value):
|
|
assert value,name
|
|
checks[name]=True
|
|
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
def redirect_request(self,*args,**kwargs):return None
|
|
def call(s,path,data=None,inertia=False,html=False):
|
|
jar,op=s;headers={'Accept':'text/html' if html else 'application/json'}
|
|
if inertia:headers['Accept']='text/html'
|
|
body=None
|
|
if data is not None:
|
|
body=json.dumps(data).encode();headers.update({'Content-Type':'application/json','X-XSRF-TOKEN':next((urllib.parse.unquote(c.value) for c in jar if c.name=='XSRF-TOKEN'),'')})
|
|
try:r=op.open(urllib.request.Request(f['url']+path,data=body,headers=headers),timeout=30)
|
|
except urllib.error.HTTPError as e:r=e
|
|
assert r.headers.get('X-QA-Target')=='promoter_context_20261015_test:55441'
|
|
raw=r.read();v=None
|
|
try:v=json.loads(raw)
|
|
except ValueError:
|
|
class Parser(HTMLParser):
|
|
def __init__(self):super().__init__();self.on=False;self.data=[]
|
|
def handle_starttag(self,t,a):
|
|
if t=='script' and dict(a).get('type')=='application/json':self.on=True
|
|
def handle_endtag(self,t):
|
|
if t=='script':self.on=False
|
|
def handle_data(self,d):
|
|
if self.on:self.data.append(d)
|
|
p=Parser();p.feed(raw.decode('utf-8'))
|
|
if p.data:v=json.loads(''.join(p.data))
|
|
return r.status,v,raw
|
|
def login(name):
|
|
jar=http.cookiejar.CookieJar();s=jar,urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar),NoRedirect());a=f['actors'][name]
|
|
call(s,'/login');st,_,_=call(s,'/login',{'email':a['email'],'password':a['password']});check(name+'_normal_login',st in [200,204,302])
|
|
b=hmac.new(base64.b32decode(a['secret']),struct.pack('>Q',int(time.time()/30)),hashlib.sha1).digest();off=b[-1]&15;code=str((struct.unpack('>I',b[off:off+4])[0]&0x7fffffff)%1000000).zfill(6)
|
|
st,_,_=call(s,'/two-factor-challenge',{'code':code});check(name+'_Fortify_MFA',st in [200,204,302]);return s
|
|
s=login('dual_promoter')
|
|
st,create,_=call(s,'/minhas-oportunidades/create',inertia=True)
|
|
if st!=(403 if phase=='before' else 200):print(json.dumps({'unexpected_create_status':st,'message':create.get('message') if isinstance(create,dict) else None,'component':create.get('component') if isinstance(create,dict) else None}))
|
|
check('create_status_'+('403_before' if phase=='before' else '200_after'),st==(403 if phase=='before' else 200))
|
|
st,edit,_=call(s,f"/minhas-oportunidades/{f['record_id']}/edit",inertia=True);check('own_editor_status_'+('403_before' if phase=='before' else '200_after'),st==(403 if phase=='before' else 200))
|
|
st,index,_=call(s,'/minhas-oportunidades',inertia=True);check('listing_HTTP200',st==200)
|
|
ids=[x['id'] for x in index['props']['records']['data']]
|
|
check('own_record_scope_'+phase,(f['record_id'] in ids)==(phase=='after'))
|
|
check('foreign_record_excluded',f['foreign_record_id'] not in ids)
|
|
st,_,_=call(s,f"/minhas-oportunidades/{f['foreign_record_id']}/edit",inertia=True);check('foreign_editor_403',st==403)
|
|
if phase=='after':
|
|
check('create_editor_component',create['component']=='management/opportunity-draft')
|
|
check('existing_editor_component',edit['component']=='management/opportunity-draft')
|
|
check('own_editor_is_editable',edit['props']['readOnly'] is False)
|
|
su=login('super_only');st,_,raw=call(su,'/minhas-oportunidades/create',html=True);check('superuser_without_promoter_still_403',st==403)
|
|
check('visible_reason_without_role_grant',b'contexto ativo de Promotor' in raw and b'perfil de Promotor' in raw)
|
|
(out/'QA-RECUSA-VISIVEL.html').write_bytes(raw)
|
|
proof={'phase':phase,'target':'promoter_context_20261015_test:55441','checks':checks,'count':len(checks),'authentication':'normal Fortify login and synthetic QA MFA','HTTP_business_saves':0,'workflow_transitions':0,'new_role_grants':0,'exceptional_elevations_requested':0,'active_changes':0,'DOC01_reinstalled':False,'accepted_tests_repeated':False}
|
|
(out/('HTTP-'+phase.upper()+'.json')).write_text(json.dumps(proof,ensure_ascii=False,indent=2)+'\n','utf-8');print(json.dumps({'phase':phase,'checks':len(checks),'HTTP_business_saves':0,'active_changes':0}))
|