or an SSL connection is already established.
"""
if self.ssl or self._starttls_done:
- raise self.abort('TLS session already established')
+ raise self.AbortError('TLS session already established')
- typ, dat = self._imap._simple_command("STARTTLS")
- if typ != 'OK':
- raise self.Error("Couldn't establish TLS session")
+ typ, data = self._imap._simple_command("STARTTLS")
+ self._checkok('starttls', typ, data)
- self._tls_established = True
+ self._starttls_done = True
self._imap.sock = tls.wrap_socket(self._imap.sock, self.host, ssl_context)
self._imap.file = self._imap.sock.makefile()
- return self._imap._untagged_response(typ, dat, "STARTTLS")
+ return data[0]
def login(self, username, password):
"""Login using *username* and *password*, returning the
Raises IMAPClient.Error if the command fails.
"""
if typ != expected:
- raise self.Error('%s failed: %r' % (command, data[0]))
+ raise self.Error("%s failed: %s" % (command, to_unicode(data[0])))
def _consume_until_tagged_response(self, tag, command):
tagged_commands = self._imap.tagged_commands
--- /dev/null
+# Copyright (c) 2015, Menno Smits
+# Released subject to the New BSD License
+# Please see http://en.wikipedia.org/wiki/BSD_licenses
+
+from __future__ import unicode_literals
+
+from mock import Mock, patch, sentinel
+
+from ..imapclient import IMAPClient
+from .imapclient_test import IMAPClientTest
+
+
+class TestStarttls(IMAPClientTest):
+
+ def setUp(self):
+ super(TestStarttls, self).setUp()
+
+ patcher = patch("imapclient.imapclient.tls")
+ self.tls = patcher.start()
+ self.addCleanup(patcher.stop)
+
+ self.client._imap.sock = sentinel.old_sock
+
+ self.new_sock = Mock()
+ self.new_sock.makefile.return_value = sentinel.file
+ self.tls.wrap_socket.return_value = self.new_sock
+
+ self.client.host = sentinel.host
+ self.client.ssl = False
+ self.client._starttls_done = False
+ self.client._imap._simple_command.return_value = "OK", [b'start TLS negotiation']
+
+ def test_works(self):
+ resp = self.client.starttls(sentinel.ssl_context)
+
+ self.tls.wrap_socket.assert_called_once_with(
+ sentinel.old_sock,
+ sentinel.host,
+ sentinel.ssl_context,
+ )
+ self.new_sock.makefile.assert_called_once_with()
+ self.assertEqual(self.client._imap.file, sentinel.file)
+ self.assertEqual(resp, b'start TLS negotiation')
+
+ def test_command_fails(self):
+ self.client._imap._simple_command.return_value = "NO", [b'sorry']
+
+ with self.assertRaises(IMAPClient.Error) as raised:
+ self.client.starttls(sentinel.ssl_context)
+ self.assertEqual(str(raised.exception), "starttls failed: sorry")
+
+ def test_fails_if_called_twice(self):
+ self.client.starttls(sentinel.ssl_context)
+ self.assert_tls_already_established()
+
+ def test_fails_if_ssl_true(self):
+ self.client.ssl = True
+ self.assert_tls_already_established()
+
+ 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")