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é). # unitaires de pages (pas besoin du serveur complet — juste que CONFIG soit peuplé).
from dash import Dash as _Dash 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: class FakeResponse:
+18 -14
View File
@@ -5,29 +5,27 @@ from src.subscriptions import client
def test_auth_and_base_url_used(fake_httpx): def test_auth_and_base_url_used(fake_httpx):
fake_httpx["queue"].append(fake_httpx["Response"](200, {"handle": "decpinfo-1"})) 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] call = fake_httpx["calls"][0]
assert call["url"] == "https://api.test/v1/customer/decpinfo-1" assert call["url"] == "https://api.test/v1/customer/decpinfo-1"
assert call["auth"] == ("priv_test", "") 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"})) 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 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): def test_update_customer(fake_httpx):
fake_httpx["queue"].append(fake_httpx["Response"](404, {"error": "not found"}))
fake_httpx["queue"].append(fake_httpx["Response"](200, {"handle": "decpinfo-1"})) fake_httpx["queue"].append(fake_httpx["Response"](200, {"handle": "decpinfo-1"}))
result = client.get_or_create_customer("decpinfo-1", "a@b.fr") client.update_customer("decpinfo-1", {"email": "a@b.fr"})
assert result == {"handle": "decpinfo-1"} call = fake_httpx["calls"][0]
assert fake_httpx["calls"][1]["method"] == "POST" assert call["method"] == "PUT"
assert fake_httpx["calls"][1]["json"] == {"handle": "decpinfo-1", "email": "a@b.fr"} 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["queue"].append(
fake_httpx["Response"]( fake_httpx["Response"](
200, 200,
@@ -39,7 +37,10 @@ def test_create_subscription_session_returns_url(fake_httpx):
) )
) )
url = client.create_subscription_session( 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" assert url == "https://checkout.reepay.com/#/sub-1"
body = fake_httpx["calls"][0]["json"] 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["customer"] == "decpinfo-1"
assert body["signup_method"] == "link" assert body["signup_method"] == "link"
assert "prepare_subscription" not in body assert "prepare_subscription" not in body
assert "accept_url" not in body
def test_create_subscription_session_no_trial(fake_httpx): 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( 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 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", "current_period_end": "2099-01-01T00:00:00+00:00",
} }
view = compte_abonnement._active_view(row) 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): 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): def test_subscribe_redirects_to_hosted_url(logged_in_client, monkeypatch):
client, uid = logged_in_client client, uid = logged_in_client
monkeypatch.setattr( monkeypatch.setattr(frisbii_client, "update_customer", lambda h, d: {})
frisbii_client, "get_or_create_customer", lambda h, e: {"handle": h}
)
monkeypatch.setattr( monkeypatch.setattr(
frisbii_client, frisbii_client,
"create_subscription_session", "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"}) resp = client.post("/subscriptions/subscribe", data={"plan": "simple"})
assert resp.status_code == 303 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" "decpinfo-%d" % uid, "sub_42", "expired", "2020-01-01T00:00:00+00:00"
) )
captured = {} captured = {}
monkeypatch.setattr( monkeypatch.setattr(frisbii_client, "update_customer", lambda h, d: {})
frisbii_client, "get_or_create_customer", lambda h, e: {"handle": h}
)
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 captured["no_trial"] = no_trial
return "https://pay.test/cs_9" 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): def test_subscribe_api_error_redirects_with_error(logged_in_client, monkeypatch):
client, _ = logged_in_client client, _ = logged_in_client
def boom(h, e): def boom(h, d):
raise frisbii_client.FrisbiiError(500, "boom") 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"}) resp = client.post("/subscriptions/subscribe", data={"plan": "simple"})
assert resp.status_code == 302 assert resp.status_code == 302
assert "error=frisbii" in resp.headers["Location"] assert "error=frisbii" in resp.headers["Location"]
@@ -139,7 +142,7 @@ def test_subscribe_skips_if_already_active(logged_in_client, monkeypatch):
) )
called = [] called = []
monkeypatch.setattr( 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"}) resp = client.post("/subscriptions/subscribe", data={"plan": "simple"})
# Doit rediriger vers la page abonnement, sans appeler Frisbii. # Doit rediriger vers la page abonnement, sans appeler Frisbii.
Binary file not shown.