]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Fix capabilities handling around STARTTLS
authorMenno Smits <menno@freshfoo.com>
Tue, 15 Dec 2015 10:39:22 +0000 (23:39 +1300)
committerMenno Smits <menno@freshfoo.com>
Tue, 15 Dec 2015 10:39:22 +0000 (23:39 +1300)
Fresh capabilities weren't being retrieved after STARTTLS but before
authentication. This has been fixed and now all STARTTLS/capabilities
handling is addressed within capabilities().

Fixes #195.

--HG--
branch : stable

doc/src/releases.rst
imapclient/imapclient.py
imapclient/test/test_imapclient.py
imapclient/test/test_starttls.py
imapclient/test/testable_imapclient.py

index 19b9d36ec0a9184007ace3f535fe8d6748213057..1d0cb6bb65439d8fe8f0cef91a39ce6a06066d49 100644 (file)
@@ -1,5 +1,21 @@
 :tocdepth: 1
 
+===============
+ Version 1.0.1
+===============
+
+Changed
+-------
+- Minimum backports.ssl dependency is now 0.0.9 (an important
+  performance issue was addressed)
+- setuptools 18.8.1 now used due to strange zip file error for 17.1
+
+Fixed
+-----
+- Unit test for version strings were updated to now always include the
+  patch version.
+
+
 ===============
  Version 1.0.0
 ===============
index 649bae9ab3fe04b8bc5d458cadae61aea09307dc..43f0e6bc32fd51c787528b658f5f04bd8a46f8bd 100644 (file)
@@ -200,10 +200,6 @@ class IMAPClient(object):
 
         self._starttls_done = True
 
-        # Clear cached capabilities as per
-        # https://tools.ietf.org/html/rfc2595#section-3.1
-        self._cached_capabilities = None
-
         self._imap.sock = tls.wrap_socket(self._imap.sock, ssl_context, self.host)
         self._imap.file = self._imap.sock.makefile()
         return data[0]
@@ -286,14 +282,21 @@ class IMAPClient(object):
     def capabilities(self):
         """Returns the server capability list.
 
-        If the session is authenticated and the server has returned a
-        CAPABILITY response at authentication time, this response
-        will be returned. Otherwise, the CAPABILITY command will be
-        issued to the server, with the results cached for future calls.
+        If the session is authenticated and the server has returned an
+        untagged CAPABILITY response at authentication time, this
+        response will be returned. Otherwise, the CAPABILITY command
+        will be issued to the server, with the results cached for
+        future calls.
 
-        If the session is not yet authenticated, the cached
-        capabilities determined at connection time will be returned.
+        If the session is not yet authenticated, the capabilities
+        requested at connection time will be returned.
         """
+        # Ensure cached capabilities aren't used post-STARTTLS. As per
+        # https://tools.ietf.org/html/rfc2595#section-3.1
+        if self._starttls_done and self._imap.state == 'NONAUTH':
+            self._cached_capabilities = None
+            return self._do_capabilites()
+
         # If a capability response has been cached, use that.
         if self._cached_capabilities:
             return self._cached_capabilities
@@ -303,21 +306,25 @@ class IMAPClient(object):
         untagged = _dict_bytes_normaliser(self._imap.untagged_responses)
         response = untagged.pop(b'CAPABILITY', None)
         if response:
-            return self._save_capabilities(response[0])
+            self._cached_capabilities = self._normalise_capabilites(response[0])
+            return self._cached_capabilities
 
         # If authenticated, but don't have a capability reponse, ask for one
         if self._imap.state in ('SELECTED', 'AUTH'):
-            response = self._command_and_check('capability', unpack=True)
-            return self._save_capabilities(response)
+            self._cached_capabilities = self._do_capabilites()
+            return self._cached_capabilities
 
-        # Just return capabilities that imaplib grabbed at connection
+        # Return capabilities that imaplib requested at connection
         # time (pre-auth)
         return tuple(to_bytes(c) for c in self._imap.capabilities)
 
-    def _save_capabilities(self, raw_response):
+    def _do_capabilites(self):
+        raw_response = self._command_and_check('capability', unpack=True)
+        return self._normalise_capabilites(raw_response)
+
+    def _normalise_capabilites(self, raw_response):
         raw_response = to_bytes(raw_response)
-        self._cached_capabilities = tuple(raw_response.upper().split())
-        return self._cached_capabilities
+        return tuple(raw_response.upper().split())
 
     def has_capability(self, capability):
         """Return ``True`` if the IMAP server has the given *capability*.
index 1bf71e274914b5b56cba09be4db49bfeb29b0212..70c3dd2f0d101b1518f77aade1f5ab0515b60d30 100644 (file)
@@ -495,6 +495,23 @@ class TestCapabilities(IMAPClientTest):
         self.assertEqual(self.client.capabilities(), (b'FOO', b'BAR'))
         self.assertEqual(self.client._cached_capabilities, (b'FOO', b'BAR'))
 
+    def test_with_starttls(self):
+        # Initial connection
+        self.client._imap.capabilities = ('FOO',)
+        self.client._imap.untagged_responses = {}
+        self.client._imap.state = 'NONAUTH'
+        self.assertEqual(self.client.capabilities(), (b'FOO',))
+
+        # Now do STARTTLS; capabilities change and should be reported.
+        self.client._starttls_done = True
+        self.client._imap.capability.return_value = ('OK', [b'FOO BAR'])
+        self.assertEqual(self.client.capabilities(), (b'FOO', b'BAR'))
+
+        # Login done; capabilities change again.
+        self.client._imap.state = 'AUTH'
+        self.client._imap.capability.return_value = ('OK', [b'FOO BAR QUX'])
+        self.assertEqual(self.client.capabilities(), (b'FOO', b'BAR', b'QUX'))
+
     def test_has_capability(self):
         self.client._cached_capabilities = (b'FOO', b'MORE')
 
index 5cce708220b1e5efee16f9f94a84879215cee07c..7f5aef4a2650a52b5bc868742a7661fd57c93dec 100644 (file)
@@ -42,19 +42,6 @@ class TestStarttls(IMAPClientTest):
         self.assertEqual(self.client._imap.file, sentinel.file)
         self.assertEqual(resp, b'start TLS negotiation')
 
-    def test_clears_cached_capabilities(self):
-        # As per https://tools.ietf.org/html/rfc2595#section-3.1
-
-        self.client._save_capabilities(b"FOO BAR")
-        self.assertEqual(self.client.capabilities(), (b"FOO", b"BAR"))
-
-        self.client.starttls(sentinel.ssl_context)
-
-        # Now verify that the cache has been cleared and new
-        # capabilities are read.
-        self.client._imap.untagged_responses = {'CAPABILITY': [b'META']}
-        self.assertEqual(self.client.capabilities(), (b"META",))
-
     def test_command_fails(self):
         self.client._imap._simple_command.return_value = "NO", [b'sorry']
 
index f6d716156012fbca32c1a893bbceaf830292537b..90dff935c775f23c1e7da0679e8f91186afa0da5 100644 (file)
@@ -25,6 +25,7 @@ class MockIMAP4(Mock):
         self.sent = b''  # Accumulates what was given to send()
         self.tagged_commands = {}
         self.debug = 0
+        self._starttls_done = False
 
     def send(self, data):
         self.sent += data