From 8dcfc604a027127b7cf6a6737b0d880aad7d356b Mon Sep 17 00:00:00 2001 From: imperosol Date: Tue, 26 Nov 2024 17:16:57 +0100 Subject: [PATCH] write tests --- subscription/tests/__init__.py | 0 .../{tests.py => tests/test_dates.py} | 2 + subscription/tests/test_new_susbcription.py | 151 ++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 subscription/tests/__init__.py rename subscription/{tests.py => tests/test_dates.py} (99%) create mode 100644 subscription/tests/test_new_susbcription.py diff --git a/subscription/tests/__init__.py b/subscription/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/subscription/tests.py b/subscription/tests/test_dates.py similarity index 99% rename from subscription/tests.py rename to subscription/tests/test_dates.py index d853c55a..181246b1 100644 --- a/subscription/tests.py +++ b/subscription/tests/test_dates.py @@ -12,6 +12,8 @@ # OR WITHIN THE LOCAL FILE "LICENSE" # # +"""Tests focused on the computing of subscription end, start and duration""" + from datetime import date import freezegun diff --git a/subscription/tests/test_new_susbcription.py b/subscription/tests/test_new_susbcription.py new file mode 100644 index 00000000..8ea51d68 --- /dev/null +++ b/subscription/tests/test_new_susbcription.py @@ -0,0 +1,151 @@ +"""Tests focused on testing subscription creation""" + +from datetime import timedelta +from typing import Callable + +import pytest +from dateutil.relativedelta import relativedelta +from django.test import Client +from django.urls import reverse +from django.utils.timezone import localdate +from model_bakery import baker +from pytest_django.asserts import assertRedirects +from pytest_django.fixtures import SettingsWrapper + +from core.baker_recipes import board_user, old_subscriber_user, subscriber_user +from core.models import User +from subscription.forms import SubscriptionExistingUserForm, SubscriptionNewUserForm + + +@pytest.mark.django_db +@pytest.mark.parametrize( + "user_factory", + [old_subscriber_user.make, lambda: baker.make(User)], +) +def test_form_existing_user_valid( + user_factory: Callable[[], User], settings: SettingsWrapper +): + """Test `SubscriptionExistingUserForm`""" + user = user_factory() + data = { + "member": user, + "subscription_type": "deux-semestres", + "location": settings.SITH_SUBSCRIPTION_LOCATIONS[0][0], + "payment_method": settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0][0], + } + form = SubscriptionExistingUserForm(data) + + assert form.is_valid() + form.save() + user.refresh_from_db() + assert user.is_subscribed + + +@pytest.mark.django_db +def test_form_existing_user_invalid(settings: SettingsWrapper): + """Test `SubscriptionExistingUserForm`, with users that shouldn't subscribe.""" + user = subscriber_user.make() + # make sure the current subscription will end in a long time + last_sub = user.subscriptions.order_by("subscription_end").last() + last_sub.subscription_end = localdate() + timedelta(weeks=50) + last_sub.save() + data = { + "member": user, + "subscription_type": "deux-semestres", + "location": settings.SITH_SUBSCRIPTION_LOCATIONS[0][0], + "payment_method": settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0][0], + } + form = SubscriptionExistingUserForm(data) + + assert not form.is_valid() + with pytest.raises(ValueError): + form.save() + + +@pytest.mark.django_db +def test_form_new_user(settings: SettingsWrapper): + data = { + "first_name": "John", + "last_name": "Doe", + "email": "jdoe@utbm.fr", + "date_of_birth": localdate() - relativedelta(years=18), + "subscription_type": "deux-semestres", + "location": settings.SITH_SUBSCRIPTION_LOCATIONS[0][0], + "payment_method": settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0][0], + } + form = SubscriptionNewUserForm(data) + assert form.is_valid() + form.save() + user = User.objects.get(email="jdoe@utbm.fr") + assert user.username == "jdoe" + assert user.is_subscribed + + # if trying to instantiate a new form with the same email, + # it should fail + form = SubscriptionNewUserForm(data) + assert not form.is_valid() + with pytest.raises(ValueError): + form.save() + + +@pytest.mark.django_db +@pytest.mark.parametrize( + "user_factory", [lambda: baker.make(User, is_superuser=True), board_user.make] +) +def test_load_page(client: Client, user_factory: Callable[[], User]): + """Just check the page doesn't crash.""" + client.force_login(user_factory()) + res = client.get(reverse("subscription:subscription")) + assert res.status_code == 200 + + +@pytest.mark.django_db +def test_submit_form_existing_user(client: Client, settings: SettingsWrapper): + client.force_login(board_user.make()) + user = old_subscriber_user.make() + response = client.post( + reverse("subscription:fragment-existing-user"), + { + "member": user.id, + "subscription_type": "deux-semestres", + "location": settings.SITH_SUBSCRIPTION_LOCATIONS[0][0], + "payment_method": settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0][0], + }, + ) + user.refresh_from_db() + assert user.is_subscribed + current_subscription = user.subscriptions.order_by("-subscription_start").first() + assertRedirects( + response, + reverse( + "subscription:creation-success", + kwargs={"subscription_id": current_subscription.id}, + ), + ) + + +@pytest.mark.django_db +def test_submit_form_new_user(client: Client, settings: SettingsWrapper): + client.force_login(board_user.make()) + response = client.post( + reverse("subscription:fragment-new-user"), + { + "first_name": "John", + "last_name": "Doe", + "email": "jdoe@utbm.fr", + "date_of_birth": localdate() - relativedelta(years=18), + "subscription_type": "deux-semestres", + "location": settings.SITH_SUBSCRIPTION_LOCATIONS[0][0], + "payment_method": settings.SITH_SUBSCRIPTION_PAYMENT_METHOD[0][0], + }, + ) + user = User.objects.get(email="jdoe@utbm.fr") + assert user.is_subscribed + current_subscription = user.subscriptions.order_by("-subscription_start").first() + assertRedirects( + response, + reverse( + "subscription:creation-success", + kwargs={"subscription_id": current_subscription.id}, + ), + )