stupa-pdf-api/test_financing.py

178 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""
Test script to debug financing fields issue in STUPA PDF API
"""
import json
import base64
import requests
from typing import Dict, Any
# Test data with financing fields
def create_test_form_data() -> Dict[str, Any]:
"""Create test form data with financing fields"""
return {
# Basic fields
"pa-first-name": {"/V": "Test"},
"pa-last-name": {"/V": "User"},
"pa-email": {"/V": "test@example.com"},
"pa-project-name": {"/V": "Test Project"},
"pa-start-date": {"/V": "2025-01-01"},
"pa-end-date": {"/V": "2025-06-30"},
"pa-participants": {"/V": 10},
"pa-project-description": {"/V": "Test description"},
"pa-requested-amount-euro-sum": {"/V": 1000},
# Cost positions
"pa-cost-1-name": {"/V": "Test Cost"},
"pa-cost-1-amount-euro": {"/V": 1000},
# QSM Financing fields - this is what should be saved but might be missing
"pa-qsm-financing": {"/V": "vwv-3-2-1-1"},
"pa-qsm-stellenfinanzierungen": {"/V": True},
"pa-qsm-studierende": {"/V": True},
"pa-qsm-individuell": {"/V": True},
# VSM Financing fields
"pa-vsm-financing": {"/V": "lhg-01"},
"pa-vsm-aufgaben": {"/V": True},
"pa-vsm-individuell": {"/V": True},
}
def test_create_application():
"""Test creating an application with financing fields"""
print("=== Testing CREATE Application ===")
form_data = create_test_form_data()
form_json = json.dumps(form_data)
form_json_b64 = base64.b64encode(form_json.encode('utf-8')).decode('utf-8')
# Test QSM variant
print("\n--- Testing QSM Application ---")
response = requests.post(
"http://localhost:8000/applications",
params={
"return_format": "json",
"variant": "QSM"
},
data={
"form_json_b64": form_json_b64
}
)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
# Check if we got JSON or PDF
content_type = response.headers.get('content-type', '')
if 'application/json' in content_type:
data = response.json()
print(f"Created Application: {data['pa_id']}")
print(f"Key: {data['pa_key']}")
return data['pa_id'], data['pa_key']
else:
# PDF response, extract from headers
pa_id = response.headers.get('X-PA-ID')
pa_key = response.headers.get('X-PA-KEY')
print(f"Created Application: {pa_id}")
print(f"Key: {pa_key}")
return pa_id, pa_key
else:
print(f"Error: {response.text}")
return None, None
def test_update_application(pa_id: str, pa_key: str):
"""Test updating an application with financing fields"""
print(f"\n=== Testing UPDATE Application {pa_id} ===")
# Create form data with updated financing fields
form_data = create_test_form_data()
# Change financing code to test if it saves
form_data["pa-qsm-financing"] = {"/V": "vwv-3-2-1-2"} # Different value
form_json = json.dumps(form_data)
form_json_b64 = base64.b64encode(form_json.encode('utf-8')).decode('utf-8')
response = requests.put(
f"http://localhost:8000/applications/{pa_id}",
params={
"return_format": "json"
},
data={
"form_json_b64": form_json_b64
},
headers={
"X-PA-KEY": pa_key
}
)
print(f"Status Code: {response.status_code}")
if response.status_code != 200:
print(f"Error: {response.text}")
def test_get_application(pa_id: str, pa_key: str):
"""Test retrieving an application to check if financing fields are saved"""
print(f"\n=== Testing GET Application {pa_id} ===")
response = requests.get(
f"http://localhost:8000/applications/{pa_id}",
headers={
"X-PA-KEY": pa_key
}
)
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
data = response.json()
# Check financing fields - correct payload structure
payload = data.get('payload', {})
pa = payload.get('pa', {})
project = pa.get('project', {})
financing = project.get('financing', {})
print(f"\nFinancing data in response:")
print(json.dumps(financing, indent=2))
# Check specific fields
qsm = financing.get('qsm', {})
vsm = financing.get('vsm', {})
print(f"\nQSM financing code: {qsm.get('code')}")
print(f"QSM flags: {qsm.get('flags')}")
print(f"VSM financing code: {vsm.get('code')}")
print(f"VSM flags: {vsm.get('flags')}")
# Check if fields are missing (the main issue)
if not qsm.get('code') and not vsm.get('code'):
print("\n❌ ISSUE FOUND: No financing code saved!")
else:
print("\n✅ Financing code found")
if not qsm.get('flags') and not vsm.get('flags'):
print("❌ ISSUE FOUND: No financing flags saved!")
else:
print("✅ Financing flags found")
else:
print(f"Error: {response.text}")
def main():
"""Run all tests"""
print("🧪 Testing Financing Fields Issue")
print("=" * 50)
# Create application
pa_id, pa_key = test_create_application()
if pa_id and pa_key:
# Update application
test_update_application(pa_id, pa_key)
# Check if data was saved correctly
test_get_application(pa_id, pa_key)
print("\n" + "=" * 50)
print("🔍 Check the API logs with: docker compose logs api | grep DEBUG")
if __name__ == "__main__":
main()