#!/bin/bash # Test script for Supabase Edge Functions # Make sure to set: export SUPABASE_ACCESS_TOKEN=your_service_role_key # Configuration SUPABASE_URL="https://lovable.backoffice.biz.id" SERVICE_ROLE_KEY="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJzdXBhYmFzZSIsImlhdCI6MTc2NjAzNzEyMCwiZXhwIjo0OTIxNzEwNzIwLCJyb2xlIjoic2VydmljZV9yb2xlIn0.t6D9VwaukYGq4c_VbW1bkd3ZkKgldpCKRR13nN14XXc" if [ -z "$SERVICE_ROLE_KEY" ]; then echo "❌ Error: SUPABASE_ACCESS_TOKEN environment variable not set" echo "Run: export SUPABASE_ACCESS_TOKEN=your_service_role_key" exit 1 fi echo "🚀 Testing Supabase Edge Functions" echo "==================================" echo "URL: $SUPABASE_URL" echo "" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to test API test_function() { local func_name=$1 local payload=$2 local expected_status=${3:-200} echo -e "${YELLOW}Testing: $func_name${NC}" echo "Payload: $payload" response=$(curl -s -w "%{http_code}" \ -X POST "$SUPABASE_URL/rest/v1/functions" \ -H "Authorization: Bearer $SERVICE_ROLE_KEY" \ -H "apikey: $SERVICE_ROLE_KEY" \ -H "Content-Type: application/json" \ -d "{ \"name\": \"$func_name\", \"verify_jwt\": false }") # Extract status code (last 3 characters) status_code="${response: -3}" # Extract response body (everything except last 3 characters) response_body="${response%???}" if [ "$status_code" = "200" ] || [ "$status_code" = "201" ]; then echo -e "${GREEN}✅ Success: $func_name${NC}" echo "Response: $response_body" else echo -e "${RED}❌ Error: $func_name (Status: $status_code)${NC}" echo "Response: $response_body" fi echo "----------------------------------------" echo "" } # Test function invocation test_invoke_function() { local func_name=$1 local payload=$2 echo -e "${YELLOW}Invoking: $func_name${NC}" response=$(curl -s -w "%{http_code}" \ -X POST "$SUPABASE_URL/functions/v1/$func_name" \ -H "Authorization: Bearer $SERVICE_ROLE_KEY" \ -H "Content-Type: application/json" \ -d "$payload") # Extract status code (last 3 characters) status_code="${response: -3}" # Extract response body (everything except last 3 characters) response_body="${response%???}" if [ "$status_code" = "200" ]; then echo -e "${GREEN}✅ Function executed successfully${NC}" echo "Response: $response_body" else echo -e "${RED}❌ Function invocation failed (Status: $status_code)${NC}" echo "Response: $response_body" fi echo "----------------------------------------" echo "" } # 1. Test creating/updating functions echo "📋 Testing Function Creation/Update" echo "" test_function "send-email-v2" test_function "create-meet-link" test_function "send-consultation-reminder" test_function "daily-reminders" test_function "pakasir-webhook" # 2. Test function invocations echo "🔧 Testing Function Invocations" echo "" # Test send-email-v2 (will fail without proper Mailketing token, but should reach the function) test_invoke_function "send-email-v2" '{ "to": "dwinx.ramz@gmail.com", "api_token": "b4d1beb302282f411d196146f6806eab", "from_name": "Test Hub", "from_email": "with@dwindi.com", "subject": "Test Email", "html_body": "

Test Email

This is a test from send-email-v2

" }' # Test create-meet-link (will fail without proper n8n webhook, but should reach the function) # Note: Will use webhook path based on integration_n8n_test_mode setting test_invoke_function "create-meet-link" '{ "slot_id": "test-slot-123", "date": "2025-12-23", "start_time": "14:00:00", "end_time": "15:00:00", "client_name": "Test Client", "client_email": "dewe.pw@gmail.com", "topic": "Test Topic", "notes": "Test notes" }' # Test daily-reminders (should work with database queries) test_invoke_function "daily-reminders" '{}' echo "✨ Testing Complete!" echo "" echo "📝 Notes:" echo "- send-email-v2: Needs valid Mailketing API token to actually send emails" echo "- create-meet-link: Needs n8n webhook configured in platform_settings" echo " - Will use /webhook-test/ if integration_n8n_test_mode is enabled" echo " - Will use /webhook/ if integration_n8n_test_mode is disabled" echo "- Functions are deployed and accessible via the functions/v1/ endpoint" echo "" echo "🌐 Function URLs:" echo "- https://lovable.backoffice.biz.id/functions/v1/send-email-v2" echo "- https://lovable.backoffice.biz.id/functions/v1/create-meet-link" echo "- https://lovable.backoffice.biz.id/functions/v1/daily-reminders"