fix: prevent name change request if requested name is identical to the existing name

This commit is contained in:
Bianca Severino
2021-10-04 17:23:18 -04:00
parent 41f8759eab
commit e9d22fc190
2 changed files with 37 additions and 2 deletions

View File

@@ -243,9 +243,13 @@ def _validate_name_change(user_profile, data, field_errors):
return None
old_name = user_profile.name
new_name = data['name']
if old_name == new_name:
return None
try:
validate_name(data['name'])
validate_name(new_name)
except ValidationError as err:
field_errors["name"] = {
"developer_message": f"Error thrown from validate_name: '{err.message}'",
@@ -253,7 +257,7 @@ def _validate_name_change(user_profile, data, field_errors):
}
return None
if _does_name_change_require_verification(user_profile, old_name, data['name']):
if _does_name_change_require_verification(user_profile, old_name, new_name):
err_msg = 'This name change requires ID verification.'
field_errors['name'] = {
'developer_message': err_msg,

View File

@@ -4,6 +4,7 @@ Most of the functionality is covered in test_views.py.
"""
import datetime
import itertools
import unicodedata
from unittest.mock import Mock, patch
@@ -18,6 +19,7 @@ from django.test.client import RequestFactory
from django.urls import reverse
from edx_name_affirmation.toggles import VERIFIED_NAME_FLAG
from edx_toggles.toggles.testutils import override_waffle_flag
from pytz import UTC
from social_django.models import UserSocialAuth
from common.djangoapps.student.models import (
AccountRecovery,
@@ -363,6 +365,35 @@ class TestAccountApi(UserSettingsEventTestMixin, EmailTemplateTagMixin, CreateAc
assert 'Valid e-mail address required.' in field_errors['email']['developer_message']
assert 'Full Name cannot contain the following characters: < >' in field_errors['name']['user_message']
@override_waffle_flag(VERIFIED_NAME_FLAG, active=True)
def test_validate_name_change_same_name(self):
"""
Test that saving the user's profile name without changing it should not raise an error.
"""
account_settings = get_account_settings(self.default_request)[0]
# Add name change history to the profile metadeta; if the user has at least one certificate,
# too many name changes will trigger the verification requirement, but this should only happen
# if the name has actually been changed
user_profile = UserProfile.objects.get(user=self.user)
meta = user_profile.get_meta()
meta['old_names'] = []
for num in range(3):
meta['old_names'].append(
[f'old_name_{num}', 'test', datetime.datetime.now(UTC).isoformat()]
)
user_profile.set_meta(meta)
user_profile.save()
with patch(
'openedx.core.djangoapps.user_api.accounts.api.get_certificates_for_user',
return_value=['mock_certificate']
):
update_account_settings(self.user, {'name': account_settings['name']})
# The name should not be added to profile metadata
updated_meta = user_profile.get_meta()
self.assertEqual(meta, updated_meta)
@patch('edx_name_affirmation.name_change_validator.NameChangeValidator', Mock())
@patch('edx_name_affirmation.name_change_validator.NameChangeValidator.validate', Mock(return_value=False))
@override_waffle_flag(VERIFIED_NAME_FLAG, active=True)