Changeset - 88a5fb51d837
[Not reviewed]
default
0 2 0
Thomas De Schampheleire - 10 years ago 2015-08-02 21:25:05
thomas.de.schampheleire@gmail.com
e-mail: properly handle no recipients when there is no email_to set

When the configuration file does not contain a value for email_to, and no
recipients are specified in a call to send_email, recipients would be set to
[None, admins]
which causes an error when logging this list as ' '.join(recipients).
2 files changed with 32 insertions and 3 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/celerylib/tasks.py
Show inline comments
 
@@ -267,9 +267,16 @@ def send_email(recipients, subject, body
 

	
 
    if not recipients:
 
        # if recipients are not defined we send to email_config + all admins
 
        admins = [u.email for u in User.query()
 
                  .filter(User.admin == True).all()]
 
        recipients = [email_config.get('email_to')] + admins
 
        recipients = [u.email for u in User.query()
 
                      .filter(User.admin == True).all()]
 
        if email_config.get('email_to') is not None:
 
            recipients += [email_config.get('email_to')]
 

	
 
        # If there are still no recipients, there are no admins and no address
 
        # configured in email_to, so return.
 
        if not recipients:
 
            log.error("No recipients specified and no fallback available.")
 
            return False
 

	
 
        log.warning("No recipients specified for '%s' - sending to admins %s", subject, ' '.join(recipients))
 

	
kallithea/tests/other/test_mail.py
Show inline comments
 
@@ -68,3 +68,25 @@ class TestMail(BaseTestCase):
 
        self.assertIn('Subject: %s' % subject, smtplib_mock.lastmsg)
 
        self.assertIn(body, smtplib_mock.lastmsg)
 
        self.assertIn(html_body, smtplib_mock.lastmsg)
 

	
 
    def test_send_mail_no_recipients_no_email_to(self):
 
        mailserver = 'smtp.mailserver.org'
 
        recipients = []
 
        envelope_from = 'noreply@mailserver.org'
 
        subject = 'subject'
 
        body = 'body'
 
        html_body = 'html_body'
 

	
 
        config_mock = {
 
                'smtp_server': mailserver,
 
                'app_email_from': envelope_from,
 
        }
 
        with mock.patch('kallithea.lib.celerylib.tasks.config', config_mock):
 
            kallithea.lib.celerylib.tasks.send_email(recipients, subject, body, html_body)
 

	
 
        self.assertSetEqual(smtplib_mock.lastdest, set([TEST_USER_ADMIN_EMAIL]))
 
        self.assertEqual(smtplib_mock.lastsender, envelope_from)
 
        self.assertIn('From: %s' % envelope_from, smtplib_mock.lastmsg)
 
        self.assertIn('Subject: %s' % subject, smtplib_mock.lastmsg)
 
        self.assertIn(body, smtplib_mock.lastmsg)
 
        self.assertIn(html_body, smtplib_mock.lastmsg)
0 comments (0 inline, 0 general)