Found in the clean-Windows recovery test: a single TLS handshake failure mid-job failed a whole sync. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
64 lines
2.0 KiB
Python
64 lines
2.0 KiB
Python
import httpx
|
|
|
|
from gti_fiscal_agent.client import HubClient
|
|
|
|
|
|
def test_identity_requests_authenticated_identity_only(monkeypatch):
|
|
calls = {}
|
|
payload = {
|
|
"agent_id": 2,
|
|
"installation_id": "00000000-0000-4000-8000-000000000222",
|
|
"hostname": "DESKTOP-B0HL02I",
|
|
"version": "1.1.1",
|
|
"status": "OFFLINE",
|
|
}
|
|
|
|
class Response:
|
|
status_code = 200
|
|
|
|
def raise_for_status(self):
|
|
return None
|
|
|
|
def json(self):
|
|
return payload
|
|
|
|
def fake_get(url, *, headers, timeout):
|
|
calls.update(url=url, headers=headers, timeout=timeout)
|
|
return Response()
|
|
|
|
monkeypatch.setattr(httpx, "get", fake_get)
|
|
client = HubClient("https://hub.example", "test-agent-token")
|
|
assert client.identity() == payload
|
|
|
|
assert calls["url"] == "https://hub.example/v1/agents/me/identity"
|
|
assert calls["headers"] == {"Authorization": "Bearer test-agent-token"}
|
|
|
|
|
|
def test_transport_errors_and_proxy_5xx_are_retried_but_client_errors_are_not(monkeypatch):
|
|
import httpx
|
|
from gti_fiscal_agent import client as c
|
|
monkeypatch.setattr(c.time, "sleep", lambda s: None)
|
|
calls = {"n": 0}
|
|
|
|
def flaky():
|
|
calls["n"] += 1
|
|
if calls["n"] == 1:
|
|
raise httpx.ConnectError("ssl handshake failure")
|
|
if calls["n"] == 2:
|
|
return httpx.Response(502)
|
|
return httpx.Response(200, json={"ok": True})
|
|
|
|
assert c._resilient(flaky).status_code == 200 and calls["n"] == 3
|
|
calls["n"] = 0
|
|
assert c._resilient(lambda: (calls.__setitem__("n", calls["n"] + 1), httpx.Response(401))[1]).status_code == 401 and calls["n"] == 1 # 4xx: no retry
|
|
calls["n"] = 0
|
|
|
|
def always_down():
|
|
calls["n"] += 1
|
|
raise httpx.ConnectError("down")
|
|
|
|
import pytest
|
|
with pytest.raises(httpx.ConnectError):
|
|
c._resilient(always_down)
|
|
assert calls["n"] == 5 # bounded: 5 attempts, then the job fails cleanly
|