From b7a88a387caf6d0cb24061bcc5353496174bf298 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 11 Aug 2020 01:03:29 +0200 Subject: [PATCH] More tests in WEI app, but we can still go further --- apps/note/templatetags/pretty_money.py | 25 +-- apps/permission/test.py | 2 - apps/wei/tests/test_wei_registration.py | 215 ++++++++++++++++++++++-- apps/wei/views.py | 23 +-- locale/de/LC_MESSAGES/django.po | 2 +- locale/fr/LC_MESSAGES/django.po | 2 +- 6 files changed, 232 insertions(+), 37 deletions(-) diff --git a/apps/note/templatetags/pretty_money.py b/apps/note/templatetags/pretty_money.py index 265870a8..c7050da8 100644 --- a/apps/note/templatetags/pretty_money.py +++ b/apps/note/templatetags/pretty_money.py @@ -5,17 +5,20 @@ from django import template def pretty_money(value): - if value % 100 == 0: - return "{:s}{:d} €".format( - "- " if value < 0 else "", - abs(value) // 100, - ) - else: - return "{:s}{:d}.{:02d} €".format( - "- " if value < 0 else "", - abs(value) // 100, - abs(value) % 100, - ) + try: + if value % 100 == 0: + return "{:s}{:d} €".format( + "- " if value < 0 else "", + abs(value) // 100, + ) + else: + return "{:s}{:d}.{:02d} €".format( + "- " if value < 0 else "", + abs(value) // 100, + abs(value) % 100, + ) + except (ValueError, TypeError): + return "0 €" register = template.Library() diff --git a/apps/permission/test.py b/apps/permission/test.py index e728e9a6..e8452e8a 100644 --- a/apps/permission/test.py +++ b/apps/permission/test.py @@ -82,5 +82,3 @@ class PermissionQueryTestCase(TestCase): if instanced.query: print("Compiled query:", instanced.query) raise - - print("All permission queries are well formed") diff --git a/apps/wei/tests/test_wei_registration.py b/apps/wei/tests/test_wei_registration.py index 814aafe9..2ff0bbf1 100644 --- a/apps/wei/tests/test_wei_registration.py +++ b/apps/wei/tests/test_wei_registration.py @@ -1,6 +1,8 @@ # Copyright (C) 2018-2020 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later + import subprocess +from datetime import timedelta from django.conf import settings from django.contrib.auth.models import User @@ -15,6 +17,27 @@ from ..forms import CurrentSurvey from ..models import WEIClub, Bus, BusTeam, WEIRole, WEIRegistration, WEIMembership +class TestWEIList(TestCase): + fixtures = ('initial',) + + def setUp(self): + self.user = User.objects.create_superuser( + username="weiadmin", + password="admin", + email="admin@example.com", + ) + self.client.force_login(self.user) + sess = self.client.session + sess["permission_mask"] = 42 + sess.save() + + def test_current_wei_detail(self): + """ + Test that when no WEI is created, the WEI button redirect to the WEI list + """ + response = self.client.get(reverse("wei:current_wei_detail")) + self.assertRedirects(response, reverse("wei:wei_list"), 302, 200) + class TestWEIRegistration(TestCase): """ Test the whole WEI app @@ -43,11 +66,11 @@ class TestWEIRegistration(TestCase): parent_club_id=2, membership_fee_paid=12500, membership_fee_unpaid=5500, - membership_start=str(self.year) + "-08-01", + membership_start=str(self.year) + "-01-01", membership_end=str(self.year) + "-12-31", year=self.year, - date_start=str(self.year) + "-09-01", - date_end=str(self.year) + "-09-03", + date_start=timezone.now().date() + timedelta(days=2), + date_end=str(self.year) + "-12-31", ) NoteClub.objects.create(club=self.wei) self.bus = Bus.objects.create( @@ -132,6 +155,12 @@ class TestWEIRegistration(TestCase): self.assertRedirects(response, reverse("wei:wei_detail", kwargs=dict(pk=self.wei.pk)), 302, 200) self.assertTrue(qs.exists()) + # Check that if the WEI is started, we can't update a wei + self.wei.date_start = '2000-01-01' + self.wei.save() + response = self.client.get(reverse("wei:wei_update", kwargs=dict(pk=self.wei.pk))) + self.assertRedirects(response, reverse("wei:wei_closed", kwargs=dict(pk=self.wei.pk)), 302, 200) + def test_wei_closed(self): """ Test display the page when a WEI is closed. @@ -150,6 +179,9 @@ class TestWEIRegistration(TestCase): """ Test create a new bus. """ + response = self.client.get(reverse("wei:add_bus", kwargs=dict(pk=self.wei.pk))) + self.assertEqual(response.status_code, 200) + response = self.client.post(reverse("wei:add_bus", kwargs=dict(pk=self.wei.pk)), dict( wei=self.wei.id, name="Create Bus Test", @@ -160,6 +192,12 @@ class TestWEIRegistration(TestCase): bus = qs.get() self.assertRedirects(response, reverse("wei:manage_bus", kwargs=dict(pk=bus.pk)), 302, 200) + # Check that if the WEI is started, we can't create a bus + self.wei.date_start = '2000-01-01' + self.wei.save() + response = self.client.get(reverse("wei:add_bus", kwargs=dict(pk=self.wei.pk))) + self.assertRedirects(response, reverse("wei:wei_closed", kwargs=dict(pk=self.wei.pk)), 302, 200) + def test_detail_bus(self): """ Test display the information about a bus. @@ -171,6 +209,9 @@ class TestWEIRegistration(TestCase): """ Test update a bus. """ + response = self.client.get(reverse("wei:update_bus", kwargs=dict(pk=self.bus.pk))) + self.assertEqual(response.status_code, 200) + response = self.client.post(reverse("wei:update_bus", kwargs=dict(pk=self.bus.pk)), dict( name="Update Bus Test", description="This bus was updated.", @@ -179,10 +220,19 @@ class TestWEIRegistration(TestCase): self.assertRedirects(response, reverse("wei:manage_bus", kwargs=dict(pk=self.bus.pk)), 302, 200) self.assertTrue(qs.exists()) + # Check that if the WEI is started, we can't update a bus + self.wei.date_start = '2000-01-01' + self.wei.save() + response = self.client.get(reverse("wei:update_bus", kwargs=dict(pk=self.bus.pk))) + self.assertRedirects(response, reverse("wei:wei_closed", kwargs=dict(pk=self.wei.pk)), 302, 200) + def test_add_team(self): """ Test create a new team. """ + response = self.client.get(reverse("wei:add_team", kwargs=dict(pk=self.bus.pk))) + self.assertEqual(response.status_code, 200) + response = self.client.post(reverse("wei:add_team", kwargs=dict(pk=self.bus.pk)), dict( bus=self.bus.id, name="Create Team Test", @@ -194,6 +244,12 @@ class TestWEIRegistration(TestCase): team = qs.get() self.assertRedirects(response, reverse("wei:manage_bus_team", kwargs=dict(pk=team.pk)), 302, 200) + # Check that if the WEI is started, we can't create a team + self.wei.date_start = '2000-01-01' + self.wei.save() + response = self.client.get(reverse("wei:add_team", kwargs=dict(pk=self.bus.pk))) + self.assertRedirects(response, reverse("wei:wei_closed", kwargs=dict(pk=self.wei.pk)), 302, 200) + def test_detail_team(self): """ Test display the detail about a team. @@ -205,6 +261,9 @@ class TestWEIRegistration(TestCase): """ Test update a team. """ + response = self.client.get(reverse("wei:update_bus_team", kwargs=dict(pk=self.team.pk))) + self.assertEqual(response.status_code, 200) + response = self.client.post(reverse("wei:update_bus_team", kwargs=dict(pk=self.team.pk)), dict( name="Update Team Test", color="#A6AA", @@ -214,11 +273,42 @@ class TestWEIRegistration(TestCase): self.assertRedirects(response, reverse("wei:manage_bus_team", kwargs=dict(pk=self.team.pk)), 302, 200) self.assertTrue(qs.exists()) + # Check that if the WEI is started, we can't update a team + self.wei.date_start = '2000-01-01' + self.wei.save() + response = self.client.get(reverse("wei:update_bus_team", kwargs=dict(pk=self.team.pk))) + self.assertRedirects(response, reverse("wei:wei_closed", kwargs=dict(pk=self.wei.pk)), 302, 200) + def test_register_2a(self): """ Test register a new 2A+ to the WEI. """ + response = self.client.get(reverse("wei:wei_register_2A", kwargs=dict(wei_pk=self.wei.pk))) + self.assertEqual(response.status_code, 200) + + response = self.client.get(reverse("wei:wei_register_2A_myself", kwargs=dict(wei_pk=self.wei.pk))) + self.assertEqual(response.status_code, 200) + user = User.objects.create(username="toto", email="toto@example.com") + + # Try with an invalid form + response = self.client.post(reverse("wei:wei_register_2A", kwargs=dict(wei_pk=self.wei.pk)), dict( + user=user.id, + soge_credit=True, + birth_date='2000-01-01', + gender='nonbinary', + clothing_cut='female', + clothing_size='XS', + health_issues='I am a bot', + emergency_contact_name='NoteKfet2020', + emergency_contact_phone='+33123456789', + bus=[], + team=[], + roles=[], + )) + self.assertEqual(response.status_code, 200) + self.assertFalse(response.context["form"].is_valid()) + response = self.client.post(reverse("wei:wei_register_2A", kwargs=dict(wei_pk=self.wei.pk)), dict( user=user.id, soge_credit=True, @@ -237,12 +327,42 @@ class TestWEIRegistration(TestCase): self.assertTrue(qs.exists()) self.assertRedirects(response, reverse("wei:wei_survey", kwargs=dict(pk=qs.get().pk)), 302, 302) + # Check that the user can't be registered twice + response = self.client.post(reverse("wei:wei_register_2A", kwargs=dict(wei_pk=self.wei.pk)), dict( + user=user.id, + soge_credit=True, + birth_date='2000-01-01', + gender='nonbinary', + clothing_cut='female', + clothing_size='XS', + health_issues='I am a bot', + emergency_contact_name='NoteKfet2020', + emergency_contact_phone='+33123456789', + bus=[self.bus.id], + team=[self.team.id], + roles=[role.id for role in WEIRole.objects.filter(~Q(name="1A")).all()], + )) + self.assertEqual(response.status_code, 200) + self.assertTrue("This user is already registered to this WEI." in str(response.context["form"].errors)) + + # Check that if the WEI is started, we can't register anyone + self.wei.date_start = '2000-01-01' + self.wei.save() + response = self.client.get(reverse("wei:wei_register_2A", kwargs=dict(wei_pk=self.wei.pk))) + self.assertRedirects(response, reverse("wei:wei_closed", kwargs=dict(pk=self.wei.pk)), 302, 200) + def test_register_1a(self): """ Test register a first year member to the WEI and complete the survey. """ + response = self.client.get(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=self.wei.pk))) + self.assertEqual(response.status_code, 200) + + response = self.client.get(reverse("wei:wei_register_1A_myself", kwargs=dict(wei_pk=self.wei.pk))) + self.assertEqual(response.status_code, 200) + user = User.objects.create(username="toto", email="toto@example.com") - response = self.client.post(reverse("wei:wei_register_1A_myself", kwargs=dict(wei_pk=self.wei.pk)), dict( + response = self.client.post(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=self.wei.pk)), dict( user=user.id, soge_credit=True, birth_date='2000-01-01', @@ -273,6 +393,57 @@ class TestWEIRegistration(TestCase): survey = CurrentSurvey(registration) self.assertTrue(survey.is_complete()) + # Check that the user can't be registered twice + response = self.client.post(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=self.wei.pk)), dict( + user=user.id, + soge_credit=True, + birth_date='2000-01-01', + gender='nonbinary', + clothing_cut='female', + clothing_size='XS', + health_issues='I am a bot', + emergency_contact_name='NoteKfet2020', + emergency_contact_phone='+33123456789', + ml_events_registration=True, + ml_sport_registration=False, + ml_art_registration=False, + )) + self.assertEqual(response.status_code, 200) + self.assertTrue("This user is already registered to this WEI." in str(response.context["form"].errors)) + + # Check that the user can't be registered twice as a first year member + second_wei = WEIClub.objects.create( + name="Second WEI", + year=self.year + 1, + date_start=str(self.year + 1) + "-01-01", + date_end=str(self.year + 1) + "-12-31", + membership_start=str(self.year) + "-01-01", + membership_end=str(self.year + 1) + "-12-31", + ) + response = self.client.post(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=second_wei.pk)), dict( + user=user.id, + soge_credit=True, + birth_date='2000-01-01', + gender='nonbinary', + clothing_cut='female', + clothing_size='XS', + health_issues='I am a bot', + emergency_contact_name='NoteKfet2020', + emergency_contact_phone='+33123456789', + ml_events_registration=True, + ml_sport_registration=False, + ml_art_registration=False, + )) + self.assertEqual(response.status_code, 200) + self.assertTrue("This user can't be in her/his first year since he/she has already participated to a WEI." + in str(response.context["form"].errors)) + + # Check that if the WEI is started, we can't register anyone + self.wei.date_start = '2000-01-01' + self.wei.save() + response = self.client.get(reverse("wei:wei_register_1A", kwargs=dict(wei_pk=self.wei.pk))) + self.assertRedirects(response, reverse("wei:wei_closed", kwargs=dict(pk=self.wei.pk)), 302, 200) + def test_wei_survey_ended(self): """ Test display the end page of a survey. @@ -281,6 +452,12 @@ class TestWEIRegistration(TestCase): self.assertEqual(response.status_code, 200) def test_update_registration(self): + """ + Test update a registration. + """ + response = self.client.get(reverse("wei:wei_update_registration", kwargs=dict(pk=self.registration.pk))) + self.assertEqual(response.status_code, 200) + self.registration.information = dict( preferred_bus_pk=[], preferred_team_pk=[], @@ -310,6 +487,12 @@ class TestWEIRegistration(TestCase): self.assertTrue(qs.exists()) self.assertRedirects(response, reverse("wei:validate_registration", kwargs=dict(pk=qs.get().pk)), 302, 200) + # Check that if the WEI is started, we can't update a registration + self.wei.date_start = '2000-01-01' + self.wei.save() + response = self.client.get(reverse("wei:wei_update_registration", kwargs=dict(wei_pk=self.registration.pk))) + self.assertRedirects(response, reverse("wei:wei_closed", kwargs=dict(pk=self.wei.pk)), 302, 200) + def test_delete_registration(self): """ Test delete a WEI registration. @@ -321,6 +504,9 @@ class TestWEIRegistration(TestCase): """ Test validate a membership. """ + response = self.client.get(reverse("wei:validate_registration", kwargs=dict(pk=self.registration.pk))) + self.assertEqual(response.status_code, 200) + response = self.client.post(reverse("wei:validate_registration", kwargs=dict(pk=self.registration.pk)), dict( roles=[WEIRole.objects.get(name="GC WEI").id], bus=self.bus.pk, @@ -359,28 +545,35 @@ class TestWEIRegistration(TestCase): def test_registrations_list(self): """ - Test display the registration list + Test display the registration list, with or without a research """ response = self.client.get(reverse("wei:wei_registrations", kwargs=dict(pk=self.wei.pk))) self.assertEqual(response.status_code, 200) + response = self.client.get(reverse("wei:wei_registrations", kwargs=dict(pk=self.wei.pk)) + "?search=.") + self.assertEqual(response.status_code, 200) + def test_memberships_list(self): """ - Test display the memberships list + Test display the memberships list, with or without a research """ response = self.client.get(reverse("wei:wei_memberships", kwargs=dict(pk=self.wei.pk))) self.assertEqual(response.status_code, 200) + response = self.client.get(reverse("wei:wei_memberships", kwargs=dict(pk=self.wei.pk)) + "?search=.") + self.assertEqual(response.status_code, 200) + def is_latex_installed(self): """ Check if LaTeX is installed in the machine. Don't check pages that generate a PDF file if LaTeX is not installed, like in Gitlab. """ - return subprocess.call( - ["which", "pdflatex"], - stdout=open('/dev/null', 'wb'), - stderr=open('/dev/null', 'wb'), - ) == 0 + with open("/dev/null", "wb") as devnull: + return subprocess.call( + ["which", "pdflatex"], + stdout=devnull, + stderr=devnull, + ) == 0 def test_memberships_pdf_list(self): """ diff --git a/apps/wei/views.py b/apps/wei/views.py index 44054ec6..ca63aa9c 100644 --- a/apps/wei/views.py +++ b/apps/wei/views.py @@ -235,7 +235,7 @@ class WEIRegistrationsView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTable pattern = self.request.GET.get("search", "") - if not pattern: + if pattern: qs = qs.filter( Q(user__first_name__iregex=pattern) | Q(user__last_name__iregex=pattern) @@ -266,7 +266,7 @@ class WEIUpdateView(ProtectQuerysetMixin, LoginRequiredMixin, UpdateView): today = date.today() # We can't update a past WEI # But we can update it while it is not officially opened - if today > wei.membership_end: + if today > wei.date_start: return redirect(reverse_lazy('wei:wei_closed', args=(wei.pk,))) return super().dispatch(request, *args, **kwargs) @@ -492,7 +492,7 @@ class WEIRegister1AView(ProtectQuerysetMixin, LoginRequiredMixin, CreateView): # Check if the user can be in her/his first year (yeah, no cheat) if WEIRegistration.objects.filter(user=form.instance.user).exists(): form.add_error('user', _("This user can't be in her/his first year since he/she has already" - " participed to a WEI.")) + " participated to a WEI.")) return self.form_invalid(form) return super().form_valid(form) @@ -1046,19 +1046,20 @@ class MemberListRenderView(LoginRequiredMixin, View): f.write(tex.encode("UTF-8")) del tex - error = subprocess.Popen( - ["pdflatex", "{}/wei-list.tex".format(tmp_dir)], - cwd=tmp_dir, - stdin=open(os.devnull, "r"), - stderr=open(os.devnull, "wb"), - stdout=open(os.devnull, "wb"), - ).wait() + with open(os.devnull, "wb") as devnull: + error = subprocess.Popen( + ["pdflatex", "{}/wei-list.tex".format(tmp_dir)], + cwd=tmp_dir, + stderr=devnull, + stdout=devnull, + ).wait() if error: raise IOError("An error attempted while generating a WEI list (code=" + str(error) + ")") # Display the generated pdf as a HTTP Response - pdf = open("{}/wei-list.pdf".format(tmp_dir), 'rb').read() + with open("{}/wei-list.pdf".format(tmp_dir), 'rb') as f: + pdf = f.read() response = HttpResponse(pdf, content_type="application/pdf") response['Content-Disposition'] = "inline;filename=Liste%20des%20participants%20au%20WEI.pdf" except IOError as e: diff --git a/locale/de/LC_MESSAGES/django.po b/locale/de/LC_MESSAGES/django.po index 257522b0..4486c987 100644 --- a/locale/de/LC_MESSAGES/django.po +++ b/locale/de/LC_MESSAGES/django.po @@ -2547,7 +2547,7 @@ msgstr "" #: apps/wei/views.py:494 msgid "" -"This user can't be in her/his first year since he/she has already participed " +"This user can't be in her/his first year since he/she has already participated " "to a WEI." msgstr "" diff --git a/locale/fr/LC_MESSAGES/django.po b/locale/fr/LC_MESSAGES/django.po index 0d94e2c3..527f3c37 100644 --- a/locale/fr/LC_MESSAGES/django.po +++ b/locale/fr/LC_MESSAGES/django.po @@ -2642,7 +2642,7 @@ msgstr "Cette personne est déjà inscrite au WEI." #: apps/wei/views.py:494 msgid "" -"This user can't be in her/his first year since he/she has already participed " +"This user can't be in her/his first year since he/she has already participated " "to a WEI." msgstr "" "Cet utilisateur ne peut pas être en première année puisqu'iel a déjà "