Skip to content

Commit

Permalink
RegistrationService: Check registration before completing preparation
Browse files Browse the repository at this point in the history
This makes sure that users cannot skip registration steps by editing the
url and still complete the registration.

This fixes the last part of #92.
  • Loading branch information
matthijskooijman committed Apr 11, 2020
1 parent b8ac90f commit ba4e3aa
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
32 changes: 31 additions & 1 deletion apps/registrations/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
from django.utils.translation import gettext as _

from apps.events.models import Event
from apps.people.models import EmergencyContact

from .models import Registration, RegistrationFieldOption
from .models import Registration, RegistrationField, RegistrationFieldOption


class RegistrationStatusService:
Expand All @@ -28,6 +29,35 @@ def preparation_completed(registration):
if not registration.status.PREPARATION_IN_PROGRESS:
raise ValidationError(_("Registration no longer in progress"))

user = registration.user

if not hasattr(user, 'address'):
raise ValidationError(_("Address incomplete"))

if user.emergency_contacts.count() < EmergencyContact.MIN_PER_USER:
raise ValidationError(_("Not enough emergency contacts"))

if not user.first_name or not user.last_name:
raise ValidationError(_("Name (partially) empty"))

# Check that all fields have a value (taking dependencies into account)
# TODO: Should this code live somewhere else?
# TODO: How about fields where all options do not have their dependencies fulfilled? Should those be omitted?
selected_options = RegistrationFieldOption.objects.filter(
registrationfieldvalue__registration=registration,
)
all_fields = RegistrationField.objects.filter(
event=registration.event_id,
)
required_fields = all_fields.filter(
Q(depends=None) | Q(depends__in=selected_options),
)
missing_fields = required_fields.exclude(
registrationfieldvalue__registration=registration,
)
if missing_fields.exists():
raise ValidationError(_("Missing registration options"))

registration.status = Registration.statuses.PREPARATION_COMPLETE
registration.save()

Expand Down
60 changes: 59 additions & 1 deletion apps/registrations/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.conf import settings
from django.core import mail
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from django.forms.models import model_to_dict
from django.test import TestCase, skipUnlessDBFeature
Expand All @@ -11,7 +12,7 @@
from apps.core.models import ConsentLog
from apps.events.tests.factories import EventFactory
from apps.people.models import Address, EmergencyContact, MedicalDetails
from apps.people.tests.factories import ArtaUserFactory, MedicalDetailsFactory
from apps.people.tests.factories import AddressFactory, ArtaUserFactory, EmergencyContactFactory, MedicalDetailsFactory

from ..models import Registration, RegistrationFieldValue
from ..services import RegistrationStatusService
Expand All @@ -35,6 +36,63 @@ def setUpTestData(cls):
cls.option_nl = RegistrationFieldOptionFactory(field=cls.origin, title="NL", slots=2)
cls.option_intl = RegistrationFieldOptionFactory(field=cls.origin, title="INTL", slots=2)

def incomplete_registration_helper(
self, empty_field=None, with_emergency_contact=True, with_address=True, options=True,
exception=ValidationError,
):
if options is True:
options = [self.player, self.option_m, self.option_nl]
reg = RegistrationFactory(event=self.event, preparation_in_progress=True, options=options)

if with_emergency_contact:
EmergencyContactFactory(user=reg.user)
if with_address:
AddressFactory(user=reg.user)
# MedicalDetails is optional, so no need to create it

if empty_field:
setattr(reg.user, empty_field, '')
reg.user.save()

if exception:
with self.assertRaises(exception):
RegistrationStatusService.preparation_completed(reg)
else:
RegistrationStatusService.preparation_completed(reg)

reg.refresh_from_db()
if exception:
self.assertEqual(reg.status, Registration.statuses.PREPARATION_IN_PROGRESS)
else:
self.assertEqual(reg.status, Registration.statuses.PREPARATION_COMPLETE)

def test_missing_first_name(self):
""" Check that a missing first name prevents completing preparation """
self.incomplete_registration_helper(empty_field='first_name')

def test_missing_last_name(self):
""" Check that a missing last name prevents completing preparation """
self.incomplete_registration_helper(empty_field='first_name')

def test_missing_address(self):
""" Check that a missing address prevents completing preparation """
self.incomplete_registration_helper(with_address=False)

def test_missing_emergency_contacts(self):
""" Check that a missing emergency contacts prevent completing preparation """
self.incomplete_registration_helper(with_emergency_contact=False)

def test_missing_options(self):
""" Check that missing options prevent completing preparation """
self.incomplete_registration_helper(options=[])

def test_partial_options(self):
""" Check that incomplete options prevent completing preparation """
self.incomplete_registration_helper(options=[self.player])

def test_optional_options(self):
""" Check that a omitting an optional option does not prevent completing preparation """
self.incomplete_registration_helper(options=[self.crew], exception=None)

def test_register_until_option_full(self):
""" Register until the option slots are taken and the next registration ends up on the waiting list. """
Expand Down

0 comments on commit ba4e3aa

Please sign in to comment.