From 3d4ea88269014fd29fc0f4294aade4ef3947b75a Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Sun, 7 Sep 2025 09:07:40 +0200 Subject: [PATCH] Fix jwt-token --- .env.test.example | 20 ++++++++ run-tests.sh | 103 ++++++++++++++++++++++++++++++++++++++++ test_drone_curl.sh | 23 +++++++-- test_drone_data.py | 15 ++++-- test_orlan_detection.py | 14 +++++- test_orlan_scenario.py | 14 +++++- 6 files changed, 178 insertions(+), 11 deletions(-) create mode 100644 .env.test.example create mode 100644 run-tests.sh diff --git a/.env.test.example b/.env.test.example new file mode 100644 index 0000000..8d4c3c2 --- /dev/null +++ b/.env.test.example @@ -0,0 +1,20 @@ +# Environment configuration for test scripts +# Copy this to .env in the project root or set these as environment variables + +# API Base URL - Update this to your deployment +API_BASE_URL=http://localhost:3002/api + +# For production deployment, use your domain: +# API_BASE_URL=https://your-domain.com/uggla/api + +# Base path (should match VITE_BASE_PATH) +VITE_BASE_PATH=/uggla + +# Alternative configurations: +# For local development: +# API_BASE_URL=http://localhost:3002/api +# VITE_BASE_PATH= + +# For production with different base path: +# API_BASE_URL=https://your-domain.com +# VITE_BASE_PATH=/your-custom-path diff --git a/run-tests.sh b/run-tests.sh new file mode 100644 index 0000000..4eafcac --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,103 @@ +#!/bin/bash + +# Test runner script for Uggla Drone Detection System +# Automatically sets up environment and runs test scripts + +set -e + +# Colors for output +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +echo -e "${BLUE}======================================${NC}" +echo -e "${BLUE} Uggla Test Runner${NC}" +echo -e "${BLUE}======================================${NC}" +echo + +# Check if .env exists, if not suggest creating it +if [ ! -f ".env" ]; then + echo -e "${YELLOW}Warning: .env file not found.${NC}" + echo "You can:" + echo "1. Copy .env.test.example to .env and customize it" + echo "2. Set environment variables manually" + echo "3. Use default localhost settings" + echo + read -p "Continue with default localhost settings? (y/N): " CONTINUE + if [[ ! $CONTINUE =~ ^[Yy]$ ]]; then + echo "Please create .env file and try again." + exit 1 + fi + + # Set default environment variables + export API_BASE_URL="http://localhost:3002/api" + export VITE_BASE_PATH="" + echo -e "${GREEN}Using default localhost configuration${NC}" +else + echo -e "${GREEN}Loading environment from .env file${NC}" + # Load environment variables from .env file + export $(grep -v '^#' .env | xargs) +fi + +echo -e "${BLUE}Current configuration:${NC}" +echo "API_BASE_URL: ${API_BASE_URL:-http://localhost:3002/api}" +echo "VITE_BASE_PATH: ${VITE_BASE_PATH:-}" +echo + +# Check which test to run +if [ $# -eq 0 ]; then + echo "Available tests:" + echo "1. test_drone_data.py - Comprehensive drone simulation" + echo "2. test_orlan_scenario.py - Military drone approach scenario" + echo "3. test_orlan_detection.py - Long-range Orlan detection test" + echo "4. test_drone_curl.sh - Simple curl-based API test" + echo + read -p "Select test (1-4): " TEST_CHOICE + + case $TEST_CHOICE in + 1) TEST_SCRIPT="test_drone_data.py" ;; + 2) TEST_SCRIPT="test_orlan_scenario.py" ;; + 3) TEST_SCRIPT="test_orlan_detection.py" ;; + 4) TEST_SCRIPT="test_drone_curl.sh" ;; + *) echo "Invalid choice"; exit 1 ;; + esac +else + TEST_SCRIPT=$1 +fi + +echo -e "${YELLOW}Running test: $TEST_SCRIPT${NC}" +echo + +# Check if script exists +if [ ! -f "$TEST_SCRIPT" ]; then + echo "Error: Test script $TEST_SCRIPT not found" + exit 1 +fi + +# Run the test script +if [[ $TEST_SCRIPT == *.py ]]; then + # Check if Python and required packages are available + if ! command -v python3 &> /dev/null; then + echo "Error: Python 3 is required to run Python test scripts" + exit 1 + fi + + # Check if requests module is available + python3 -c "import requests" 2>/dev/null || { + echo "Error: 'requests' module is required. Install with: pip install requests" + exit 1 + } + + python3 "$TEST_SCRIPT" +elif [[ $TEST_SCRIPT == *.sh ]]; then + # Make sure the script is executable + chmod +x "$TEST_SCRIPT" + ./"$TEST_SCRIPT" +else + echo "Error: Unknown script type for $TEST_SCRIPT" + exit 1 +fi + +echo +echo -e "${GREEN}Test completed!${NC}" diff --git a/test_drone_curl.sh b/test_drone_curl.sh index ccfbf9e..252df2d 100644 --- a/test_drone_curl.sh +++ b/test_drone_curl.sh @@ -1,15 +1,30 @@ #!/bin/bash # Simple bash script to test drone detection API with curl -API_BASE="http://selfservice.cqers.com/drones/api" -# Alternative for local testing: "http://localhost:3002/api" +# Load environment variables from .env file if it exists +if [ -f ".env" ]; then + export $(grep -v '^#' .env | xargs) +fi + +# Configuration from environment variables +API_BASE_URL=${API_BASE_URL:-"http://localhost:3002/api"} +BASE_PATH=${VITE_BASE_PATH:-""} + +# If BASE_PATH is set, construct the full URL +if [ ! -z "$BASE_PATH" ] && [[ ! "$API_BASE_URL" == *"/api" ]]; then + # Remove any existing path suffixes and add base path + DOMAIN=$(echo "$API_BASE_URL" | sed 's|/api$||' | sed 's|/drones/api$||' | sed 's|/uggla/api$||') + API_BASE_URL="${DOMAIN}${BASE_PATH}/api" +fi echo "🚁 Drone Detection API Test Script" echo "==================================" +echo "🔗 Using API Base URL: $API_BASE_URL" +echo "" # Test API health echo "🔍 Testing API health..." -curl -s "$API_BASE/health" | jq '.' || echo "Health check failed" +curl -s "$API_BASE_URL/health" | jq '.' || echo "Health check failed" echo "" # Send test detection data @@ -39,7 +54,7 @@ echo "" # Send the detection echo "Sending detection..." RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" \ - -X POST "$API_BASE/detections" \ + -X POST "$API_BASE_URL/detectors" \ -H "Content-Type: application/json" \ -d "$DETECTION_DATA") diff --git a/test_drone_data.py b/test_drone_data.py index df468f3..7327d88 100644 --- a/test_drone_data.py +++ b/test_drone_data.py @@ -11,11 +11,20 @@ import time import random import math import argparse +import os from datetime import datetime, timedelta -# Configuration -API_BASE_URL = "https://selfservice.cqers.com/drones/api" -# Alternative for local testing: "http://localhost:3002/api" +# Configuration from environment variables +API_BASE_URL = os.getenv('API_BASE_URL', 'http://localhost:3002/api') +BASE_PATH = os.getenv('VITE_BASE_PATH', '').rstrip('/') + +# If BASE_PATH is set, construct the full URL +if BASE_PATH and not API_BASE_URL.endswith('/api'): + # Extract domain from API_BASE_URL and add base path + domain = API_BASE_URL.replace('/api', '').replace('/drones/api', '').replace('/uggla/api', '') + API_BASE_URL = f"{domain}{BASE_PATH}/api" + +print(f"🔗 Using API Base URL: {API_BASE_URL}") # Debug configuration DEBUG_MODE = False # Set to True to enable payload debugging diff --git a/test_orlan_detection.py b/test_orlan_detection.py index aef4520..514a22b 100644 --- a/test_orlan_detection.py +++ b/test_orlan_detection.py @@ -15,10 +15,20 @@ import requests import json import time import math +import os from datetime import datetime -# Configuration -API_BASE_URL = "http://selfservice.cqers.com/drones/api" +# Configuration from environment variables +API_BASE_URL = os.getenv('API_BASE_URL', 'http://localhost:3002/api') +BASE_PATH = os.getenv('VITE_BASE_PATH', '').rstrip('/') + +# If BASE_PATH is set, construct the full URL +if BASE_PATH and not API_BASE_URL.endswith('/api'): + # Extract domain from API_BASE_URL and add base path + domain = API_BASE_URL.replace('/api', '').replace('/drones/api', '').replace('/uggla/api', '') + API_BASE_URL = f"{domain}{BASE_PATH}/api" + +print(f"🔗 Using API Base URL: {API_BASE_URL}") # Detection range parameters (approximate) MAX_DETECTION_RANGE_KM = 25.0 # Maximum range for drone detection diff --git a/test_orlan_scenario.py b/test_orlan_scenario.py index 6a5f9ef..89520a8 100644 --- a/test_orlan_scenario.py +++ b/test_orlan_scenario.py @@ -9,10 +9,20 @@ import requests import json import time import math +import os from datetime import datetime -# Configuration -API_BASE_URL = "http://selfservice.cqers.com/drones/api" +# Configuration from environment variables +API_BASE_URL = os.getenv('API_BASE_URL', 'http://localhost:3002/api') +BASE_PATH = os.getenv('VITE_BASE_PATH', '').rstrip('/') + +# If BASE_PATH is set, construct the full URL +if BASE_PATH and not API_BASE_URL.endswith('/api'): + # Extract domain from API_BASE_URL and add base path + domain = API_BASE_URL.replace('/api', '').replace('/drones/api', '').replace('/uggla/api', '') + API_BASE_URL = f"{domain}{BASE_PATH}/api" + +print(f"🔗 Using API Base URL: {API_BASE_URL}") def haversine_distance(lat1, lon1, lat2, lon2): """Calculate distance between two points in kilometers"""