2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# Copyright 2023 © AE UTBM
|
|
|
|
# ae@utbm.fr / ae.info@utbm.fr
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# This file is part of the website of the UTBM Student Association (AE UTBM),
|
|
|
|
# https://ae.utbm.fr.
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2024-09-22 23:37:25 +00:00
|
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
2023-04-04 16:39:45 +00:00
|
|
|
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
|
2024-09-23 08:25:27 +00:00
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
|
2023-04-04 16:39:45 +00:00
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
2017-04-24 15:51:12 +00:00
|
|
|
#
|
|
|
|
#
|
2024-11-26 16:16:57 +00:00
|
|
|
"""Tests focused on the computing of subscription end, start and duration"""
|
|
|
|
|
2017-08-29 13:06:52 +00:00
|
|
|
from datetime import date
|
2017-04-24 15:51:12 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
import freezegun
|
|
|
|
import pytest
|
2024-06-24 11:07:36 +00:00
|
|
|
from django.conf import settings
|
2015-12-15 16:50:50 +00:00
|
|
|
from django.test import TestCase
|
2024-06-24 11:07:36 +00:00
|
|
|
|
2017-08-29 16:26:27 +00:00
|
|
|
from core.models import User
|
2024-06-24 11:07:36 +00:00
|
|
|
from subscription.models import Subscription
|
2017-08-29 16:26:27 +00:00
|
|
|
|
2017-08-29 13:06:52 +00:00
|
|
|
|
2024-06-26 17:10:24 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("today", "duration", "expected_start"),
|
|
|
|
[
|
|
|
|
(date(2020, 9, 18), 1, date(2020, 9, 18)),
|
|
|
|
(date(2020, 9, 18), 2, date(2020, 9, 18)),
|
|
|
|
(date(2020, 5, 17), 3, date(2020, 2, 15)),
|
|
|
|
(date(2021, 1, 18), 4, date(2020, 8, 15)),
|
|
|
|
(date(2020, 9, 18), 4, date(2020, 8, 15)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_subscription_compute_start_from_today(today, duration, expected_start):
|
|
|
|
with freezegun.freeze_time(today):
|
|
|
|
assert Subscription.compute_start(duration=duration) == expected_start
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("start_date", "duration", "expected_start"),
|
|
|
|
[
|
|
|
|
(date(2020, 5, 17), 1, date(2020, 5, 17)),
|
|
|
|
(date(2020, 5, 17), 2, date(2020, 5, 17)),
|
|
|
|
(date(2020, 5, 17), 3, date(2020, 2, 15)),
|
|
|
|
(date(2020, 1, 11), 3, date(2019, 8, 15)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_subscription_compute_start_explicit(start_date, duration, expected_start):
|
|
|
|
assert Subscription.compute_start(start_date, duration=duration) == expected_start
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("today", "duration", "expected_end"),
|
|
|
|
[
|
|
|
|
(date(2020, 9, 18), 1, date(2021, 3, 18)),
|
|
|
|
(date(2020, 9, 18), 2, date(2021, 9, 18)),
|
|
|
|
(date(2020, 9, 18), 3, date(2022, 2, 15)),
|
2024-07-18 18:23:30 +00:00
|
|
|
(date(2020, 5, 17), 4, date(2022, 2, 15)),
|
2024-06-26 17:10:24 +00:00
|
|
|
(date(2020, 9, 18), 0.33, date(2020, 11, 18)),
|
|
|
|
(date(2020, 9, 18), 0.67, date(2021, 1, 19)),
|
|
|
|
(date(2020, 9, 18), 0.5, date(2020, 12, 18)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_subscription_compute_end_from_today(today, duration, expected_end):
|
|
|
|
with freezegun.freeze_time(today):
|
|
|
|
assert Subscription.compute_end(duration=duration) == expected_end
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
("start_date", "duration", "expected_end"),
|
|
|
|
[
|
|
|
|
(date(2020, 9, 18), 3, date(2022, 3, 18)),
|
|
|
|
(date(2020, 9, 18), 4, date(2022, 9, 18)),
|
|
|
|
],
|
|
|
|
)
|
2024-07-18 18:23:30 +00:00
|
|
|
def test_subscription_compute_end(start_date, duration, expected_end):
|
2024-06-26 17:10:24 +00:00
|
|
|
assert Subscription.compute_end(duration, start_date) == expected_end
|
2015-12-15 16:50:50 +00:00
|
|
|
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2024-07-23 22:39:26 +00:00
|
|
|
class TestSubscriptionIntegration(TestCase):
|
2023-05-02 09:00:23 +00:00
|
|
|
@classmethod
|
2024-06-26 17:10:24 +00:00
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.user = User.objects.get(username="public")
|
2017-08-29 16:26:27 +00:00
|
|
|
|
2020-08-28 19:19:27 +00:00
|
|
|
def test_duration_one_month(self):
|
|
|
|
s = Subscription(
|
2024-06-26 17:10:24 +00:00
|
|
|
member=self.user,
|
2020-08-28 19:19:27 +00:00
|
|
|
subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
|
|
|
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
|
|
|
)
|
|
|
|
s.subscription_start = date(2017, 8, 29)
|
|
|
|
s.subscription_end = s.compute_end(duration=0.166, start=s.subscription_start)
|
|
|
|
s.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert s.subscription_end == date(2017, 9, 29)
|
2020-08-28 19:19:27 +00:00
|
|
|
|
2017-08-29 16:26:27 +00:00
|
|
|
def test_duration_two_months(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
s = Subscription(
|
2024-06-26 17:10:24 +00:00
|
|
|
member=self.user,
|
2018-10-04 19:29:19 +00:00
|
|
|
subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
|
|
|
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
|
|
|
)
|
2017-08-29 16:26:27 +00:00
|
|
|
s.subscription_start = date(2017, 8, 29)
|
2020-08-28 19:19:27 +00:00
|
|
|
s.subscription_end = s.compute_end(duration=0.333, start=s.subscription_start)
|
2017-08-29 16:26:27 +00:00
|
|
|
s.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert s.subscription_end == date(2017, 10, 29)
|
2017-08-29 16:26:27 +00:00
|
|
|
|
2020-08-28 19:19:27 +00:00
|
|
|
def test_duration_one_day(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
s = Subscription(
|
2024-06-26 17:10:24 +00:00
|
|
|
member=self.user,
|
2018-10-04 19:29:19 +00:00
|
|
|
subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
|
|
|
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
|
|
|
)
|
2018-09-20 12:30:35 +00:00
|
|
|
s.subscription_start = date(2017, 8, 29)
|
2018-10-04 19:29:19 +00:00
|
|
|
s.subscription_end = s.compute_end(
|
|
|
|
duration=settings.SITH_SUBSCRIPTIONS["un-jour"]["duration"],
|
|
|
|
start=s.subscription_start,
|
|
|
|
)
|
2018-09-20 12:30:35 +00:00
|
|
|
s.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert s.subscription_end == date(2017, 8, 30)
|
2018-09-20 12:30:35 +00:00
|
|
|
|
2017-08-29 16:26:27 +00:00
|
|
|
def test_duration_three_months(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
s = Subscription(
|
2024-06-26 17:10:24 +00:00
|
|
|
member=self.user,
|
2018-10-04 19:29:19 +00:00
|
|
|
subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
|
|
|
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
|
|
|
)
|
2017-08-29 16:26:27 +00:00
|
|
|
s.subscription_start = date(2017, 8, 29)
|
2018-10-04 19:29:19 +00:00
|
|
|
s.subscription_end = s.compute_end(duration=0.5, start=s.subscription_start)
|
2017-08-29 16:26:27 +00:00
|
|
|
s.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert s.subscription_end == date(2017, 11, 29)
|
2017-08-29 16:26:27 +00:00
|
|
|
|
|
|
|
def test_duration_four_months(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
s = Subscription(
|
2024-06-26 17:10:24 +00:00
|
|
|
member=self.user,
|
2018-10-04 19:29:19 +00:00
|
|
|
subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
|
|
|
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
|
|
|
)
|
2017-08-29 16:26:27 +00:00
|
|
|
s.subscription_start = date(2017, 8, 29)
|
2018-10-04 19:29:19 +00:00
|
|
|
s.subscription_end = s.compute_end(duration=0.67, start=s.subscription_start)
|
2017-08-29 16:26:27 +00:00
|
|
|
s.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert s.subscription_end == date(2017, 12, 30)
|
2018-10-04 19:29:19 +00:00
|
|
|
|
2018-09-01 15:45:13 +00:00
|
|
|
def test_duration_six_weeks(self):
|
2018-10-04 19:29:19 +00:00
|
|
|
s = Subscription(
|
2024-06-26 17:10:24 +00:00
|
|
|
member=self.user,
|
2018-10-04 19:29:19 +00:00
|
|
|
subscription_type=list(settings.SITH_SUBSCRIPTIONS.keys())[3],
|
|
|
|
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
|
|
|
)
|
2018-09-01 15:45:13 +00:00
|
|
|
s.subscription_start = date(2018, 9, 1)
|
2018-10-04 19:29:19 +00:00
|
|
|
s.subscription_end = s.compute_end(duration=0.23, start=s.subscription_start)
|
2018-09-01 15:45:13 +00:00
|
|
|
s.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert s.subscription_end == date(2018, 10, 13)
|
2017-09-04 09:25:28 +00:00
|
|
|
|
2017-09-07 09:21:32 +00:00
|
|
|
def test_dates_sliding_with_subscribed_user(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
user = self.user
|
2018-10-04 19:29:19 +00:00
|
|
|
s = Subscription(
|
|
|
|
member=user,
|
|
|
|
subscription_type="deux-semestres",
|
|
|
|
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
|
|
|
)
|
2017-09-07 09:21:32 +00:00
|
|
|
s.subscription_start = date(2015, 8, 29)
|
2018-10-04 19:29:19 +00:00
|
|
|
s.subscription_end = s.compute_end(
|
|
|
|
duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]["duration"],
|
|
|
|
start=s.subscription_start,
|
|
|
|
)
|
2017-09-07 09:21:32 +00:00
|
|
|
s.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert s.subscription_end == date(2016, 8, 29)
|
|
|
|
with freezegun.freeze_time("2016-08-25"):
|
|
|
|
d = Subscription.compute_end(
|
|
|
|
duration=settings.SITH_SUBSCRIPTIONS["deux-semestres"]["duration"],
|
|
|
|
user=user,
|
|
|
|
)
|
|
|
|
assert d == date(2017, 8, 29)
|
2017-09-07 09:21:32 +00:00
|
|
|
|
2018-04-16 12:46:13 +00:00
|
|
|
def test_dates_renewal_sliding_during_two_free_monthes(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
user = self.user
|
2018-10-04 19:29:19 +00:00
|
|
|
s = Subscription(
|
|
|
|
member=user,
|
|
|
|
subscription_type="deux-mois-essai",
|
|
|
|
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
|
|
|
)
|
2018-04-16 12:46:13 +00:00
|
|
|
s.subscription_start = date(2015, 8, 29)
|
2018-10-04 19:29:19 +00:00
|
|
|
s.subscription_end = s.compute_end(
|
|
|
|
duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]["duration"],
|
|
|
|
start=s.subscription_start,
|
|
|
|
)
|
2018-04-16 12:46:13 +00:00
|
|
|
s.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert s.subscription_end == date(2015, 10, 29)
|
|
|
|
with freezegun.freeze_time("2015-09-25"):
|
|
|
|
d = Subscription.compute_end(
|
|
|
|
duration=settings.SITH_SUBSCRIPTIONS["deux-semestres"]["duration"],
|
|
|
|
user=user,
|
|
|
|
)
|
|
|
|
assert d == date(2016, 10, 29)
|
2018-04-16 12:46:13 +00:00
|
|
|
|
|
|
|
def test_dates_renewal_sliding_after_two_free_monthes(self):
|
2024-06-26 17:10:24 +00:00
|
|
|
user = self.user
|
2018-10-04 19:29:19 +00:00
|
|
|
s = Subscription(
|
|
|
|
member=user,
|
|
|
|
subscription_type="deux-mois-essai",
|
|
|
|
payment_method=settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0],
|
|
|
|
)
|
2018-04-16 12:46:13 +00:00
|
|
|
s.subscription_start = date(2015, 8, 29)
|
2018-10-04 19:29:19 +00:00
|
|
|
s.subscription_end = s.compute_end(
|
|
|
|
duration=settings.SITH_SUBSCRIPTIONS[s.subscription_type]["duration"],
|
|
|
|
start=s.subscription_start,
|
|
|
|
)
|
2018-04-16 12:46:13 +00:00
|
|
|
s.save()
|
2024-06-26 17:10:24 +00:00
|
|
|
assert s.subscription_end == date(2015, 10, 29)
|
|
|
|
with freezegun.freeze_time("2015-11-05"):
|
|
|
|
d = Subscription.compute_end(
|
|
|
|
duration=settings.SITH_SUBSCRIPTIONS["deux-semestres"]["duration"],
|
|
|
|
user=user,
|
|
|
|
)
|
|
|
|
assert d == date(2016, 11, 5)
|