healthchecks/hc/lib/tests/test_emails.py
Pēteris Caune fca600659d
Improve hc.lib.emails.send()
- add optional `from_email` argument
- add test cases that exercise the retry loop
2021-08-03 19:02:25 +03:00

28 lines
780 B
Python

from smtplib import SMTPServerDisconnected
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)