83 lines
8.0 KiB
Python
83 lines
8.0 KiB
Python
from pathlib import Path
|
|
import json,re,urllib.request,urllib.parse,http.cookiejar,urllib.error,uuid,hashlib,hmac,base64,struct,time,copy,subprocess
|
|
O=Path(__file__).parent
|
|
F=json.loads(Path('C:/Users/macbook/.config/investmoz/classificacao-QA-20261015/fixture-private.json').read_text(encoding='utf8'))
|
|
BASE=F['url'];ID=F['record_id'];CHECKS={}
|
|
def check(k,v):
|
|
if not v: raise AssertionError(k)
|
|
CHECKS[k]=True
|
|
class NoRedirect(urllib.request.HTTPRedirectHandler):
|
|
def redirect_request(self,*args,**kwargs):return None
|
|
def session():
|
|
jar=http.cookiejar.CookieJar();return jar,urllib.request.build_opener(urllib.request.HTTPCookieProcessor(jar),NoRedirect())
|
|
def call(s,path,data=None,method=None):
|
|
jar,op=s;headers={'Accept':'application/json'};body=None
|
|
if data is not None:
|
|
body=json.dumps(data).encode();headers['Content-Type']='application/json';headers['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(BASE+path,data=body,headers=headers,method=method),timeout=60)
|
|
except urllib.error.HTTPError as e:r=e
|
|
raw=r.read()
|
|
try:v=json.loads(raw)
|
|
except:v={'raw_length':len(raw)}
|
|
return r.status,r.headers,v
|
|
def login(role):
|
|
s=session();call(s,'/login');a=F['actors'][role];st,h,v=call(s,'/login',{'email':a['email'],'password':a['password']})
|
|
check(role+'_login',st in [200,204,302])
|
|
if a['secret']:
|
|
n=int(time.time()/30);b=hmac.new(base64.b32decode(a['secret']),struct.pack('>Q',n),hashlib.sha1).digest();off=b[-1]&15;code=str((struct.unpack('>I',b[off:off+4])[0]&0x7fffffff)%1000000).zfill(6)
|
|
st,h,v=call(s,'/two-factor-challenge',{'code':code})
|
|
if st not in [200,204,302]:print(json.dumps({'MFA_status':st,'message':v.get('message'),'errors':v.get('errors')}))
|
|
check(role+'_MFA_completed',st in [200,204,302])
|
|
return s
|
|
def listing(s):
|
|
st,h,v=call(s,f'/minhas-oportunidades/{ID}/classification');check('listing_HTTP200',st==200);return v
|
|
def save(s,payload,etag=None):
|
|
if etag is None:etag=listing(s)['etag']
|
|
return call(s,f'/minhas-oportunidades/{ID}/draft',{'etag':etag,'canonical':payload,'reason':'Verificação dirigida sintética de associações na QA isolada.'},'PUT')
|
|
def review(s,hash,state,etag=None):
|
|
if etag is None:etag=listing(s)['etag']
|
|
return call(s,f'/minhas-oportunidades/{ID}/classification/review',{'etag':etag,'hash':hash,'state':state,'reason':'Decisão sintética independente desta associação e versão na QA.'})
|
|
P=login('promoter');R=login('reviewer');X=login('outsider');A=session()
|
|
data=listing(P);check('nine_simultaneous_relations',len(data['rows'])==9);check('four_dimensions',len(set(r['dimension'] for r in data['rows']))==4);check('all_preliminary',all(r['state']=='preliminary' for r in data['rows']));check('promoter_cannot_review_UI_metadata',not any(r['can_review'] for r in data['rows']))
|
|
st,h,v=save(P,F['payload']);check('preliminary_save_without_editorial_transition',st==200)
|
|
st,h,v=save(P,F['payload']);check('repeated_save_no_relationship_duplicates',st==200 and len(listing(P)['rows'])==9)
|
|
fresh=login('promoter');check('new_session_recovers_all_values',listing(fresh)['rows'][0]['relation']==F['payload']['relations'][0])
|
|
st,h,v=call(X,f'/minhas-oportunidades/{ID}/classification');check('outsider_read_403',st==403)
|
|
st,h,v=save(X,F['payload'],data['etag']);check('outsider_edit_403',st==403)
|
|
st,h,v=review(P,data['rows'][0]['hash'],'confirmed');check('author_self_confirmation_403',st==403)
|
|
st,h,v=review(X,data['rows'][0]['hash'],'confirmed',data['etag']);check('outsider_confirmation_403',st==403)
|
|
st,h,v=save(R,F['payload'],data['etag']);check('reviewer_cannot_edit_promoter_draft',st==403)
|
|
bad=copy.deepcopy(F['payload']);bad['relations'].append(bad['relations'][0]);st,h,v=save(P,bad);check('duplicate_relation_422',st==422)
|
|
bad=copy.deepcopy(F['payload']);bad['relations'][0]['classification']['confirmed']=True;st,h,v=save(P,bad);check('client_certification_422',st==422)
|
|
bad=copy.deepcopy(F['payload']);bad['relations'][0]['related_id']='invalid-reference';st,h,v=save(P,bad);check('invalid_reference_visible_422_not_500',st==422)
|
|
bad=copy.deepcopy(F['payload']);bad['relations'][4]['classification']['external_status']='documented_granted';st,h,v=save(P,bad);check('benefit_grant_without_act_422',st==422)
|
|
before=listing(R);hash0=before['rows'][0]['hash'];st,h,v=review(R,hash0,'confirmed');check('independent_confirmation_200',st==200)
|
|
after=listing(R);check('one_confirmation_other_eight_preliminary',sum(r['state']=='confirmed' for r in after['rows'])==1)
|
|
st,h,v=review(R,hash0,'rejected');check('rejection_recorded',st==200)
|
|
st,h,v=review(R,hash0,'confirmed');check('confirmation_after_reasoned_rejection',st==200)
|
|
st,h,v=review(R,hash0,'revoked');check('revocation_recorded',st==200)
|
|
st,h,v=review(R,hash0,'confirmed');check('new_reasoned_confirmation_preserves_history',st==200 and len(listing(R)['rows'][0]['history'])==5)
|
|
st,h,v=review(R,hash0,'confirmed',before['etag']);check('stale_decision_409',st==409)
|
|
# An incomplete source is allowed as preliminary, but it cannot acquire confirmation.
|
|
missing=copy.deepcopy(F['payload']);missing['relations'][1]['classification']['citation']='';st,h,v=save(P,missing);check('incomplete_evidence_preliminary_save',st==200)
|
|
hash_missing=listing(R)['rows'][1]['hash'];st,h,v=review(R,hash_missing,'confirmed');check('incomplete_evidence_confirmation_422',st==422)
|
|
st,h,v=save(P,F['payload']);check('restore_original_relation_content',st==200)
|
|
pub=json.loads(subprocess.check_output(['C:/laragon/bin/php/php-8.5.11-Win32-vs17-x64/php.exe',str(O/'public-fixture-QA.php')],text=True,encoding='utf8'))
|
|
check('public_fixture_explicitly_not_editorial_retest',pub['observed_editorial_transition'] is False)
|
|
(O/'PUBLIC-FIXTURE-QA.json').write_text(json.dumps(pub,indent=2),encoding='utf8')
|
|
slug=pub['slug']
|
|
st,h,v=call(A,'/api/v1/investment/opportunities/'+slug);check('public_detail_API_200',st==200)
|
|
rows=v['context']['classification'];check('public_eight_relations_excludes_reserved',len(rows)==8 and 'QA_PRIVATE_SOURCE_NEVER_PUBLIC' not in json.dumps(v));check('benefit_not_presented_available',all(r['availability_confirmed'] is False for r in rows));check('documents_authors_private',all('document_id' not in r and 'actor_id' not in r for r in rows))
|
|
params={'association_type':'corridor','association_target':F['payload']['relations'][0]['related_id'],'association_state':'confirmed','chain_id':F['chain_id'],'category':'investment_project'}
|
|
st,h,v=call(A,'/api/v1/investment/search?'+urllib.parse.urlencode(params));check('combined_filters_AND_across_relations',st==200 and v['total']==1 and v['data'][0]['id']==ID)
|
|
check('restricted_opportunity_excluded',pub['restricted_fixture_id'] not in [r['id'] for r in v['data']])
|
|
st,h,m=call(A,'/api/v1/investment/map?'+urllib.parse.urlencode(params));check('map_count_matches_catalogue',st==200 and m['matched_count']==1)
|
|
badparams=params|{'association_state':'rejected'};st,h,v=call(A,'/api/v1/investment/search?'+urllib.parse.urlencode(badparams));check('mismatched_confirmation_zero',st==200 and v['total']==0)
|
|
# Corrections preserve the published classification until another editorial publication.
|
|
changed=copy.deepcopy(F['payload']);changed['relations'][0]['classification']['citation']='Correção sintética privada, secção 2';st,h,v=save(P,changed);check('draft_correction_save',st==200)
|
|
current=listing(R);check('corrected_relation_not_automatically_confirmed',current['rows'][0]['state']=='preliminary');st,h,v=call(A,'/api/v1/investment/opportunities/'+slug);check('private_draft_correction_not_public',v['context']['classification'][0]['citation']=='Caso de teste corridor, secção 1')
|
|
st,h,v=review(R,hash0,'revoked');check('revoke_previous_published_version_after_draft_correction',st==200);st,h,v=call(A,'/api/v1/investment/search?'+urllib.parse.urlencode(params));check('revoked_relation_removed_from_filter',st==200 and v['total']==0)
|
|
(O/'HTTP-DELTA-QA.json').write_text(json.dumps({'result':'passed','checks':CHECKS,'count':len(CHECKS),'record_id':ID,'public_fixture':pub,'editorial_workflow_retested':False,'active_changes':0},ensure_ascii=False,indent=2),encoding='utf8')
|
|
print(json.dumps({'result':'passed','checks':len(CHECKS),'record_id':ID}))
|
|
|