Skip to content

Commit

Permalink
fix: registering new user with a name longer than 255 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Alipov authored and farhaanbukhsh committed Nov 13, 2024
1 parent 421f28f commit 15aa04b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions openedx/core/djangoapps/user_authn/views/registration_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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]
)
Expand Down
24 changes: 24 additions & 0 deletions openedx/core/djangoapps/user_authn/views/tests/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 15aa04b

Please sign in to comment.