From 15aa04b858b04e2c51190d1e3883331b3111ea7e Mon Sep 17 00:00:00 2001 From: Dima Alipov Date: Tue, 23 Apr 2024 14:47:07 +0300 Subject: [PATCH] fix: registering new user with a name longer than 255 characters --- .../user_authn/views/registration_form.py | 3 +++ .../user_authn/views/tests/test_register.py | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/openedx/core/djangoapps/user_authn/views/registration_form.py b/openedx/core/djangoapps/user_authn/views/registration_form.py index 7a0207f8b93c..978d303c9a1f 100644 --- a/openedx/core/djangoapps/user_authn/views/registration_form.py +++ b/openedx/core/djangoapps/user_authn/views/registration_form.py @@ -148,6 +148,7 @@ class AccountCreationForm(forms.Form): _EMAIL_INVALID_MSG = _("A properly formatted e-mail is required") _NAME_TOO_SHORT_MSG = _("Your legal name must be a minimum of one character long") + _NAME_TOO_LONG_MSG = _("Your legal name is too long. It must not exceed %(max_length)s characters") # TODO: Resolve repetition @@ -167,9 +168,11 @@ class AccountCreationForm(forms.Form): name = forms.CharField( min_length=accounts.NAME_MIN_LENGTH, + max_length=accounts.NAME_MAX_LENGTH, error_messages={ "required": _NAME_TOO_SHORT_MSG, "min_length": _NAME_TOO_SHORT_MSG, + "max_length": _NAME_TOO_LONG_MSG % {"max_length": accounts.NAME_MAX_LENGTH}, }, validators=[validate_name] ) 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 16f7da8010d6..77b5d074fba2 100644 --- a/openedx/core/djangoapps/user_authn/views/tests/test_register.py +++ b/openedx/core/djangoapps/user_authn/views/tests/test_register.py @@ -316,6 +316,30 @@ def test_register_fullname_url_validation_error(self): } ) + def test_register_fullname_max_lenghth_validation_error(self): + """ + Full name error detection test if the length exceeds 255 characters. + """ + expected_error_message = f"Your legal name is too long. It must not exceed {NAME_MAX_LENGTH} characters" + + response = self.client.post(self.url, { + "email": self.EMAIL, + "name": "x" * 256, + "username": self.USERNAME, + "password": self.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": expected_error_message}], + "error_code": "validation-error" + } + ) + def test_register_fullname_html_validation_error(self): """ Test for catching invalid full name errors