Files
bakery-ia/test_new.sh
2025-07-26 18:46:52 +02:00

117 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# Configuration
API_BASE="http://localhost:8000"
EMAIL="test@bakery.com"
PASSWORD="TestPassword123!"
echo "🧪 Testing New Tenant-Scoped API Architecture"
echo "=============================================="
# Step 1: Health Check
echo "1. Testing Gateway Health..."
curl -s -X GET "$API_BASE/health" | echo
# Step 2: Register User
echo -e "\n2. Registering User..."
REGISTER_RESPONSE=$(curl -s -X POST "$API_BASE/api/v1/auth/register" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$EMAIL\",
\"password\": \"$PASSWORD\",
\"full_name\": \"Test User\"
}")
echo "Registration Response: $REGISTER_RESPONSE"
# Step 3: Login
echo -e "\n3. Logging in..."
LOGIN_RESPONSE=$(curl -s -X POST "$API_BASE/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d "{
\"email\": \"$EMAIL\",
\"password\": \"$PASSWORD\"
}")
# Extract token
ACCESS_TOKEN=$(echo "$LOGIN_RESPONSE" | grep -o '"access_token":"[^"]*"' | cut -d'"' -f4)
echo "Login Response: $LOGIN_RESPONSE"
echo "Access Token: ${ACCESS_TOKEN:0:50}..."
# ✅ NEW: Step 3.5 - Verify Token Works
echo -e "\n3.5. Verifying Access Token..."
TOKEN_TEST_RESPONSE=$(curl -s -X POST "$API_BASE/api/v1/auth/verify" \
-H "Authorization: Bearer $ACCESS_TOKEN")
echo "Token Verification Response: $TOKEN_TEST_RESPONSE"
# Check if token verification was successful
if echo "$TOKEN_TEST_RESPONSE" | grep -q '"user_id"'; then
echo "✅ Token verification PASSED"
else
echo "❌ Token verification FAILED"
echo "Stopping test - token is not working"
exit 1
fi
# ✅ NEW: Step 3.6 - Test a Protected Endpoint
echo -e "\n3.6. Testing Protected Endpoint (User Profile)..."
USER_PROFILE_RESPONSE=$(curl -s -X GET "$API_BASE/api/v1/users/me" \
-H "Authorization: Bearer $ACCESS_TOKEN")
echo "User Profile Response: $USER_PROFILE_RESPONSE"
# Check if protected endpoint works
if echo "$USER_PROFILE_RESPONSE" | grep -q '"email"'; then
echo "✅ Protected endpoint access PASSED"
else
echo "❌ Protected endpoint access FAILED"
echo "Response was: $USER_PROFILE_RESPONSE"
echo "Continuing with bakery registration anyway..."
fi
# Step 4: Register Bakery
echo -e "\n4. Registering Bakery..."
echo "Using Token: ${ACCESS_TOKEN:0:50}..."
echo "Making request to: $API_BASE/api/v1/tenants/register"
BAKERY_RESPONSE=$(curl -s -v -X POST "$API_BASE/api/v1/tenants/register" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d '{
"name": "Test Bakery API",
"business_type": "bakery",
"address": "Calle Test 123",
"city": "Madrid",
"postal_code": "28001",
"phone": "+34600123456"
}' 2>&1)
echo "Full Response (including headers): $BAKERY_RESPONSE"
# Extract tenant ID
TENANT_ID=$(echo "$BAKERY_RESPONSE" | grep -o '"id":"[^"]*"' | cut -d'"' -f4)
echo "Bakery Response: $BAKERY_RESPONSE"
echo "Tenant ID: $TENANT_ID"
# Step 5: Test Tenant-Scoped Endpoint
echo -e "\n5. Testing Tenant Sales Endpoint..."
SALES_RESPONSE=$(curl -s -X GET "$API_BASE/api/v1/tenants/$TENANT_ID/sales" \
-H "Authorization: Bearer $ACCESS_TOKEN")
echo "Sales Response: $SALES_RESPONSE"
# Step 6: Test Import Validation
echo -e "\n6. Testing Import Validation..."
VALIDATION_RESPONSE=$(curl -s -X POST "$API_BASE/api/v1/tenants/$TENANT_ID/sales/import/validate" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-d '{
"data": "date,product,quantity,revenue\n2024-01-01,bread,10,25.50",
"data_format": "csv"
}')
echo "Validation Response: $VALIDATION_RESPONSE"
echo -e "\n✅ API Test Complete!"
echo "If you see responses for each step, the new architecture is working!"