Add subcription feature

This commit is contained in:
Urtzi Alfaro
2026-01-13 22:22:38 +01:00
parent b931a5c45e
commit 6ddf608d37
61 changed files with 7915 additions and 1238 deletions

View File

@@ -0,0 +1,129 @@
#!/bin/bash
# Script to generate a comprehensive test report for the subscription creation flow
# This script checks all components and generates a detailed report
echo "📊 Generating Subscription Creation Flow Test Report"
echo "===================================================="
echo "Report generated on: $(date)"
echo ""
# Test 1: Check if database migration was applied
echo "🔍 Test 1: Database Migration Check"
echo "-----------------------------------"
POD_NAME=$(kubectl get pods -n bakery-ia -l app=auth-service -o jsonpath='{.items[0].metadata.name}')
MIGRATION_STATUS=$(kubectl exec -n bakery-ia $POD_NAME -- psql -U auth_user -d auth_db -c "SELECT version_num FROM alembic_version" -t -A)
if [[ "$MIGRATION_STATUS" == "20260113_add_payment_columns" ]]; then
echo "✅ PASS: Database migration '20260113_add_payment_columns' is applied"
else
echo "❌ FAIL: Database migration not found. Current version: $MIGRATION_STATUS"
fi
echo ""
# Test 2: Check if payment columns exist in users table
echo "🔍 Test 2: Payment Columns in Users Table"
echo "------------------------------------------"
COLUMNS=$(kubectl exec -n bakery-ia $POD_NAME -- psql -U auth_user -d auth_db -c "\d users" -t -A | grep -E "payment_customer_id|default_payment_method_id")
if [[ -n "$COLUMNS" ]]; then
echo "✅ PASS: Payment columns found in users table"
echo " Found columns:"
echo " $COLUMNS" | sed 's/^/ /'
else
echo "❌ FAIL: Payment columns not found in users table"
fi
echo ""
# Test 3: Check if gateway route exists
echo "🔍 Test 3: Gateway Route Configuration"
echo "--------------------------------------"
GATEWAY_POD=$(kubectl get pods -n bakery-ia -l app=gateway -o jsonpath='{.items[0].metadata.name}')
ROUTE_CHECK=$(kubectl exec -n bakery-ia $GATEWAY_POD -- grep -c "create-for-registration" /app/app/routes/subscription.py)
if [[ "$ROUTE_CHECK" -gt 0 ]]; then
echo "✅ PASS: Gateway route for 'create-for-registration' is configured"
else
echo "❌ FAIL: Gateway route for 'create-for-registration' not found"
fi
echo ""
# Test 4: Check if tenant service endpoint exists
echo "🔍 Test 4: Tenant Service Endpoint"
echo "-----------------------------------"
TENANT_POD=$(kubectl get pods -n bakery-ia -l app=tenant-service -o jsonpath='{.items[0].metadata.name}')
ENDPOINT_CHECK=$(kubectl exec -n bakery-ia $TENANT_POD -- grep -c "create-for-registration" /app/app/api/subscription.py)
if [[ "$ENDPOINT_CHECK" -gt 0 ]]; then
echo "✅ PASS: Tenant service endpoint 'create-for-registration' is configured"
else
echo "❌ FAIL: Tenant service endpoint 'create-for-registration' not found"
fi
echo ""
# Test 5: Test user registration (create a test user)
echo "🔍 Test 5: User Registration Test"
echo "--------------------------------"
TEST_EMAIL="test_$(date +%Y%m%d%H%M%S)@example.com"
REGISTRATION_RESPONSE=$(curl -X POST "https://bakery-ia.local/api/v1/auth/register-with-subscription" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d "{\"email\":\"$TEST_EMAIL\",\"password\":\"SecurePassword123!\",\"full_name\":\"Test User\",\"subscription_plan\":\"basic\",\"payment_method_id\":\"pm_test123\"}" \
-k -s)
if echo "$REGISTRATION_RESPONSE" | grep -q "access_token"; then
echo "✅ PASS: User registration successful"
USER_ID=$(echo "$REGISTRATION_RESPONSE" | python3 -c "import sys, json; print(json.load(sys.stdin)['user']['id'])")
echo " Created user ID: $USER_ID"
else
echo "❌ FAIL: User registration failed"
echo " Response: $REGISTRATION_RESPONSE"
fi
echo ""
# Test 6: Check if user has payment fields
echo "🔍 Test 6: User Payment Fields"
echo "------------------------------"
if [[ -n "$USER_ID" ]]; then
USER_DATA=$(kubectl exec -n bakery-ia $POD_NAME -- psql -U auth_user -d auth_db -c "SELECT payment_customer_id, default_payment_method_id FROM users WHERE id = '$USER_ID'" -t -A)
if [[ -n "$USER_DATA" ]]; then
echo "✅ PASS: User has payment fields in database"
echo " Payment data: $USER_DATA"
else
echo "❌ FAIL: User payment fields not found"
fi
else
echo "⚠️ SKIP: User ID not available from previous test"
fi
echo ""
# Test 7: Check subscription creation in onboarding progress
echo "🔍 Test 7: Subscription in Onboarding Progress"
echo "---------------------------------------------"
if [[ -n "$USER_ID" ]]; then
# This would require authentication, so we'll skip for now
echo "⚠️ SKIP: Requires authentication (would need to implement token handling)"
else
echo "⚠️ SKIP: User ID not available from previous test"
fi
echo ""
# Summary
echo "📋 Test Summary"
echo "==============="
echo "The subscription creation flow test report has been generated."
echo ""
echo "Components tested:"
echo " 1. Database migration"
echo " 2. Payment columns in users table"
echo " 3. Gateway route configuration"
echo " 4. Tenant service endpoint"
echo " 5. User registration"
echo " 6. User payment fields"
echo " 7. Subscription in onboarding progress"
echo ""
echo "For a complete integration test, run:"
echo " ./scripts/run_subscription_integration_test.sh"
echo ""
echo "🎉 Report generation completed!"

View File

@@ -0,0 +1,145 @@
#!/bin/bash
# Script to run the subscription creation integration test inside Kubernetes
# This script creates a test pod that runs the integration test
set -e
echo "🚀 Starting subscription creation integration test..."
# Check if there's already a test pod running
EXISTING_POD=$(kubectl get pod subscription-integration-test -n bakery-ia 2>/dev/null || echo "")
if [ -n "$EXISTING_POD" ]; then
echo "🧹 Cleaning up existing test pod..."
kubectl delete pod subscription-integration-test -n bakery-ia --wait=true
echo "✅ Existing pod cleaned up"
fi
# Determine the correct image to use by checking the existing tenant service deployment
IMAGE=$(kubectl get deployment tenant-service -n bakery-ia -o jsonpath='{.spec.template.spec.containers[0].image}')
if [ -z "$IMAGE" ]; then
echo "❌ Could not determine tenant service image. Is the tenant service deployed?"
exit 1
fi
echo "📦 Using image: $IMAGE"
# Create a test pod that runs the integration test with a simple command
echo "🔧 Creating test pod..."
kubectl run subscription-integration-test \
--image="$IMAGE" \
--namespace=bakery-ia \
--restart=Never \
--env="GATEWAY_URL=http://gateway-service:8000" \
--env="STRIPE_SECRET_KEY=$(kubectl get secret payment-secrets -n bakery-ia -o jsonpath='{.data.STRIPE_SECRET_KEY}' | base64 -d)" \
--command -- /bin/sh -c "
set -e
echo '🧪 Setting up test environment...' &&
cd /app &&
echo '📋 Installing test dependencies...' &&
pip install pytest pytest-asyncio httpx stripe --quiet &&
echo '✅ Dependencies installed' &&
echo '' &&
echo '🔧 Configuring test to use internal gateway service URL...' &&
# Backup original file before modification
cp tests/integration/test_subscription_creation_flow.py tests/integration/test_subscription_creation_flow.py.bak &&
# Update the test file to use the internal gateway service URL
sed -i 's|self.base_url = \"https://bakery-ia.local\"|self.base_url = \"http://gateway-service:8000\"|g' tests/integration/test_subscription_creation_flow.py &&
echo '✅ Test configured for internal Kubernetes networking' &&
echo '' &&
echo '🧪 Running subscription creation integration test...' &&
echo '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━' &&
python -m pytest tests/integration/test_subscription_creation_flow.py -v --tb=short -s --color=yes &&
TEST_RESULT=\$? &&
echo '━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━' &&
echo '' &&
echo '📋 Restoring original test file...' &&
mv tests/integration/test_subscription_creation_flow.py.bak tests/integration/test_subscription_creation_flow.py &&
echo '✅ Original test file restored' &&
echo '' &&
if [ \$TEST_RESULT -eq 0 ]; then
echo '🎉 Integration test PASSED!'
else
echo '❌ Integration test FAILED!'
fi &&
exit \$TEST_RESULT
"
# Wait for the test pod to start
echo "⏳ Waiting for test pod to start..."
sleep 5
# Follow the logs in real-time
echo "📋 Following test execution logs..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
# Stream logs while the pod is running
kubectl logs -f subscription-integration-test -n bakery-ia 2>/dev/null || true
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# Wait for the pod to complete with a timeout
echo "⏳ Waiting for test pod to complete..."
TIMEOUT=600 # 10 minutes timeout
COUNTER=0
while [ $COUNTER -lt $TIMEOUT ]; do
POD_STATUS=$(kubectl get pod subscription-integration-test -n bakery-ia -o jsonpath='{.status.phase}' 2>/dev/null)
if [ "$POD_STATUS" == "Succeeded" ] || [ "$POD_STATUS" == "Failed" ]; then
break
fi
sleep 2
COUNTER=$((COUNTER + 2))
done
if [ $COUNTER -ge $TIMEOUT ]; then
echo "⏰ Timeout waiting for test to complete after $TIMEOUT seconds"
echo "📋 Fetching final logs before cleanup..."
kubectl logs subscription-integration-test -n bakery-ia --tail=100
echo "🧹 Cleaning up test pod due to timeout..."
kubectl delete pod subscription-integration-test -n bakery-ia --wait=false
exit 1
fi
# Get the final status
POD_STATUS=$(kubectl get pod subscription-integration-test -n bakery-ia -o jsonpath='{.status.phase}')
CONTAINER_EXIT_CODE=$(kubectl get pod subscription-integration-test -n bakery-ia -o jsonpath='{.status.containerStatuses[0].state.terminated.exitCode}' 2>/dev/null || echo "unknown")
echo ""
echo "📊 Test Results:"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Pod Status: $POD_STATUS"
echo "Exit Code: $CONTAINER_EXIT_CODE"
# Determine if the test passed
if [ "$POD_STATUS" == "Succeeded" ] && [ "$CONTAINER_EXIT_CODE" == "0" ]; then
echo ""
echo "✅ Integration test PASSED!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
RESULT=0
else
echo ""
echo "❌ Integration test FAILED!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# Show additional logs if failed
if [ "$POD_STATUS" == "Failed" ]; then
echo ""
echo "📋 Last 50 lines of logs:"
kubectl logs subscription-integration-test -n bakery-ia --tail=50
fi
RESULT=1
fi
# Clean up the test pod
echo ""
echo "🧹 Cleaning up test pod..."
kubectl delete pod subscription-integration-test -n bakery-ia --wait=false
echo "🏁 Integration test process completed!"
exit $RESULT