Skip to content

Commit

Permalink
Don't validate mandatory fields and date_of_birth in PATCH signup req…
Browse files Browse the repository at this point in the history
…uest if value is missing from the payload
  • Loading branch information
jorilindell committed Aug 16, 2023
1 parent 6b76c82 commit aea117e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
23 changes: 9 additions & 14 deletions registrations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,21 @@ def validate(self, data):

falsy_values = ("", None)

# Validate mandatory fields
for field in registration.mandatory_fields:
if self.instance and field not in data.keys():
# Use existing value if field is missing in the payload
val = getattr(self.instance, field)
else:
val = data.get(field)

if val in falsy_values:
# Don't validate field if request method is PATCH and field is missing from the payload
if self.partial and field not in data.keys():
continue
elif data.get(field) in falsy_values:
errors[field] = _("This field must be specified.")

# Validate date_of_birth if audience_min_age or registration.audience_max_age is defined
# Don't validate date_of_birth if request method is PATCH and field is missing from the payload
if (
registration.audience_min_age not in falsy_values
or registration.audience_max_age not in falsy_values
):
if self.instance and "date_of_birth" not in data.keys():
# Use existing value if date_of_birth is missing in the payload
date_of_birth = self.instance.date_of_birth

else:
date_of_birth = data.get("date_of_birth")
) and not (self.partial and "date_of_birth" not in data.keys()):
date_of_birth = data.get("date_of_birth")

if date_of_birth in falsy_values:
errors["date_of_birth"] = _("This field must be specified.")
Expand Down
50 changes: 50 additions & 0 deletions registrations/tests/test_signup_patch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest
from freezegun import freeze_time
from rest_framework import status

from events.tests.utils import versioned_reverse as reverse
from registrations.models import MandatoryFields, SignUp

# === util methods ===


def patch_signup(api_client, signup_pk, signup_data):
signup_url = reverse(
"signup-detail",
kwargs={"pk": signup_pk},
)

response = api_client.patch(signup_url, signup_data, format="json")
return response


def assert_patch_signup(api_client, signup_pk, signup_data):
response = patch_signup(api_client, signup_pk, signup_data)

assert response.status_code == status.HTTP_200_OK
assert response.data["id"] == signup_pk

return response


@freeze_time("2023-03-14 03:30:00+02:00")
@pytest.mark.django_db
def test__patch_presence_status_of_signup(api_client, registration, signup, user):
registration.audience_min_age = 10
registration.mandatory_fields = [
MandatoryFields.PHONE_NUMBER,
MandatoryFields.STREET_ADDRESS,
]
registration.save()
signup.date_of_birth = "2011-01-01"
signup.phone_number = "0441234567"
signup.street_address = "Street address"
signup.save()
api_client.force_authenticate(user)

signup_data = {
"presence_status": SignUp.PresenceStatus.PRESENT,
}

response = assert_patch_signup(api_client, signup.id, signup_data)
assert response.data["presence_status"] == SignUp.PresenceStatus.PRESENT
26 changes: 1 addition & 25 deletions registrations/tests/test_signup_put.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_framework import status

from events.tests.utils import versioned_reverse as reverse
from registrations.models import MandatoryFields, SignUp
from registrations.models import SignUp

# === util methods ===

Expand Down Expand Up @@ -54,30 +54,6 @@ def test__update_signup(user_api_client, registration, signup, user):
assert db_signup.last_modified_by_id == user.id


@freeze_time("2023-03-14 03:30:00+02:00")
@pytest.mark.django_db
def test__update_presence_status_of_signup(api_client, registration, signup, user):
registration.audience_min_age = 10
registration.mandatory_fields = [
MandatoryFields.PHONE_NUMBER,
MandatoryFields.STREET_ADDRESS,
]
registration.save()
signup.date_of_birth = "2011-01-01"
signup.phone_number = "0441234567"
signup.street_address = "Street address"
signup.save()
api_client.force_authenticate(user)

signup_data = {
"registration": registration.id,
"presence_status": SignUp.PresenceStatus.PRESENT,
}

response = assert_update_signup(api_client, signup.id, signup_data)
assert response.data["presence_status"] == SignUp.PresenceStatus.PRESENT


@freeze_time("2023-03-14 03:30:00+02:00")
@pytest.mark.django_db
def test__created_regular_user_can_update_signup(
Expand Down

0 comments on commit aea117e

Please sign in to comment.