Files
bakery-ia/test_training_safeguards.sh
2025-08-08 09:08:41 +02:00

149 lines
4.7 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
echo "🧪 Testing Training Safeguards"
echo "============================="
# Create user and tenant without importing data
echo "1. Creating user and tenant..."
# Register user
EMAIL="training.test.$(date +%s)@bakery.com"
REGISTER_RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d "{\"email\": \"$EMAIL\", \"password\": \"TestPassword123!\", \"full_name\": \"Training Test User\", \"role\": \"admin\"}")
ACCESS_TOKEN=$(echo "$REGISTER_RESPONSE" | python3 -c "
import json, sys
try:
data = json.load(sys.stdin)
print(data.get('access_token', ''))
except:
pass
" 2>/dev/null)
if [ -z "$ACCESS_TOKEN" ]; then
echo "❌ User registration failed"
echo "Response: $REGISTER_RESPONSE"
exit 1
fi
echo "✅ User registered successfully"
# Create tenant
TENANT_RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/tenants/register" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d '{"name": "Training Test Bakery", "business_type": "bakery", "address": "Test Address", "city": "Madrid", "postal_code": "28001", "phone": "+34600123456"}')
TENANT_ID=$(echo "$TENANT_RESPONSE" | python3 -c "
import json, sys
try:
data = json.load(sys.stdin)
print(data.get('id', ''))
except:
pass
" 2>/dev/null)
if [ -z "$TENANT_ID" ]; then
echo "❌ Tenant creation failed"
echo "Response: $TENANT_RESPONSE"
exit 1
fi
echo "✅ Tenant created: $TENANT_ID"
# 2. Test training WITHOUT data (should fail gracefully)
echo ""
echo "2. Testing training WITHOUT sales data (should fail gracefully)..."
TRAINING_RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/tenants/$TENANT_ID/training/jobs" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d '{}')
echo "Training Response (no data):"
echo "$TRAINING_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$TRAINING_RESPONSE"
# Check if the job was created but will fail
JOB_ID=$(echo "$TRAINING_RESPONSE" | python3 -c "
import json, sys
try:
data = json.load(sys.stdin)
print(data.get('job_id', ''))
except:
pass
" 2>/dev/null)
if [ -n "$JOB_ID" ]; then
echo "✅ Training job created: $JOB_ID"
echo "⏳ Waiting 10 seconds to see if safeguard triggers..."
sleep 10
# Check training job status
STATUS_RESPONSE=$(curl -s "http://localhost:8000/api/v1/tenants/$TENANT_ID/training/jobs/$JOB_ID/status" \
-H "Authorization: Bearer $ACCESS_TOKEN")
echo "Job Status:"
echo "$STATUS_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$STATUS_RESPONSE"
else
echo " No job ID returned - training may have been rejected immediately"
fi
echo ""
echo "3. Now importing some test data..."
IMPORT_RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/tenants/$TENANT_ID/sales/import" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d '{"data": "date,product,quantity,revenue\n2024-01-01,bread,10,20.0\n2024-01-02,croissant,5,15.0\n2024-01-03,pastry,8,24.0", "data_format": "csv", "filename": "test_data.csv"}')
echo "Import Response:"
echo "$IMPORT_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$IMPORT_RESPONSE"
echo ""
echo "4. Testing training WITH sales data..."
TRAINING_WITH_DATA_RESPONSE=$(curl -s -X POST "http://localhost:8000/api/v1/tenants/$TENANT_ID/training/jobs" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d '{}')
echo "Training Response (with data):"
echo "$TRAINING_WITH_DATA_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$TRAINING_WITH_DATA_RESPONSE"
JOB_ID_WITH_DATA=$(echo "$TRAINING_WITH_DATA_RESPONSE" | python3 -c "
import json, sys
try:
data = json.load(sys.stdin)
print(data.get('job_id', ''))
except:
pass
" 2>/dev/null)
if [ -n "$JOB_ID_WITH_DATA" ]; then
echo "✅ Training job with data created: $JOB_ID_WITH_DATA"
echo "⏳ Monitoring progress for 30 seconds..."
for i in {1..6}; do
sleep 5
STATUS_RESPONSE=$(curl -s "http://localhost:8000/api/v1/tenants/$TENANT_ID/training/jobs/$JOB_ID_WITH_DATA/status" \
-H "Authorization: Bearer $ACCESS_TOKEN")
STATUS=$(echo "$STATUS_RESPONSE" | python3 -c "
import json, sys
try:
data = json.load(sys.stdin)
print(data.get('status', 'unknown'))
except:
print('error')
" 2>/dev/null)
echo "[$i/6] Status: $STATUS"
if [ "$STATUS" = "completed" ] || [ "$STATUS" = "failed" ]; then
echo "Final Status Response:"
echo "$STATUS_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$STATUS_RESPONSE"
break
fi
done
fi
echo ""
echo "🏁 Training safeguard test completed!"