diff --git a/common/djangoapps/student/management/commands/recover_account.py b/common/djangoapps/student/management/commands/recover_account.py index a2b11e7784..55cb3ea6d8 100644 --- a/common/djangoapps/student/management/commands/recover_account.py +++ b/common/djangoapps/student/management/commands/recover_account.py @@ -18,6 +18,7 @@ from edx_ace import ace from edx_ace.recipient import Recipient from common.djangoapps.student.models import AccountRecoveryConfiguration +from openedx.core.djangoapps.user_authn.toggles import should_redirect_to_authn_microfrontend from openedx.core.djangoapps.ace_common.template_context import get_base_template_context from openedx.core.djangoapps.lang_pref import LANGUAGE_KEY from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers @@ -104,12 +105,16 @@ class Command(BaseCommand): """ message_context = get_base_template_context(site) email = user.email + if should_redirect_to_authn_microfrontend(): + site_url = settings.AUTHN_MICROFRONTEND_URL + else: + site_url = configuration_helpers.get_value('SITE_NAME', settings.SITE_NAME) message_context.update({ 'email': email, 'platform_name': configuration_helpers.get_value('PLATFORM_NAME', settings.PLATFORM_NAME), - 'reset_link': '{protocol}://{site}{link}?track=pwreset'.format( + 'reset_link': '{protocol}://{site_url}{link}?track=pwreset'.format( protocol='http', - site=configuration_helpers.get_value('SITE_NAME', settings.SITE_NAME), + site_url=site_url, link=reverse('password_reset_confirm', kwargs={ 'uidb36': int_to_base36(user.id), 'token': default_token_generator.make_token(user), diff --git a/common/djangoapps/student/management/tests/test_recover_account.py b/common/djangoapps/student/management/tests/test_recover_account.py index 15a0770fd2..cb7333f8fe 100644 --- a/common/djangoapps/student/management/tests/test_recover_account.py +++ b/common/djangoapps/student/management/tests/test_recover_account.py @@ -7,17 +7,24 @@ import pytest import six from django.core import mail +from django.conf import settings from django.contrib.auth import get_user_model from django.core.management import call_command, CommandError from django.core.files.uploadedfile import SimpleUploadedFile -from django.test import TestCase, RequestFactory +from django.test import TestCase, RequestFactory, override_settings + +from edx_toggles.toggles.testutils import override_waffle_flag from testfixtures import LogCapture from common.djangoapps.student.tests.factories import UserFactory - from common.djangoapps.student.models import AccountRecoveryConfiguration +from openedx.core.djangolib.testing.utils import skip_unless_lms +from openedx.core.djangoapps.user_authn.toggles import REDIRECT_TO_AUTHN_MICROFRONTEND + LOGGER_NAME = 'common.djangoapps.student.management.commands.recover_account' +FEATURES_WITH_AUTHN_MFE_ENABLED = settings.FEATURES.copy() +FEATURES_WITH_AUTHN_MFE_ENABLED['ENABLE_AUTHN_MICROFRONTEND'] = True class RecoverAccountTests(TestCase): @@ -65,6 +72,24 @@ class RecoverAccountTests(TestCase): # try to login with previous password assert not self.client.login(username=self.user.username, password='password') + @override_settings(FEATURES=FEATURES_WITH_AUTHN_MFE_ENABLED) + @override_waffle_flag(REDIRECT_TO_AUTHN_MICROFRONTEND, active=True) + @skip_unless_lms + def test_authn_mfe_url_in_reset_link(self): + """ + send password reset link to learner with authn mfe. + :return: + """ + + with NamedTemporaryFile() as csv: + csv = self._write_test_csv(csv, lines=['amy,amy@edx.com,amy@newemail.com\n']) + call_command("recover_account", "--csv_file_path={}".format(csv.name)) + + assert len(mail.outbox) == 1 + + authn_mfe_url = re.findall(settings.AUTHN_MICROFRONTEND_URL, mail.outbox[0].body)[0] + self.assertEqual(authn_mfe_url, settings.AUTHN_MICROFRONTEND_URL) + def test_file_not_found_error(self): """ Test command error raised when csv path is invalid