#!/usr/bin/env bash set -Eeuo pipefail # Advanced n8n Workflow Testing Script # Tests n8n API, credentials, workflows, and RAG functionality # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # Configuration CTID="${1:-769276659}" CT_IP="${2:-192.168.45.45}" N8N_EMAIL="${3:-admin@userman.de}" N8N_PASSWORD="${4:-FAmeVE7t9d1iMIXWA1}" # From JSON output TESTS_PASSED=0 TESTS_FAILED=0 print_test() { echo -e "${BLUE}[TEST]${NC} $1"; } print_pass() { echo -e "${GREEN}[PASS]${NC} $1"; ((TESTS_PASSED++)); } print_fail() { echo -e "${RED}[FAIL]${NC} $1"; ((TESTS_FAILED++)); } print_info() { echo -e "${BLUE}[INFO]${NC} $1"; } echo -e "${BLUE}========================================${NC}" echo -e "${BLUE}n8n Workflow & API Test Suite${NC}" echo -e "${BLUE}========================================${NC}" echo "" # Test 1: n8n API Login print_test "Testing n8n API login..." LOGIN_RESPONSE=$(pct exec "${CTID}" -- bash -lc "curl -s -X POST 'http://127.0.0.1:5678/rest/login' \ -H 'Content-Type: application/json' \ -c /tmp/n8n_test_cookies.txt \ -d '{\"emailOrLdapLoginId\":\"${N8N_EMAIL}\",\"password\":\"${N8N_PASSWORD}\"}'" 2>/dev/null || echo '{"error":"failed"}') if echo "$LOGIN_RESPONSE" | grep -q '"id"'; then print_pass "Successfully logged into n8n API" USER_ID=$(echo "$LOGIN_RESPONSE" | grep -oP '"id"\s*:\s*"\K[^"]+' | head -1) print_info "User ID: ${USER_ID}" else print_fail "n8n API login failed: ${LOGIN_RESPONSE}" fi # Test 2: List credentials print_test "Listing n8n credentials..." CREDS_RESPONSE=$(pct exec "${CTID}" -- bash -lc "curl -s -X GET 'http://127.0.0.1:5678/rest/credentials' \ -H 'Content-Type: application/json' \ -b /tmp/n8n_test_cookies.txt" 2>/dev/null || echo '[]') POSTGRES_CRED=$(echo "$CREDS_RESPONSE" | grep -oP '"type"\s*:\s*"postgres".*?"name"\s*:\s*"\K[^"]+' | head -1 || echo "") OLLAMA_CRED=$(echo "$CREDS_RESPONSE" | grep -oP '"type"\s*:\s*"ollamaApi".*?"name"\s*:\s*"\K[^"]+' | head -1 || echo "") if [[ -n "$POSTGRES_CRED" ]]; then print_pass "PostgreSQL credential found: ${POSTGRES_CRED}" else print_fail "PostgreSQL credential not found" fi if [[ -n "$OLLAMA_CRED" ]]; then print_pass "Ollama credential found: ${OLLAMA_CRED}" else print_fail "Ollama credential not found" fi # Test 3: List workflows print_test "Listing n8n workflows..." WORKFLOWS_RESPONSE=$(pct exec "${CTID}" -- bash -lc "curl -s -X GET 'http://127.0.0.1:5678/rest/workflows' \ -H 'Content-Type: application/json' \ -b /tmp/n8n_test_cookies.txt" 2>/dev/null || echo '{"data":[]}') WORKFLOW_COUNT=$(echo "$WORKFLOWS_RESPONSE" | grep -o '"id"' | wc -l || echo "0") if [[ "$WORKFLOW_COUNT" -gt 0 ]]; then print_pass "Found ${WORKFLOW_COUNT} workflow(s)" # Extract workflow details WORKFLOW_NAMES=$(echo "$WORKFLOWS_RESPONSE" | grep -oP '"name"\s*:\s*"\K[^"]+' || echo "") if [[ -n "$WORKFLOW_NAMES" ]]; then print_info "Workflows:" echo "$WORKFLOW_NAMES" | while read -r name; do print_info " - ${name}" done fi # Check for RAG workflow if echo "$WORKFLOWS_RESPONSE" | grep -q "RAG KI-Bot"; then print_pass "RAG KI-Bot workflow found" # Check if workflow is active RAG_ACTIVE=$(echo "$WORKFLOWS_RESPONSE" | grep -A 10 "RAG KI-Bot" | grep -oP '"active"\s*:\s*\K(true|false)' | head -1 || echo "false") if [[ "$RAG_ACTIVE" == "true" ]]; then print_pass "RAG workflow is active" else print_fail "RAG workflow is not active" fi else print_fail "RAG KI-Bot workflow not found" fi else print_fail "No workflows found in n8n" fi # Test 4: Check webhook endpoints print_test "Checking webhook endpoints..." WEBHOOK_RESPONSE=$(pct exec "${CTID}" -- bash -lc "curl -s -o /dev/null -w '%{http_code}' 'http://127.0.0.1:5678/webhook/rag-chat-webhook/chat'" 2>/dev/null || echo "000") if [[ "$WEBHOOK_RESPONSE" == "200" ]] || [[ "$WEBHOOK_RESPONSE" == "404" ]]; then # 404 is acceptable if workflow isn't triggered yet print_pass "Chat webhook endpoint is accessible (HTTP ${WEBHOOK_RESPONSE})" else print_fail "Chat webhook endpoint not accessible (HTTP ${WEBHOOK_RESPONSE})" fi # Test 5: Test n8n settings endpoint print_test "Checking n8n settings..." SETTINGS_RESPONSE=$(pct exec "${CTID}" -- bash -lc "curl -s 'http://127.0.0.1:5678/rest/settings'" 2>/dev/null || echo '{}') if echo "$SETTINGS_RESPONSE" | grep -q '"data"'; then print_pass "n8n settings endpoint accessible" # Check telemetry settings DIAGNOSTICS=$(echo "$SETTINGS_RESPONSE" | grep -oP '"diagnosticsEnabled"\s*:\s*\K(true|false)' || echo "unknown") if [[ "$DIAGNOSTICS" == "false" ]]; then print_pass "Telemetry/diagnostics disabled as configured" else print_info "Diagnostics setting: ${DIAGNOSTICS}" fi else print_fail "n8n settings endpoint not accessible" fi # Test 6: Check n8n execution history print_test "Checking workflow execution history..." EXECUTIONS_RESPONSE=$(pct exec "${CTID}" -- bash -lc "curl -s -X GET 'http://127.0.0.1:5678/rest/executions?limit=10' \ -H 'Content-Type: application/json' \ -b /tmp/n8n_test_cookies.txt" 2>/dev/null || echo '{"data":[]}') EXECUTION_COUNT=$(echo "$EXECUTIONS_RESPONSE" | grep -o '"id"' | wc -l || echo "0") print_info "Found ${EXECUTION_COUNT} workflow execution(s)" # Test 7: Verify PostgreSQL connection from n8n print_test "Testing PostgreSQL connectivity from n8n container..." PG_TEST=$(pct exec "${CTID}" -- bash -lc "docker exec n8n nc -zv postgres 5432 2>&1" || echo "failed") if echo "$PG_TEST" | grep -q "succeeded\|open"; then print_pass "n8n can connect to PostgreSQL" else print_fail "n8n cannot connect to PostgreSQL: ${PG_TEST}" fi # Test 8: Verify PostgREST connection from n8n print_test "Testing PostgREST connectivity from n8n container..." POSTGREST_TEST=$(pct exec "${CTID}" -- bash -lc "docker exec n8n nc -zv postgrest 3000 2>&1" || echo "failed") if echo "$POSTGREST_TEST" | grep -q "succeeded\|open"; then print_pass "n8n can connect to PostgREST" else print_fail "n8n cannot connect to PostgREST: ${POSTGREST_TEST}" fi # Test 9: Check n8n environment variables print_test "Verifying n8n environment configuration..." N8N_ENCRYPTION=$(pct exec "${CTID}" -- bash -lc "docker exec n8n printenv N8N_ENCRYPTION_KEY | wc -c" 2>/dev/null || echo "0") if [[ "$N8N_ENCRYPTION" -gt 10 ]]; then print_pass "n8n encryption key is configured" else print_fail "n8n encryption key not properly configured" fi WEBHOOK_URL=$(pct exec "${CTID}" -- bash -lc "docker exec n8n printenv WEBHOOK_URL" 2>/dev/null || echo "") if [[ -n "$WEBHOOK_URL" ]]; then print_pass "Webhook URL configured: ${WEBHOOK_URL}" else print_fail "Webhook URL not configured" fi # Test 10: Test document upload form endpoint print_test "Checking document upload form endpoint..." FORM_RESPONSE=$(pct exec "${CTID}" -- bash -lc "curl -s -o /dev/null -w '%{http_code}' 'http://127.0.0.1:5678/form/rag-upload-form'" 2>/dev/null || echo "000") if [[ "$FORM_RESPONSE" == "200" ]] || [[ "$FORM_RESPONSE" == "404" ]]; then print_pass "Document upload form endpoint accessible (HTTP ${FORM_RESPONSE})" else print_fail "Document upload form endpoint not accessible (HTTP ${FORM_RESPONSE})" fi # Test 11: Check n8n logs for errors print_test "Checking n8n container logs for errors..." N8N_ERRORS=$(pct exec "${CTID}" -- bash -lc "docker logs n8n 2>&1 | grep -i 'error' | grep -v 'ErrorReporter' | tail -5" || echo "") if [[ -z "$N8N_ERRORS" ]]; then print_pass "No critical errors in n8n logs" else print_info "Recent log entries (may include non-critical errors):" echo "$N8N_ERRORS" | while read -r line; do print_info " ${line}" done fi # Test 12: Verify n8n data persistence print_test "Checking n8n data volume..." N8N_DATA_SIZE=$(pct exec "${CTID}" -- bash -lc "du -sh /opt/customer-stack/volumes/n8n-data 2>/dev/null | cut -f1" || echo "0") if [[ "$N8N_DATA_SIZE" != "0" ]]; then print_pass "n8n data volume exists: ${N8N_DATA_SIZE}" else print_fail "n8n data volume issue" fi # Test 13: Check workflow reload service status print_test "Checking workflow auto-reload service..." RELOAD_STATUS=$(pct exec "${CTID}" -- bash -lc "systemctl status n8n-workflow-reload.service | grep -oP 'Active: \K[^(]+'" 2>/dev/null || echo "unknown") print_info "Workflow reload service status: ${RELOAD_STATUS}" # Cleanup pct exec "${CTID}" -- bash -lc "rm -f /tmp/n8n_test_cookies.txt" 2>/dev/null || true # Summary echo "" echo -e "${BLUE}========================================${NC}" echo -e "${BLUE}n8n Test Summary${NC}" echo -e "${BLUE}========================================${NC}" TOTAL=$((TESTS_PASSED + TESTS_FAILED)) echo -e "Total Tests: ${TOTAL}" echo -e "${GREEN}Passed: ${TESTS_PASSED}${NC}" echo -e "${RED}Failed: ${TESTS_FAILED}${NC}" echo "" if [[ $TESTS_FAILED -eq 0 ]]; then echo -e "${GREEN}✓ All n8n tests passed!${NC}" exit 0 else echo -e "${YELLOW}⚠ Some tests failed. Review output above.${NC}" exit 1 fi