Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: this changes the current phone number regex to allow the plus s… #31

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions common/djangoapps/student/tests/test_user_profile_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
18 changes: 15 additions & 3 deletions openedx/core/djangoapps/user_api/accounts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,24 @@

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"""
return re.sub("[^0-9]", "", data) or None
"""
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


class LanguageProficiencySerializer(serializers.ModelSerializer):
Expand Down
Loading