From e9e51d34d353e473b30b330a2a6ba7df1225f877 Mon Sep 17 00:00:00 2001 From: Skia Date: Mon, 16 Apr 2018 14:46:13 +0200 Subject: [PATCH] subscription: fix compute_start --- subscription/models.py | 6 ++++-- subscription/tests.py | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/subscription/models.py b/subscription/models.py index 9a820a6a..4d8381cc 100644 --- a/subscription/models.py +++ b/subscription/models.py @@ -151,7 +151,9 @@ class Subscription(models.Model): if not d: d = date.today() if user is not None and user.subscriptions.exists(): - d = user.subscriptions.last().subscription_end + last = user.subscriptions.last() + if last.is_valid_now(): + d = last.subscription_end if duration <= 2: # Sliding subscriptions for 1 or 2 semesters return d return get_start_of_semester(d) @@ -174,7 +176,7 @@ class Subscription(models.Model): try: return start.replace(month=int(round((start.month - 1 + 6 * duration) % 12 + 1, 0)), year=int(round(start.year + int(duration / 2) + (1 if int(start.month + 6 * duration) > 12 and (duration % 2 == 1 or duration < 1) else 0), 0))) - except ValueError as e: + except ValueError as e: # This catches 29th of February errors return start.replace(day=1, month=int(round((start.month + 6 * duration) % 12 + 1, 0)), year=int(round(start.year + int(duration / 2) + (1 if int(start.month + 6 * duration) > 12 and (duration % 2 == 1 or duration < 1) else 0), 0))) diff --git a/subscription/tests.py b/subscription/tests.py index f7e99993..5ad4c8c0 100644 --- a/subscription/tests.py +++ b/subscription/tests.py @@ -140,15 +140,46 @@ class SubscriptionIntegrationTest(TestCase): @mock.patch('subscription.models.date', FakeDate) def test_dates_sliding_with_subscribed_user(self): user = User.objects.filter(pk=self.user.pk).first() - s = Subscription(member=user, subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3], + s = Subscription(member=user, subscription_type='deux-semestres', payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0]) s.subscription_start = date(2015, 8, 29) - s.subscription_end = s.compute_end(duration=2, + s.subscription_end = s.compute_end(duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]['duration'], start=s.subscription_start) s.save() self.assertTrue(s.subscription_end == date(2016, 8, 29)) date_mock_today(2016, 8, 25) - d = Subscription.compute_end(duration=2, + d = Subscription.compute_end(duration=settings.SITH_SUBSCRIPTIONS['deux-semestres']['duration'], user=user) self.assertTrue(d == date(2017, 8, 29)) + @mock.patch('subscription.models.date', FakeDate) + def test_dates_renewal_sliding_during_two_free_monthes(self): + user = User.objects.filter(pk=self.user.pk).first() + s = Subscription(member=user, subscription_type='deux-mois-essai', + payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0]) + s.subscription_start = date(2015, 8, 29) + s.subscription_end = s.compute_end(duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]['duration'], + start=s.subscription_start) + s.save() + self.assertTrue(s.subscription_end == date(2015, 10, 29)) + date_mock_today(2015, 9, 25) + d = Subscription.compute_end(duration=settings.SITH_SUBSCRIPTIONS['deux-semestres']['duration'], + user=user) + self.assertTrue(d == date(2016, 10, 29)) + + @mock.patch('subscription.models.date', FakeDate) + def test_dates_renewal_sliding_after_two_free_monthes(self): + user = User.objects.filter(pk=self.user.pk).first() + s = Subscription(member=user, subscription_type='deux-mois-essai', + payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0]) + s.subscription_start = date(2015, 8, 29) + s.subscription_end = s.compute_end(duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]['duration'], + start=s.subscription_start) + s.save() + self.assertTrue(s.subscription_end == date(2015, 10, 29)) + date_mock_today(2015, 11, 5) + d = Subscription.compute_end(duration=settings.SITH_SUBSCRIPTIONS['deux-semestres']['duration'], + user=user) + print(d) + self.assertTrue(d == date(2016, 11, 5)) +