Commit 0a1f5c40 by Awais Jibran Committed by GitHub

Merge pull request #13567 from edx/aj/fix-celery-email-sending-on-registration

Fix user registration 
parents 07af5211 10f6bd17
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
This file contains celery tasks for sending email This file contains celery tasks for sending email
""" """
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail from django.core import mail
from celery.task import task # pylint: disable=no-name-in-module, import-error from celery.task import task # pylint: disable=no-name-in-module, import-error
...@@ -12,10 +13,11 @@ log = get_task_logger(__name__) ...@@ -12,10 +13,11 @@ log = get_task_logger(__name__)
@task(bind=True) @task(bind=True)
def send_activation_email(self, user, subject, message, from_address): def send_activation_email(self, user_id, subject, message, from_address):
""" """
Sending an activation email to the users. Sending an activation email to the users.
""" """
user = User.objects.get(id=user_id)
max_retries = settings.RETRY_ACTIVATION_EMAIL_MAX_ATTEMPTS max_retries = settings.RETRY_ACTIVATION_EMAIL_MAX_ATTEMPTS
retries = self.request.retries + 1 retries = self.request.retries + 1
dest_addr = user.email dest_addr = user.email
......
...@@ -9,29 +9,36 @@ from django.conf import settings ...@@ -9,29 +9,36 @@ from django.conf import settings
from student.tasks import send_activation_email from student.tasks import send_activation_email
from boto.exception import NoAuthHandlerFound from boto.exception import NoAuthHandlerFound
from lms.djangoapps.courseware.tests.factories import UserFactory
class SendActivationEmailTestCase(TestCase): class SendActivationEmailTestCase(TestCase):
""" """
Test for send activation email to user Test for send activation email to user
""" """
def setUp(self):
""" Setup components used by each test."""
super(SendActivationEmailTestCase, self).setUp()
self.student = UserFactory()
@mock.patch('time.sleep', mock.Mock(return_value=None)) @mock.patch('time.sleep', mock.Mock(return_value=None))
@mock.patch('student.tasks.log') @mock.patch('student.tasks.log')
@mock.patch('django.contrib.auth.models.User') @mock.patch('django.contrib.auth.models.User.email_user', mock.Mock(side_effect=NoAuthHandlerFound))
def test_send_email(self, mock_user, mock_log): def test_send_email(self, mock_log):
""" """
Tests retries when the activation email doesn't send Tests retries when the activation email doesn't send
""" """
from_address = 'task_testing@edX.com' from_address = 'task_testing@edX.com'
mock_user.email_user.side_effect = NoAuthHandlerFound
email_max_attempts = settings.RETRY_ACTIVATION_EMAIL_MAX_ATTEMPTS + 1 email_max_attempts = settings.RETRY_ACTIVATION_EMAIL_MAX_ATTEMPTS + 1
send_activation_email.delay(mock_user, 'Task_test', 'Task_test_message', from_address) # pylint: disable=no-member
send_activation_email.delay(self.student.id, 'Task_test', 'Task_test_message', from_address)
# Asserts sending email retry logging. # Asserts sending email retry logging.
for attempt in xrange(1, email_max_attempts): for attempt in xrange(1, email_max_attempts):
mock_log.info.assert_any_call( mock_log.info.assert_any_call(
'Retrying sending email to user {dest_addr}, attempt # {attempt} of {max_attempts}'.format( 'Retrying sending email to user {dest_addr}, attempt # {attempt} of {max_attempts}'.format(
dest_addr=mock_user.email, dest_addr=self.student.email,
attempt=attempt, attempt=attempt,
max_attempts=email_max_attempts max_attempts=email_max_attempts
)) ))
...@@ -41,7 +48,7 @@ class SendActivationEmailTestCase(TestCase): ...@@ -41,7 +48,7 @@ class SendActivationEmailTestCase(TestCase):
mock_log.error.assert_called_with( mock_log.error.assert_called_with(
'Unable to send activation email to user from "%s" to "%s"', 'Unable to send activation email to user from "%s" to "%s"',
from_address, from_address,
mock_user.email, self.student.email,
exc_info=True exc_info=True
) )
self.assertEquals(mock_log.error.call_count, 1) self.assertEquals(mock_log.error.call_count, 1)
...@@ -1790,7 +1790,7 @@ def create_account_with_params(request, params): ...@@ -1790,7 +1790,7 @@ def create_account_with_params(request, params):
'email_from_address', 'email_from_address',
settings.DEFAULT_FROM_EMAIL settings.DEFAULT_FROM_EMAIL
) )
send_activation_email.delay(user, subject, message, from_address) send_activation_email.delay(user.id, subject, message, from_address)
else: else:
registration.activate() registration.activate()
_enroll_user_in_pending_courses(user) # Enroll student in any pending courses _enroll_user_in_pending_courses(user) # Enroll student in any pending courses
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment