diff --git a/.template.env b/.template.env index 79624f9..9ced92a 100644 --- a/.template.env +++ b/.template.env @@ -63,3 +63,6 @@ FRISBII_API_BASE_URL=https://api.frisbii.com # base de l'API Frisbii FRISBII_PLAN_SIMPLE=abonnement-decp.info # handle du plan "abonnement simple" (20 € HT/mois) FRISBII_PLAN_SOUTIEN=soutien-decp.info # handle du plan "abonnement de soutien" (50 € HT/mois) FRISBII_WEBHOOK_SECRET= # secret de signature des webhooks + +# Accès gratuit temporaire +TOUS_ABONNES=false # true = ouvre gratuitement les fonctionnalités d'abonné à tout compte diff --git a/src/pages/_compte_shell.py b/src/pages/_compte_shell.py index dec2cf9..7769bf9 100644 --- a/src/pages/_compte_shell.py +++ b/src/pages/_compte_shell.py @@ -40,9 +40,12 @@ SECTIONS = [ def current_user_has_subscription() -> bool: from src.subscriptions import db + from src.utils import TOUS_ABONNES if not current_user.is_authenticated: return False + if TOUS_ABONNES: + return True return db.has_active_subscription(current_user.id) diff --git a/src/utils/__init__.py b/src/utils/__init__.py index 4a3a8b9..0a02872 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -31,6 +31,9 @@ logging.basicConfig( datefmt="%Y-%m-%d %H:%M:%S", ) DEVELOPMENT = os.getenv("DEVELOPMENT", "False").lower() == "true" +# Accès gratuit temporaire à toutes les fonctionnalités d'abonné, le temps que +# la plateforme de paiement Frisbii valide la réception de paiements. +TOUS_ABONNES = os.getenv("TOUS_ABONNES", "False").lower() == "true" logger = logging.getLogger("decp.info") if DEVELOPMENT: diff --git a/tests/test_compte_shell.py b/tests/test_compte_shell.py index bc8ce4b..95dd3a1 100644 --- a/tests/test_compte_shell.py +++ b/tests/test_compte_shell.py @@ -1,6 +1,15 @@ +from unittest.mock import patch + from src.pages import _compte_shell as shell +def _fake_user(authenticated: bool): + user = type("U", (), {})() + user.is_authenticated = authenticated + user.id = 1 + return user + + def test_visible_sections_hides_gated_without_subscription(): keys = {s["key"] for s in shell.visible_sections(has_subscription=False)} assert "admin" in keys @@ -41,3 +50,27 @@ def test_guard_redirect_allowed_returns_none(): path="/compte/admin", ) assert href is None + + +def test_has_subscription_true_for_authenticated_when_tous_abonnes(monkeypatch): + monkeypatch.setattr("src.utils.TOUS_ABONNES", True) + with patch("src.pages._compte_shell.current_user", _fake_user(True)): + assert shell.current_user_has_subscription() is True + + +def test_has_subscription_false_for_anonymous_even_with_tous_abonnes(monkeypatch): + monkeypatch.setattr("src.utils.TOUS_ABONNES", True) + with patch("src.pages._compte_shell.current_user", _fake_user(False)): + assert shell.current_user_has_subscription() is False + + +def test_has_subscription_uses_db_when_flag_off(monkeypatch): + monkeypatch.setattr("src.utils.TOUS_ABONNES", False) + with ( + patch("src.pages._compte_shell.current_user", _fake_user(True)), + patch( + "src.subscriptions.db.has_active_subscription", return_value=False + ) as mocked, + ): + assert shell.current_user_has_subscription() is False + mocked.assert_called_once_with(1)