Mercurial > kallithea
changeset 5354:1339fd56f302
e-mail: revive dead code that checks for unspecified recipients
Commit 609e06b6c52f6a8ea9581372805c4bbb60db81a1 introduced dead code:
in the beginning of send_email an assert verifies that recipients is of type
'list', so checking it for None later is useless and that branch can never
be reached.
Instead of removing the dead code, revive it and add a test case.
author | Thomas De Schampheleire <thomas.de.schampheleire@gmail.com> |
---|---|
date | Sun, 02 Aug 2015 21:17:14 +0200 |
parents | 544aa933aea7 |
children | 88a5fb51d837 |
files | kallithea/lib/celerylib/tasks.py kallithea/tests/other/test_mail.py |
diffstat | 2 files changed, 28 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/kallithea/lib/celerylib/tasks.py Sun Aug 02 20:46:17 2015 +0200 +++ b/kallithea/lib/celerylib/tasks.py Sun Aug 02 21:17:14 2015 +0200 @@ -264,15 +264,14 @@ email_prefix = email_config.get('email_prefix', '') if email_prefix: subject = "%s %s" % (email_prefix, subject) - if recipients is None: + + 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 - log.warning("recipients not specified for '%s' - sending to admins %s", subject, ' '.join(recipients)) - elif not recipients: - log.error("No recipients specified") - return False + + log.warning("No recipients specified for '%s' - sending to admins %s", subject, ' '.join(recipients)) mail_from = email_config.get('app_email_from', 'Kallithea') user = email_config.get('smtp_username')
--- a/kallithea/tests/other/test_mail.py Sun Aug 02 20:46:17 2015 +0200 +++ b/kallithea/tests/other/test_mail.py Sun Aug 02 21:17:14 2015 +0200 @@ -44,3 +44,27 @@ 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(self): + mailserver = 'smtp.mailserver.org' + recipients = [] + envelope_from = 'noreply@mailserver.org' + email_to = 'admin@mailserver.org' + subject = 'subject' + body = 'body' + html_body = 'html_body' + + config_mock = { + 'smtp_server': mailserver, + 'app_email_from': envelope_from, + 'email_to': email_to, + } + 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, email_to])) + 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)