Files
dtf-system/tests/scanning_test.py
Cauê Faleiros 29b79db806 fix: a PDF's resolution is worked out at the scan, not at checkout
create_quote downloaded every PDF sheet (up to 150 MB) and parsed it inside
the customer's request, behind nginx's 30 s. The worker's scan already reads
every upload before the Site lets the customer continue, so it now stores
the PDF's DPI on the upload (uploads.pdf_checked, pdf_dpi) and the checkout
reads the number. An upload scanned before this, or whose work-out failed,
is read at checkout as before (Critical Corrections 2, step 2).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-10 10:47:08 -03:00

36 lines
1.9 KiB
Python

"""Harmless EICAR anti-malware test and blocked download/quote regressions."""
import io
from uuid import uuid4
from PIL import Image
from app.core import db
from tests.smoke_test import Client, upload_bytes, item_spec
def run():
customer=Client();customer.call('/session')
# Standard antivirus test string, not executable malware.
marker=b'X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*'
uid=upload_bytes(customer,marker,name='SECURITY-EICAR.cdr',expected_scan='rejected')
customer.call('/operator/uploads/'+uid+'/download',operator=True,expected=409)
customer.call('/quotes',{'request_key':str(uuid4()),'customer':{'cnpj':'11222333000181','zap':'11999999999','mail':'security@example.test'},
'items':[item_spec('file','1',0,uid)],'freight':{'service':'pickup'}},expected=409)
clean=upload_bytes(customer,b'Harmless local artwork fixture',name='SECURITY-CLEAN.cdr')
customer.call('/operator/uploads/'+clean+'/download',operator=True)
# A PDF is measured when the scan finishes, so checkout never downloads it.
out=io.BytesIO()
Image.new('RGB',(600,300),'white').save(out,'PDF',resolution=150)
pdf=upload_bytes(customer,out.getvalue(),name='SCAN-PDF.cdr')
with db.connect() as c:
rows={str(row['id']):row for row in c.execute(
'SELECT id,name,pdf_checked,pdf_dpi FROM dtf_local.uploads WHERE id::text=ANY(%s)',
([pdf,uid,clean],)).fetchall()}
assert rows[pdf]['name'].lower().endswith('.pdf'),rows[pdf]
assert rows[pdf]['pdf_checked'] is True,rows[pdf]
assert isinstance(rows[pdf]['pdf_dpi'],int) and rows[pdf]['pdf_dpi']>0,rows[pdf]
for not_pdf in (uid,clean):
assert rows[not_pdf]['pdf_checked'] is False,rows[not_pdf]
print('PASS: real ClamAV detects EICAR; rejected artwork cannot be downloaded or quoted; clean artwork is released; the scanned PDF carries its DPI.')
if __name__=='__main__':run()