JEI_Import_Documents-App/scripts/test_fedex_flow.py

69 lines
2.4 KiB
Python
Raw Permalink Normal View History

"""End-to-end test of the Fedex workflow via Next.js API."""
import urllib.request
import urllib.parse
import json
import sys
BASE = 'http://localhost:6002'
# Use first 2 unpaid invoices
inv_ids = ['bz2wi3hg2tmhfp9', 'blcxmt7pwlmvxk7']
# 1) Save shipments (upsert waybill + fedex_ref)
for i, iid in enumerate(inv_ids):
payload = json.dumps({
'invoice_id': iid,
'waybill_no': f'7712345678{i:02d}',
'fedex_ref_no': f'FX-REF-TEST{i:02d}',
}).encode()
req = urllib.request.Request(
f'{BASE}/api/fedex/save-shipment',
data=payload,
headers={'Content-Type': 'application/json'},
)
try:
resp = urllib.request.urlopen(req).read().decode()
print(f'[save {i}] -> {resp}')
except urllib.error.HTTPError as e:
print(f'[save {i}] FAILED -> {e.code} {e.read().decode()}')
sys.exit(1)
# 2) Create batch with one doc attached
boundary = '----fedextest123'
parts = []
def add(name, val):
parts.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{name}"\r\n\r\n{val}\r\n'.encode())
def add_file(name, filename, mime, content):
parts.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{name}"; filename="{filename}"\r\nContent-Type: {mime}\r\n\r\n'.encode())
parts.append(content + b'\r\n')
add('import_date', '2026-09-15')
add('vat_paid', '1234.56')
add('duties_paid', '789.10')
add('notes', 'Test batch from end-to-end verification')
add('invoice_ids', json.dumps(inv_ids))
add('waybill_by_invoice', json.dumps({inv_ids[0]: '771234567800', inv_ids[1]: '771234567801'}))
add('fedex_ref_by_invoice', json.dumps({inv_ids[0]: 'FX-REF-TEST00', inv_ids[1]: 'FX-REF-TEST01'}))
add_file('doc_waybill', 'waybill.pdf', 'application/pdf', b'%PDF-1.4\n%fake test pdf for waybill\n%%EOF')
body = b''.join(parts) + f'--{boundary}--\r\n'.encode()
req = urllib.request.Request(
f'{BASE}/api/fedex/create-batch',
data=body,
headers={'Content-Type': f'multipart/form-data; boundary={boundary}'},
)
try:
resp = urllib.request.urlopen(req).read().decode()
print(f'[create-batch] -> {resp}')
except urllib.error.HTTPError as e:
print(f'[create-batch] FAILED -> {e.code} {e.read().decode()}')
sys.exit(1)
result = json.loads(resp)
batch_id = result['batchId']
print(f'\nNew batch ID: {batch_id}')
# Save batch ID for next step
with open('scripts/.last_batch_id', 'w') as f:
f.write(batch_id)