From aea117e159473eaa70373455b29495d86e3a76f8 Mon Sep 17 00:00:00 2001 From: Jori Lindell Date: Wed, 16 Aug 2023 11:47:32 +0300 Subject: [PATCH] Don't validate mandatory fields and date_of_birth in PATCH signup request if value is missing from the payload --- registrations/serializers.py | 23 +++++------ registrations/tests/test_signup_patch.py | 50 ++++++++++++++++++++++++ registrations/tests/test_signup_put.py | 26 +----------- 3 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 registrations/tests/test_signup_patch.py diff --git a/registrations/serializers.py b/registrations/serializers.py index 59a4aa3cd..5e098720c 100644 --- a/registrations/serializers.py +++ b/registrations/serializers.py @@ -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.") diff --git a/registrations/tests/test_signup_patch.py b/registrations/tests/test_signup_patch.py new file mode 100644 index 000000000..62d698082 --- /dev/null +++ b/registrations/tests/test_signup_patch.py @@ -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 diff --git a/registrations/tests/test_signup_put.py b/registrations/tests/test_signup_put.py index 9cb812401..f5855e5b2 100644 --- a/registrations/tests/test_signup_put.py +++ b/registrations/tests/test_signup_put.py @@ -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 === @@ -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(