Exemple #1
0
    def test_send_email(self, mock_log):
        """
        Tests retries when the activation email doesn't send
        """
        from_address = '*****@*****.**'
        email_max_attempts = settings.RETRY_ACTIVATION_EMAIL_MAX_ATTEMPTS

        # pylint: disable=no-member
        send_activation_email.delay('Task_test', 'Task_test_message', from_address, self.student.email)

        # Asserts sending email retry logging.
        for attempt in range(email_max_attempts):
            mock_log.info.assert_any_call(
                'Retrying sending email to user {dest_addr}, attempt # {attempt} of {max_attempts}'.format(
                    dest_addr=self.student.email,
                    attempt=attempt,
                    max_attempts=email_max_attempts
                ))
        self.assertEquals(mock_log.info.call_count, 6)

        # Asserts that the error was logged on crossing max retry attempts.
        mock_log.error.assert_called_with(
            'Unable to send activation email to user from "%s" to "%s"',
            from_address,
            self.student.email,
            exc_info=True
        )
        self.assertEquals(mock_log.error.call_count, 1)
def compose_and_send_activation_email(user, profile, user_registration=None):
    """
    Construct all the required params and send the activation email
    through celery task

    Arguments:
        user: current logged-in user
        profile: profile object of the current logged-in user
        user_registration: registration of the current logged-in user
    """
    dest_addr = user.email
    if user_registration is None:
        user_registration = Registration.objects.get(user=user)
    context = generate_activation_email_context(user, user_registration)
    subject = render_to_string('emails/activation_email_subject.txt', context)
    # Email subject *must not* contain newlines
    subject = ''.join(subject.splitlines())
    message_for_activation = render_to_string('emails/activation_email.txt',
                                              context)
    from_address = configuration_helpers.get_value('email_from_address',
                                                   settings.DEFAULT_FROM_EMAIL)
    from_address = configuration_helpers.get_value(
        'ACTIVATION_EMAIL_FROM_ADDRESS', from_address)
    if settings.FEATURES.get('REROUTE_ACTIVATION_EMAIL'):
        dest_addr = settings.FEATURES['REROUTE_ACTIVATION_EMAIL']
        message_for_activation = ("Activation for %s (%s): %s\n" %
                                  (user, user.email, profile.name) + '-' * 80 +
                                  '\n\n' + message_for_activation)
    send_activation_email.delay(subject, message_for_activation, from_address,
                                dest_addr)
Exemple #3
0
def compose_and_send_activation_email(user, profile, user_registration=None):
    """
    Construct all the required params and send the activation email
    through celery task

    Arguments:
        user: current logged-in user
        profile: profile object of the current logged-in user
        user_registration: registration of the current logged-in user
    """
    dest_addr = user.email
    if user_registration is None:
        user_registration = Registration.objects.get(user=user)
    context = generate_activation_email_context(user, user_registration)
    subject = render_to_string('emails/activation_email_subject.txt', context)
    # Email subject *must not* contain newlines
    subject = ''.join(subject.splitlines())
    message_for_activation = render_to_string('emails/activation_email.txt', context)
    from_address = configuration_helpers.get_value('email_from_address', settings.DEFAULT_FROM_EMAIL)
    from_address = configuration_helpers.get_value('ACTIVATION_EMAIL_FROM_ADDRESS', from_address)
    if settings.FEATURES.get('REROUTE_ACTIVATION_EMAIL'):
        dest_addr = settings.FEATURES['REROUTE_ACTIVATION_EMAIL']
        message_for_activation = ("Activation for %s (%s): %s\n" % (user, user.email, profile.name) +
                                  '-' * 80 + '\n\n' + message_for_activation)
    send_activation_email.delay(subject, message_for_activation, from_address, dest_addr)
def compose_and_send_activation_email(user, profile, user_registration=None):
    """
    Construct all the required params and send the activation email
    through celery task

    Arguments:
        user: current logged-in user
        profile: profile object of the current logged-in user
        user_registration: registration of the current logged-in user
    """
    route_enabled = settings.FEATURES.get('REROUTE_ACTIVATION_EMAIL')

    root_url = configuration_helpers.get_value('LMS_ROOT_URL', settings.LMS_ROOT_URL)
    msg = compose_activation_email(root_url, user, user_registration, route_enabled, profile.name)

    send_activation_email.delay(str(msg))
 def test_UnrecoverableSendError(self, mock_log):
     """
     Tests that a major failure of the send is logged
     """
     from_address = '*****@*****.**'
     send_activation_email.delay(self.msg, from_address=from_address)
     # Asserts that the error was logged
     mock_log.exception.assert_called_with(
         'Unable to send activation email to user from "%s" to "%s"',
         from_address,
         self.student.email,
         exc_info=True
     )
     # Assert that nothing else was logged
     self.assertEquals(mock_log.info.call_count, 0)
     self.assertEquals(mock_log.error.call_count, 0)
     self.assertEquals(mock_log.exception.call_count, 1)
    def test_UnrecoverableSendError(self, mock_log):
        """
        Tests that a major failure of the send is logged
        """
        from_address = '*****@*****.**'

        send_activation_email.delay(str(self.msg), from_address=from_address)

        # Asserts that the error was logged
        mock_log.exception.assert_called_with(
            'Unable to send activation email to user from "%s" to "%s"',
            from_address,
            self.student.email,
        )

        # Assert that nothing else was logged
        self.assertEqual(mock_log.info.call_count, 0)
        self.assertEqual(mock_log.error.call_count, 0)
        self.assertEqual(mock_log.exception.call_count, 1)