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
This commit is contained in:
Zainab Amir
2021-09-08 10:44:54 +05:00
committed by GitHub
parent d25ace50b4
commit bf76fb3f7f
3 changed files with 27 additions and 16 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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):