-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
443 lines (358 loc) · 14.7 KB
/
test_api.py
File metadata and controls
443 lines (358 loc) · 14.7 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""
Comprehensive test suite for HTML Checker API
Tests all endpoints with various scenarios and edge cases
"""
import pytest
import requests
import io
from pathlib import Path
# Base URL for the deployed API
BASE_URL = "https://html-checker-yc8t.onrender.com"
class TestRootEndpoint:
"""Test the root endpoint GET /"""
def test_root_returns_html(self):
"""Test that root endpoint returns HTML content"""
response = requests.get(f"{BASE_URL}/")
assert response.status_code == 200
assert "text/html" in response.headers.get("content-type", "")
assert len(response.text) > 0
def test_root_contains_html_structure(self):
"""Test that root endpoint contains valid HTML structure"""
response = requests.get(f"{BASE_URL}/")
content = response.text.lower()
assert "<html" in content or "<!doctype html>" in content
assert "<body" in content or response.status_code == 200
class TestUploadEndpoint:
"""Test the upload endpoint POST /upload"""
def test_upload_simple_html_with_citations(self):
"""Test uploading HTML with simple cite markers"""
html_content = """
<html>
<body>
<p>This is a test [cite: 123].</p>
<p>Another paragraph [cite: 456].</p>
</body>
</html>
"""
files = {
'file': ('test.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["original_filename"] == "test.html"
assert "output_filename" in data
assert "download_url" in data
assert data["statistics"]["total_citations_removed"] == 2
assert data["statistics"]["cite_with_numbers"] == 2
assert data["statistics"]["cite_start_markers"] == 0
def test_upload_html_with_cite_start(self):
"""Test uploading HTML with [cite_start] markers"""
html_content = """
<html>
<body>
<p>[cite_start] This is a citation.</p>
<p>Normal paragraph.</p>
<div>[cite_start]</div>
</body>
</html>
"""
files = {
'file': ('cite_start.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["statistics"]["cite_start_markers"] == 2
def test_upload_html_with_multiple_citations(self):
"""Test HTML with multiple citation formats (commas and dashes)"""
html_content = """
<html>
<body>
<p>Multiple cites [cite: 123, 124, 125].</p>
<p>Range cite [cite: 105-107].</p>
<p>Mixed [cite: 1, 2-4, 5].</p>
</body>
</html>
"""
files = {
'file': ('multi_cite.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["statistics"]["cite_with_numbers"] == 3
def test_upload_html_with_only_cite_tags(self):
"""Test HTML with tags containing only cite markers"""
html_content = """
<html>
<body>
<p>[cite: 123]</p>
<div>[cite_start]</div>
<span>[cite: 456, 789]</span>
<p>This should stay [cite: 999].</p>
</body>
</html>
"""
files = {
'file': ('only_cite.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["statistics"]["total_citations_removed"] >= 4
def test_upload_clean_html_no_citations(self):
"""Test uploading HTML without any citations"""
html_content = """
<html>
<body>
<p>Clean paragraph without citations.</p>
<p>Another clean paragraph.</p>
</body>
</html>
"""
files = {
'file': ('clean.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["statistics"]["total_citations_removed"] == 0
def test_upload_large_html_file(self):
"""Test uploading a larger HTML file with many citations"""
paragraphs = [f"<p>Paragraph {i} [cite: {i}].</p>" for i in range(100)]
html_content = f"""
<html>
<body>
{''.join(paragraphs)}
</body>
</html>
"""
files = {
'file': ('large.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["statistics"]["total_citations_removed"] == 100
def test_upload_non_html_file(self):
"""Test uploading non-HTML file (should fail)"""
txt_content = "This is a text file, not HTML."
files = {
'file': ('test.txt', io.BytesIO(txt_content.encode('utf-8')), 'text/plain')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 400
assert "Only HTML files are allowed" in response.json()["detail"]
def test_upload_without_file(self):
"""Test upload endpoint without providing a file"""
response = requests.post(f"{BASE_URL}/upload")
assert response.status_code == 422 # Unprocessable Entity
def test_upload_empty_html_file(self):
"""Test uploading empty HTML file"""
html_content = ""
files = {
'file': ('empty.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["statistics"]["total_citations_removed"] == 0
def test_upload_html_with_special_characters(self):
"""Test HTML with special characters and citations"""
html_content = """
<html>
<body>
<p>Special chars: & < > " [cite: 123].</p>
<p>Unicode: 你好世界 [cite: 456].</p>
<p>Emoji: 😀🎉 [cite: 789].</p>
</body>
</html>
"""
files = {
'file': ('special.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["statistics"]["cite_with_numbers"] == 3
def test_upload_html_with_nested_tags(self):
"""Test HTML with nested tags and citations"""
html_content = """
<html>
<body>
<div>
<p>Outer paragraph [cite: 1].</p>
<div>
<span>Nested span [cite: 2].</span>
<p>Nested paragraph [cite: 3].</p>
</div>
</div>
</body>
</html>
"""
files = {
'file': ('nested.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["statistics"]["total_citations_removed"] >= 3
class TestDownloadEndpoint:
"""Test the download endpoint GET /download/{filename}"""
def test_download_nonexistent_file(self):
"""Test downloading a file that doesn't exist"""
response = requests.get(f"{BASE_URL}/download/nonexistent.html")
assert response.status_code == 404
assert "File not found" in response.json()["detail"]
def test_upload_and_download_workflow(self):
"""Test complete workflow: upload file, then download cleaned version"""
# Step 1: Upload file
html_content = """
<html>
<body>
<p>Test content [cite: 123].</p>
</body>
</html>
"""
files = {
'file': ('workflow.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
upload_response = requests.post(f"{BASE_URL}/upload", files=files)
assert upload_response.status_code == 200
upload_data = upload_response.json()
output_filename = upload_data["output_filename"]
# Step 2: Download the cleaned file
download_url = upload_data["download_url"]
download_response = requests.get(f"{BASE_URL}{download_url}")
assert download_response.status_code == 200
assert "text/html" in download_response.headers.get("content-type", "")
# Verify citations are removed
cleaned_content = download_response.text
assert "[cite:" not in cleaned_content
assert "[cite_start]" not in cleaned_content
def test_download_with_path_traversal_attempt(self):
"""Test download endpoint with path traversal attempt (security test)"""
response = requests.get(f"{BASE_URL}/download/../../../etc/passwd")
# Should either return 404 or handle the path safely
assert response.status_code in [404, 422]
class TestStaticFiles:
"""Test static file serving"""
def test_static_files_accessible(self):
"""Test that static files endpoint is accessible"""
# Try to access static directory (may or may not have files)
response = requests.get(f"{BASE_URL}/static/")
# Should either return files or 404, but not 500
assert response.status_code in [200, 403, 404]
def test_public_templates_accessible(self):
"""Test that public templates endpoint is accessible"""
response = requests.get(f"{BASE_URL}/templates/public/")
# Should either return files or 404, but not 500
assert response.status_code in [200, 403, 404]
class TestEdgeCases:
"""Test edge cases and error handling"""
def test_upload_html_with_malformed_citations(self):
"""Test HTML with malformed citation markers"""
html_content = """
<html>
<body>
<p>Malformed [cite: abc].</p>
<p>Incomplete [cite: .</p>
<p>Valid [cite: 123].</p>
</body>
</html>
"""
files = {
'file': ('malformed.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
# Only the valid citation should be counted
assert data["statistics"]["cite_with_numbers"] >= 1
def test_upload_html_with_whitespace_variations(self):
"""Test citations with various whitespace patterns"""
html_content = """
<html>
<body>
<p>Normal [cite: 123].</p>
<p>Extra spaces [cite: 456 ].</p>
<p>No spaces [cite:789].</p>
<p>Tabs [cite: 111 ].</p>
</body>
</html>
"""
files = {
'file': ('whitespace.html', io.BytesIO(html_content.encode('utf-8')), 'text/html')
}
response = requests.post(f"{BASE_URL}/upload", files=files)
assert response.status_code == 200
data = response.json()
assert data["success"] is True
assert data["statistics"]["cite_with_numbers"] >= 3
def test_concurrent_uploads(self):
"""Test handling multiple concurrent uploads"""
html_content = """
<html>
<body>
<p>Concurrent test [cite: 999].</p>
</body>
</html>
"""
# Simulate concurrent requests
import concurrent.futures
def upload_file(index):
files = {
'file': (f'concurrent_{index}.html',
io.BytesIO(html_content.encode('utf-8')),
'text/html')
}
return requests.post(f"{BASE_URL}/upload", files=files)
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(upload_file, i) for i in range(5)]
results = [f.result() for f in concurrent.futures.as_completed(futures)]
# All requests should succeed
for response in results:
assert response.status_code == 200
data = response.json()
assert data["success"] is True
class TestAPIDocumentation:
"""Test API documentation endpoints"""
def test_docs_endpoint(self):
"""Test that API documentation is available"""
response = requests.get(f"{BASE_URL}/docs")
assert response.status_code == 200
def test_openapi_schema(self):
"""Test that OpenAPI schema is available"""
response = requests.get(f"{BASE_URL}/openapi.json")
assert response.status_code == 200
schema = response.json()
assert "openapi" in schema
assert "info" in schema
assert schema["info"]["title"] == "HTML Cite Cleaner"
# Test runner with detailed reporting
if __name__ == "__main__":
print("=" * 80)
print("HTML Checker API - Comprehensive Test Suite")
print(f"Testing: {BASE_URL}")
print("=" * 80)
print()
# Run tests with verbose output
pytest.main([
__file__,
"-v", # Verbose
"--tb=short", # Short traceback
"-s", # Show print statements
"--color=yes", # Colored output
"-x", # Stop on first failure (remove this to run all tests)
])