def connect(self): """Open asyncio streams to the server and check response status. """ try: self.reader, self.writer = yield from asyncio.open_connection( host=self.hostname, port=self.port) except (ConnectionRefusedError, OSError) as e: message = "Error connection to {} on port {}".format( self.hostname, self.port) raise SMTPConnectError(SMTP_NO_CONNECTION, message) code, message = yield from self.get_response() if code != SMTP_READY: raise SMTPConnectError(code, message) if self.debug: logger.debug("connected: %s %s", code, message) self.ready.set_result(True)
def fake_send_mail(subject, message, from_email, recipient_list, fail_silently=False, auth_user=None, auth_password=None, connection=None): raise SMTPConnectError(1337, "SMTP is too awesome")
def test_send_mail_fails(self, mail_managers, send_mail): send_mail.side_effect = SMTPConnectError(10, msg="Can't connect") result, message = send_html_email( "*****@*****.**", "Subject", "Test", "base.html") self.assertTrue(send_mail.called) self.assertFalse(mail_managers.called) self.assertEqual(send_mail.call_args[0][2], settings.DEFAULT_FROM_EMAIL) self.assertFalse(result) self.assertEqual(message, "(10, \"Can't connect\")")
def __init__(self, host='', port=0, local_hostname=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None): """Initialize a new instance. If specified, `host' is the name of the remote host to which to connect. If specified, `port' specifies the port to which to connect. By default, smtplib.SMTP_PORT is used. If a host is specified the connect method is called, and if it returns anything other than a success code an SMTPConnectError is raised. If specified, `local_hostname` is used as the FQDN of the local host in the HELO/EHLO command. Otherwise, the local hostname is found using socket.getfqdn(). The `source_address` parameter takes a 2-tuple (host, port) for the socket to bind to as its source address before connecting. If the host is '' and port is 0, the OS default behavior will be used. """ self._host = host self.timeout = timeout self.esmtp_features = {} self.command_encoding = 'ascii' self.source_address = source_address if host: (code, msg) = self.connect(host, port) if code != 220: self.close() raise SMTPConnectError(code, msg) if local_hostname is not None: self.local_hostname = local_hostname else: # RFC 2821 says we should use the fqdn in the EHLO/HELO verb, and # if that can't be calculated, that we should use a domain literal # instead (essentially an encoded IP address like [A.B.C.D]). fqdn = socket.getfqdn() if '.' in fqdn: self.local_hostname = fqdn else: # We can't find an fqdn hostname, so use a domain literal addr = '127.0.0.1' try: addr = socket.gethostbyname(socket.gethostname()) except socket.gaierror: pass self.local_hostname = '[%s]' % addr
def test_conn_err_retry(self, retry, get_conn): """ Test that celery handles SMTPConnectError by retrying. """ get_conn.return_value.open.side_effect = SMTPConnectError(424, "Bad Connection") test_email = { 'action': 'Send email', 'send_to': 'myself', 'subject': 'test subject for myself', 'message': 'test message for myself' } response = self.client.post(self.send_mail_url, test_email) self.assertEquals(json.loads(response.content), self.success_content) self.assertTrue(retry.called) (__, kwargs) = retry.call_args exc = kwargs['exc'] self.assertIsInstance(exc, SMTPConnectError)
def test_conn_err_retry(self, retry, get_conn): """ Test that celery handles SMTPConnectError by retrying. """ get_conn.return_value.open.side_effect = SMTPConnectError( 424, "Bad Connection") test_email = { 'action': 'Send email', 'to_option': 'myself', 'subject': 'test subject for myself', 'message': 'test message for myself' } self.client.post(self.url, test_email) self.assertTrue(retry.called) (_, kwargs) = retry.call_args exc = kwargs['exc'] self.assertTrue(type(exc) == SMTPConnectError)
def test_conn_err_retry(self, retry, get_conn): """ Test that celery handles SMTPConnectError by retrying. """ get_conn.return_value.open.side_effect = SMTPConnectError( 424, "Bad Connection") test_email = { 'action': 'Send email', 'send_to': '["myself"]', 'subject': 'test subject for myself', 'message': 'test message for myself' } response = self.client.post(self.send_mail_url, test_email) assert json.loads( response.content.decode('utf-8')) == self.success_content assert retry.called (__, kwargs) = retry.call_args exc = kwargs['exc'] assert isinstance(exc, SMTPConnectError)
def test_max_retry_after_smtp_connect_error(self): self._test_max_retry_limit_causes_failure( SMTPConnectError(424, "Bad Connection"))
def test_retry_after_smtp_connect_error(self): self._test_retry_after_limited_retry_error( SMTPConnectError(424, "Bad Connection"))
def mock_smtp_connect_error(mock_smtp: MagicMock) -> MagicMock: from smtplib import SMTPConnectError mock_smtp.side_effect = SMTPConnectError(400, "error") return mock_smtp