Also fixed some minor bugs in sort()'s charset handling.
not be supported by all IMAP servers.
"""
if not self.has_capability('SORT'):
- raise self.Error('The server does not support the SORT extension')
+ raise ValueError('The server does not support the SORT extension')
args = [
_normalise_sort_criteria(sort_criteria),
raise ValueError('server does not support %s threading algorithm'
% algorithm)
- args = [algorithm]
- if charset:
- args.append(to_bytes(charset))
- args.extend(_normalise_search_criteria(criteria, charset))
-
+ args = [algorithm, to_bytes(charset)] + \
+ _normalise_search_criteria(criteria, charset)
data = self._raw_command_untagged(b'THREAD', args)
return parse_response(data)
self.assertFalse(self.client.has_capability('BAR'))
-class TestThread(IMAPClientTest):
-
- def test_thread_without_uid(self):
- self.client._cached_capabilities = (b'THREAD=REFERENCES',)
- self.client.use_uid = False
- self.client._imap.thread.return_value = ('OK', [b'(1 2)(3)(4 5 6)'])
-
- threads = self.client.thread()
-
- self.assertSequenceEqual(threads, ((1, 2), (3,), (4, 5, 6)))
-
- def test_thread_with_uid(self):
- self.client._cached_capabilities = (b'THREAD=REFERENCES',)
- self.client.use_uid = True
- self.client._imap.uid.return_value = ('OK', [b'(1 2)(3)(4 5 6)'])
-
- threads = self.client.thread()
-
- self.assertSequenceEqual(threads, ((1, 2), (3,), (4, 5, 6)))
-
- def test_no_support(self):
- self.client._cached_capabilities = (b'NOT-THREAD',)
- self.assertRaises(ValueError, self.client.thread)
-
- def test_no_support2(self):
- self.client._cached_capabilities = (b'THREAD=FOO',)
- self.assertRaises(ValueError, self.client.thread)
-
- def test_charset_with_uid(self):
- self.client._cached_capabilities = (b'THREAD=REFERENCES',)
- self.client._imap.uid.return_value = ('OK', [])
-
- self.client.thread(criteria='\u261e', charset='utf-8')
-
- self.client._imap.uid.assert_called_once_with('thread', b'REFERENCES', b'utf-8', b'(\xe2\x98\x9e)')
-
- def test_charset_without_uid(self):
- self.client._cached_capabilities = (b'THREAD=REFERENCES',)
- self.client.use_uid = False
- self.client._imap.thread.return_value = ('OK', [])
-
- self.client.thread(criteria='\u261e', charset='utf-8')
-
- self.client._imap.thread.assert_called_once_with(b'REFERENCES', b'utf-8', b'(\xe2\x98\x9e)')
-
- def test_all_args_with_uid(self):
- self.client._cached_capabilities = (b'THREAD=FOO',)
- self.client._imap.uid.return_value = ('OK', [])
-
- self.client.thread(algorithm='FOO', criteria='STUFF', charset='ASCII')
-
- self.client._imap.uid.assert_called_once_with('thread', b'FOO', b'ASCII', b'(STUFF)')
-
- def test_all_args_without_uid(self):
- self.client.use_uid = False
- self.client._cached_capabilities = (b'THREAD=FOO',)
- self.client._imap.thread.return_value = ('OK', [])
-
- self.client.thread(algorithm='FOO', criteria='STUFF', charset='ASCII')
-
- self.client._imap.thread.assert_called_once_with(b'FOO', b'ASCII', b'(STUFF)')
-
-
class TestId(IMAPClientTest):
def test_id(self):
self.client._imap._get_response.return_value = b'blah'
self.client._imap.tagged_commands['tag'] = ('NO', ['go away'])
- expected_error = "unexpected response while waiting for continuation response: \('NO', \['go away'\]\)"
+ expected_error = "unexpected response while waiting for continuation response: \(u?'NO', \[u?'go away'\]\)"
with self.assertRaisesRegex(IMAPClient.AbortError, expected_error):
self.client._raw_command(b'FOO', [b'\xff'])
from __future__ import unicode_literals
-from six import PY3
+from mock import Mock
from .imapclient_test import IMAPClientTest
-from .testable_imapclient import TestableIMAPClient as IMAPClient
-search_command = 'search' if PY3 else b'search'
+class TestSearchBase(IMAPClientTest):
+ def setUp(self):
+ super(TestSearchBase, self).setUp()
+ self.client._raw_command_untagged = Mock()
+ self.client._raw_command_untagged.return_value = [b'1 2 44']
-class TestSearch(IMAPClientTest):
+ def check_call(self, expected_args):
+ self.client._raw_command_untagged.assert_called_once_with(
+ b'SEARCH', expected_args)
- def test_with_uid(self):
- self.client.use_uid = True
- self.client._imap.uid.return_value = ('OK', [b'1 2 44'])
- result = self.client.search('FOO')
+class TestSearch(TestSearchBase):
- self.client._imap.uid.assert_called_once_with(search_command, b'(FOO)')
+ def test_bytes_criteria(self):
+ result = self.client.search([b'FOO', b'BAR'])
+
+ self.check_call([b'FOO', b'BAR'])
self.assertEqual(result, [1, 2, 44])
self.assertEqual(result.modseq, None)
- def test_with_uid_none(self):
- self.client.use_uid = True
- self.client._imap.uid.return_value = ('OK', [None])
-
- result = self.client.search('FOO')
-
- self.client._imap.uid.assert_called_once_with(search_command, b'(FOO)')
- self.assertEqual(result, [])
- self.assertEqual(result.modseq, None)
+ def test_bytes_criteria_with_charset(self):
+ self.client.search([b'FOO', b'BAR'], 'utf-92')
- def test_without_uid(self):
- self.client.use_uid = False
- self.client._imap.search.return_value = ('OK', [b'1 2 44'])
+ self.check_call([b'CHARSET', b'utf-92', b'FOO', b'BAR'])
- result = self.client.search('FOO')
+ def test_unicode_criteria(self):
+ result = self.client.search(['FOO', 'BAR'])
- self.client._imap.search.assert_called_once_with(None, b'(FOO)')
+ # Default conversion using us-ascii.
+ self.check_call([b'FOO', b'BAR'])
self.assertEqual(result, [1, 2, 44])
self.assertEqual(result.modseq, None)
- def test_with_uid_with_charset(self):
- self.client.use_uid = True
- self.client._imap.uid.return_value = ('OK', [b'1 2 44'])
+ def test_unicode_criteria_with_charset(self):
+ self.client.search(['FOO', '\u2639'], 'utf-8')
- result = self.client.search(['UNDELETED', 'TEXT "\u2639"'], 'UTF-8')
+ # Default conversion using us-ascii.
+ self.check_call([b'CHARSET', b'utf-8', b'FOO', b'\xe2\x98\xb9'])
- self.client._imap.uid.assert_called_once_with(
- search_command,
- b'CHARSET', b'UTF-8',
- b'(UNDELETED)',
- b'(TEXT "\xe2\x98\xb9")',
- )
- self.assertEqual(result, [1, 2, 44])
+ def test_quoting(self):
+ self.client.search(['TEXT', 'foo bar'])
- def test_without_uid_with_charset(self):
- self.client.use_uid = False
- self.client._imap.search.return_value = ('OK', [b'1 2 44'])
+ self.check_call([b'TEXT', b'"foo bar"'])
- result = self.client.search(['UNDELETED', 'TEXT "\u2639"'], 'UTF-8')
+ def test_no_results(self):
+ self.client._raw_command_untagged.return_value = [None]
- self.client._imap.search.assert_called_once_with(
- b'UTF-8',
- b'(UNDELETED)',
- b'(TEXT "\xe2\x98\xb9")',
- )
- self.assertEqual(result, [1, 2, 44])
+ result = self.client.search(['FOO'])
+ self.assertEqual(result, [])
+ self.assertEqual(result.modseq, None)
def test_modseq(self):
- self.client._imap.uid.return_value = ('OK', [b'1 2 (MODSEQ 51101)'])
+ self.client._raw_command_untagged.return_value = [b'1 2 (MODSEQ 51101)']
- result = self.client.search(['MODSEQ 40000'])
+ result = self.client.search(['MODSEQ', '40000'])
- self.client._imap.uid.assert_called_once_with(search_command, b'(MODSEQ 40000)')
+ self.check_call([b'MODSEQ', b'40000'])
self.assertEqual(result, [1, 2])
self.assertEqual(result.modseq, 51101)
- def test_error_from_server(self):
- self.client._imap.uid.return_value = ('NO', [b'bad karma'])
-
- self.assertRaisesRegex(IMAPClient.Error,
- 'bad karma',
- self.client.search, b'FOO')
-
-class TestGmailSearch(IMAPClientTest):
+class TestGmailSearch(TestSearchBase):
- def test_with_uid(self):
- self.client.use_uid = True
- self.client._imap.uid.return_value = ('OK', [b'1 2 44'])
+ def test_bytes_query(self):
+ result = self.client.gmail_search(b'foo bar')
- result = self.client.gmail_search('FOO')
-
- self.client._imap.uid.assert_called_once_with(search_command, b'X-GM-RAW')
- self.assertEqual(self.client._imap.literal, b'FOO')
- self.assertEqual(result, [1, 2, 44])
-
- def test_without_uid(self):
- self.client.use_uid = False
- self.client._imap.search.return_value = ('OK', [b'1 2 44'])
-
- result = self.client.gmail_search('FOO')
-
- self.client._imap.search.assert_called_once_with(None, b'X-GM-RAW')
- self.assertEqual(self.client._imap.literal, b'FOO')
+ self.check_call([b'X-GM-RAW', b'"foo bar"'])
self.assertEqual(result, [1, 2, 44])
- def test_with_uid_with_charset(self):
- self.client.use_uid = True
- self.client._imap.uid.return_value = ('OK', [b'1 2 44'])
-
- result = self.client.gmail_search('\u2620', 'UTF-8')
-
- self.client._imap.uid.assert_called_once_with(
- search_command,
- b'CHARSET', b'UTF-8',
- b'X-GM-RAW')
- self.assertEqual(self.client._imap.literal, b'\xe2\x98\xa0')
- self.assertEqual(result, [1, 2, 44])
-
- def test_without_uid_with_charset(self):
- self.client.use_uid = False
- self.client._imap.search.return_value = ('OK', [b'1 2 44'])
-
- result = self.client.gmail_search('\u2620', 'UTF-8')
+ def test_bytes_query_with_charset(self):
+ result = self.client.gmail_search(b'foo bar', 'utf-42')
- self.client._imap.search.assert_called_once_with(b'UTF-8', b'X-GM-RAW')
- self.assertEqual(self.client._imap.literal, b'\xe2\x98\xa0')
+ self.check_call([b'CHARSET', b'utf-42', b'X-GM-RAW', b'"foo bar"'])
self.assertEqual(result, [1, 2, 44])
- def test_error_from_server(self):
- self.client._imap.uid.return_value = ('NO', [b'bad karma'])
+ def test_unicode_criteria_with_charset(self):
+ self.client.gmail_search('foo \u2639', 'utf-8')
- self.assertRaisesRegex(IMAPClient.Error,
- 'bad karma',
- self.client.gmail_search, b'FOO')
+ self.check_call([b'CHARSET', b'utf-8', b'X-GM-RAW', b'"foo \xe2\x98\xb9"'])
from __future__ import unicode_literals
-from six import PY3
+from mock import Mock
from .imapclient_test import IMAPClientTest
-sort_command = 'sort' if PY3 else b'sort'
-
-
class TestSort(IMAPClientTest):
def setUp(self):
super(TestSort, self).setUp()
self.client._cached_capabilities = (b'SORT',)
+ self.client._raw_command_untagged = Mock()
+ self.client._raw_command_untagged.return_value = b'9 8 7'
+
+ def check_call(self, expected_args):
+ self.client._raw_command_untagged.assert_called_once_with(
+ b'SORT', expected_args, unpack=True)
def test_no_support(self):
self.client._cached_capabilities = (b'BLAH',)
- self.assertRaises(ValueError, self.client.thread)
-
- def test_without_uid(self):
- self.client.use_uid = False
- self.client._imap.sort.return_value = ('OK', [b'9 8 7'])
+ self.assertRaises(ValueError, self.client.sort, 'ARRIVAL')
+ def test_single_criteria(self):
ids = self.client.sort('arrival')
- self.client._imap.sort.assert_called_once_with(
- '(ARRIVAL)',
- b'UTF-8',
- b'(ALL)',
- )
- self.assertSequenceEqual(ids, [9, 8, 7])
-
- def test_with_uid(self):
- self.client.use_uid = True
- self.client._imap.uid.return_value = ('OK', [b'9 8 7'])
-
- ids = self.client.sort(['foo', 'bar'])
-
- self.client._imap.uid.assert_called_once_with(
- sort_command,
- '(FOO BAR)',
- b'UTF-8',
- b'(ALL)',
- )
+ self.check_call([b'(ARRIVAL)', b'UTF-8', b'ALL'])
self.assertSequenceEqual(ids, [9, 8, 7])
- def test_charset_without_uid(self):
- self.client.use_uid = False
- self.client._imap.sort.return_value = ('OK', [''])
-
- self.client.sort('arrival', criteria=['\u261e', 'UNDELETED'], charset='utf-7')
-
- self.client._imap.sort.assert_called_once_with(
- '(ARRIVAL)',
- b'utf-7',
- b'(+Jh4-)', b'(UNDELETED)',
- )
+ def test_multiple_criteria(self):
+ self.client.sort(['arrival', b'SUBJECT'])
- def test_unicode_with_uid(self):
- self.client._imap.uid.return_value = ('OK', [''])
+ self.check_call([b'(ARRIVAL SUBJECT)', b'UTF-8', b'ALL'])
- self.client.sort('arrival', criteria=['\u261e', 'UNDELETED'], charset='utf-7')
+ def test_all_args(self):
+ self.client.sort('arrival', ['TEXT', '\u261e'], 'UTF-7')
- self.client._imap.uid.assert_called_once_with(
- sort_command,
- '(ARRIVAL)',
- b'utf-7',
- b'(+Jh4-)', b'(UNDELETED)',
- )
+ self.check_call([b'(ARRIVAL)', b'UTF-7', b'TEXT', b'+Jh4-'])
--- /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
+
+from .imapclient_test import IMAPClientTest
+
+
+class TestThread(IMAPClientTest):
+
+ def setUp(self):
+ super(TestThread, self).setUp()
+ self.client._cached_capabilities = (b'THREAD=REFERENCES',)
+ self.client._raw_command_untagged = Mock()
+ self.client._raw_command_untagged.return_value = [b'(1 2)(3)(4 5 6)']
+
+ def check_call(self, expected_args):
+ self.client._raw_command_untagged.assert_called_once_with(
+ b'THREAD', expected_args)
+
+ def test_no_thread_support(self):
+ self.client._cached_capabilities = (b'NOT-THREAD',)
+ self.assertRaises(ValueError, self.client.thread)
+
+ def test_unsupported_algorithm(self):
+ self.client._cached_capabilities = (b'THREAD=FOO',)
+ self.assertRaises(ValueError, self.client.thread)
+
+ def test_defaults(self):
+ threads = self.client.thread()
+
+ self.check_call([b'REFERENCES', b'UTF-8', b'ALL'])
+ self.assertSequenceEqual(threads, ((1, 2), (3,), (4, 5, 6)))
+
+ def test_all_args(self):
+ self.client._cached_capabilities = (b'THREAD=COTTON',)
+
+ self.client.thread('COTTON', ['TEXT', '\u261e'], 'UTF-7')
+
+ self.check_call([b'COTTON', b'UTF-7', b'TEXT', b'+Jh4-'])
from imapclient.imapclient import (
join_message_ids,
- normalise_search_criteria,
+ _normalise_search_criteria,
normalise_text_list,
seq_to_parenstr,
seq_to_parenstr_upper,
class Test_normalise_search_criteria(unittest.TestCase):
def check(self, criteria, charset, expected):
- self.assertEqual(normalise_search_criteria(criteria, charset), expected)
+ self.assertEqual(_normalise_search_criteria(criteria, charset), expected)
def test_unicode(self):
- self.check('Foo', None, [b'(Foo)'])
+ self.check('Foo', None, [b'Foo'])
def test_binary(self):
- self.check(b'FOO', None, [b'(FOO)'])
+ self.check(b'FOO', None, [b'FOO'])
def test_unicode_with_charset(self):
- self.check('\u263a', 'UTF-8', [b'(\xe2\x98\xba)'])
+ self.check('\u263a', 'UTF-8', [b'\xe2\x98\xba'])
def test_binary_with_charset(self):
- self.check(b'FOO', 'UTF-9', [b'(FOO)'])
+ self.check(b'FOO', 'UTF-9', [b'FOO'])
def test_tuple(self):
- self.check(('FOO', 'BAR'), None, [b'(FOO)', b'(BAR)'])
+ self.check(('FOO', 'BAR'), None, [b'FOO', b'BAR'])
def test_list(self):
- self.check(['FOO', 'BAR'], None, [b'(FOO)', b'(BAR)'])
+ self.check(['FOO', 'BAR'], None, [b'FOO', b'BAR'])
def test_mixed_list(self):
- self.check(['FOO', b'BAR'], None, [b'(FOO)', b'(BAR)'])
+ self.check(['FOO', b'BAR'], None, [b'FOO', b'BAR'])
def test_None(self):
- self.assertRaises(ValueError, normalise_search_criteria, None, None)
+ self.assertRaises(ValueError, _normalise_search_criteria, None, None)
def test_empty(self):
- self.assertRaises(ValueError, normalise_search_criteria, '', None)
+ self.assertRaises(ValueError, _normalise_search_criteria, '', None)