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)
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)
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'])
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'])
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)
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)
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()))
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)
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'])
def testMultipleRecipients(self): """Check multiple recipients behavior.""" msg = alerts.CreateEmail('subj', ['fake1@localhost', 'fake2@localhost']) self.assertEqual('fake1@localhost, fake2@localhost', msg['To'])
def testNoRecipients(self): """Check empty recipients behavior.""" msg = alerts.CreateEmail('subj', []) self.assertIsNone(msg)