""" Test script for creating an action mapping via the REST API """ import requests import json import sys import io # Set UTF-8 encoding for Windows console if sys.platform == 'win32': sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') # API configuration BASE_URL = "http://localhost:8000" USERNAME = "admin" PASSWORD = "admin" def test_create_action_mapping(): """Test creating a new action mapping""" print("=" * 60) print("TESTING ACTION MAPPING CREATION") print("=" * 60) # Step 1: Login to get token print("\n1. Logging in...") login_response = requests.post( f"{BASE_URL}/api/v1/auth/login", json={"username": USERNAME, "password": PASSWORD} ) if login_response.status_code != 200: print(f"❌ Login failed: {login_response.status_code}") print(login_response.text) return False token = login_response.json()["access_token"] print(f"✅ Login successful, token: {token[:20]}...") headers = {"Authorization": f"Bearer {token}"} # Step 2: Get current count of mappings print("\n2. Getting current mappings count...") get_response = requests.get(f"{BASE_URL}/api/v1/action-mappings", headers=headers) if get_response.status_code != 200: print(f"❌ GET failed: {get_response.status_code}") print(get_response.text) return False current_mappings = get_response.json() initial_count = len(current_mappings) print(f"✅ Current mapping count: {initial_count}") # Step 3: Create a new mapping print("\n3. Creating new action mapping...") new_mapping = { "name": "TEST_FOLDER_TREE_MAPPING", "output_actions": [ { "action": "GCoreDataBase", "parameters": [ {"name": "PreAlarm", "value": "10"}, {"name": "Alarm", "value": "60"} ] }, { "action": "GscMail", "parameters": [ {"name": "Receiver", "value": "test@example.com"}, {"name": "Subject", "value": "Test Alert"} ] } ] } print(f" Payload: {json.dumps(new_mapping, indent=2)}") create_response = requests.post( f"{BASE_URL}/api/v1/action-mappings", headers=headers, json=new_mapping ) print(f" Status Code: {create_response.status_code}") print(f" Response: {create_response.text}") if create_response.status_code != 201: print(f"❌ CREATE failed: {create_response.status_code}") return False print("✅ CREATE returned 201 Created") # Step 4: Verify persistence by reading back print("\n4. Verifying persistence...") verify_response = requests.get(f"{BASE_URL}/api/v1/action-mappings", headers=headers) if verify_response.status_code != 200: print(f"❌ Verification GET failed: {verify_response.status_code}") return False updated_mappings = verify_response.json() final_count = len(updated_mappings) print(f" Initial count: {initial_count}") print(f" Final count: {final_count}") if final_count == initial_count + 1: print("✅ Mapping count increased by 1 - SUCCESS!") else: print(f"❌ Mapping count did NOT increase! Expected {initial_count + 1}, got {final_count}") return False # Step 5: Find the new mapping print("\n5. Searching for new mapping...") found = False for mapping in updated_mappings: if mapping.get("name") == "TEST_FOLDER_TREE_MAPPING": found = True print(f"✅ Found new mapping: {json.dumps(mapping, indent=2)}") break if not found: print("❌ New mapping NOT found in list!") return False print("\n" + "=" * 60) print("🎉 ALL TESTS PASSED!") print("=" * 60) return True if __name__ == "__main__": try: success = test_create_action_mapping() exit(0 if success else 1) except Exception as e: print(f"\n❌ EXCEPTION: {e}") import traceback traceback.print_exc() exit(1)