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