"""Separate authenticated QA disclosure, real originals, and sector dossier continuity.""" import hashlib import json import sys import uuid from datetime import datetime, timezone from pathlib import Path from portal_profiles_http_session import BASE, EXPECTED, PRIVATE, ROOT, Session, actors, events proof = {'started_at': datetime.now(timezone.utc).isoformat(), 'target': EXPECTED, 'base': BASE, 'interface_observed': False, 'active_writes': 0, 'phase': 'initialization'} proof_path = PRIVATE / 'legacy-sectors-http-qa.json' previous = json.loads(proof_path.read_text(encoding='utf-8')) if proof_path.exists() else {} if previous: archive = PRIVATE / ('legacy-sectors-http-attempt-' + previous['started_at'].replace(':', '-') + '.json') if not archive.exists(): archive.write_text(json.dumps(previous, ensure_ascii=False, indent=2), encoding='utf-8') def save(): proof_path.write_text(json.dumps(proof, ensure_ascii=False, indent=2), encoding='utf-8') def run(): receipt = json.loads((PRIVATE / 'legacy-sectors-verify-qa.json').read_text(encoding='utf-8')) manifest = json.loads((ROOT / 'storage/app/private/document-import-staging/legacy-setores-20261010/manifest.json').read_text(encoding='utf-8')) assert receipt['target'] == {'database': 'desk_restore_20261009_174726_test', 'port': 55441} assert len(receipt['resources']) == 22 and all(c['clean_scan_for_hash'] and c['stored_original_equal'] for c in receipt['checks']) actors['library_manager'] = json.loads((PRIVATE / 'resource-http-actors-private.json').read_text(encoding='utf-8'))['manager'] anonymous = Session('outsider') manager = Session('library_manager').login() rows = receipt['resources'] proof['phase'] = 'separate_authenticated_disclosure' proof['publication_actions'] = [] for row in rows: record = next(r for r in manifest['documents'] if r['external_id'] == row['external_id']) assert hashlib.sha256(manager.call('GET', f"/management/documents/{row['document_id']}/download", binary=True)).hexdigest() == row['sha256'].strip() if record['external_id'].startswith('legacy-tempo-post-171994-'): assert not row['published'] anonymous.call('GET', f"/documentos/{row['id']}/view", status=404, binary=True) proof['empty_finance_private'] = {'resource_id': row['id'], 'external_id': row['external_id'], 'published': False} continue if not row['published']: anonymous.call('GET', f"/documentos/{row['id']}/view", status=404, binary=True) data = {k: record[k] for k in ['title', 'issuer', 'category', 'topic', 'document_type', 'source_reference', 'source_url', 'language', 'version_label', 'description']} data.update({'document_id': row['document_id'], 'document_date': record.get('document_date'), 'metadata_version': row['metadata_version'], 'territorial_scope': 'unknown', 'source_confirmation': 'identified', 'legal_status': 'not_applicable', 'rights_status': 'authorized', 'data_classification': 'public', 'independent_review': False, 'legislation': False, 'essential': False, 'source_reference': record['source_reference'] + '; utilização e divulgação do legado exclusivamente em QA autorizadas pelo responsável em 10/10/2026; sem habilitação ativa.'}) if record['external_id'].startswith('legacy-tempo-post-'): source = json.loads((ROOT / 'storage/app/private/document-import-staging' / record['relative_path']).read_text(encoding='utf-8')) image = next((r for r in rows if r['sha256'].strip() == source.get('image_sha256')), None) if image: data['associations'] = [{'target_type': 'document', 'target_key': str(image['id']), 'relationship': 'related_to', 'applicability': 'support', 'basis_reference': 'Thumbnail identificada no exemplar original do post: ' + image['source_url'], 'review_status': 'confirmed'}] manager.call('PUT', f"/management/portal-services/resources/{row['id']}", data, status=302, binary=True) manager.call('POST', f"/management/portal-services/resources/{row['id']}", {'published': True}, binary=True) proof['publication_actions'].append({'resource_id': row['id'], 'actor_id': actors['library_manager']['id'], 'authenticated': True, 'mfa_challenge_observed': any(e['actor_id'] == actors['library_manager']['id'] and e['path'] == '/two-factor-challenge' and e['status'] == 204 for e in events)}) save() assert hashlib.sha256(anonymous.call('GET', f"/documentos/{row['id']}/download", binary=True)).hexdigest() == row['sha256'].strip() proof['phase'] = 'public_consumers' articles = {} for group, expected in [('energia-mineracao', 3), ('turismo', 12), ('infraestruturas', 1)]: landing = anonymous.call('GET', '/explorar/' + group, inertia=True)['props'] assert len(landing['articles']) == expected for brief in landing['articles']: article = anonymous.call('GET', brief['href'], inertia=True)['props']['article'] assert article['html'].strip() and article['image'] and not article['calculation_use'] assert article['territorial_references'] == [] assert hashlib.sha256(anonymous.call('GET', article['image']['href'], binary=True)).hexdigest() == article['image']['sha256'] anonymous.call('GET', article['library_href'], inertia=True) anonymous.call('GET', article['start_href'], status=401, binary=True) articles[brief['href']] = article missing = anonymous.call('GET', '/explorar/' + group + '?q=zzzz-no-content-qa', inertia=True)['props'] assert not missing['match'] and missing['articles'] == [] hit = anonymous.call('GET', '/explorar/energia-mineracao?q=Energia', inertia=True)['props'] assert hit['match'] and any(a['href'] == '/energia' for a in hit['articles']) assert articles['/energia']['id'] != articles['/mineracao']['id'] assert 'href="/energia"' in articles['/energia-mineracao']['html'] and 'href="/mineracao"' in articles['/energia-mineracao']['html'] assert '