CalejoControl/test_discovery_endpoints.py

90 lines
3.7 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
"""
Test script to verify discovery API endpoints are working
"""
import requests
import json
def test_discovery_endpoints(base_url="http://localhost:8081"):
"""Test all discovery API endpoints"""
print(f"Testing discovery endpoints at {base_url}")
print("=" * 60)
# Test 1: Discovery status
print("\n1. Testing discovery status endpoint...")
try:
response = requests.get(f"{base_url}/api/v1/dashboard/discovery/status")
if response.status_code == 200:
status_data = response.json()
print(f" ✓ Status endpoint working")
print(f" - Is scanning: {status_data['status']['is_scanning']}")
print(f" - Current scan ID: {status_data['status']['current_scan_id']}")
print(f" - Total endpoints: {status_data['status']['total_discovered_endpoints']}")
else:
print(f" ✗ Status endpoint failed: {response.status_code}")
except Exception as e:
print(f" ✗ Status endpoint error: {e}")
# Test 2: Start discovery scan
print("\n2. Testing discovery scan endpoint...")
try:
response = requests.post(f"{base_url}/api/v1/dashboard/discovery/scan")
if response.status_code == 200:
scan_data = response.json()
print(f" ✓ Scan endpoint working")
print(f" - Scan ID: {scan_data.get('scan_id', 'N/A')}")
print(f" - Message: {scan_data.get('message', 'N/A')}")
# Store scan ID for later testing
scan_id = scan_data.get('scan_id')
else:
print(f" ✗ Scan endpoint failed: {response.status_code}")
scan_id = None
except Exception as e:
print(f" ✗ Scan endpoint error: {e}")
scan_id = None
# Test 3: Check scan status after starting
if scan_id:
print(f"\n3. Checking scan status for {scan_id}...")
import time
time.sleep(2) # Wait a bit for scan to start
try:
response = requests.get(f"{base_url}/api/v1/dashboard/discovery/status")
if response.status_code == 200:
status_data = response.json()
print(f" ✓ Status check working")
print(f" - Is scanning: {status_data['status']['is_scanning']}")
print(f" - Current scan ID: {status_data['status']['current_scan_id']}")
else:
print(f" ✗ Status check failed: {response.status_code}")
except Exception as e:
print(f" ✗ Status check error: {e}")
# Test 4: Test discovery results endpoint (even if no results yet)
print("\n4. Testing discovery results endpoint...")
test_scan_id = "test_scan_123"
try:
response = requests.get(f"{base_url}/api/v1/dashboard/discovery/results/{test_scan_id}")
if response.status_code == 404:
print(f" ✓ Results endpoint working (correctly returned 404 for non-existent scan)")
elif response.status_code == 200:
print(f" ✓ Results endpoint working")
results_data = response.json()
print(f" - Scan ID: {results_data.get('scan_id', 'N/A')}")
print(f" - Status: {results_data.get('status', 'N/A')}")
print(f" - Endpoints found: {len(results_data.get('discovered_endpoints', []))}")
else:
print(f" ✗ Results endpoint unexpected response: {response.status_code}")
except Exception as e:
print(f" ✗ Results endpoint error: {e}")
print("\n" + "=" * 60)
print("Discovery API testing completed!")
if __name__ == "__main__":
# Test local test environment
test_discovery_endpoints("http://localhost:8081")