#!/usr/bin/env python3 """ Test script for action mappings endpoint """ import requests import json import sys # API endpoint BASE_URL = "http://localhost:8000" LOGIN_ENDPOINT = f"{BASE_URL}/api/v1/auth/login" ACTION_MAPPINGS_ENDPOINT = f"{BASE_URL}/api/v1/configuration/action-mappings/export" # Default admin credentials (from init_db.py or common defaults) USERNAME = "admin" PASSWORD = "admin" def main(): print("=" * 60) print("Testing Action Mappings Endpoint") print("=" * 60) # Step 1: Login to get token print(f"\n[1/3] Logging in as '{USERNAME}'...") try: login_response = requests.post( LOGIN_ENDPOINT, data={ "username": USERNAME, "password": PASSWORD }, headers={"Content-Type": "application/x-www-form-urlencoded"} ) if login_response.status_code != 200: print(f" ❌ Login failed: {login_response.status_code}") print(f" Response: {login_response.text}") print(f"\n Trying alternative credentials...") # Try alternative common credentials alternatives = [ ("admin", "masterkey"), ("sysadmin", "masterkey"), ("administrator", "admin"), ] for alt_user, alt_pass in alternatives: print(f" Trying {alt_user}:{alt_pass}...") login_response = requests.post( LOGIN_ENDPOINT, data={"username": alt_user, "password": alt_pass}, headers={"Content-Type": "application/x-www-form-urlencoded"} ) if login_response.status_code == 200: print(f" ✅ Login successful with {alt_user}") break else: print(f"\n ❌ All login attempts failed") print(f"\n You may need to create a test user first.") return 1 else: print(f" ✅ Login successful") token_data = login_response.json() access_token = token_data.get("access_token") if not access_token: print(f" ❌ No access token in response") return 1 except requests.exceptions.ConnectionError: print(f" ❌ Cannot connect to API server at {BASE_URL}") print(f" Make sure the services are running") return 1 except Exception as e: print(f" ❌ Login error: {e}") return 1 # Step 2: Call action mappings endpoint print(f"\n[2/3] Calling action mappings endpoint...") try: headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } mappings_response = requests.get( ACTION_MAPPINGS_ENDPOINT, headers=headers, timeout=30 ) if mappings_response.status_code != 200: print(f" ❌ Request failed: {mappings_response.status_code}") print(f" Response: {mappings_response.text}") return 1 print(f" ✅ Request successful") except Exception as e: print(f" ❌ Request error: {e}") return 1 # Step 3: Parse and display results print(f"\n[3/3] Analyzing results...") try: result = mappings_response.json() print(f"\n Success: {result.get('success', False)}") print(f" Total Count: {result.get('total_count', 0)}") mappings = result.get('mappings', []) print(f" Mappings Found: {len(mappings)}") if result.get('error_message'): print(f" Error: {result['error_message']}") # Display detailed results print("\n" + "=" * 60) print("RESULTS") print("=" * 60) if mappings: total_actions = sum(len(m.get('actions', [])) for m in mappings) print(f"\n✅ SUCCESS: Found {len(mappings)} Rules markers with {total_actions} total actions") print(f"\nFirst 5 mappings:") for i, mapping in enumerate(mappings[:5], 1): actions = mapping.get('actions', []) print(f"\n [{i}] Rules marker at offset {mapping.get('start_offset', 0)}") print(f" Actions ({len(actions)}):") for action in actions[:3]: print(f" - {action}") if len(actions) > 3: print(f" ... and {len(actions) - 3} more") if len(mappings) > 5: print(f"\n ... and {len(mappings) - 5} more Rules markers") print(f"\n{'='*60}") print(f"✅ TEST PASSED: Action mappings endpoint is working!") print(f"{'='*60}") return 0 else: print(f"\n❌ FAILED: No mappings found (expected ~60)") print(f"\nFull response:") print(json.dumps(result, indent=2)) print(f"\n{'='*60}") print(f"❌ TEST FAILED: Action mappings endpoint returns empty array") print(f"{'='*60}") return 1 except Exception as e: print(f" ❌ Parse error: {e}") print(f" Raw response: {mappings_response.text[:500]}") return 1 if __name__ == "__main__": sys.exit(main())