import requests import json # Base URL base_url = "http://localhost:8000" print("=== Testing Action Mapping CRUD Operations ===\n") # Step 1: Authenticate print("Step 1: Authenticating...") auth_url = f"{base_url}/api/v1/auth/login" auth_payload = { "username": "admin", "password": "admin123" } try: auth_response = requests.post(auth_url, json=auth_payload) if auth_response.status_code != 200: print(f"Auth failed: {auth_response.text}") exit(1) token = auth_response.json().get("access_token") print(f"Successfully authenticated!\n") headers = { "Content-Type": "application/json", "Authorization": f"Bearer {token}" } # Step 2: CREATE - Create a new action mapping print("Step 2: CREATE - Creating new action mapping...") create_url = f"{base_url}/api/v1/configuration/action-mappings" create_payload = { "name": "CRUD Test Mapping", "output_actions": [ { "action": "SendMail", "parameters": { "Server": "mail.example.com", "To": "test@example.com" } } ] } create_response = requests.post(create_url, json=create_payload, headers=headers) print(f"CREATE Status: {create_response.status_code}") if create_response.status_code == 201: print("CREATE successful!") create_data = create_response.json() print(json.dumps(create_data, indent=2)) else: print(f"CREATE failed: {create_response.text}") exit(1) # Step 3: READ - List all action mappings to find the ID print("\nStep 3: READ - Reading all action mappings...") read_url = f"{base_url}/api/v1/configuration/action-mappings/export" read_response = requests.get(read_url, headers=headers) if read_response.status_code == 200: read_data = read_response.json() print(f"Total mappings: {read_data.get('total_mappings', 0)}") # Find our test mapping test_mapping_id = None for idx, mapping in enumerate(read_data.get("mappings", []), 1): if mapping.get("name") == "CRUD Test Mapping": test_mapping_id = idx print(f"\nFound our test mapping at ID {test_mapping_id}:") print(json.dumps(mapping, indent=2)) break if not test_mapping_id: print("ERROR: Could not find the created mapping!") exit(1) else: print(f"READ failed: {read_response.text}") exit(1) # Step 4: UPDATE - Update the action mapping print(f"\nStep 4: UPDATE - Updating action mapping ID {test_mapping_id}...") update_url = f"{base_url}/api/v1/configuration/action-mappings/{test_mapping_id}" update_payload = { "name": "CRUD Test Mapping (Updated)", "output_actions": [ { "action": "SendMail", "parameters": { "Server": "mail.updated.com", "To": "updated@example.com", "Subject": "Updated Test" } }, { "action": "StartEvent", "parameters": { "TypeID": "100", "ForeignKey": "test-key" } } ] } update_response = requests.put(update_url, json=update_payload, headers=headers) print(f"UPDATE Status: {update_response.status_code}") if update_response.status_code == 200: print("UPDATE successful!") update_data = update_response.json() print(json.dumps(update_data, indent=2)) else: print(f"UPDATE failed: {update_response.text}") # Step 5: READ again to verify update print("\nStep 5: READ - Verifying update...") read_response2 = requests.get(read_url, headers=headers) if read_response2.status_code == 200: read_data2 = read_response2.json() for idx, mapping in enumerate(read_data2.get("mappings", []), 1): if idx == test_mapping_id: print(f"Mapping ID {test_mapping_id} after update:") print(json.dumps(mapping, indent=2)) if mapping.get("name") == "CRUD Test Mapping (Updated)": print("\nUpdate verified successfully!") else: print("\nWARNING: Update may not have been applied") break # Step 6: DELETE - Delete the action mapping print(f"\nStep 6: DELETE - Deleting action mapping ID {test_mapping_id}...") delete_url = f"{base_url}/api/v1/configuration/action-mappings/{test_mapping_id}" delete_response = requests.delete(delete_url, headers=headers) print(f"DELETE Status: {delete_response.status_code}") if delete_response.status_code == 204: print("DELETE successful!") else: print(f"DELETE response: {delete_response.text}") # Step 7: READ final to verify deletion print("\nStep 7: READ - Verifying deletion...") read_response3 = requests.get(read_url, headers=headers) if read_response3.status_code == 200: read_data3 = read_response3.json() print(f"Total mappings after delete: {read_data3.get('total_mappings', 0)}") found = False for mapping in read_data3.get("mappings", []): if mapping.get("name") in ["CRUD Test Mapping", "CRUD Test Mapping (Updated)"]: found = True print("\nWARNING: Test mapping still exists after deletion!") break if not found: print("\nDeletion verified successfully!") print("\n=== CRUD Test Complete ===") except Exception as e: print(f"Error: {e}") import traceback traceback.print_exc()