Improve user delete flow
This commit is contained in:
@@ -827,24 +827,195 @@ fi
|
||||
echo ""
|
||||
|
||||
# =================================================================
|
||||
# SUMMARY AND CLEANUP
|
||||
# STEP 6: ADMIN USER DELETION TEST (NEW)
|
||||
# =================================================================
|
||||
|
||||
echo -e "${CYAN}📊 IMPROVED ONBOARDING FLOW TEST SUMMARY${NC}"
|
||||
echo -e "${CYAN}=========================================${NC}"
|
||||
echo -e "${STEP_ICONS[4]} ${PURPLE}STEP 6: ADMIN USER DELETION TEST${NC}"
|
||||
echo "Testing complete admin user deletion with all associated data cleanup"
|
||||
echo ""
|
||||
|
||||
log_step "6.1. Getting deletion preview for test user"
|
||||
|
||||
# First, get a preview of what would be deleted
|
||||
DELETION_PREVIEW_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X GET "$API_BASE/api/v1/users/delete/$USER_ID/deletion-preview" \
|
||||
-H "Authorization: Bearer $ACCESS_TOKEN")
|
||||
|
||||
# Extract HTTP code and response
|
||||
HTTP_CODE=$(echo "$DELETION_PREVIEW_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
|
||||
DELETION_PREVIEW_RESPONSE=$(echo "$DELETION_PREVIEW_RESPONSE" | sed '/HTTP_CODE:/d')
|
||||
|
||||
echo "Deletion Preview HTTP Status: $HTTP_CODE"
|
||||
echo "Deletion Preview Response:"
|
||||
echo "$DELETION_PREVIEW_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$DELETION_PREVIEW_RESPONSE"
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ]; then
|
||||
# Extract preview information
|
||||
TOTAL_TENANTS=$(extract_json_field "$DELETION_PREVIEW_RESPONSE" "tenant_associations" | python3 -c "
|
||||
import json, sys
|
||||
try:
|
||||
data = json.load(sys.stdin)
|
||||
print(data.get('total_tenants', 0))
|
||||
except:
|
||||
print(0)
|
||||
" 2>/dev/null)
|
||||
|
||||
OWNED_TENANTS=$(extract_json_field "$DELETION_PREVIEW_RESPONSE" "tenant_associations" | python3 -c "
|
||||
import json, sys
|
||||
try:
|
||||
data = json.load(sys.stdin)
|
||||
print(data.get('owned_tenants', 0))
|
||||
except:
|
||||
print(0)
|
||||
" 2>/dev/null)
|
||||
|
||||
log_success "Deletion preview obtained successfully"
|
||||
echo " User to delete: $TEST_EMAIL"
|
||||
echo " Total tenant associations: $TOTAL_TENANTS"
|
||||
echo " Owned tenants: $OWNED_TENANTS"
|
||||
echo ""
|
||||
|
||||
log_step "6.2. Executing admin user deletion"
|
||||
echo "This will delete:"
|
||||
echo " ✓ User account and authentication data"
|
||||
echo " ✓ All tenant memberships and owned tenants"
|
||||
echo " ✓ All training models and artifacts"
|
||||
echo " ✓ All forecasts and predictions"
|
||||
echo " ✓ All notification preferences and logs"
|
||||
echo ""
|
||||
|
||||
# Wait a moment to show the preview
|
||||
sleep 2
|
||||
|
||||
# Execute the deletion
|
||||
DELETION_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X DELETE "$API_BASE/api/v1/users/delete/$USER_ID" \
|
||||
-H "Authorization: Bearer $ACCESS_TOKEN")
|
||||
|
||||
# Extract HTTP code and response
|
||||
HTTP_CODE=$(echo "$DELETION_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
|
||||
DELETION_RESPONSE=$(echo "$DELETION_RESPONSE" | sed '/HTTP_CODE:/d')
|
||||
|
||||
echo "Admin Deletion HTTP Status: $HTTP_CODE"
|
||||
echo "Admin Deletion Response:"
|
||||
echo "$DELETION_RESPONSE" | python3 -m json.tool 2>/dev/null || echo "$DELETION_RESPONSE"
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ]; then
|
||||
DELETION_SUCCESS=$(extract_json_field "$DELETION_RESPONSE" "success")
|
||||
|
||||
if [ "$DELETION_SUCCESS" = "True" ] || [ "$DELETION_SUCCESS" = "true" ]; then
|
||||
log_success "Admin user deletion initiated successfully"
|
||||
echo " Status: Processing in background"
|
||||
echo " Message: $(extract_json_field "$DELETION_RESPONSE" "message")"
|
||||
|
||||
log_step "6.3. Monitoring deletion progress (background task)"
|
||||
echo " Note: Deletion runs as background task for better performance"
|
||||
echo " Monitoring for 30 seconds to check completion..."
|
||||
|
||||
# Monitor for completion by trying to access user data
|
||||
MONITOR_COUNT=0
|
||||
MAX_MONITOR_ATTEMPTS=30
|
||||
|
||||
while [ $MONITOR_COUNT -lt $MAX_MONITOR_ATTEMPTS ]; do
|
||||
sleep 1
|
||||
MONITOR_COUNT=$((MONITOR_COUNT + 1))
|
||||
|
||||
# Try to get user info (should fail when deletion completes)
|
||||
CHECK_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X GET "$API_BASE/api/v1/users/me" \
|
||||
-H "Authorization: Bearer $ACCESS_TOKEN" 2>/dev/null)
|
||||
|
||||
CHECK_HTTP_CODE=$(echo "$CHECK_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
|
||||
|
||||
if [ "$CHECK_HTTP_CODE" = "401" ] || [ "$CHECK_HTTP_CODE" = "404" ]; then
|
||||
log_success "User deletion completed (user no longer accessible)"
|
||||
echo " Deletion verified after ${MONITOR_COUNT}s"
|
||||
break
|
||||
elif [ $MONITOR_COUNT -eq $MAX_MONITOR_ATTEMPTS ]; then
|
||||
log_warning "Deletion monitoring timed out after ${MAX_MONITOR_ATTEMPTS}s"
|
||||
echo " Deletion may still be processing in background"
|
||||
echo " Check server logs for completion status"
|
||||
fi
|
||||
|
||||
# Show progress every 5 seconds
|
||||
if [ $((MONITOR_COUNT % 5)) -eq 0 ]; then
|
||||
echo " Monitoring... ${MONITOR_COUNT}s/${MAX_MONITOR_ATTEMPTS}s"
|
||||
fi
|
||||
done
|
||||
|
||||
else
|
||||
log_error "Admin user deletion failed"
|
||||
echo "Response: $DELETION_RESPONSE"
|
||||
fi
|
||||
|
||||
elif [ "$HTTP_CODE" = "400" ]; then
|
||||
log_error "Deletion request was invalid"
|
||||
echo "Error details: $DELETION_RESPONSE"
|
||||
|
||||
elif [ "$HTTP_CODE" = "403" ]; then
|
||||
log_error "Insufficient permissions for deletion"
|
||||
echo "Note: Only admin users can delete other admin users"
|
||||
|
||||
elif [ "$HTTP_CODE" = "404" ]; then
|
||||
log_error "User not found for deletion"
|
||||
echo "User ID: $USER_ID may have already been deleted"
|
||||
|
||||
else
|
||||
log_error "Admin user deletion failed (HTTP $HTTP_CODE)"
|
||||
echo "Response: $DELETION_RESPONSE"
|
||||
fi
|
||||
|
||||
else
|
||||
log_error "Failed to get deletion preview (HTTP $HTTP_CODE)"
|
||||
echo "Cannot proceed with deletion test"
|
||||
echo "Response: $DELETION_PREVIEW_RESPONSE"
|
||||
fi
|
||||
|
||||
log_step "6.4. Verifying cleanup completion"
|
||||
|
||||
# Try to login with the deleted user (should fail)
|
||||
VERIFY_LOGIN_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X POST "$API_BASE/api/v1/auth/login" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"email\": \"$TEST_EMAIL\",
|
||||
\"password\": \"$TEST_PASSWORD\"
|
||||
}")
|
||||
|
||||
VERIFY_HTTP_CODE=$(echo "$VERIFY_LOGIN_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
|
||||
VERIFY_LOGIN_RESPONSE=$(echo "$VERIFY_LOGIN_RESPONSE" | sed '/HTTP_CODE:/d')
|
||||
|
||||
if [ "$VERIFY_HTTP_CODE" = "401" ] || [ "$VERIFY_HTTP_CODE" = "404" ]; then
|
||||
log_success "Verification: User login properly blocked (user deleted)"
|
||||
echo " HTTP Status: $VERIFY_HTTP_CODE"
|
||||
elif [ "$VERIFY_HTTP_CODE" = "200" ]; then
|
||||
log_warning "Verification: User can still login (deletion may not be complete)"
|
||||
echo " This could indicate deletion is still processing"
|
||||
else
|
||||
log_warning "Verification: Unexpected login response (HTTP $VERIFY_HTTP_CODE)"
|
||||
echo " Response: $VERIFY_LOGIN_RESPONSE"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "✅ Completed Onboarding Steps:"
|
||||
|
||||
# =================================================================
|
||||
# Update the SUMMARY section to include Step 6
|
||||
# =================================================================
|
||||
|
||||
# Replace the existing summary section with this updated version:
|
||||
|
||||
echo -e "${CYAN}📊 COMPLETE ONBOARDING + DELETION FLOW TEST SUMMARY${NC}"
|
||||
echo -e "${CYAN}===================================================${NC}"
|
||||
|
||||
echo ""
|
||||
echo "✅ Completed All Test Steps:"
|
||||
echo " ${STEP_ICONS[0]} Step 1: User Registration ✓"
|
||||
echo " ${STEP_ICONS[1]} Step 2: Bakery Registration ✓"
|
||||
echo " ${STEP_ICONS[2]} Step 3: FULL Sales Data Upload ✓"
|
||||
echo " ${STEP_ICONS[3]} Step 4: Model Training with FULL Data ✓"
|
||||
echo " ${STEP_ICONS[4]} Step 5: Onboarding Complete ✓"
|
||||
echo " 🗑️ Step 6: Admin User Deletion Test ✓"
|
||||
|
||||
echo ""
|
||||
echo "📋 Test Results:"
|
||||
echo " User ID: $USER_ID"
|
||||
echo " Tenant ID: $TENANT_ID"
|
||||
echo " Original User ID: $USER_ID"
|
||||
echo " Original Tenant ID: $TENANT_ID"
|
||||
echo " Training Task ID: $TRAINING_TASK_ID"
|
||||
echo " Test Email: $TEST_EMAIL"
|
||||
echo " FULL CSV Used: $REAL_CSV_FILE"
|
||||
@@ -865,14 +1036,41 @@ else
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "🧹 Cleanup:"
|
||||
echo " To clean up test data, you may want to remove:"
|
||||
echo " - Test user: $TEST_EMAIL"
|
||||
echo " - Test tenant: $TENANT_ID"
|
||||
echo "🗑️ Deletion Test Results:"
|
||||
if [ "$DELETION_SUCCESS" = "True" ] || [ "$DELETION_SUCCESS" = "true" ]; then
|
||||
echo " ✅ Admin user deletion: SUCCESS"
|
||||
echo " ✅ Associated data cleanup: INITIATED"
|
||||
echo " ✅ User authentication: BLOCKED"
|
||||
echo " 📊 Tenant associations cleaned: $TOTAL_TENANTS"
|
||||
echo " 🏢 Owned tenants handled: $OWNED_TENANTS"
|
||||
else
|
||||
echo " ❌ Admin user deletion: FAILED or INCOMPLETE"
|
||||
echo " ⚠️ Manual cleanup may be required"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "🧹 Cleanup Status:"
|
||||
if [ "$DELETION_SUCCESS" = "True" ] || [ "$DELETION_SUCCESS" = "true" ]; then
|
||||
echo " ✅ Automatic cleanup completed via admin deletion"
|
||||
echo " ✅ Test user and tenant data removed"
|
||||
echo " ✅ Training models and forecasts deleted"
|
||||
echo " ✅ All associated data cleaned up"
|
||||
else
|
||||
echo " ⚠️ Automatic cleanup failed - manual cleanup needed:"
|
||||
echo " - Test user: $TEST_EMAIL"
|
||||
echo " - Test tenant: $TENANT_ID"
|
||||
echo " - Training models and forecasts"
|
||||
fi
|
||||
|
||||
# Cleanup temporary files
|
||||
rm -f "$VALIDATION_DATA_FILE"
|
||||
rm -f /tmp/bakery_coords.env
|
||||
|
||||
echo ""
|
||||
log_success "Improved onboarding flow simulation completed successfully!"
|
||||
echo -e "${CYAN}The user journey through all 5 onboarding steps has been tested with FULL dataset.${NC}"
|
||||
if [ "$DELETION_SUCCESS" = "True" ] || [ "$DELETION_SUCCESS" = "true" ]; then
|
||||
log_success "Complete onboarding + deletion flow test finished successfully!"
|
||||
echo -e "${CYAN}✅ All steps completed: Registration → Onboarding → Training → Deletion → Cleanup${NC}"
|
||||
else
|
||||
log_warning "Onboarding flow completed, but deletion test had issues"
|
||||
echo -e "${YELLOW}⚠️ Onboarding steps passed, but admin deletion needs investigation${NC}"
|
||||
fi
|
||||
Reference in New Issue
Block a user