feat: avoid certain strings in the suffix appended to edX usernames (#29524)

ENT-2824
This commit is contained in:
John Nagro
2021-12-13 15:32:51 -05:00
committed by GitHub
parent 2d3e774db1
commit 3873ff7de9
4 changed files with 29 additions and 14 deletions

View File

@@ -87,6 +87,7 @@ from lms.djangoapps.verify_student.models import SSOVerification
from lms.djangoapps.verify_student.utils import earliest_allowed_verification_date
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
from openedx.core.djangoapps.user_api import accounts
from openedx.core.djangoapps.user_api.accounts.utils import username_suffix_generator
from openedx.core.djangoapps.user_authn import cookies as user_authn_cookies
from openedx.core.djangoapps.user_authn.toggles import is_require_third_party_auth_enabled
from common.djangoapps.third_party_auth.utils import (
@@ -1007,7 +1008,8 @@ def get_username(strategy, details, backend, user=None, *args, **kwargs): # lin
# The final_username may be empty and will skip the loop.
# We are using our own version of user_exists to avoid possible case sensitivity issues.
while not final_username or len(final_username) < min_length or user_exists({'username': final_username}):
username = short_username + uuid4().hex[:uuid_length]
# adding a dash between user-supplied and system-generated values to avoid weird combinations
username = short_username + '-' + username_suffix_generator(uuid_length)
final_username = slug_func(clean_func(username[:max_length]))
logger.info('[THIRD_PARTY_AUTH] New username generated. Username: {username}'.format(
username=final_username))

View File

@@ -72,13 +72,13 @@ class PipelineOverridesTest(SamlIntegrationTestUtilities, IntegrationTestMixin,
)
@ddt.data(
('S', 'S9fe2', False),
('S', 'S9fe2', True),
('S', 'S-9fe2', False),
('S', 'S-9fe2', True),
('S.K', 'S_K', False),
('S.K.', 'S_K', False),
('S.K.', 'S_K_9fe2', True),
('S.K.', 'S_K_-9fe2', True),
('usernamewithcharacterlengthofmorethan30chars', 'usernamewithcharacterlengthofm', False),
('usernamewithcharacterlengthofmorethan30chars', 'usernamewithcharacterlengt9fe2', True),
('usernamewithcharacterlengthofmorethan30chars', 'usernamewithcharacterlengt-9fe', True),
)
@ddt.unpack
@mock.patch('common.djangoapps.third_party_auth.pipeline.user_exists')
@@ -92,9 +92,7 @@ class PipelineOverridesTest(SamlIntegrationTestUtilities, IntegrationTestMixin,
}
mock_user_exists.side_effect = [already_exists, False]
__, strategy = self.get_request_and_strategy()
with mock.patch('common.djangoapps.third_party_auth.pipeline.uuid4') as mock_uuid:
uuid4 = mock.Mock()
type(uuid4).hex = mock.PropertyMock(return_value='9fe2c4e93f654fdbb24c02b15259716c')
mock_uuid.return_value = uuid4
with mock.patch('common.djangoapps.third_party_auth.pipeline.username_suffix_generator') as mock_suffix:
mock_suffix.return_value = '9fe2'
final_username = pipeline.get_username(strategy, details, self.provider.backend_class())
assert expected_username == final_username['username']