From 69228b59f0dab5233b5c3e98548d3edf5680af0e Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Fri, 12 Jul 2024 17:45:17 -0500 Subject: [PATCH 1/3] feat: this changes the current phone number regex to allow the plus symbol in the first character --- openedx/core/djangoapps/user_api/accounts/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openedx/core/djangoapps/user_api/accounts/serializers.py b/openedx/core/djangoapps/user_api/accounts/serializers.py index 75ecfa35bf2b..a3aced755193 100644 --- a/openedx/core/djangoapps/user_api/accounts/serializers.py +++ b/openedx/core/djangoapps/user_api/accounts/serializers.py @@ -53,7 +53,7 @@ class PhoneNumberSerializer(serializers.BaseSerializer): # lint-amnesty, pylint def to_internal_value(self, data): """Remove all non numeric characters in phone number""" - return re.sub("[^0-9]", "", data) or None + return re.sub(r'(?!^)\+|[^0-9+]', "", data) or None class LanguageProficiencySerializer(serializers.ModelSerializer): From 98aa5aaf0817b4d535f9540cf5c2e42b94ee85da Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Tue, 16 Jul 2024 16:22:51 -0500 Subject: [PATCH 2/3] feat: split phone number tests --- .../tests/test_user_profile_properties.py | 60 ++++++++++++------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/common/djangoapps/student/tests/test_user_profile_properties.py b/common/djangoapps/student/tests/test_user_profile_properties.py index 8310759266bf..886c026421e6 100644 --- a/common/djangoapps/student/tests/test_user_profile_properties.py +++ b/common/djangoapps/student/tests/test_user_profile_properties.py @@ -107,23 +107,43 @@ def test_invalidate_cache_user_profile_country_updated(self): assert cache.get(cache_key) != country assert cache.get(cache_key) is None - def test_phone_number_can_only_contain_digits(self): - # validating the profile will fail, because there are letters - # in the phone number - self.profile.phone_number = 'abc' - pytest.raises(ValidationError, self.profile.full_clean) - # fail if mixed digits/letters - self.profile.phone_number = '1234gb' - pytest.raises(ValidationError, self.profile.full_clean) - # fail if whitespace - self.profile.phone_number = ' 123' - pytest.raises(ValidationError, self.profile.full_clean) - # fail with special characters - self.profile.phone_number = '123!@#$%^&*' - pytest.raises(ValidationError, self.profile.full_clean) - # valid phone number - self.profile.phone_number = '123456789' - try: - self.profile.full_clean() - except ValidationError: - self.fail("This phone number should be valid.") + def test_valid_phone_numbers(self): + """ + Test that valid phone numbers are accepted. + + Expected behavior: + - The phone number '+123456789' should be considered valid. + - The phone number '123456789' (without '+') should also be valid. + + This test verifies that valid phone numbers are accepted by the profile model validation. + """ + valid_numbers = ['+123456789', '123456789'] + + for number in valid_numbers: + self.profile.phone_number = number + + try: + self.profile.full_clean() + except ValidationError: + self.fail("This phone number should be valid.") + + def test_invalid_phone_numbers(self): + """ + Test that invalid phone numbers raise ValidationError. + + Expected behavior: + - Phone numbers with letters, mixed digits/letters, whitespace, + or special characters should raise a ValidationError. + + This test verifies that invalid phone numbers are rejected by the profile model validation. + """ + invalid_phone_numbers = [ + 'abc', # Letters in the phone number + '1234gb', # Mixed digits and letters + ' 123', # Whitespace + '123!@#$%^&*' # Special characters + ] + + for number in invalid_phone_numbers: + self.profile.phone_number = number + pytest.raises(ValidationError, self.profile.full_clean) From 469a7762202960f96f7b0ebe45b62ae11626ba8c Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Tue, 16 Jul 2024 16:23:27 -0500 Subject: [PATCH 3/3] chore: update PhonenumberSerializer docstring --- .../djangoapps/user_api/accounts/serializers.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/openedx/core/djangoapps/user_api/accounts/serializers.py b/openedx/core/djangoapps/user_api/accounts/serializers.py index a3aced755193..d23330742fba 100644 --- a/openedx/core/djangoapps/user_api/accounts/serializers.py +++ b/openedx/core/djangoapps/user_api/accounts/serializers.py @@ -48,11 +48,23 @@ class PhoneNumberSerializer(serializers.BaseSerializer): # lint-amnesty, pylint: disable=abstract-method """ - Class to serialize phone number into a digit only representation + Class to serialize phone number into a digit only representation. + + This serializer removes all non-numeric characters from the phone number, + allowing '+' only at the beginning of the number. """ def to_internal_value(self, data): - """Remove all non numeric characters in phone number""" + """ + Remove all non-numeric characters from the phone number. + + Args: + data (str): The input phone number string. + + Returns: + str or None: The cleaned phone number string containing only digits, + with an optional '+' at the beginning. + """ return re.sub(r'(?!^)\+|[^0-9+]', "", data) or None