Files
compras/scripts/check-public-demo-evidence.py
T
nala 47649bd46c
tests / ci (push) Canceled after 0s
commit compras1 3011
2026-11-30 09:33:36 +02:00

74 lines
5.4 KiB
Python

"""Native private audit and signed-contract exercise, synthetic company only."""
import hashlib
import importlib.util
import io
import json
from pathlib import Path
import sys
import uuid
import zipfile
from datetime import date
spec = importlib.util.spec_from_file_location('demo', '/root/check-public-demonstration.py')
m = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
assert sys.argv[1] == '--exercise-authorized-synthetic'
origin = sys.argv[2] if len(sys.argv) > 2 else 'http://127.0.0.1:49815'
credentials = json.loads(Path('/root/compras-demo-access.json').read_text())
clients = {key: m.Client(credentials[key], origin) for key in ['admin', 'direction', 'auditor', 'requester']}
auditor = clients['auditor']
page = auditor.request('/auditoria/privada')
assert page['status'] == 200
exports = page['page']['props']['exports']['data']
if not exports or '--refresh-audit' in sys.argv:
result = auditor.request('/auditoria/privada', 'POST', {'operation_key': str(uuid.uuid4()), 'families': ['purchasing', 'stock', 'finance', 'configuration'],
'purpose': 'Pacote fictício para apresentação e conferência de integridade.'})
assert result['status'] in [302, 303], f"Audit generation failed: {result['status']} {result['text'][:600]}"
exports = auditor.request('/auditoria/privada')['page']['props']['exports']['data']
export = exports[0]
download = auditor.request(export['download_url'], inertia=False)
assert download['status'] == 200 and hashlib.sha256(download['raw']).hexdigest() == export['sha256']
with zipfile.ZipFile(io.BytesIO(download['raw'])) as z:
assert z.testzip() is None and 'manifesto.json' in z.namelist()
manifest = json.loads(z.read('manifesto.json'))
for entry in [*manifest['datasets'], *manifest['originals']]:
assert hashlib.sha256(z.read(entry['file'])).hexdigest() == entry['sha256']
audit_members = len(z.namelist())
assert clients['requester'].request(export['download_url'])['status'] == 403
direction = clients['direction']
page = direction.request('/pessoas/1')
assert page['status'] == 200 and page['page']['props']['canFormalize']
record = page['page']['props']['process']
if record['status'] == 'contract_draft':
stream = b'BT /F1 16 Tf 30 700 Td (DEMONSTRACAO - DOCUMENTO FICTICIO) Tj 0 -30 Td (Sem contrato, assinatura ou admissao real.) Tj ET'
objects = [b'<< /Type /Catalog /Pages 2 0 R >>', b'<< /Type /Pages /Kids [3 0 R] /Count 1 >>',
b'<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>',
b'<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>', b'<< /Length '+str(len(stream)).encode()+b' >>\nstream\n'+stream+b'\nendstream']
pdf = b'%PDF-1.4\n'; offsets = []
for i, obj in enumerate(objects, 1):
offsets.append(len(pdf)); pdf += str(i).encode()+b' 0 obj\n'+obj+b'\nendobj\n'
start = len(pdf); pdf += b'xref\n0 6\n0000000000 65535 f \n'
for offset in offsets: pdf += f'{offset:010d} 00000 n \n'.encode()
pdf += b'trailer\n<< /Size 6 /Root 1 0 R >>\nstartxref\n'+str(start).encode()+b'\n%%EOF\n'
boundary = 'compras-demo-'+uuid.uuid4().hex
fields = {'version': str(record['version']), 'operation_key': str(uuid.uuid4()), 'signed_on': date.today().isoformat(),
'reference': 'DEMO-ASSINADO-001', 'reason': 'Confirmação fictícia pela direcção, exclusivamente para apresentação.', 'no_conflict': '1'}
body = b''
for key, value in fields.items(): body += f'--{boundary}\r\nContent-Disposition: form-data; name="{key}"\r\n\r\n{value}\r\n'.encode()
body += f'--{boundary}\r\nContent-Disposition: form-data; name="signed_document"; filename="demonstracao.pdf"\r\nContent-Type: application/pdf\r\n\r\n'.encode()+pdf+f'\r\n--{boundary}--\r\n'.encode()
content_type = 'multipart/form-data; boundary='+boundary
assert clients['admin'].request('/pessoas/1/formalizar', 'POST', body, content_type=content_type)['status'] == 403
result = direction.request('/pessoas/1/formalizar', 'POST', body, content_type=content_type)
assert result['status'] in [302, 303], f"Formalization failed: {result['status']} {result['text'][:600]}"
assert direction.request('/pessoas/1/formalizar', 'POST', body, content_type=content_type)['status'] in [302, 303]
record = direction.request('/pessoas/1')['page']['props']['process']
assert record['status'] == 'contract_formalized' and 'path' not in record['contract']['signed_document']
signed = direction.request('/pessoas/1/contrato-assinado', inertia=False)
assert signed['status'] == 200 and hashlib.sha256(signed['raw']).hexdigest() == record['contract']['signed_document']['sha256']
assert clients['requester'].request('/pessoas/1/contrato-assinado')['status'] in [403, 404]
proof = {'state': 'passed', 'origin': origin, 'normal_login_sessions': 4, 'audit_export_id': export['id'], 'audit_zip_sha256': export['sha256'],
'audit_zip_members_verified': audit_members, 'audit_unauthorized_download_refused': True, 'people_formalized_by_direction': True,
'admin_without_direction_authority_refused': True, 'formalization_replay_one_effect': True, 'signed_document_hash_verified': True,
'signed_document_private_path_redacted': True, 'unauthorized_contract_download_refused': True, 'employee_admission_or_rh_dispatch': False}
Path('/var/www/compras-deploy/demonstration-20261129/evidence-check.json').write_text(json.dumps(proof, indent=2))
print(json.dumps(proof, indent=2))