You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

36 lines
1.0 KiB

from smtplib import SMTPServerDisconnected, SMTPDataError
from unittest.mock import Mock, patch
from django.test import TestCase
from hc.lib.emails import EmailThread
@patch("hc.lib.emails.time.sleep")
class EmailsTestCase(TestCase):
def test_it_retries(self, mock_time):
mock_msg = Mock()
mock_msg.send = Mock(side_effect=[SMTPServerDisconnected, None])
t = EmailThread(mock_msg)
t.run()
self.assertEqual(mock_msg.send.call_count, 2)
def test_it_limits_retries(self, mock_time):
mock_msg = Mock()
mock_msg.send = Mock(side_effect=SMTPServerDisconnected)
with self.assertRaises(SMTPServerDisconnected):
t = EmailThread(mock_msg)
t.run()
self.assertEqual(mock_msg.send.call_count, 3)
def test_it_retries_smtp_data_error(self, mock_time):
mock_msg = Mock()
mock_msg.send = Mock(side_effect=[SMTPDataError(454, "hello"), None])
t = EmailThread(mock_msg)
t.run()
self.assertEqual(mock_msg.send.call_count, 2)