The PHP app runs on Cafe24 shared web hosting -- calling it 'cloud' was misleading. Renames the directory, the edge client module, and every doc/script/identifier that used the term. - cloud/ -> server/ (incl. server/deploy/build_cafe24_package.sh paths) - edge/cloudclient.py -> edge/serverclient.py, CloudClient -> ServerClient - cloud/docs/cloud-deploy.md -> server/docs/server-deploy.md - CLOUD_* env/config keys -> SERVER_* - tests renamed to match Tests: PHP 109 pass (4 skipped), pytest 167 pass. dist/ regenerated.
38 lines
1.9 KiB
Python
38 lines
1.9 KiB
Python
import json
|
|
from edge.serverclient import ServerClient
|
|
from edge.alert import sign
|
|
|
|
class _Resp:
|
|
def __init__(self, code, body=""): self.status_code = code; self.text = body
|
|
def json(self): return json.loads(self.text)
|
|
|
|
def test_fetch_config_signs_and_parses():
|
|
captured = {}
|
|
def fake_get(url, headers=None, timeout=None, **kw):
|
|
captured["url"] = url; captured["headers"] = headers; captured["kw"] = kw
|
|
return _Resp(200, json.dumps({"version": 3, "zones": [], "thresholds": {}}))
|
|
c = ServerClient("https://x", "pi-1", "sek", nonce_fn=lambda: "n1", now_fn=lambda: 1000.0, http_get=fake_get)
|
|
cfg = c.fetch_config()
|
|
assert cfg["version"] == 3
|
|
assert "device_id=pi-1" in captured["url"]
|
|
assert captured["headers"]["X-Signature"] == sign("sek", "n1", "1000.0", "")
|
|
# cafe24 WAF blocks the default python-requests UA; a redirect must not be auto-followed.
|
|
assert captured["headers"]["User-Agent"] == "move-detect-edge/1.0"
|
|
assert captured["kw"].get("allow_redirects") is False
|
|
|
|
def test_fetch_config_none_on_error():
|
|
c = ServerClient("https://x", "pi-1", "sek", lambda: "n", lambda: 1.0, http_get=lambda *a, **k: _Resp(401))
|
|
assert c.fetch_config() is None
|
|
|
|
def test_send_heartbeat_signs_body():
|
|
captured = {}
|
|
def fake_post(url, data=None, headers=None, timeout=None, **kw):
|
|
captured["data"] = data; captured["headers"] = headers; captured["kw"] = kw
|
|
return _Resp(200)
|
|
c = ServerClient("https://x", "pi-1", "sek", lambda: "n1", lambda: 1000.0, http_post=fake_post)
|
|
assert c.send_heartbeat({"fps": 12.0}) is True
|
|
body = captured["data"]
|
|
assert json.loads(body)["device_id"] == "pi-1"
|
|
assert captured["headers"]["X-Signature"] == sign("sek", "n1", "1000.0", body)
|
|
assert captured["headers"]["User-Agent"] == "move-detect-edge/1.0"
|
|
assert captured["kw"].get("allow_redirects") is False
|