From: Ben Gotow Date: Sat, 3 Sep 2016 02:07:47 +0000 (-0700) Subject: Tests for bad data in folder_status, more robust handling X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=04a88c67cce77f690be7b1041eb495121a5a9a14;p=imapclient.git Tests for bad data in folder_status, more robust handling 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= --- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index eb335c0..969b605 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -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)) diff --git a/imapclient/test/test_imapclient.py b/imapclient/test/test_imapclient.py index 71d3867..7fd2053 100644 --- a/imapclient/test/test_imapclient.py +++ b/imapclient/test/test_imapclient.py @@ -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):