]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Added starttls unit tests and cleaned up starttls
authorMenno Smits <menno@freshfoo.com>
Mon, 13 Jul 2015 23:24:28 +0000 (11:24 +1200)
committerMenno Smits <menno@freshfoo.com>
Mon, 13 Jul 2015 23:24:28 +0000 (11:24 +1200)
- use _checkok() to check command response
- set _starttls_done, not _tls_established
- return data[0], don't check untagged responses

Also standardised error raised by _check_resp() across all Python
versions.

imapclient/imapclient.py
imapclient/test/test_starttls.py [new file with mode: 0644]

index 7208a94e4d8e52fb21a11e13df2230e3de33bfe5..b08e922df35e626f203363037f05c2bcca2da63e 100644 (file)
@@ -177,16 +177,15 @@ class IMAPClient(object):
         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
@@ -1000,7 +999,7 @@ class IMAPClient(object):
         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
diff --git a/imapclient/test/test_starttls.py b/imapclient/test/test_starttls.py
new file mode 100644 (file)
index 0000000..a8ad7ee
--- /dev/null
@@ -0,0 +1,63 @@
+# 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")