Exemple #1
0
    def test_initialize_connection(self) -> None:
        # Test the new connection case
        with mock.patch.object(EmailBackend, "open", return_value=True):
            backend = initialize_connection(None)
            self.assertTrue(isinstance(backend, EmailBackend))

        backend = mock.MagicMock(spec=SMTPBackend)
        backend.connection = mock.MagicMock(spec=SMTP)

        self.assertTrue(isinstance(backend, SMTPBackend))

        # Test the old connection case when it is still open
        backend.open.return_value = False
        backend.connection.noop.return_value = [250]
        initialize_connection(backend)
        self.assertEqual(backend.open.call_count, 1)
        self.assertEqual(backend.connection.noop.call_count, 1)

        # Test the old connection case when it was closed by the server
        backend.connection.noop.return_value = [404]
        backend.close.return_value = False
        initialize_connection(backend)
        # 2 more calls to open, 1 more call to noop and 1 call to close
        self.assertEqual(backend.open.call_count, 3)
        self.assertEqual(backend.connection.noop.call_count, 2)
        self.assertEqual(backend.close.call_count, 1)

        # Test backoff procedure
        backend.open.side_effect = OSError
        with self.assertRaises(OSError):
            initialize_connection(backend)
        # 3 more calls to open as we try 3 times before giving up
        self.assertEqual(backend.open.call_count, 6)
Exemple #2
0
 def send_email(self, event: Dict[str, Any]) -> None:
     # Copy the event, so that we don't pass the `failed_tries'
     # data to send_email (which neither takes that
     # argument nor needs that data).
     copied_event = copy.deepcopy(event)
     if "failed_tries" in copied_event:
         del copied_event["failed_tries"]
     handle_send_email_format_changes(copied_event)
     self.connection = initialize_connection(self.connection)
     send_email(**copied_event, connection=self.connection)
Exemple #3
0
 def __init__(self) -> None:
     super().__init__()
     self.connection: EmailBackend = initialize_connection(None)