|
| 1 | +""" |
| 2 | +Comprehensive test for all API endpoints to verify commit_message handling |
| 3 | +Tests that commit_message doesn't interfere with config validation |
| 4 | +""" |
| 5 | +import requests |
| 6 | +import json |
| 7 | +import time |
| 8 | + |
| 9 | +BASE_URL = "http://localhost:8099/api" |
| 10 | +API_KEY = "" # Add your API key if needed |
| 11 | + |
| 12 | +def test_create_helper(): |
| 13 | + """Test helper creation with commit_message""" |
| 14 | + print("\n=== Testing Helper Creation ===") |
| 15 | + response = requests.post( |
| 16 | + f"{BASE_URL}/helpers/create", |
| 17 | + json={ |
| 18 | + "type": "input_boolean", |
| 19 | + "config": { |
| 20 | + "name": "test_comprehensive_helper", |
| 21 | + "icon": "mdi:test-tube" |
| 22 | + }, |
| 23 | + "commit_message": "Test helper: comprehensive test for commit_message handling" |
| 24 | + }, |
| 25 | + headers={"X-API-Key": API_KEY} if API_KEY else {} |
| 26 | + ) |
| 27 | + print(f"Status: {response.status_code}") |
| 28 | + print(f"Response: {response.json()}") |
| 29 | + assert response.status_code == 200, f"Helper creation failed: {response.text}" |
| 30 | + return response.json() |
| 31 | + |
| 32 | +def test_create_automation(): |
| 33 | + """Test automation creation with commit_message""" |
| 34 | + print("\n=== Testing Automation Creation ===") |
| 35 | + response = requests.post( |
| 36 | + f"{BASE_URL}/automations/create", |
| 37 | + json={ |
| 38 | + "id": "test_comprehensive_automation", |
| 39 | + "alias": "Test Comprehensive Automation", |
| 40 | + "description": "Test automation for commit_message", |
| 41 | + "trigger": [{"platform": "state", "entity_id": "input_boolean.test_comprehensive_helper", "to": "on"}], |
| 42 | + "action": [{"service": "logbook.log", "data": {"name": "Test", "message": "Automation triggered"}}], |
| 43 | + "mode": "single", |
| 44 | + "commit_message": "Test automation: comprehensive test for commit_message handling" |
| 45 | + }, |
| 46 | + headers={"X-API-Key": API_KEY} if API_KEY else {} |
| 47 | + ) |
| 48 | + print(f"Status: {response.status_code}") |
| 49 | + print(f"Response: {response.json()}") |
| 50 | + assert response.status_code == 200, f"Automation creation failed: {response.text}" |
| 51 | + return response.json() |
| 52 | + |
| 53 | +def test_create_script(): |
| 54 | + """Test script creation with commit_message""" |
| 55 | + print("\n=== Testing Script Creation ===") |
| 56 | + # Test Format 1: Dictionary with script_id as key |
| 57 | + response = requests.post( |
| 58 | + f"{BASE_URL}/scripts/create", |
| 59 | + json={ |
| 60 | + "test_comprehensive_script": { |
| 61 | + "alias": "Test Comprehensive Script", |
| 62 | + "sequence": [{"service": "logbook.log", "data": {"name": "Test", "message": "Script executed"}}], |
| 63 | + "mode": "single", |
| 64 | + "icon": "mdi:test-tube" |
| 65 | + }, |
| 66 | + "commit_message": "Test script: comprehensive test for commit_message handling" |
| 67 | + }, |
| 68 | + headers={"X-API-Key": API_KEY} if API_KEY else {} |
| 69 | + ) |
| 70 | + print(f"Status: {response.status_code}") |
| 71 | + print(f"Response: {response.json()}") |
| 72 | + assert response.status_code == 200, f"Script creation failed: {response.text}" |
| 73 | + return response.json() |
| 74 | + |
| 75 | +def test_create_theme(): |
| 76 | + """Test theme creation with commit_message""" |
| 77 | + print("\n=== Testing Theme Creation ===") |
| 78 | + response = requests.post( |
| 79 | + f"{BASE_URL}/themes/create", |
| 80 | + json={ |
| 81 | + "theme_name": "test_comprehensive_theme", |
| 82 | + "theme_config": { |
| 83 | + "primary-color": "#ff0000", |
| 84 | + "accent-color": "#00ff00" |
| 85 | + }, |
| 86 | + "commit_message": "Test theme: comprehensive test for commit_message handling" |
| 87 | + }, |
| 88 | + headers={"X-API-Key": API_KEY} if API_KEY else {} |
| 89 | + ) |
| 90 | + print(f"Status: {response.status_code}") |
| 91 | + print(f"Response: {response.json()}") |
| 92 | + assert response.status_code == 200, f"Theme creation failed: {response.text}" |
| 93 | + return response.json() |
| 94 | + |
| 95 | +def test_delete_helper(): |
| 96 | + """Test helper deletion with commit_message""" |
| 97 | + print("\n=== Testing Helper Deletion ===") |
| 98 | + response = requests.delete( |
| 99 | + f"{BASE_URL}/helpers/delete/input_boolean.test_comprehensive_helper", |
| 100 | + params={"commit_message": "Test: Delete helper with custom commit message"}, |
| 101 | + headers={"X-API-Key": API_KEY} if API_KEY else {} |
| 102 | + ) |
| 103 | + print(f"Status: {response.status_code}") |
| 104 | + print(f"Response: {response.json()}") |
| 105 | + assert response.status_code == 200, f"Helper deletion failed: {response.text}" |
| 106 | + return response.json() |
| 107 | + |
| 108 | +def test_delete_automation(): |
| 109 | + """Test automation deletion with commit_message""" |
| 110 | + print("\n=== Testing Automation Deletion ===") |
| 111 | + response = requests.delete( |
| 112 | + f"{BASE_URL}/automations/delete/test_comprehensive_automation", |
| 113 | + params={"commit_message": "Test: Delete automation with custom commit message"}, |
| 114 | + headers={"X-API-Key": API_KEY} if API_KEY else {} |
| 115 | + ) |
| 116 | + print(f"Status: {response.status_code}") |
| 117 | + print(f"Response: {response.json()}") |
| 118 | + assert response.status_code == 200, f"Automation deletion failed: {response.text}" |
| 119 | + return response.json() |
| 120 | + |
| 121 | +def test_delete_script(): |
| 122 | + """Test script deletion with commit_message""" |
| 123 | + print("\n=== Testing Script Deletion ===") |
| 124 | + response = requests.delete( |
| 125 | + f"{BASE_URL}/scripts/delete/test_comprehensive_script", |
| 126 | + params={"commit_message": "Test: Delete script with custom commit message"}, |
| 127 | + headers={"X-API-Key": API_KEY} if API_KEY else {} |
| 128 | + ) |
| 129 | + print(f"Status: {response.status_code}") |
| 130 | + print(f"Response: {response.json()}") |
| 131 | + assert response.status_code == 200, f"Script deletion failed: {response.text}" |
| 132 | + return response.json() |
| 133 | + |
| 134 | +def test_delete_theme(): |
| 135 | + """Test theme deletion with commit_message""" |
| 136 | + print("\n=== Testing Theme Deletion ===") |
| 137 | + response = requests.delete( |
| 138 | + f"{BASE_URL}/themes/delete", |
| 139 | + params={ |
| 140 | + "theme_name": "test_comprehensive_theme", |
| 141 | + "commit_message": "Test: Delete theme with custom commit message" |
| 142 | + }, |
| 143 | + headers={"X-API-Key": API_KEY} if API_KEY else {} |
| 144 | + ) |
| 145 | + print(f"Status: {response.status_code}") |
| 146 | + print(f"Response: {response.json()}") |
| 147 | + assert response.status_code == 200, f"Theme deletion failed: {response.text}" |
| 148 | + return response.json() |
| 149 | + |
| 150 | +def test_git_history(): |
| 151 | + """Test git history to verify commit messages""" |
| 152 | + print("\n=== Testing Git History ===") |
| 153 | + response = requests.get( |
| 154 | + f"{BASE_URL}/backup/history", |
| 155 | + params={"limit": 10}, |
| 156 | + headers={"X-API-Key": API_KEY} if API_KEY else {} |
| 157 | + ) |
| 158 | + print(f"Status: {response.status_code}") |
| 159 | + history = response.json() |
| 160 | + print(f"Recent commits:") |
| 161 | + for commit in history.get("commits", [])[:5]: |
| 162 | + print(f" - {commit.get('hash', '')[:8]}: {commit.get('message', '')}") |
| 163 | + return history |
| 164 | + |
| 165 | +if __name__ == "__main__": |
| 166 | + print("Starting comprehensive API endpoint tests...") |
| 167 | + print("=" * 60) |
| 168 | + |
| 169 | + try: |
| 170 | + # Test creation endpoints |
| 171 | + test_create_helper() |
| 172 | + time.sleep(1) |
| 173 | + test_create_automation() |
| 174 | + time.sleep(1) |
| 175 | + test_create_script() |
| 176 | + time.sleep(1) |
| 177 | + test_create_theme() |
| 178 | + time.sleep(1) |
| 179 | + |
| 180 | + # Test deletion endpoints |
| 181 | + test_delete_script() |
| 182 | + time.sleep(1) |
| 183 | + test_delete_automation() |
| 184 | + time.sleep(1) |
| 185 | + test_delete_helper() |
| 186 | + time.sleep(1) |
| 187 | + test_delete_theme() |
| 188 | + time.sleep(1) |
| 189 | + |
| 190 | + # Verify git history |
| 191 | + test_git_history() |
| 192 | + |
| 193 | + print("\n" + "=" * 60) |
| 194 | + print("✅ All tests passed!") |
| 195 | + |
| 196 | + except AssertionError as e: |
| 197 | + print(f"\n❌ Test failed: {e}") |
| 198 | + exit(1) |
| 199 | + except Exception as e: |
| 200 | + print(f"\n❌ Unexpected error: {e}") |
| 201 | + exit(1) |
| 202 | + |
0 commit comments