diff --git a/common/djangoapps/student/management/commands/change_enterprise_user_username.py b/common/djangoapps/student/management/commands/change_enterprise_user_username.py deleted file mode 100644 index 51d037f6a3e2..000000000000 --- a/common/djangoapps/student/management/commands/change_enterprise_user_username.py +++ /dev/null @@ -1,60 +0,0 @@ -""" -Django management command for changing an enterprise user's username. -""" - - -import logging - -from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user -from django.core.management import BaseCommand - -from enterprise.models import EnterpriseCustomerUser - -LOGGER = logging.getLogger(__name__) - - -class Command(BaseCommand): - """ - Updates the username value for a given user. - - This is NOT MEANT for general use, and is specifically limited to Enterprise Users since - only they could potentially experience the issue of overwritten usernames. - - See ENT-832 for details on the bug that modified usernames for some Enterprise Users. - """ - help = 'Update the username of a given user.' - - def add_arguments(self, parser): - parser.add_argument( - '-u', - '--user_id', - action='store', - dest='user_id', - default=None, - help='The ID of the user to update.' - ) - - parser.add_argument( - '-n', - '--new_username', - action='store', - dest='new_username', - default=None, - help='The username value to set for the user.' - ) - - def handle(self, *args, **options): - user_id = options.get('user_id') - new_username = options.get('new_username') - - try: - EnterpriseCustomerUser.objects.get(user_id=user_id) - except EnterpriseCustomerUser.DoesNotExist: - LOGGER.info(f'User {user_id} must be an Enterprise User.') - return - - user = User.objects.get(id=user_id) - user.username = new_username - user.save() - - LOGGER.info(f'User {user_id} has been updated with username {new_username}.') diff --git a/common/djangoapps/student/management/tests/test_change_enterprise_user_username.py b/common/djangoapps/student/management/tests/test_change_enterprise_user_username.py deleted file mode 100644 index a1354feab555..000000000000 --- a/common/djangoapps/student/management/tests/test_change_enterprise_user_username.py +++ /dev/null @@ -1,67 +0,0 @@ -""" -Tests for the django management command `change_enterprise_user_username`. -""" - - -from unittest import mock -from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user -from django.contrib.sites.models import Site -from django.core.management import call_command -from django.db.models.signals import post_save -from django.test import TestCase -from enterprise.models import EnterpriseCustomer, EnterpriseCustomerUser -from pytest import mark - -from common.djangoapps.student.tests.factories import UserFactory - - -@mark.django_db -class ChangeEnterpriseUserUsernameCommandTests(TestCase): - """ - Test command `change_enterprise_user_username`. - """ - command = 'change_enterprise_user_username' - - @mock.patch('common.djangoapps.student.management.commands.change_enterprise_user_username.LOGGER') - def test_user_not_enterprise(self, logger_mock): - """ - Test that the command does not update a user's username if it is not linked to an Enterprise. - """ - user = UserFactory.create(is_active=True, username='old_username', email='test@example.com') - new_username = 'new_username' - - post_save_handler = mock.MagicMock() - post_save.connect(post_save_handler, sender=User) - - call_command(self.command, user_id=user.id, new_username=new_username) - - logger_mock.info.assert_called_with(f'User {user.id} must be an Enterprise User.') - post_save_handler.assert_not_called() - - @mock.patch('common.djangoapps.student.management.commands.change_enterprise_user_username.LOGGER') - def test_username_updated_successfully(self, logger_mock): - """ - Test that the command updates the user's username when the user is linked to an Enterprise. - """ - user = UserFactory.create(is_active=True, username='old_username', email='test@example.com') - site, _ = Site.objects.get_or_create(domain='example.com') - enterprise_customer = EnterpriseCustomer.objects.create( - name='Test EnterpriseCustomer', - site=site - ) - EnterpriseCustomerUser.objects.create( - user_id=user.id, - enterprise_customer=enterprise_customer - ) - new_username = 'new_username' - - post_save_handler = mock.MagicMock() - post_save.connect(post_save_handler, sender=User) - - call_command(self.command, user_id=user.id, new_username=new_username) - - logger_mock.info.assert_called_with(f'User {user.id} has been updated with username {new_username}.') - post_save_handler.assert_called() - - updated_user = User.objects.get(id=user.id) - assert updated_user.username == new_username