Nombre de jours d'essai en dur plutot que via appels HTTP
This commit is contained in:
@@ -1,11 +1,4 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
|
||||||
|
|
||||||
from src.subscriptions import client
|
|
||||||
from src.utils import logger
|
|
||||||
|
|
||||||
_TTL_SECONDS = 3600
|
|
||||||
_trial_cache: dict[str, tuple[float, int | None]] = {}
|
|
||||||
|
|
||||||
|
|
||||||
def _handle(env_name: str) -> str:
|
def _handle(env_name: str) -> str:
|
||||||
@@ -18,12 +11,14 @@ PLANS = {
|
|||||||
"label": "Abonnement",
|
"label": "Abonnement",
|
||||||
"prix_ht": 20,
|
"prix_ht": 20,
|
||||||
"description": "Accès aux fonctionnalités supplémentaires de colibre.",
|
"description": "Accès aux fonctionnalités supplémentaires de colibre.",
|
||||||
|
"trial_days": 2,
|
||||||
},
|
},
|
||||||
"soutien": {
|
"soutien": {
|
||||||
"env": "FRISBII_PLAN_SOUTIEN",
|
"env": "FRISBII_PLAN_SOUTIEN",
|
||||||
"label": "Abonnement de soutien ✊",
|
"label": "Abonnement de soutien ✊",
|
||||||
"prix_ht": 50,
|
"prix_ht": 50,
|
||||||
"description": "Mêmes fonctionnalités, contribution renforcée au projet.",
|
"description": "Mêmes fonctionnalités, contribution renforcée au projet.",
|
||||||
|
"trial_days": 2,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,30 +45,7 @@ def plan_meta(key: str) -> dict | None:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def _parse_trial_interval(plan: dict) -> int | None:
|
|
||||||
unit = plan.get("trial_interval_unit", "")
|
|
||||||
length = plan.get("trial_interval_length")
|
|
||||||
if unit == "days" and isinstance(length, int) and length > 0:
|
|
||||||
return length
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def trial_days(key: str) -> int | None:
|
def trial_days(key: str) -> int | None:
|
||||||
handle = resolve_handle(key)
|
if not resolve_handle(key):
|
||||||
if not handle:
|
|
||||||
return None
|
return None
|
||||||
now = time.monotonic()
|
return PLANS[key]["trial_days"]
|
||||||
cached = _trial_cache.get(handle)
|
|
||||||
if cached and now - cached[0] < _TTL_SECONDS:
|
|
||||||
return cached[1]
|
|
||||||
try:
|
|
||||||
plan = client.get_plan(handle)
|
|
||||||
except client.FrisbiiError:
|
|
||||||
logger.warning("Impossible de lire le plan Frisbii %s", handle)
|
|
||||||
return cached[1] if cached else None
|
|
||||||
# L'API Frisbii/Reepay renvoie une liste de versions ; on prend la dernière (la plus récente).
|
|
||||||
if isinstance(plan, list):
|
|
||||||
plan = plan[-1] if plan else {}
|
|
||||||
days = _parse_trial_interval(plan)
|
|
||||||
_trial_cache[handle] = (now, days)
|
|
||||||
return days
|
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from src.subscriptions import client, plans
|
from src.subscriptions import plans
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def _env(monkeypatch):
|
def _env(monkeypatch):
|
||||||
monkeypatch.setenv("FRISBII_PLAN_SIMPLE", "plan_simple")
|
monkeypatch.setenv("FRISBII_PLAN_SIMPLE", "plan_simple")
|
||||||
monkeypatch.setenv("FRISBII_PLAN_SOUTIEN", "plan_soutien")
|
monkeypatch.setenv("FRISBII_PLAN_SOUTIEN", "plan_soutien")
|
||||||
plans._trial_cache.clear()
|
|
||||||
|
|
||||||
|
|
||||||
def test_resolve_handle(monkeypatch):
|
def test_resolve_handle(monkeypatch):
|
||||||
@@ -15,29 +14,13 @@ def test_resolve_handle(monkeypatch):
|
|||||||
assert plans.resolve_handle("inconnu") is None
|
assert plans.resolve_handle("inconnu") is None
|
||||||
|
|
||||||
|
|
||||||
def test_trial_days_parses_and_caches(monkeypatch):
|
def test_trial_days_returns_configured_value():
|
||||||
calls = []
|
|
||||||
|
|
||||||
def fake_get_plan(handle):
|
|
||||||
calls.append(handle)
|
|
||||||
return {"trial_interval_unit": "days", "trial_interval_length": 2}
|
|
||||||
|
|
||||||
monkeypatch.setattr(client, "get_plan", fake_get_plan)
|
|
||||||
assert plans.trial_days("simple") == 2
|
assert plans.trial_days("simple") == 2
|
||||||
assert plans.trial_days("simple") == 2 # depuis le cache
|
assert plans.trial_days("soutien") == 2
|
||||||
assert calls == ["plan_simple"] # un seul appel API
|
|
||||||
|
|
||||||
|
|
||||||
def test_trial_days_none_on_error(monkeypatch):
|
def test_trial_days_none_when_plan_handle_unset(monkeypatch):
|
||||||
def fake_get_plan(handle):
|
monkeypatch.delenv("FRISBII_PLAN_SIMPLE", raising=False)
|
||||||
raise client.FrisbiiError(500, "boom")
|
|
||||||
|
|
||||||
monkeypatch.setattr(client, "get_plan", fake_get_plan)
|
|
||||||
assert plans.trial_days("simple") is None
|
|
||||||
|
|
||||||
|
|
||||||
def test_trial_days_none_when_no_trial(monkeypatch):
|
|
||||||
monkeypatch.setattr(client, "get_plan", lambda h: {})
|
|
||||||
assert plans.trial_days("simple") is None
|
assert plans.trial_days("simple") is None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user