Amélioration des tests

This commit is contained in:
Colin Maudry
2026-06-29 16:53:19 +02:00
parent db148eef90
commit d2f23ae6c8
5 changed files with 35 additions and 28 deletions
+2 -2
View File
@@ -6,9 +6,9 @@ import pytest
# unitaires de pages (pas besoin du serveur complet — juste que CONFIG soit peuplé).
from dash import Dash as _Dash
from src.pages import compte_abonnement # noqa: F401
_Dash(__name__, use_pages=True, pages_folder="", assets_folder="assets")
_Dash(__name__, assets_folder="assets")
from src.pages import compte_abonnement # noqa: F401, E402 # doit venir après Dash()
class FakeResponse:
+18 -14
View File
@@ -5,29 +5,27 @@ 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")
client.get_customer("decpinfo-1")
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):
def test_get_customer(fake_httpx):
fake_httpx["queue"].append(fake_httpx["Response"](200, {"handle": "decpinfo-1"}))
result = client.get_or_create_customer("decpinfo-1", "a@b.fr")
result = client.get_customer("decpinfo-1")
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"}))
def test_update_customer(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 fake_httpx["calls"][1]["method"] == "POST"
assert fake_httpx["calls"][1]["json"] == {"handle": "decpinfo-1", "email": "a@b.fr"}
client.update_customer("decpinfo-1", {"email": "a@b.fr"})
call = fake_httpx["calls"][0]
assert call["method"] == "PUT"
assert call["json"] == {"email": "a@b.fr"}
def test_create_subscription_session_returns_url(fake_httpx):
def test_create_subscription_session_with_customer_handle(fake_httpx):
fake_httpx["queue"].append(
fake_httpx["Response"](
200,
@@ -39,7 +37,10 @@ def test_create_subscription_session_returns_url(fake_httpx):
)
)
url = client.create_subscription_session(
"plan_simple", "decpinfo-1", "https://app/ok", "https://app/ko"
"plan_simple",
"https://app/ok",
"https://app/ko",
customer_handle="decpinfo-1",
)
assert url == "https://checkout.reepay.com/#/sub-1"
body = fake_httpx["calls"][0]["json"]
@@ -47,7 +48,6 @@ def test_create_subscription_session_returns_url(fake_httpx):
assert body["customer"] == "decpinfo-1"
assert body["signup_method"] == "link"
assert "prepare_subscription" not in body
assert "accept_url" not in body
def test_create_subscription_session_no_trial(fake_httpx):
@@ -62,7 +62,11 @@ def test_create_subscription_session_no_trial(fake_httpx):
)
)
client.create_subscription_session(
"plan_simple", "decpinfo-1", "https://app/ok", "https://app/ko", no_trial=True
"plan_simple",
"https://app/ok",
"https://app/ko",
no_trial=True,
customer_handle="decpinfo-1",
)
assert fake_httpx["calls"][0]["json"]["no_trial"] is True
@@ -29,7 +29,7 @@ def test_active_view_shows_cancel(monkeypatch):
"current_period_end": "2099-01-01T00:00:00+00:00",
}
view = compte_abonnement._active_view(row)
assert "Résilier" in str(view)
assert "Me désabonner" in str(view)
def test_active_view_trial_banner(monkeypatch):
+14 -11
View File
@@ -13,13 +13,16 @@ def _sign(secret, timestamp, event_id):
def test_subscribe_redirects_to_hosted_url(logged_in_client, monkeypatch):
client, uid = logged_in_client
monkeypatch.setattr(
frisbii_client, "get_or_create_customer", lambda h, e: {"handle": h}
)
monkeypatch.setattr(frisbii_client, "update_customer", lambda h, d: {})
monkeypatch.setattr(
frisbii_client,
"create_subscription_session",
lambda plan, cust, ok, ko, no_trial=False: "https://pay.test/cs_1",
lambda plan,
ok,
ko,
no_trial=False,
customer_handle=None,
create_customer=None: "https://pay.test/cs_1",
)
resp = client.post("/subscriptions/subscribe", data={"plan": "simple"})
assert resp.status_code == 303
@@ -39,11 +42,11 @@ def test_subscribe_disables_trial_after_first_use(logged_in_client, monkeypatch)
"decpinfo-%d" % uid, "sub_42", "expired", "2020-01-01T00:00:00+00:00"
)
captured = {}
monkeypatch.setattr(
frisbii_client, "get_or_create_customer", lambda h, e: {"handle": h}
)
monkeypatch.setattr(frisbii_client, "update_customer", lambda h, d: {})
def fake_session(plan, cust, ok, ko, no_trial=False):
def fake_session(
plan, ok, ko, no_trial=False, customer_handle=None, create_customer=None
):
captured["no_trial"] = no_trial
return "https://pay.test/cs_9"
@@ -62,10 +65,10 @@ def test_subscribe_unknown_plan(logged_in_client):
def test_subscribe_api_error_redirects_with_error(logged_in_client, monkeypatch):
client, _ = logged_in_client
def boom(h, e):
def boom(h, d):
raise frisbii_client.FrisbiiError(500, "boom")
monkeypatch.setattr(frisbii_client, "get_or_create_customer", boom)
monkeypatch.setattr(frisbii_client, "update_customer", boom)
resp = client.post("/subscriptions/subscribe", data={"plan": "simple"})
assert resp.status_code == 302
assert "error=frisbii" in resp.headers["Location"]
@@ -139,7 +142,7 @@ def test_subscribe_skips_if_already_active(logged_in_client, monkeypatch):
)
called = []
monkeypatch.setattr(
frisbii_client, "get_or_create_customer", lambda h, e: called.append(h)
frisbii_client, "update_customer", lambda h, d: called.append(h)
)
resp = client.post("/subscriptions/subscribe", data={"plan": "simple"})
# Doit rediriger vers la page abonnement, sans appeler Frisbii.
Binary file not shown.