feat(abonnement): client HTTP Frisbii (#90)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DRGb8NAMwaTZxUszSbaj4N
This commit is contained in:
Colin Maudry
2026-06-25 18:47:21 +02:00
parent b0543c0c03
commit 007cb28b21
5 changed files with 177 additions and 0 deletions
View File
+36
View File
@@ -0,0 +1,36 @@
import json as _json
import pytest
class FakeResponse:
def __init__(self, status_code: int, payload=None):
self.status_code = status_code
self._payload = payload if payload is not None else {}
def json(self):
return self._payload
@property
def text(self):
return _json.dumps(self._payload)
@pytest.fixture
def fake_httpx(monkeypatch):
"""Capture les appels httpx.request du client Frisbii et renvoie des réponses programmées."""
from src.subscriptions import client
calls = []
queue = []
def _fake_request(method, url, **kwargs):
calls.append({"method": method, "url": url, **kwargs})
if queue:
return queue.pop(0)
return FakeResponse(200, {})
monkeypatch.setenv("FRISBII_API_KEY", "priv_test")
monkeypatch.setenv("FRISBII_API_BASE_URL", "https://api.test")
monkeypatch.setattr(client.httpx, "request", _fake_request)
return {"calls": calls, "queue": queue, "Response": FakeResponse}
+61
View File
@@ -0,0 +1,61 @@
import pytest
from src.subscriptions import client
def test_auth_and_base_url_used(fake_httpx):
fake_httpx["queue"].append(fake_httpx["Response"](200, {"handle": "decpinfo-1"}))
client.get_or_create_customer("decpinfo-1", "a@b.fr")
call = fake_httpx["calls"][0]
assert call["url"] == "https://api.test/v1/customer/decpinfo-1"
assert call["auth"] == ("priv_test", "")
def test_get_or_create_customer_existing(fake_httpx):
fake_httpx["queue"].append(fake_httpx["Response"](200, {"handle": "decpinfo-1"}))
result = client.get_or_create_customer("decpinfo-1", "a@b.fr")
assert result == {"handle": "decpinfo-1"}
assert len(fake_httpx["calls"]) == 1 # pas de POST de création
def test_get_or_create_customer_creates_on_404(fake_httpx):
fake_httpx["queue"].append(fake_httpx["Response"](404, {"error": "not found"}))
fake_httpx["queue"].append(fake_httpx["Response"](200, {"handle": "decpinfo-1"}))
result = client.get_or_create_customer("decpinfo-1", "a@b.fr")
assert result == {"handle": "decpinfo-1"}
assert fake_httpx["calls"][1]["method"] == "POST"
assert fake_httpx["calls"][1]["json"] == {"handle": "decpinfo-1", "email": "a@b.fr"}
def test_create_subscription_session_returns_url(fake_httpx):
fake_httpx["queue"].append(
fake_httpx["Response"](200, {"id": "cs_1", "url": "https://pay.test/cs_1"})
)
url = client.create_subscription_session(
"plan_simple", "decpinfo-1", "https://app/ok", "https://app/ko"
)
assert url == "https://pay.test/cs_1"
body = fake_httpx["calls"][0]["json"]
assert body["prepare_subscription"] == {
"plan": "plan_simple",
"customer": "decpinfo-1",
}
assert body["accept_url"] == "https://app/ok"
assert body["cancel_url"] == "https://app/ko"
def test_create_subscription_session_no_trial(fake_httpx):
fake_httpx["queue"].append(
fake_httpx["Response"](200, {"url": "https://pay.test/cs_2"})
)
client.create_subscription_session(
"plan_simple", "decpinfo-1", "https://app/ok", "https://app/ko", no_trial=True
)
assert fake_httpx["calls"][0]["json"]["prepare_subscription"]["no_trial"] is True
def test_http_error_raises_frisbii_error(fake_httpx):
fake_httpx["queue"].append(fake_httpx["Response"](500, {"error": "boom"}))
with pytest.raises(client.FrisbiiError) as exc:
client.get_plan("plan_simple")
assert exc.value.status_code == 500