From 7d029f82833be5657faa98be305b059d3f9bc0a1 Mon Sep 17 00:00:00 2001 From: Attiya Ishaque Date: Thu, 12 Aug 2021 16:07:32 +0500 Subject: [PATCH] [VAN-332] Full name validation on registration page. (#28444) --- .../user_authn/views/registration_form.py | 10 +++++++++ .../user_authn/views/tests/test_register.py | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/openedx/core/djangoapps/user_authn/views/registration_form.py b/openedx/core/djangoapps/user_authn/views/registration_form.py index 04407171f1..bee86c733f 100644 --- a/openedx/core/djangoapps/user_authn/views/registration_form.py +++ b/openedx/core/djangoapps/user_authn/views/registration_form.py @@ -89,6 +89,14 @@ def contains_html(value): return bool(regex.search(value)) +def contains_url(value): + """ + Validator method to check whether full name contains url + """ + regex = re.findall(r'https|http?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+', value) + return bool(regex) + + def validate_name(name): """ Verifies a Full_Name is valid, raises a ValidationError otherwise. @@ -97,6 +105,8 @@ def validate_name(name): """ if contains_html(name): raise forms.ValidationError(_('Full Name cannot contain the following characters: < >')) + if contains_url(name): + raise forms.ValidationError(_('Enter a valid name')) class UsernameField(forms.CharField): diff --git a/openedx/core/djangoapps/user_authn/views/tests/test_register.py b/openedx/core/djangoapps/user_authn/views/tests/test_register.py index c242b2c8a1..f89f10a6f9 100644 --- a/openedx/core/djangoapps/user_authn/views/tests/test_register.py +++ b/openedx/core/djangoapps/user_authn/views/tests/test_register.py @@ -237,6 +237,27 @@ class RegistrationViewValidationErrorTest(ThirdPartyAuthTestMixin, UserAPITestCa } ) + def test_register_fullname_url_validation_error(self): + """ + Test for catching invalid full name errors + """ + response = self.client.post(self.url, { + "email": "bob@example.com", + "name": "Bob Smith http://test.com", + "username": "bob", + "password": "password", + "honor_code": "true", + }) + assert response.status_code == 400 + response_json = json.loads(response.content.decode('utf-8')) + self.assertDictEqual( + response_json, + { + "name": [{"user_message": 'Enter a valid name'}], + "error_code": "validation-error" + } + ) + @override_waffle_flag(REGISTRATION_FAILURE_LOGGING_FLAG, True) def test_registration_failure_logging(self): # Register a user