2025-06-19 09:52:15 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
# furt-lua/scripts/test_smtp.sh
|
|
|
|
|
# Test SMTP mail functionality
|
|
|
|
|
|
|
|
|
|
SERVER_URL="http://127.0.0.1:8080"
|
|
|
|
|
|
|
|
|
|
echo "Testing Furt SMTP Mail Functionality"
|
|
|
|
|
echo "========================================"
|
|
|
|
|
|
|
|
|
|
# Test 1: Server Health Check
|
|
|
|
|
echo ""
|
|
|
|
|
echo "[1] Testing Health Check..."
|
|
|
|
|
health_response=$(curl -s "$SERVER_URL/health")
|
|
|
|
|
echo "Response: $health_response"
|
|
|
|
|
|
|
|
|
|
# Check if server is responding
|
|
|
|
|
if echo "$health_response" | grep -q "healthy"; then
|
|
|
|
|
echo "[OK] Server is healthy"
|
|
|
|
|
else
|
|
|
|
|
echo "[ERROR] Server not responding or unhealthy"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Test 2: Invalid Mail Request (missing fields)
|
|
|
|
|
echo ""
|
|
|
|
|
echo "[2] Testing validation (missing fields)..."
|
|
|
|
|
invalid_response=$(curl -s -X POST "$SERVER_URL/v1/mail/send" \
|
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
|
-d '{"name":"Test"}')
|
|
|
|
|
echo "Response: $invalid_response"
|
|
|
|
|
|
|
|
|
|
# Check for validation error
|
|
|
|
|
if echo "$invalid_response" | grep -q "Missing required fields"; then
|
|
|
|
|
echo "[OK] Validation working correctly"
|
|
|
|
|
else
|
|
|
|
|
echo "[ERROR] Validation failed"
|
|
|
|
|
fi
|
|
|
|
|
|
2025-09-07 21:25:25 +02:00
|
|
|
# Test 3: Invalid Email Format
|
2025-06-19 09:52:15 +02:00
|
|
|
echo ""
|
|
|
|
|
echo "[3] Testing email validation..."
|
|
|
|
|
email_validation_response=$(curl -s -X POST "$SERVER_URL/v1/mail/send" \
|
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
|
-d '{"name":"Test","email":"invalid-email","message":"Test"}')
|
|
|
|
|
echo "Response: $email_validation_response"
|
|
|
|
|
|
|
|
|
|
# Check for email validation error
|
|
|
|
|
if echo "$email_validation_response" | grep -q "error"; then
|
|
|
|
|
echo "[OK] Email validation working"
|
|
|
|
|
else
|
|
|
|
|
echo "[ERROR] Email validation failed"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Test 4: Valid Mail Request (REAL SMTP TEST)
|
|
|
|
|
echo ""
|
|
|
|
|
echo "[4] Testing REAL mail sending..."
|
2025-09-07 21:25:25 +02:00
|
|
|
echo "WARNING: This will send a real email to admin@example.com"
|
2025-06-19 09:52:15 +02:00
|
|
|
read -p "Continue with real mail test? (y/N): " -n 1 -r
|
|
|
|
|
echo
|
|
|
|
|
|
|
|
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
|
|
|
echo "Sending real test email..."
|
2025-09-07 21:25:25 +02:00
|
|
|
|
2025-06-19 09:52:15 +02:00
|
|
|
mail_response=$(curl -s -X POST "$SERVER_URL/v1/mail/send" \
|
|
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
|
-d '{
|
|
|
|
|
"name": "Furt Test User",
|
2025-09-07 21:25:25 +02:00
|
|
|
"email": "test@example.com",
|
2025-06-19 09:52:15 +02:00
|
|
|
"subject": "Furt SMTP Test - Week 2 Success!",
|
|
|
|
|
"message": "This is a test email from the Furt Lua HTTP-Server.\n\nSMTP Integration is working!\n\nTimestamp: '$(date)'\nServer: furt-lua v1.0"
|
|
|
|
|
}')
|
2025-09-07 21:25:25 +02:00
|
|
|
|
2025-06-19 09:52:15 +02:00
|
|
|
echo "Response: $mail_response"
|
2025-09-07 21:25:25 +02:00
|
|
|
|
2025-06-19 09:52:15 +02:00
|
|
|
# Check for success
|
|
|
|
|
if echo "$mail_response" | grep -q '"success":true'; then
|
|
|
|
|
echo "[OK] MAIL SENT SUCCESSFULLY!"
|
2025-09-07 21:25:25 +02:00
|
|
|
echo "Check admin@example.com inbox"
|
|
|
|
|
|
2025-06-19 09:52:15 +02:00
|
|
|
# Extract request ID
|
|
|
|
|
request_id=$(echo "$mail_response" | grep -o '"request_id":"[^"]*"' | cut -d'"' -f4)
|
|
|
|
|
echo "Request ID: $request_id"
|
|
|
|
|
else
|
|
|
|
|
echo "[ERROR] Mail sending failed"
|
|
|
|
|
echo "Check server logs and SMTP credentials"
|
2025-09-07 21:25:25 +02:00
|
|
|
|
2025-06-19 09:52:15 +02:00
|
|
|
# Show error details
|
|
|
|
|
if echo "$mail_response" | grep -q "error"; then
|
|
|
|
|
error_msg=$(echo "$mail_response" | grep -o '"error":"[^"]*"' | cut -d'"' -f4)
|
|
|
|
|
echo "Error: $error_msg"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
echo "Skipping real mail test"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Test 5: Performance Test
|
|
|
|
|
echo ""
|
|
|
|
|
echo "[5] Testing response time..."
|
|
|
|
|
start_time=$(date +%s%N)
|
|
|
|
|
perf_response=$(curl -s "$SERVER_URL/health")
|
|
|
|
|
end_time=$(date +%s%N)
|
|
|
|
|
|
|
|
|
|
duration_ms=$(( (end_time - start_time) / 1000000 ))
|
|
|
|
|
echo "Response time: ${duration_ms}ms"
|
|
|
|
|
|
|
|
|
|
if [ $duration_ms -lt 100 ]; then
|
|
|
|
|
echo "[OK] Response time excellent (< 100ms)"
|
|
|
|
|
elif [ $duration_ms -lt 500 ]; then
|
|
|
|
|
echo "[OK] Response time good (< 500ms)"
|
|
|
|
|
else
|
|
|
|
|
echo "[WARN] Response time slow (> 500ms)"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
echo "SMTP Test Complete!"
|
|
|
|
|
echo "===================="
|
|
|
|
|
echo "[OK] Health check working"
|
|
|
|
|
echo "[OK] Input validation working"
|
|
|
|
|
echo "[OK] Email format validation working"
|
|
|
|
|
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
|
|
|
echo "Real mail test executed"
|
|
|
|
|
fi
|
|
|
|
|
echo "Performance: ${duration_ms}ms"
|
|
|
|
|
|
|
|
|
|
echo ""
|
|
|
|
|
echo "Week 2 Challenge Status:"
|
|
|
|
|
echo " SMTP Integration: COMPLETE"
|
2025-09-07 21:25:25 +02:00
|
|
|
echo " Environment Variables: CHECK .env"
|
2025-06-19 09:52:15 +02:00
|
|
|
echo " Native Lua Implementation: DONE"
|
|
|
|
|
echo " Production Ready: READY FOR TESTING"
|
|
|
|
|
|