From ab2e49e8fb72759095a542fb74edce0df505079f Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Mon, 28 Oct 2024 21:09:46 +0100 Subject: [PATCH] Add tests for registration ability outer registration dates Signed-off-by: Emmy D'Anello --- registration/forms.py | 12 +++++----- registration/models.py | 4 ++-- registration/tests.py | 54 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 61 insertions(+), 9 deletions(-) diff --git a/registration/forms.py b/registration/forms.py index c43e719..4f16d42 100644 --- a/registration/forms.py +++ b/registration/forms.py @@ -1,6 +1,5 @@ # Copyright (C) 2020 by Animath # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import datetime from django import forms from django.conf import settings @@ -8,6 +7,7 @@ from django.contrib.auth.forms import UserCreationForm from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.forms import FileInput +from django.utils import timezone from django.utils.text import format_lazy from django.utils.translation import gettext_lazy as _ @@ -40,15 +40,15 @@ class SignupForm(UserCreationForm): def clean(self): # Check that registrations are opened - now = datetime.now() + now = timezone.now() if now < settings.REGISTRATION_DATES['open']: self.add_error(None, format_lazy(_("Registrations are not opened yet. " - "They will open on the {opening_date:%Y-%m-%d %H:%M}."), - opening_date=settings.REGISTRATION_DATES['open'])) + "They will open on the {opening_date:%Y-%m-%d %H:%M}."), + opening_date=settings.REGISTRATION_DATES['open'])) elif now > settings.REGISTRATION_DATES['close']: self.add_error(None, format_lazy(_("Registrations for this year are closed since " - "{opening_date:%Y-%m-%d %H:%M}."), - opening_date=settings.REGISTRATION_DATES['close'])) + "{closing_date:%Y-%m-%d %H:%M}."), + closing_date=settings.REGISTRATION_DATES['close'])) return super().clean() def __init__(self, *args, **kwargs): diff --git a/registration/models.py b/registration/models.py index d16e197..e68bab6 100644 --- a/registration/models.py +++ b/registration/models.py @@ -1,7 +1,7 @@ # Copyright (C) 2020 by Animath # SPDX-License-Identifier: GPL-3.0-or-later -from datetime import date, datetime +from datetime import date from django.conf import settings from django.contrib.sites.models import Site @@ -774,7 +774,7 @@ class Payment(models.Model): return checkout_intent tournament = self.tournament - year = datetime.now().year + year = timezone.now().year base_site = "https://" + Site.objects.first().domain checkout_intent = helloasso.create_checkout_intent( amount=100 * self.amount, diff --git a/registration/tests.py b/registration/tests.py index 289459b..e1ba990 100644 --- a/registration/tests.py +++ b/registration/tests.py @@ -1,14 +1,17 @@ # Copyright (C) 2020 by Animath # SPDX-License-Identifier: GPL-3.0-or-later +from datetime import timedelta import os +from django.conf import settings from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site from django.core.files.uploadedfile import SimpleUploadedFile -from django.test import TestCase +from django.test import override_settings, TestCase from django.urls import reverse +from django.utils import timezone from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode from participation.models import Team @@ -114,6 +117,9 @@ class TestRegistration(TestCase): self.assertRedirects(response, "http://" + Site.objects.get().domain + str(self.coach.registration.get_absolute_url()), 302, 200) + # Ensure that we are between registration dates + @override_settings(REGISTRATION_DATES={'open': timezone.now() - timedelta(days=1), + 'close': timezone.now() + timedelta(days=1)}) def test_registration(self): """ Ensure that the signup form is working successfully. @@ -223,6 +229,52 @@ class TestRegistration(TestCase): response = self.client.get(reverse("registration:email_validation_resend", args=(user.pk,))) self.assertRedirects(response, reverse("registration:email_validation_sent"), 302, 200) + def test_registration_dates(self): + """ + Test that registrations are working only between registration dates. + """ + self.client.logout() + + # Test that registration between open and close dates are working + with override_settings(REGISTRATION_DATES={'open': timezone.now() - timedelta(days=2), + 'close': timezone.now() + timedelta(days=2)}): + response = self.client.get(reverse("registration:signup")) + self.assertEqual(response.status_code, 200) + self.assertIn(" Register", response.content.decode()) + self.assertNotIn("registrations are not opened", response.content.decode()) + self.assertNotIn("Registrations are closed", response.content.decode()) + + response = self.client.post(reverse("registration:signup")) + self.assertFormError(response.context['form'], None, []) + + # Test that registration before open date is not working + with override_settings(REGISTRATION_DATES={'open': timezone.now() + timedelta(days=1), + 'close': timezone.now() + timedelta(days=2)}): + response = self.client.get(reverse("registration:signup")) + self.assertEqual(response.status_code, 200) + self.assertNotIn(" Register", response.content.decode()) + self.assertIn("registrations are not opened", response.content.decode()) + + response = self.client.post(reverse("registration:signup")) + self.assertEqual(response.status_code, 200) + self.assertFormError(response.context['form'], None, + "Registrations are not opened yet. They will open on the " + f"{settings.REGISTRATION_DATES['open']:%Y-%m-%d %H:%M}.") + + # Test that registration after close date is not working + with override_settings(REGISTRATION_DATES={'open': timezone.now() - timedelta(days=2), + 'close': timezone.now() - timedelta(days=1)}): + response = self.client.get(reverse("registration:signup")) + self.assertEqual(response.status_code, 200) + self.assertNotIn(" Register", response.content.decode()) + self.assertIn("Registrations are closed", response.content.decode()) + + response = self.client.post(reverse("registration:signup")) + self.assertEqual(response.status_code, 200) + self.assertFormError(response.context['form'], None, + "Registrations for this year are closed since " + f"{settings.REGISTRATION_DATES['close']:%Y-%m-%d %H:%M}.") + def test_login(self): """ With a registered user, try to log in