#!/usr/bin/env python3 """ Test script for PDF generation functionality Tests both single label and batch PDF generation """ import json import requests import sys # Configuration BASE_URL = "http://localhost:8080" LOGIN_URL = f"{BASE_URL}/login" SINGLE_PDF_URL = f"{BASE_URL}/labels/api/generate-pdf" BATCH_PDF_URL = f"{BASE_URL}/labels/api/generate-pdf/1/true" # Test credentials TEST_USERNAME = "admin" TEST_PASSWORD = "admin123" def test_pdf_generation(): """Test PDF generation endpoints""" session = requests.Session() # Step 1: Login print("=" * 70) print("Step 1: Logging in...") print("=" * 70) login_data = { 'username': TEST_USERNAME, 'password': TEST_PASSWORD } response = session.post(LOGIN_URL, data=login_data) if response.status_code not in [200, 302]: # Accept both OK and redirect print(f"❌ Login failed with status {response.status_code}") print(f"Response: {response.text[:200]}") return False print(f"✓ Login successful (status: {response.status_code})") # Step 2: Test Single Label PDF Generation print("\n" + "=" * 70) print("Step 2: Testing Single Label PDF Generation...") print("=" * 70) order_data = { "id": 1, "comanda_productie": "CP00000711", "cod_articol": "ART001", "descr_com_prod": "Memory Foam Pillow - Premium Quality", "cantitate": 5, "com_achiz_client": "PO2026001", "nr_linie_com_client": "001", "customer_name": "ACME Corporation", "customer_article_number": "ACME-MFP-001", "data_livrara": "2026-02-11", "dimensiune": "Standard (50x70cm)", "piece_number": 1, "total_pieces": 5 } response = session.post( SINGLE_PDF_URL, json=order_data, headers={'Content-Type': 'application/json'} ) if response.status_code == 200: if response.headers.get('Content-Type') == 'application/pdf': print(f"✓ Single label PDF generated successfully") print(f" Content-Type: {response.headers.get('Content-Type')}") print(f" PDF Size: {len(response.content)} bytes") # Save to file for manual inspection with open('/tmp/test_label_single.pdf', 'wb') as f: f.write(response.content) print(f" Saved to: /tmp/test_label_single.pdf") else: print(f"❌ Invalid content type: {response.headers.get('Content-Type')}") print(f"Response: {response.text[:200]}") return False else: print(f"❌ Single PDF generation failed with status {response.status_code}") print(f"Response: {response.text[:200]}") return False # Step 3: Test Batch PDF Generation print("\n" + "=" * 70) print("Step 3: Testing Batch PDF Generation...") print("=" * 70) response = session.post(BATCH_PDF_URL) if response.status_code == 200: if response.headers.get('Content-Type') == 'application/pdf': print(f"✓ Batch PDF generated successfully") print(f" Content-Type: {response.headers.get('Content-Type')}") print(f" PDF Size: {len(response.content)} bytes") # Save to file for manual inspection with open('/tmp/test_label_batch.pdf', 'wb') as f: f.write(response.content) print(f" Saved to: /tmp/test_label_batch.pdf") else: print(f"❌ Invalid content type: {response.headers.get('Content-Type')}") print(f"Response: {response.text[:200]}") return False else: print(f"❌ Batch PDF generation failed with status {response.status_code}") print(f"Response: {response.text[:200]}") return False # Step 4: Verify order marked as printed print("\n" + "=" * 70) print("Step 4: Verifying order status...") print("=" * 70) # Check if order is marked as printed import pymysql try: conn = pymysql.connect( host='127.0.0.1', port=3306, user='quality_user', password='quality_secure_password_2026', database='quality_db' ) cursor = conn.cursor() cursor.execute("SELECT id, comanda_productie, printed_labels FROM order_for_labels WHERE id = 1") row = cursor.fetchone() cursor.close() conn.close() if row: order_id, cp_code, printed_status = row print(f"Order ID: {order_id}, CP Code: {cp_code}") if printed_status == 1: print(f"✓ Order marked as printed (printed_labels = {printed_status})") else: print(f"⚠ Order NOT marked as printed (printed_labels = {printed_status})") else: print("⚠ Order not found in database") except Exception as e: print(f"⚠ Could not verify order status: {e}") print("\n" + "=" * 70) print("✓ All PDF generation tests completed successfully!") print("=" * 70) return True if __name__ == '__main__': try: success = test_pdf_generation() sys.exit(0 if success else 1) except Exception as e: print(f"\n❌ Unexpected error: {e}") import traceback traceback.print_exc() sys.exit(1)