diff --git a/app/models.py b/app/models.py index 6146bbc3..d0bb193a 100644 --- a/app/models.py +++ b/app/models.py @@ -57,6 +57,8 @@ from app.utils import ( Base = declarative_base() +PADDLE_SUBSCRIPTION_GRACE_DAYS = 14 + class TSVector(sa.types.TypeDecorator): impl = TSVECTOR @@ -751,8 +753,12 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle): sub = Subscription.get_by(user_id=self.id) if sub: - # sub is active until the next billing_date + 1 - if sub.next_bill_date >= arrow.now().shift(days=-1).date(): + # grace period is 14 days + # sub is active until the next billing_date + PADDLE_SUBSCRIPTION_GRACE_DAYS + if ( + sub.next_bill_date + >= arrow.now().shift(days=-PADDLE_SUBSCRIPTION_GRACE_DAYS).date() + ): return sub # past subscription, user is considered not having a subscription = free plan else: @@ -1944,7 +1950,6 @@ class AppleSubscription(Base, ModelMixin): user = orm.relationship(User) def is_valid(self): - # Todo: take into account grace period? return self.expires_date > arrow.now().shift(days=-_APPLE_GRACE_PERIOD_DAYS) diff --git a/tests/test_models.py b/tests/test_models.py index c9f1b699..cc3c6b9f 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,5 +1,7 @@ +import random from uuid import UUID +import arrow import pytest from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN @@ -12,6 +14,9 @@ from app.models import ( Mailbox, SenderFormatEnum, EnumE, + Subscription, + PlanEnum, + PADDLE_SUBSCRIPTION_GRACE_DAYS, ) from tests.utils import login, create_new_user @@ -258,3 +263,24 @@ def test_can_create_new_alias_disabled_user(): user.disabled = True assert not user.can_create_new_alias() + + +def test_user_get_subscription_grace_period(flask_client): + user = create_new_user() + sub = Subscription.create( + user_id=user.id, + cancel_url="https://checkout.paddle.com/subscription/cancel?user=1234", + update_url="https://checkout.paddle.com/subscription/update?user=1234", + subscription_id=str(random.random()), + event_time=arrow.now(), + next_bill_date=arrow.now().shift(days=-PADDLE_SUBSCRIPTION_GRACE_DAYS).date(), + plan=PlanEnum.monthly, + commit=True, + ) + + assert user.get_subscription() is not None + + sub.next_bill_date = ( + arrow.now().shift(days=-(PADDLE_SUBSCRIPTION_GRACE_DAYS + 1)).date() + ) + assert user.get_subscription() is None