]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Tests for bad data in folder_status, more robust handling
authorBen Gotow <bengotow@gmail.com>
Sat, 3 Sep 2016 02:07:47 +0000 (19:07 -0700)
committerBen Gotow <bengotow@gmail.com>
Thu, 8 Sep 2016 17:21:39 +0000 (10:21 -0700)
In the last day we’ve seen this ValueError 249,915 times. It looks like the response can be more than two parts if A) the folder name contains spaces or B) the server decides to send us other data (or IMAPClient rolls it into the response buffer, not clear yet.) Either way, we should be able to handle these scenarios without crashing.

Relevant internal logs: https://nylas.loggly.com/search#terms=%22folder_status%20ValueError%22&from=2016-09-02T01%3A57%3A05.585Z&until=2016-09-03T01%3A57%3A05.585Z&source_group=

imapclient/imapclient.py
imapclient/test/test_imapclient.py

index eb335c0190469bc018b4f7aff47f6c8108b70eed..969b6057c683f1e34ec51141db4910ea4835eae0 100644 (file)
@@ -635,10 +635,11 @@ class IMAPClient(object):
             what = normalise_text_list(what)
         what_ = '(%s)' % (' '.join(what))
 
-        data = self._command_and_check('status', self._normalise_folder(folder), what_)
+        fname = self._normalise_folder(folder)
+        data = self._command_and_check('status', fname, what_)
         response = parse_response(data)
         try:
-            _, status_items = response
+            status_items = response[-1]
         except ValueError:
             raise Exception('folder_status ValueError: Input ' + str(response))
         return dict(as_pairs(status_items))
index 71d3867ded3aef51fc38d6dcef5ab31c36207e50..7fd205374fe3a412265a0b347d757657b7dd95ab 100644 (file)
@@ -146,6 +146,27 @@ class TestListFolders(IMAPClientTest):
         self.assertEqual(folders, [((br'\HasNoChildren',), b'/', 'last')])
 
 
+class TestFolderStatus(IMAPClientTest):
+    def test_extra_response(self):
+        # In production, we've seen folder names containing spaces come back
+        # like this and be broken into two components in the tuple.
+        server_response = ["My files (UIDNEXT 24369)"]
+        mock = Mock(return_value=server_response)
+        self.client._command_and_check = mock
+
+        resp = self.client.folder_status('My files', ['UIDNEXT'])
+        self.assertEqual(resp, {'UIDNEXT': 24369})
+
+        # We've also seen the response contain mailboxes we didn't
+        # ask for. In all known cases, the desired mailbox is last.
+        server_response = ["sent (UIDNEXT 24369)\nINBOX (UIDNEXT 24369)"]
+        mock = Mock(return_value=server_response)
+        self.client._command_and_check = mock
+
+        resp = self.client.folder_status('INBOX', ['UIDNEXT'])
+        self.assertEqual(resp, {'UIDNEXT': 24369})
+
+
 class TestSelectFolder(IMAPClientTest):
 
     def test_normal(self):