"""Customer identity, correction and final-file trust boundaries against local stack.""" import os from uuid import uuid4 from urllib.request import urlopen from tests.smoke_test import Client, approved_quote, upload_bytes, item_spec, register def run(): customer=Client();other=Client();customer.call('/session');other.call('/session') uid=upload_bytes(customer,b'LOCAL ORIGINAL ONLY') item=item_spec('file','1.01',0,uid) profile={'cnpj':'11222333000181','zap':'11999999999','mail':'workflow-'+uuid4().hex[:8]+'@example.test'} q=customer.call('/quotes',{'request_key':str(uuid4()),'customer':profile,'items':[item],'freight':{'service':'pickup'}}) approved_quote(customer,q,[item]) order=customer.call('/orders/dev-paid',{'quote_id':q['id']});oid=order['id'] # A Site order is born marked as one (Dropstar's rule D-21). assert next(o for o in customer.call('/operator/board',operator=True)['orders'] if o['id']==oid)['origin']=='site' before=list(customer.jar)[0].value password='local-test-password-'+uuid4().hex register(customer,profile,password) assert customer.call('/account/me')['customer']['mail']==profile['mail'] assert [e['id'] for e in customer.call('/customer/orders')['items'] if e['kind']=='order'][0]==oid # The list filters, pages and counts by group. listing=customer.call('/customer/orders?status=prod&page=1&size=1') assert listing['counts']['prod']>=1 and listing['total']==listing['counts']['prod'] and len(listing['items'])==1, listing assert customer.call('/customer/orders?status=fin')['items']==[] assert customer.call('/customer/orders?number='+str(order['number']))['items'][0]['id']==oid # The account's details change; the CNPJ does not, and e-mail and password need the password. address={'recipient':'Workflow Ltda','street':'Rua de Teste','number':'10','complement':'', 'district':'Centro','city':'Franca','state':'SP','postal_code':'14400000'} saved=customer.call('/account/profile',{'zap':'(16) 98888-7777','address':address})['customer'] assert saved['zap']=='16988887777' and saved['cnpj']==profile['cnpj'] and saved['address']['city']=='Franca' assert customer.call('/account/me')['customer']['address']['postal_code']=='14400000' customer.call('/account/profile',{'zap':'16988887777','cnpj':'00000000000000'},expected=422) customer.call('/account/email',{'email':'x-'+profile['mail'],'password':'wrong-password'},expected=401) customer.call('/account/password',{'current':'wrong-password','new':'another-password-123'},expected=401) other.call('/account/profile',{'zap':'16988887777'},expected=401) print('PASS: customer order list filters and pages; account details change, CNPJ does not') # Email/CNPJ do not grant ownership; only current guest session is migrated. other.call('/customer/orders/'+oid,expected=404) other.call('/account/login',{'email':profile['mail'],'password':'wrong-password'},expected=401) revoked=Client() import http.cookiejar cookie=http.cookiejar.Cookie(0,'dtf_session',before,None,False,'localhost.local',False,False,'/',True,False,None,True,None,None,{},False) revoked.jar.set_cookie(cookie) revoked.call('/customer/orders',expected=401) account_scope=customer.call('/session')['cart_scope'] cookie.value=account_scope;revoked.jar.set_cookie(cookie) revoked.call('/customer/orders',expected=401) other.call('/account/login',{'email':profile['mail'],'password':password}) assert other.call('/customer/orders/'+oid)['id']==oid print('PASS: registration claims only current guest records, cross-session account login, revoked sessions, owner UUID is not a credential') def move(state,version): return customer.call('/operator/orders/'+oid+'/move',{'state':state,'version':version,'reason':'Please replace the artwork' if state=='cor' else ''},operator=True)['version'] # Arte conferida is the print queue (2026-10-23): it takes approved finals only. version=0 customer.call('/operator/orders/'+oid+'/move',{'state':'tra','version':version},operator=True,expected=409) final_id=upload_bytes(customer,b'LOCAL FINAL VERSION ONE',order_id=oid) customer.call('/uploads/'+final_id,expected=404) body={'version':version,'files':[{'item_index':0,'upload_id':final_id}],'note':'Manually checked final'} customer.call('/operator/orders/'+oid+'/final-files',body,expected=401) version=customer.call('/operator/orders/'+oid+'/final-files',body,operator=True)['version'] # A final the team uploaded is not the customer's art: "Arte tratada" (2026-10-27). treated=lambda:next(o for o in customer.call('/operator/board',operator=True)['orders'] if o['id']==oid)['treated'] operator_email=os.environ.get('OPERATOR_EMAIL','operator@example.test') assert treated()=={'items':[0],'by':operator_email}, treated() detail=customer.call('/customer/orders/'+oid) final=detail['files'][0] link=customer.call('/customer/orders/'+oid+'/files/'+final['id']+'/download') assert urlopen(link['url']).read()==b'LOCAL FINAL VERSION ONE' guest=Client();guest.call('/session');guest.call('/customer/orders/'+oid+'/files/'+final['id']+'/download',expected=404) version=move('tra',version) # The print queue's files come named after their order, straight from storage. queued=customer.call('/operator/queue/finals',operator=True)['files'] mine=[f for f in queued if urlopen(f['url']).read()==b'LOCAL FINAL VERSION ONE'] assert len(mine)==1 and mine[0]['name'].startswith('DTF-%d-item-1-' % mine[0]['number']), queued customer.call('/operator/queue/finals',expected=401) version=move('imp',version);version=move('cor',version) customer.call('/customer/orders/'+oid+'/files/'+final['id']+'/download',expected=404) premature_final=upload_bytes(customer,b'FINAL BEFORE CUSTOMER CORRECTION',order_id=oid) version=customer.call('/operator/orders/'+oid+'/final-files', {'version':version,'files':[{'item_index':0,'upload_id':premature_final}], 'note':'Prepared before customer sent the new correction'},operator=True)['version'] correction_id=upload_bytes(customer,b'LOCAL CORRECTED ORIGINAL') payload={'version':version,'files':[{'item_index':0,'upload_id':correction_id}],'note':'Replaced the artwork as requested'} guest.call('/customer/orders/'+oid+'/corrections',payload,expected=404) customer.call('/customer/orders/'+oid+'/corrections',{**payload,'version':0},expected=409) version=customer.call('/customer/orders/'+oid+'/corrections',payload)['version'] files=customer.call('/operator/orders/'+oid+'/files',operator=True) assert any(f['kind']=='correction' and f['active'] and f['upload_id']==correction_id for f in files) assert not any(f['kind']=='final' and f['active'] for f in files), 'new correction kept a stale final active' customer.call('/operator/orders/'+oid+'/move',{'state':'tra','version':version},operator=True,expected=409) # The correction itself can be approved as the final of the item it corrects. version=customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':[{'item_index':0,'upload_id':correction_id}],'note':'Correction is print-ready'},operator=True)['version'] files=customer.call('/operator/orders/'+oid+'/files',operator=True) assert any(f['kind']=='final' and f['active'] and f['upload_id']==correction_id for f in files) # The customer's own correction approved as it is: not treated. assert treated() is None, treated() customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':[{'item_index':0,'upload_id':correction_id}],'note':'Twice'},operator=True,expected=409) new_final=upload_bytes(customer,b'LOCAL FINAL VERSION TWO',order_id=oid) version=customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':[{'item_index':0,'upload_id':new_final}],'note':'Checked corrected final'},operator=True)['version'] assert treated()=={'items':[0],'by':operator_email}, treated() stats=customer.call('/operator/stats?days=7',operator=True)['treated'] assert stats['orders']>=1 and any(t['orders']>=1 for t in stats['by']), stats for state in ('tra','imp','rev','fin'):version=move(state,version) # In the queue the finals stay as approved. customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':[{'item_index':0,'upload_id':new_final}],'note':'Too late'},operator=True,expected=409) detail=other.call('/customer/orders/'+oid) assert detail['state']=='fin' assert sum(f['active'] and f['kind']=='final' for f in detail['files'])==1 assert any(h['reason']=='Please replace the artwork' for h in detail['history']) print('PASS: final-file gate, final revisions, secure customer downloads, correction history/uploads, old final invalidation and reapproval') old_cookie=list(other.jar)[0].value other.call('/account/logout',{}) cookie.value=old_cookie;revoked.jar.set_cookie(cookie);revoked.call('/customer/orders',expected=401) other.call('/session');assert other.call('/customer/orders')['items']==[] print('PASS: logout revokes server session and signed-out visitors cannot see account orders') treating() def treating(): """"Tratar a arte" (Dropstar, 2026-10-29): the item's name in the folder, and a treated file that replaces only its own item's final.""" customer=Client();customer.call('/session') originals=[upload_bytes(customer,b'LOCAL TREAT ORIGINAL %d' % n) for n in (1,2)] items=[item_spec('file','1.01',0,uid) for uid in originals] profile={'cnpj':'11222333000181','zap':'11999999999','mail':'treat-'+uuid4().hex[:8]+'@example.test'} q=customer.call('/quotes',{'request_key':str(uuid4()),'customer':profile,'items':items,'freight':{'service':'pickup'}}) approved_quote(customer,q,items) order=customer.call('/orders/dev-paid',{'quote_id':q['id']});oid=order['id'];number=order['number'] files=lambda:[f for f in customer.call('/operator/orders/'+oid+'/files',operator=True) if f['kind']=='final' and f['active']] first=[upload_bytes(customer,b'LOCAL FIRST FINAL %d' % n,order_id=oid) for n in (1,2)] version=customer.call('/operator/orders/'+oid+'/final-files',{'version':0,'note':'Aprovado', 'files':[{'item_index':i,'upload_id':u} for i,u in enumerate(first)]},operator=True)['version'] # Where each item is in the stage folder, named as the folders save it. folder=customer.call('/operator/orders/'+oid+'/folder-files',operator=True) assert folder['state']=='rec' and [f['item'] for f in folder['files']]==[1,2], folder assert all(f['name'].startswith('DTF-%d-item-%d' % (number,f['item'])) for f in folder['files']), folder customer.call('/operator/orders/'+oid+'/folder-files',expected=401) # Saved over with the same name and sent back: item 1 changes, item 2 keeps its final. png=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR'+(6059).to_bytes(4,'big')+(2000).to_bytes(4,'big')+b'\x08\x06\x00\x00\x00'+b'\x00'*4 treated=upload_bytes(customer,png,name='DTF-%d-item-1.png' % number,order_id=oid) both=[{'item_index':0,'upload_id':treated},{'item_index':1,'upload_id':first[1]}] version=customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':both,'note':'Arte tratada'},operator=True)['version'] assert sorted((f['item_index'],f['upload_id']) for f in files())==[(0,treated),(1,first[1])], files() assert customer.call('/operator/orders/'+oid+'/folder-files',operator=True)['files'][0]['name']=='DTF-%d-item-1.png' % number # Nothing new: refused. customer.call('/operator/orders/'+oid+'/final-files',{'version':version,'files':both,'note':'De novo'},operator=True,expected=409) customer.call('/operator/orders/'+oid+'/cancel',{'note':'Teste de arte tratada'},operator=True) print('PASS: treating the art: the file in the folder, and one item replaced while the other keeps its final') if __name__=='__main__':run()