More WEI tests, > 97 % coverage

This commit is contained in:
Yohann D'ANELLO 2020-08-11 13:30:44 +02:00
parent 106e97f5df
commit 71f6436d06
4 changed files with 270 additions and 63 deletions

View File

@ -25,9 +25,7 @@ class WEISurveyInformation:
If the algorithm ran, return the prefered bus according to the survey.
In the other case, return None.
"""
if not self.valid:
return None
return Bus.objects.get(pk=self.selected_bus_pk)
return Bus.objects.get(pk=self.selected_bus_pk) if self.valid else None
def save(self, registration) -> None:
"""

View File

@ -85,21 +85,21 @@ class WEIRegistrationTable(tables.Table):
)
def render_validate(self, record):
if PermissionBackend.check_perm(get_current_authenticated_user(), "wei.add_weimembership", WEIMembership(
hasperm = PermissionBackend.check_perm(
get_current_authenticated_user(), "wei.add_weimembership", WEIMembership(
club=record.wei,
user=record.user,
date_start=timezone.now().date(),
date_end=timezone.now().date(),
fee=0,
registration=record,
)):
return _("Validate")
return format_html("<span class='no-perm'></span>")
)
)
return _("Validate") if hasperm else format_html("<span class='no-perm'></span>")
def render_delete(self, record):
if PermissionBackend.check_perm(get_current_authenticated_user(), "wei.delete_weimembership", record):
return _("Delete")
return format_html("<span class='no-perm'></span>")
hasperm = PermissionBackend.check_perm(get_current_authenticated_user(), "wei.delete_weimembership", record)
return _("Delete") if hasperm else format_html("<span class='no-perm'></span>")
class Meta:
attrs = {

View File

@ -2,7 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later
import subprocess
from datetime import timedelta
from datetime import timedelta, date
from django.conf import settings
from django.contrib.auth.models import User
@ -11,9 +11,10 @@ from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
from member.models import Membership
from note.models import NoteClub
from note.models import NoteClub, SpecialTransaction
from treasury.models import SogeCredit
from ..forms import CurrentSurvey
from ..forms import CurrentSurvey, WEISurveyAlgorithm, WEISurvey
from ..models import WEIClub, Bus, BusTeam, WEIRole, WEIRegistration, WEIMembership
@ -38,6 +39,7 @@ class TestWEIList(TestCase):
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
@ -54,6 +56,7 @@ class TestWEIRegistration(TestCase):
password="admin",
email="admin@example.com",
)
self.user.save()
self.client.force_login(self.user)
sess = self.client.session
sess["permission_mask"] = 42
@ -66,11 +69,11 @@ class TestWEIRegistration(TestCase):
parent_club_id=2,
membership_fee_paid=12500,
membership_fee_unpaid=5500,
membership_start=str(self.year) + "-01-01",
membership_end=str(self.year) + "-12-31",
membership_start=date(self.year, 1, 1),
membership_end=date(self.year, 12, 31),
year=self.year,
date_start=timezone.now().date() + timedelta(days=2),
date_end=str(self.year) + "-12-31",
date_end=date(self.year, 12, 31),
)
NoteClub.objects.create(club=self.wei)
self.bus = Bus.objects.create(
@ -89,7 +92,7 @@ class TestWEIRegistration(TestCase):
wei_id=self.wei.id,
soge_credit=True,
caution_check=True,
birth_date="2000-01-01",
birth_date=date(2000, 1, 1),
gender="nonbinary",
clothing_cut="male",
clothing_size="XL",
@ -99,7 +102,7 @@ class TestWEIRegistration(TestCase):
ml_events_registration=True,
ml_sport_registration=True,
ml_art_registration=True,
first_year=True,
first_year=False,
)
def test_create_wei(self):
@ -156,7 +159,7 @@ class TestWEIRegistration(TestCase):
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.date_start = date(2000, 1, 1)
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)
@ -190,10 +193,11 @@ class TestWEIRegistration(TestCase):
qs = Bus.objects.filter(name="Create Bus Test")
self.assertTrue(qs.exists())
bus = qs.get()
CurrentSurvey.get_algorithm_class().get_bus_information(bus).save()
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.date_start = date(2000, 1, 1)
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)
@ -221,7 +225,7 @@ class TestWEIRegistration(TestCase):
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.date_start = date(2000, 1, 1)
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)
@ -245,7 +249,7 @@ class TestWEIRegistration(TestCase):
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.date_start = date(2000, 1, 1)
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)
@ -274,7 +278,7 @@ class TestWEIRegistration(TestCase):
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.date_start = date(2000, 1, 1)
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)
@ -286,16 +290,13 @@ class TestWEIRegistration(TestCase):
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',
birth_date=date(2000, 1, 1),
gender='nonbinary',
clothing_cut='female',
clothing_size='XS',
@ -312,7 +313,7 @@ class TestWEIRegistration(TestCase):
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',
birth_date=date(2000, 1, 1),
gender='nonbinary',
clothing_cut='female',
clothing_size='XS',
@ -331,7 +332,7 @@ class TestWEIRegistration(TestCase):
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',
birth_date=date(2000, 1, 1),
gender='nonbinary',
clothing_cut='female',
clothing_size='XS',
@ -345,8 +346,22 @@ class TestWEIRegistration(TestCase):
self.assertEqual(response.status_code, 200)
self.assertTrue("This user is already registered to this WEI." in str(response.context["form"].errors))
# Test the render of the page to register ourself if we have already opened a Société générale account
SogeCredit.objects.create(user=self.user, credit_transaction=SpecialTransaction.objects.create(
source_id=4, # Bank transfer
destination=self.user.note,
quantity=1,
amount=0,
reason="Test",
first_name="toto",
last_name="toto",
bank="Société générale",
))
response = self.client.get(reverse("wei:wei_register_2A_myself", kwargs=dict(wei_pk=self.wei.pk)))
self.assertEqual(response.status_code, 200)
# Check that if the WEI is started, we can't register anyone
self.wei.date_start = '2000-01-01'
self.wei.date_start = date(2000, 1, 1)
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)
@ -365,7 +380,7 @@ class TestWEIRegistration(TestCase):
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',
birth_date=date(2000, 1, 1),
gender='nonbinary',
clothing_cut='female',
clothing_size='XS',
@ -392,12 +407,15 @@ class TestWEIRegistration(TestCase):
self.assertIsNotNone(getattr(survey.information, "word" + str(i)), "Survey page #" + str(i) + " failed")
survey = CurrentSurvey(registration)
self.assertTrue(survey.is_complete())
survey.select_bus(self.bus)
survey.save()
self.assertIsNotNone(survey.information.get_selected_bus())
# 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',
birth_date=date(2000, 1, 1),
gender='nonbinary',
clothing_cut='female',
clothing_size='XS',
@ -423,7 +441,7 @@ class TestWEIRegistration(TestCase):
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',
birth_date=date(2000, 1, 1),
gender='nonbinary',
clothing_cut='female',
clothing_size='XS',
@ -439,11 +457,14 @@ class TestWEIRegistration(TestCase):
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.date_start = date(2000, 1, 1)
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)
response = self.client.get(reverse("wei:wei_survey", kwargs=dict(pk=registration.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.
@ -455,9 +476,6 @@ class TestWEIRegistration(TestCase):
"""
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=[],
@ -465,6 +483,9 @@ class TestWEIRegistration(TestCase):
)
self.registration.save()
response = self.client.get(reverse("wei:wei_update_registration", kwargs=dict(pk=self.registration.pk)))
self.assertEqual(response.status_code, 200)
response = self.client.post(
reverse("wei:wei_update_registration", kwargs=dict(pk=self.registration.pk)),
dict(
@ -487,16 +508,95 @@ class TestWEIRegistration(TestCase):
self.assertTrue(qs.exists())
self.assertRedirects(response, reverse("wei:validate_registration", kwargs=dict(pk=qs.get().pk)), 302, 200)
# Check the page when the registration is already validated
membership = WEIMembership(
user=self.user,
club=self.wei,
registration=self.registration,
bus=self.bus,
team=self.team,
)
membership._soge = True
membership._force_renew_parent = True
membership.save()
soge_credit = SogeCredit.objects.get(user=self.user)
soge_credit.credit_transaction = SpecialTransaction.objects.create(
source_id=4, # Bank transfer
destination=self.user.note,
quantity=1,
amount=0,
reason="Test",
first_name="toto",
last_name="toto",
bank="Société générale",
)
soge_credit.save()
sess = self.client.session
sess["permission_mask"] = 0
sess.save()
response = self.client.get(reverse("wei:wei_update_registration", kwargs=dict(pk=self.registration.pk)))
self.assertEqual(response.status_code, 200)
sess["permission_mask"] = 42
sess.save()
response = self.client.post(
reverse("wei:wei_update_registration", kwargs=dict(pk=self.registration.pk)),
dict(
user=self.user.id,
soge_credit=False,
birth_date='2015-01-01',
gender='male',
clothing_cut='female',
clothing_size='L',
health_issues='I am really a bot',
emergency_contact_name='Note Kfet 2020',
emergency_contact_phone='+33600000000',
bus=[self.bus.id],
team=[self.team.id],
roles=[role.id for role in WEIRole.objects.filter(name="Adhérent WEI").all()],
information_json=self.registration.information_json,
)
)
qs = WEIRegistration.objects.filter(user_id=self.user.id, clothing_size="L")
self.assertTrue(qs.exists())
self.assertRedirects(response, reverse("wei:validate_registration", kwargs=dict(pk=qs.get().pk)), 302, 200)
# Test invalid form
response = self.client.post(
reverse("wei:wei_update_registration", kwargs=dict(pk=self.registration.pk)),
dict(
user=self.user.id,
soge_credit=False,
birth_date='2015-01-01',
gender='male',
clothing_cut='female',
clothing_size='L',
health_issues='I am really a bot',
emergency_contact_name='Note Kfet 2020',
emergency_contact_phone='+33600000000',
bus=[],
team=[],
roles=[],
information_json=self.registration.information_json,
)
)
self.assertFalse(response.context["membership_form"].is_valid())
# Check that if the WEI is started, we can't update a registration
self.wei.date_start = '2000-01-01'
self.wei.date_start = date(2000, 1, 1)
self.wei.update_membership_dates()
self.wei.save()
response = self.client.get(reverse("wei:validate_registration", kwargs=dict(pk=self.registration.pk)))
response = self.client.get(reverse("wei:wei_update_registration", kwargs=dict(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.
"""
response = self.client.get(reverse("wei:wei_delete_registration", kwargs=dict(pk=self.registration.pk)))
self.assertEqual(response.status_code, 200)
response = self.client.delete(reverse("wei:wei_delete_registration", kwargs=dict(pk=self.registration.pk)))
self.assertRedirects(response, reverse("wei:wei_detail", kwargs=dict(pk=self.wei.pk)), 302, 200)
@ -507,6 +607,32 @@ class TestWEIRegistration(TestCase):
response = self.client.get(reverse("wei:validate_registration", kwargs=dict(pk=self.registration.pk)))
self.assertEqual(response.status_code, 200)
self.registration.first_year = True
self.registration.save()
response = self.client.get(reverse("wei:validate_registration", kwargs=dict(pk=self.registration.pk)))
self.assertEqual(response.status_code, 200)
self.registration.first_year = False
self.registration.save()
# Check that a team must belong to the bus
second_bus = Bus.objects.create(wei=self.wei, name="Second bus")
second_team = BusTeam.objects.create(bus=second_bus, name="Second team", color=42)
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,
team=second_team.pk,
credit_type=4, # Bank transfer
credit_amount=420,
last_name="admin",
first_name="admin",
bank="Société générale",
))
self.assertEqual(response.status_code, 200)
self.assertFalse(response.context["form"].is_valid())
self.assertTrue("This team doesn&#39;t belong to the given bus." in str(response.context["form"].errors))
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,
@ -543,6 +669,12 @@ class TestWEIRegistration(TestCase):
self.assertFalse(kfet_membership.transaction.valid)
self.assertFalse(bde_membership.transaction.valid)
# Check that if the WEI is started, we can't update a wei
self.wei.date_start = date(2000, 1, 1)
self.wei.save()
response = self.client.get(reverse("wei:validate_registration", kwargs=dict(pk=self.registration.pk)))
self.assertRedirects(response, reverse("wei:wei_closed", kwargs=dict(pk=self.wei.pk)), 302, 200)
def test_registrations_list(self):
"""
Test display the registration list, with or without a research
@ -579,34 +711,114 @@ class TestWEIRegistration(TestCase):
"""
Test display the membership list as a PDF file
"""
if not self.is_latex_installed():
return
response = self.client.get(reverse("wei:wei_memberships_pdf", kwargs=dict(wei_pk=self.wei.pk)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response["content-type"], "application/pdf")
if self.is_latex_installed():
response = self.client.get(reverse("wei:wei_memberships_pdf", kwargs=dict(wei_pk=self.wei.pk)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response["content-type"], "application/pdf")
def test_bus_memberships_pdf_list(self):
"""
Test display the membership list of a bus as a PDF file
"""
if not self.is_latex_installed():
return
response = self.client.get(reverse("wei:wei_memberships_bus_pdf", kwargs=dict(wei_pk=self.wei.pk,
bus_pk=self.bus.pk)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response["content-type"], "application/pdf")
if self.is_latex_installed():
response = self.client.get(reverse("wei:wei_memberships_bus_pdf", kwargs=dict(wei_pk=self.wei.pk,
bus_pk=self.bus.pk)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response["content-type"], "application/pdf")
def test_team_memberships_pdf_list(self):
"""
Test display the membership list of a bus team as a PDF file
"""
if not self.is_latex_installed():
return
if self.is_latex_installed():
response = self.client.get(reverse("wei:wei_memberships_team_pdf", kwargs=dict(wei_pk=self.wei.pk,
bus_pk=self.bus.pk,
team_pk=self.team.pk)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response["content-type"], "application/pdf")
response = self.client.get(reverse("wei:wei_memberships_team_pdf", kwargs=dict(wei_pk=self.wei.pk,
bus_pk=self.bus.pk,
team_pk=self.team.pk)))
self.assertEqual(response.status_code, 200)
self.assertEqual(response["content-type"], "application/pdf")
class TestDefaultWEISurvey(TestCase):
"""
Doesn't test anything, just cover the default Survey classes.
"""
def check_not_implemented(self, fun: callable, *args, **kwargs):
self.assertRaises(NotImplementedError, fun, *args, **kwargs)
def test_survey_classes(self):
WEISurveyAlgorithm.get_bus_information_class()
self.check_not_implemented(WEISurveyAlgorithm.get_survey_class)
self.check_not_implemented(WEISurveyAlgorithm.get_registrations)
self.check_not_implemented(WEISurveyAlgorithm.get_buses)
self.check_not_implemented(WEISurveyAlgorithm().run_algorithm)
self.check_not_implemented(WEISurvey, registration=None)
self.check_not_implemented(WEISurvey.get_wei)
self.check_not_implemented(WEISurvey.get_survey_information_class)
self.check_not_implemented(WEISurvey.get_algorithm_class)
self.check_not_implemented(WEISurvey.get_form_class, None)
self.check_not_implemented(WEISurvey.form_valid, None, None)
self.check_not_implemented(WEISurvey.is_complete, None)
# noinspection PyTypeChecker
WEISurvey.update_form(None, None)
self.assertEqual(CurrentSurvey.get_algorithm_class().get_survey_class(), CurrentSurvey)
self.assertEqual(CurrentSurvey.get_year(), 2020)
class TestWEISurveyAlgorithm(TestCase):
"""
Run the WEI Algorithm.
TODO: Improve this test with some test data once the algorithm will be implemented.
"""
fixtures = ("initial",)
def setUp(self) -> None:
self.year = timezone.now().year
self.wei = WEIClub.objects.create(
name="Test WEI",
email="gc.wei@example.com",
parent_club_id=2,
membership_fee_paid=12500,
membership_fee_unpaid=5500,
membership_start=date(self.year, 1, 1),
membership_end=date(self.year, 12, 31),
year=self.year,
date_start=timezone.now().date() + timedelta(days=2),
date_end=date(self.year, 12, 31),
)
NoteClub.objects.create(club=self.wei)
self.bus = Bus.objects.create(
name="Test Bus",
wei=self.wei,
description="Test Bus",
)
self.team = BusTeam.objects.create(
name="Test Team",
bus=self.bus,
color=0xFFFFFF,
description="Test Team",
)
self.user = User.objects.create(username="toto")
self.registration = WEIRegistration.objects.create(
user_id=self.user.id,
wei_id=self.wei.id,
soge_credit=True,
caution_check=True,
birth_date=date(2000, 1, 1),
gender="nonbinary",
clothing_cut="male",
clothing_size="XL",
health_issues="I am a bot",
emergency_contact_name="Pikachu",
emergency_contact_phone="+33123456789",
ml_events_registration=True,
ml_sport_registration=True,
ml_art_registration=True,
first_year=True,
)
CurrentSurvey(self.registration).save()
def test_survey_algorithm(self):
CurrentSurvey.get_algorithm_class()().run_algorithm()

View File

@ -824,10 +824,7 @@ class WEIValidateRegistrationView(ProtectQuerysetMixin, LoginRequiredMixin, Crea
# Force the membership of the clubs BDE and Kfet
membership._force_renew_parent = True
if user.profile.paid:
fee = club.membership_fee_paid
else:
fee = club.membership_fee_unpaid
fee = club.membership_fee_paid if user.profile.paid else club.membership_fee_unpaid
kfet = club.parent_club
bde = kfet.parent_club