Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def assert_tls_already_established(self):
with self.assertRaises(IMAPClient.AbortError) as raised:
self.client.starttls(sentinel.ssl_context)
self.assertEqual(str(raised.exception), "TLS session already established")
from .imapclient import IMAPClient
try:
from unittest.mock import Mock
except ImportError:
try:
from mock import Mock
except ImportError:
raise ImportError(
'mock library could not be loaded. Please install Python 3.3 or newer '
'or install the `mock` third-party package through PyPi.'
)
class TestableIMAPClient(IMAPClient):
"""Wrapper of :py:class:`imapclient.IMAPClient` that mocks all
interaction with real IMAP server.
This class should only be used in tests, where you can safely
interact with imapclient without running commands on a real
IMAP account.
"""
def __init__(self):
super(TestableIMAPClient, self).__init__('somehost')
def _create_IMAP4(self):
return MockIMAP4()
class MockIMAP4(Mock):
def test_stream(self):
fakeIMAP4_stream = Mock()
self.imaplib.IMAP4_stream.return_value = fakeIMAP4_stream
imap = IMAPClient('command', stream=True, ssl=False)
self.assertEqual(imap._imap, fakeIMAP4_stream)
self.imaplib.IMAP4_stream.assert_called_with('command')
self.assertEqual(imap.host, 'command')
self.assertEqual(imap.port, None)
self.assertEqual(imap.ssl, False)
self.assertEqual(imap.stream, True)
def test_plain(self):
fakeIMAP4 = Mock()
self.imap4.IMAP4WithTimeout.return_value = fakeIMAP4
imap = IMAPClient('1.2.3.4', ssl=False, timeout=sentinel.timeout)
self.assertEqual(imap._imap, fakeIMAP4)
self.imap4.IMAP4WithTimeout.assert_called_with(
'1.2.3.4', 143,
SocketTimeout(sentinel.timeout, sentinel.timeout)
)
self.assertEqual(imap.host, '1.2.3.4')
self.assertEqual(imap.port, 143)
self.assertEqual(imap.ssl, False)
self.assertEqual(imap.ssl_context, None)
self.assertEqual(imap.stream, False)
def test_SSL(self):
fakeIMAP4_TLS = Mock()
self.tls.IMAP4_TLS.return_value = fakeIMAP4_TLS
imap = IMAPClient('1.2.3.4', ssl_context=sentinel.context,
timeout=sentinel.timeout)
self.assertEqual(imap._imap, fakeIMAP4_TLS)
self.tls.IMAP4_TLS.assert_called_with(
'1.2.3.4', 993,
sentinel.context,
SocketTimeout(sentinel.timeout, sentinel.timeout)
)
self.assertEqual(imap.host, '1.2.3.4')
self.assertEqual(imap.port, 993)
self.assertEqual(imap.ssl, True)
self.assertEqual(imap.ssl_context, sentinel.context)
self.assertEqual(imap.stream, False)
if POLL_SUPPORT:
poll_func = self._poll_socket
else:
poll_func = self._select_poll_socket
try:
resps = []
events = poll_func(sock, timeout)
if events:
while True:
try:
line = self._imap._get_line()
except (socket.timeout, socket.error):
break
except IMAPClient.AbortError:
# An imaplib.IMAP4.abort with "EOF" is raised
# under Python 3
err = sys.exc_info()[1]
if 'EOF' in err.args[0]:
break
else:
raise
else:
resps.append(_parse_untagged_response(line))
return resps
finally:
sock.setblocking(1)
self._set_read_timeout()