Skip to content

Commit

Permalink
Bug 814647 - Fixed small issues with automatic emails, added an impor…
Browse files Browse the repository at this point in the history
…tant unit test.
  • Loading branch information
adngdb committed Jan 24, 2013
1 parent 44534e7 commit f70ad80
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 4 deletions.
14 changes: 10 additions & 4 deletions socorro/cron/jobs/automatic_emails.py
Expand Up @@ -20,7 +20,7 @@ def string_to_list(input_str):
FROM reports r
LEFT JOIN emails e ON r.email = e.email
WHERE r.date_processed > %(start_date)s
AND r.date_processed < %(end_date)s
AND r.date_processed <= %(end_date)s
AND r.email IS NOT NULL
AND (e.last_sending < %(delayed_date)s OR e.last_sending IS NULL)
AND r.product IN %(products)s
Expand Down Expand Up @@ -106,6 +106,10 @@ def run(self, connection, run_datetime):
logger = self.config.logger
cursor = connection.cursor()

if self.config.test_mode:
logger.warning('You are running Automatic Emails cron app '
'in test mode')

delay = datetime.timedelta(days=self.config.delay_between_emails)
sql_params = {
'start_date': run_datetime - datetime.timedelta(hours=1),
Expand All @@ -119,18 +123,20 @@ def run(self, connection, run_datetime):
report = dict(zip(SQL_FIELDS, row))
self.send_email(report)
self.update_user(report, utc_now(), connection)
logger.info('Automatic Email sent to %s', report['email'])
#logger.info('Automatic Email sent to %s', report['email'])

def send_email(self, report):
logger = self.config.logger
list_service = self.email_service.list()
email = report['email']

if self.config.test_mode:
logger.warning('You are running Automatic Emails cron app '
'in test mode')
email = self.config.test_email_address

if not email:
# Don't send anything to empty email addresses
return

try:
subscriber = list_service.get_subscriber(
email,
Expand Down
111 changes: 111 additions & 0 deletions socorro/unittest/cron/jobs/test_automatic_emails.py
Expand Up @@ -86,6 +86,13 @@ def setUp(self):
'1.0',
'Nightly',
'%(now)s'
), (
'9',
'me@my.name',
'EarthRaccoon',
'1.0',
'Nightly',
'%(now)s'
)
""" % {'now': now})

Expand Down Expand Up @@ -457,3 +464,107 @@ def trigger_send(template, fields):
'd@example.org',
'e@example.org'
])

@mock.patch('socorro.external.exacttarget.exacttarget.ExactTarget')
def test_email_after_delay(self, exacttarget_mock):
"""Test that a user will receive an email if he or she sends us a new
crash report after the delay is passed (but not before). """
(config_manager, json_file) = self._setup_config_manager(
delay_between_emails=1,
restrict_products=['EarthRaccoon']
)
list_service_mock = exacttarget_mock.return_value.list.return_value
list_service_mock.get_subscriber.return_value = {
'token': 'me@my.name'
}
trigger_send_mock = exacttarget_mock.return_value.trigger_send
tomorrow = utc_now() + datetime.timedelta(days=1, hours=2)
twohourslater = utc_now() + datetime.timedelta(hours=2)

with config_manager.context() as config:
# 1. Send an email to the user and update emailing data
tab = crontabber.CronTabber(config)
tab.run_all()

information = json.load(open(json_file))
assert information['automatic-emails']
assert not information['automatic-emails']['last_error']
assert information['automatic-emails']['last_success']

exacttarget_mock.return_value.trigger_send.assert_called_with(
'socorro_dev_test',
{
'EMAIL_ADDRESS_': 'me@my.name',
'EMAIL_FORMAT_': 'H',
'TOKEN': 'me@my.name'
}
)
self.assertEqual(trigger_send_mock.call_count, 1)

# 2. Test that before 'delay' is passed user doesn't receive
# another email

# Insert a new crash report with the same email address
cursor = self.conn.cursor()
cursor.execute("""
INSERT INTO reports
(uuid, email, product, version, release_channel, date_processed)
VALUES (
'50',
'me@my.name',
'EarthRaccoon',
'20.0',
'Release',
'%(onehourlater)s'
)
""" % {'onehourlater': utc_now() + datetime.timedelta(hours=1)})
self.conn.commit()

# Run crontabber with time pushed by two hours
with mock.patch('socorro.cron.crontabber.utc_now') as cronutc_mock:
with mock.patch('socorro.cron.base.utc_now') as baseutc_mock:
cronutc_mock.return_value = twohourslater
baseutc_mock.return_value = twohourslater
tab.run_all()

information = json.load(open(json_file))
assert information['automatic-emails']
assert not information['automatic-emails']['last_error']
assert information['automatic-emails']['last_success']

# No new email was sent
self.assertEqual(trigger_send_mock.call_count, 1)

# 3. Verify that, after 'delay' is passed, a new email is sent
# to our user

# Insert a new crash report with the same email address
cursor = self.conn.cursor()
cursor.execute("""
INSERT INTO reports
(uuid, email, product, version, release_channel, date_processed)
VALUES (
'51',
'me@my.name',
'EarthRaccoon',
'20.0',
'Release',
'%(tomorrow)s'
)
""" % {'tomorrow': utc_now() + datetime.timedelta(days=1)})
self.conn.commit()

# Run crontabber with time pushed by a day
with mock.patch('socorro.cron.crontabber.utc_now') as cronutc_mock:
with mock.patch('socorro.cron.base.utc_now') as baseutc_mock:
cronutc_mock.return_value = tomorrow
baseutc_mock.return_value = tomorrow
tab.run_all()

information = json.load(open(json_file))
assert information['automatic-emails']
assert not information['automatic-emails']['last_error']
assert information['automatic-emails']['last_success']

# A new email was sent
self.assertEqual(trigger_send_mock.call_count, 2)

0 comments on commit f70ad80

Please sign in to comment.