test_guard.py
1.81 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
#!/usr/bin/env python3
"""Test Agent: guard test — verify /api/cron/list health and /cron page guard presence."""
import json
import subprocess
import pytest
BASE_URL = "http://127.0.0.1:5001"
def _curl_html(path):
r = subprocess.run(
["curl", "-s", f"{BASE_URL}{path}"],
capture_output=True, text=True, timeout=10,
)
return int(r.returncode), r.stdout
def _curl_status_body(path):
r = subprocess.run(
["curl", "-s", "-o", "/dev/null", "-w", "%{http_code}", f"{BASE_URL}{path}"],
capture_output=True, text=True, timeout=10,
)
status = int(r.stdout.strip() or 0)
r2 = subprocess.run(
["curl", "-s", f"{BASE_URL}{path}"],
capture_output=True, text=True, timeout=10,
)
try:
body = json.loads(r2.stdout) if r2.stdout.strip() else {}
except Exception:
body = {"_raw": r2.stdout[:200]}
return status, body
def test_cron_page_guard_reachable():
rc, html = _curl_html("/cron")
assert rc == 0
assert "<html" in html.lower()
def test_cron_list_guard_returns_200():
status, data = _curl_status_body("/api/cron/list")
assert status == 200
assert data.get("ok") is True
def test_cron_list_guard_returns_jobs_list():
_, data = _curl_status_body("/api/cron/list")
jobs = data.get("data", [])
assert isinstance(jobs, list)
def test_cron_jobs_have_required_fields():
_, data = _curl_status_body("/api/cron/list")
jobs = data.get("data", [])
for job in jobs:
assert job.get("id"), f"missing id: {job}"
assert job.get("name"), f"missing name: {job}"
assert "schedule" in job, f"missing schedule: {list(job.keys())}"
assert isinstance(job.get("enabled"), bool), f"enabled not bool: {job.get('enabled')}"
if __name__ == "__main__":
pytest.main([__file__, "-v"])