1 # Copyright 2015 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import unittest 6 7 import webapp2 8 import webtest 9 10 from dashboard import send_stoppage_alert_emails 11 from dashboard import testing_common 12 from dashboard import utils 13 from dashboard.models import graph_data 14 from dashboard.models import sheriff 15 from dashboard.models import stoppage_alert 16 17 18 class EmailSummaryTest(testing_common.TestCase): 19 20 def setUp(self): 21 super(EmailSummaryTest, self).setUp() 22 app = webapp2.WSGIApplication( 23 [('/send_stoppage_alert_emails', 24 send_stoppage_alert_emails.SendStoppageAlertEmailsHandler)]) 25 self.testapp = webtest.TestApp(app) 26 27 def _AddSampleData(self): 28 """Puts a Test and Row in the datastore and returns the entities.""" 29 testing_common.AddTests( 30 ['M'], ['b'], {'suite': {'foo': {}, 'bar': {}, 'baz': {}}}) 31 sheriff.Sheriff( 32 email='foo (at] chromium.org', id='Foo', patterns=['*/*/*/*'], 33 stoppage_alert_delay=3).put() 34 for name in ('foo', 'bar', 'baz'): 35 test_path = 'M/b/suite/%s' % name 36 testing_common.AddRows(test_path, {100}) 37 38 def testGet_ThreeAlertsOneSheriff_EmailSent(self): 39 self._AddSampleData() 40 for name in ('foo', 'bar', 'baz'): 41 test = utils.TestKey('M/b/suite/%s' % name).get() 42 row = graph_data.Row.query(graph_data.Row.parent_test == test.key).get() 43 stoppage_alert.CreateStoppageAlert(test, row).put() 44 self.testapp.get('/send_stoppage_alert_emails') 45 messages = self.mail_stub.get_sent_messages() 46 self.assertEqual(1, len(messages)) 47 self.assertEqual('gasper-alerts (at] google.com', messages[0].sender) 48 self.assertEqual('foo (at] chromium.org', messages[0].to) 49 self.assertIn('3', messages[0].subject) 50 self.assertIn('foo', str(messages[0].body)) 51 self.assertIn('bar', str(messages[0].body)) 52 self.assertIn('baz', str(messages[0].body)) 53 stoppage_alerts = stoppage_alert.StoppageAlert.query().fetch() 54 for alert in stoppage_alerts: 55 self.assertTrue(alert.mail_sent) 56 57 def testGet_NoAlerts_EmailSent(self): 58 self.testapp.get('/send_stoppage_alert_emails') 59 messages = self.mail_stub.get_sent_messages() 60 self.assertEqual(0, len(messages)) 61 62 63 if __name__ == '__main__': 64 unittest.main() 65