Permalink
Browse files

Merge pull request #1136 from AdrianGaudebert/stage

Fixes bug 848458 - Fixed issues with invalid email addresses and unicode...
  • Loading branch information...
2 parents 37ee07c + 8076943 commit a504251e7bb1e8c0cf12073fcea5986bfd73b926 @lonnen lonnen committed Mar 13, 2013
Showing with 60 additions and 12 deletions.
  1. +16 −5 socorro/cron/jobs/automatic_emails.py
  2. +44 −7 socorro/unittest/cron/jobs/test_automatic_emails.py
@@ -3,6 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import datetime
+from email.utils import parseaddr
from configman import Namespace
@@ -128,13 +129,24 @@ def run(self, connection, run_datetime):
def send_email(self, report):
logger = self.config.logger
list_service = self.email_service.list()
- email = report['email'].strip()
if self.config.test_mode:
email = self.config.test_email_address
-
- if not email:
- # Don't send anything to empty email addresses
+ else:
+ try:
+ email = report['email'].decode('utf-8')
+ except UnicodeDecodeError:
+ # we are not able to send emails to addresses containing
+ # non-UTF-8 characters, thus filtering them out
+ return
+ # In case the email field contains a string like
+ # `Bob <bob@example.com>` then we only want the
+ # `bob@example.com` part.
+ email = parseaddr(email)[1]
+
+ if not (email.count('@') == 1 and email.split('@')[1].count('.') >= 1):
+ # this does not look like an email address, we don't try to send
+ # anything to it
return
try:
@@ -161,7 +173,6 @@ def send_email(self, report):
email, str(error_msg), exc_info=True
)
-
def update_user(self, report, sending_datetime, connection):
cursor = connection.cursor()
sql_params = {
@@ -39,7 +39,7 @@ def setUp(self):
'%(now)s'
), (
'2',
- 'quidam@example.com',
+ '"Quidam" <quidam@example.com>',
'WaterWolf',
'20.0',
'Release',
@@ -93,6 +93,13 @@ def setUp(self):
'1.0',
'Nightly',
'%(now)s'
+ ), (
+ '18',
+ 'z\xc3\x80drian@example.org',
+ 'WaterWolf',
+ '20.0',
+ 'Release',
+ '%(now)s'
)
""" % {'now': now})
@@ -145,13 +152,41 @@ def setUp(self):
)
""" % {'now': now, 'last_month': last_month})
+ # Finally some invalid email addresses
+ cursor.execute("""
+ INSERT INTO reports
+ (uuid, email, product, version, release_channel, date_processed)
+ VALUES (
+ '15',
+ ' ',
+ 'WaterWolf',
+ '20.0',
+ 'Release',
+ '%(now)s'
+ ), (
+ '16',
+ 'invalid@email',
+ 'WaterWolf',
+ '20.0',
+ 'Release',
+ '%(now)s'
+ ), (
+ '17',
+ 'i.do.not.work',
+ 'WaterWolf',
+ '20.0',
+ 'Release',
+ '%(now)s'
+ )
+ """ % {'now': now})
+
cursor.execute("""
INSERT INTO emails (email, last_sending)
VALUES (
'someone@example.com',
'%(last_month)s'
), (
- 'quidam@example.com',
+ '"Quidam" <quidam@example.com>',
'%(last_month)s'
), (
'menime@example.com',
@@ -248,13 +283,15 @@ def test_cron_job(self, exacttarget_mock):
assert information['automatic-emails']
assert not information['automatic-emails']['last_error']
assert information['automatic-emails']['last_success']
- self.assertEqual(et_mock.trigger_send.call_count, 3)
+ self.assertEqual(et_mock.trigger_send.call_count, 4)
+
+ last_email = u'z\xc0drian@example.org'
# Verify the last call to trigger_send
fields = {
- 'EMAIL_ADDRESS_': 'someone@example.com',
+ 'EMAIL_ADDRESS_': last_email,
'EMAIL_FORMAT_': 'H',
- 'TOKEN': 'someone@example.com'
+ 'TOKEN': last_email
}
et_mock.trigger_send.assert_called_with('socorro_dev_test', fields)
@@ -263,7 +300,7 @@ def test_cron_job(self, exacttarget_mock):
cursor = self.conn.cursor()
emails_list = (
'someone@example.com',
- 'quidam@example.com',
+ '"Quidam" <quidam@example.com>',
'anotherone@example.com'
)
sql = """
@@ -287,7 +324,7 @@ def test_run(self, exacttarget_mock):
job.run(self.conn, utc_now())
et_mock = exacttarget_mock.return_value
- self.assertEqual(et_mock.trigger_send.call_count, 3)
+ self.assertEqual(et_mock.trigger_send.call_count, 4)
@mock.patch('socorro.external.exacttarget.exacttarget.ExactTarget')
def test_send_email(self, exacttarget_mock):

0 comments on commit a504251

Please sign in to comment.