booklooker_client/tests/test_resilience.py

50 lines
1.6 KiB
Python

import hashlib
import hmac
import httpx
import pytest
from booklooker_client import BooklookerConfig, BooklookerWebhookHelper, SyncBooklookerClient
def test_sync_client_retries_transient_timeout() -> None:
attempts = {"count": 0}
def handler(request: httpx.Request) -> httpx.Response:
attempts["count"] += 1
if attempts["count"] == 1:
raise httpx.ReadTimeout("temporary timeout", request=request)
return httpx.Response(200, json={"status": "OK", "returnValue": "REST_API_TOKEN"})
config = BooklookerConfig(api_key="demo", max_retries=1, retry_backoff_seconds=0)
client = SyncBooklookerClient(config)
client._http = httpx.Client(transport=httpx.MockTransport(handler), base_url=config.base_url)
try:
token = client.authenticate()
finally:
client.close()
assert token.token == "REST_API_TOKEN"
assert attempts["count"] == 2
def test_webhook_signature_verification() -> None:
payload = b'{"event_type":"order.created","event_id":"evt-10"}'
secret = "top-secret"
signature = hmac.new(secret.encode("utf-8"), payload, hashlib.sha256).hexdigest()
helper = BooklookerWebhookHelper(webhook_secret=secret)
assert helper.verify_signature(payload, signature) is True
def test_import_file_rejects_oversized_input(tmp_path) -> None:
file_path = tmp_path / "payload.bin"
file_path.write_bytes(b"1234")
client = SyncBooklookerClient(BooklookerConfig(api_key="demo", max_upload_size_bytes=1))
try:
with pytest.raises(ValueError):
client.import_file(file_path, data_type=0)
finally:
client.close()