From bf76fb3f7f1ef87771e2ff723c53c65c823bb54e Mon Sep 17 00:00:00 2001 From: Zainab Amir Date: Wed, 8 Sep 2021 10:44:54 +0500 Subject: [PATCH] move send_activation_email celery task (#28666) - moved send_activation_email to user_authn app - registered task under both new and old name - exposed the old name for task invocation VAN-417 --- common/djangoapps/student/views/management.py | 2 +- .../core/djangoapps/user_authn}/tasks.py | 19 ++++++++++++---- .../user_authn}/tests/test_tasks.py | 22 +++++++++---------- 3 files changed, 27 insertions(+), 16 deletions(-) rename {common/djangoapps/student => openedx/core/djangoapps/user_authn}/tasks.py (79%) rename {common/djangoapps/student => openedx/core/djangoapps/user_authn}/tests/test_tasks.py (87%) diff --git a/common/djangoapps/student/views/management.py b/common/djangoapps/student/views/management.py index d8da732f04..70232383ff 100644 --- a/common/djangoapps/student/views/management.py +++ b/common/djangoapps/student/views/management.py @@ -50,6 +50,7 @@ from openedx.core.djangoapps.programs.models import ProgramsApiConfig # lint-am from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.djangoapps.theming import helpers as theming_helpers from openedx.core.djangoapps.user_api.preferences import api as preferences_api +from openedx.core.djangoapps.user_authn.tasks import send_activation_email from openedx.core.djangoapps.user_authn.toggles import should_redirect_to_authn_microfrontend from openedx.core.djangolib.markup import HTML, Text from openedx.features.enterprise_support.utils import is_enterprise_learner @@ -71,7 +72,6 @@ from common.djangoapps.student.models import ( # lint-amnesty, pylint: disable= email_exists_or_retired ) from common.djangoapps.student.signals import REFUND_ORDER -from common.djangoapps.student.tasks import send_activation_email from common.djangoapps.util.db import outer_atomic from common.djangoapps.util.json_request import JsonResponse from xmodule.modulestore.django import modulestore diff --git a/common/djangoapps/student/tasks.py b/openedx/core/djangoapps/user_authn/tasks.py similarity index 79% rename from common/djangoapps/student/tasks.py rename to openedx/core/djangoapps/user_authn/tasks.py index cc166dbd9f..6a3500d46d 100644 --- a/common/djangoapps/student/tasks.py +++ b/openedx/core/djangoapps/user_authn/tasks.py @@ -4,9 +4,8 @@ This file contains celery tasks for sending email import logging - -from celery.exceptions import MaxRetriesExceededError from celery import shared_task +from celery.exceptions import MaxRetriesExceededError from django.conf import settings from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user from django.contrib.sites.models import Site @@ -14,15 +13,15 @@ from edx_ace import ace from edx_ace.errors import RecoverableChannelDeliveryError from edx_ace.message import Message from edx_django_utils.monitoring import set_code_owner_attribute + from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers from openedx.core.lib.celery.task_utils import emulate_http_request log = logging.getLogger('edx.celery.task') -@shared_task(bind=True) @set_code_owner_attribute -def send_activation_email(self, msg_string, from_address=None): +def _send_activation_email(self, msg_string, from_address=None): """ Sending an activation email to the user. """ @@ -67,3 +66,15 @@ def send_activation_email(self, msg_string, from_address=None): dest_addr, ) raise Exception # lint-amnesty, pylint: disable=raise-missing-from + + +_OLD_TASK_NAME = 'common.djangoapps.student.tasks.send_activation_email' +_NEW_TASK_NAME = 'openedx.core.djangoapps.user_authn.tasks.send_activation_email' + + +# Register task under both its old and new names, +# but expose only the old-named task for invocation. +# -> Next step: Once we deploy and teach Celery workers the new name, +# set `send_activation_email` to the new-named task. +send_activation_email = shared_task(bind=True, name=_OLD_TASK_NAME)(_send_activation_email) +shared_task(bind=True, name=_NEW_TASK_NAME)(_send_activation_email) diff --git a/common/djangoapps/student/tests/test_tasks.py b/openedx/core/djangoapps/user_authn/tests/test_tasks.py similarity index 87% rename from common/djangoapps/student/tests/test_tasks.py rename to openedx/core/djangoapps/user_authn/tests/test_tasks.py index dc8d387860..09a0a5ee0a 100644 --- a/common/djangoapps/student/tests/test_tasks.py +++ b/openedx/core/djangoapps/user_authn/tests/test_tasks.py @@ -3,16 +3,16 @@ Tests for the Sending activation email celery tasks """ -from unittest import mock from django.conf import settings from django.test import TestCase - from edx_ace.errors import ChannelError, RecoverableChannelDeliveryError +from unittest import mock + +from common.djangoapps.student.models import Registration +from common.djangoapps.student.views.management import compose_activation_email, compose_and_send_activation_email from lms.djangoapps.courseware.tests.factories import UserFactory from openedx.core.djangoapps.site_configuration.tests.factories import SiteConfigurationFactory, SiteFactory -from common.djangoapps.student.models import Registration -from common.djangoapps.student.tasks import send_activation_email -from common.djangoapps.student.views.management import compose_activation_email, compose_and_send_activation_email +from openedx.core.djangoapps.user_authn.tasks import send_activation_email class SendActivationEmailTestCase(TestCase): @@ -44,8 +44,8 @@ class SendActivationEmailTestCase(TestCase): assert self.msg.context['routed_profile_name'] == '' @mock.patch('time.sleep', mock.Mock(return_value=None)) - @mock.patch('common.djangoapps.student.tasks.log') - @mock.patch('common.djangoapps.student.tasks.ace.send', mock.Mock(side_effect=RecoverableChannelDeliveryError(None, None))) # lint-amnesty, pylint: disable=line-too-long + @mock.patch('openedx.core.djangoapps.user_authn.tasks.log') + @mock.patch('openedx.core.djangoapps.user_authn.tasks.ace.send', mock.Mock(side_effect=RecoverableChannelDeliveryError(None, None))) # lint-amnesty, pylint: disable=line-too-long def test_RetrySendUntilFail(self, mock_log): """ Tests retries when the activation email doesn't send @@ -74,8 +74,8 @@ class SendActivationEmailTestCase(TestCase): ) assert mock_log.error.call_count == 1 - @mock.patch('common.djangoapps.student.tasks.log') - @mock.patch('common.djangoapps.student.tasks.ace.send', mock.Mock(side_effect=ChannelError)) + @mock.patch('openedx.core.djangoapps.user_authn.tasks.log') + @mock.patch('openedx.core.djangoapps.user_authn.tasks.ace.send', mock.Mock(side_effect=ChannelError)) def test_UnrecoverableSendError(self, mock_log): """ Tests that a major failure of the send is logged @@ -96,8 +96,8 @@ class SendActivationEmailTestCase(TestCase): assert mock_log.error.call_count == 0 assert mock_log.exception.call_count == 1 - @mock.patch('common.djangoapps.student.tasks.log') - @mock.patch('common.djangoapps.student.tasks.ace.send', mock.Mock(side_effect=ChannelError)) + @mock.patch('openedx.core.djangoapps.user_authn.tasks.log') + @mock.patch('openedx.core.djangoapps.user_authn.tasks.ace.send', mock.Mock(side_effect=ChannelError)) @mock.patch('common.djangoapps.student.views.management.theming_helpers.get_current_site') @mock.patch('openedx.core.djangoapps.site_configuration.helpers.get_current_site_configuration') def test_from_address_in_send_email(self, mock_site_configuration, mock_get_current_site, mock_log):