Ejemplo n.º 1
0
 def testValidCache(self):
   """Test valid cache."""
   osutils.WriteFile(self.token_cache_file, json.dumps(self.FAKE_CACHE))
   msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake msg')
   server = alerts.GmailServer(token_cache_file=self.token_cache_file)
   ret = server.Send(msg)
   self.assertTrue(ret)
Ejemplo n.º 2
0
 def testBasic(self):
   """Basic sanity check."""
   msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake message')
   server = alerts.SmtpServer(('localhost', 1))
   ret = server.Send(msg)
   self.assertTrue(ret)
   self.assertEqual(self.smtp_mock.call_count, 1)
Ejemplo n.º 3
0
 def testBasic(self):
   """Check default basic call."""
   msg = alerts.CreateEmail('subj', ['fake@localhost'])
   self.assertIsNotNone(msg)
   self.assertNotEqual('', msg['From'])
   self.assertEqual('subj', msg['Subject'])
   self.assertEqual('fake@localhost', msg['To'])
Ejemplo n.º 4
0
 def testExtraFields(self):
   """Check extra fields are added correctly."""
   msg = alerts.CreateEmail('subj', ['fake@localhost'],
                            extra_fields={'X-Hi': 'bye', 'field': 'data'})
   self.assertEqual('subj', msg['Subject'])
   self.assertEqual('bye', msg['X-Hi'])
   self.assertEqual('data', msg['field'])
Ejemplo n.º 5
0
 def testCacheNotExistsTokenNotExists(self):
   """Test cache not exists, token not exists."""
   msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake msg')
   server = alerts.GmailServer(token_cache_file=self.token_cache_file,
                               token_json_file=self.token_json_file)
   ret = server.Send(msg)
   self.assertFalse(ret)
Ejemplo n.º 6
0
 def testRetryException(self):
   """Verify we try sending multiple times & don't abort socket.error."""
   self.smtp_mock.side_effect = socket.error('test fail')
   msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake message')
   server = alerts.SmtpServer(('localhost', 1))
   ret = server.Send(msg)
   self.assertFalse(ret)
   self.assertEqual(self.smtp_mock.call_count, 4)
Ejemplo n.º 7
0
 def testAttachment(self):
     """Check attachment behavior."""
     msg = alerts.CreateEmail('subj', ['fake@localhost'], attachment='blah')
     # Make sure there's a payload in there somewhere.
     self.assertTrue(
         any(
             isinstance(x, email.mime.application.MIMEApplication)
             for x in msg.get_payload()))
Ejemplo n.º 8
0
 def testCacheNotExistsTokenExists(self):
   """Test cache not exists, token exists"""
   osutils.WriteFile(self.token_json_file, json.dumps(self.FAKE_TOKEN_JSON))
   msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake msg')
   server = alerts.GmailServer(token_cache_file=self.token_cache_file,
                               token_json_file=self.token_json_file)
   ret = server.Send(msg)
   self.assertTrue(ret)
   # Cache file should be auto-generated.
   self.assertExists(self.token_cache_file)
Ejemplo n.º 9
0
 def testCacheInvalidTokenNotExists(self):
   """Test cache exists but invalid, token not exists."""
   invalid_cache = self.FAKE_CACHE.copy()
   invalid_cache['invalid'] = True
   osutils.WriteFile(self.token_cache_file, json.dumps(invalid_cache))
   msg = alerts.CreateEmail('fake subject', 'fake@localhost', 'fake msg')
   server = alerts.GmailServer(token_cache_file=self.token_cache_file,
                               token_json_file=self.token_json_file)
   ret = server.Send(msg)
   self.assertFalse(ret)
   invalid_cache = json.loads(osutils.ReadFile(self.token_cache_file))
   self.assertTrue(invalid_cache['invalid'])
Ejemplo n.º 10
0
 def testMultipleRecipients(self):
   """Check multiple recipients behavior."""
   msg = alerts.CreateEmail('subj', ['fake1@localhost', 'fake2@localhost'])
   self.assertEqual('fake1@localhost, fake2@localhost', msg['To'])
Ejemplo n.º 11
0
 def testNoRecipients(self):
   """Check empty recipients behavior."""
   msg = alerts.CreateEmail('subj', [])
   self.assertIsNone(msg)