| 1 |
# Copyright (c) 2015, Menno Smits |
| 2 |
# Released subject to the New BSD License |
| 3 |
# Please see http://en.wikipedia.org/wiki/BSD_licenses |
| 4 |
|
| 5 |
import imaplib |
| 6 |
from datetime import date, datetime |
| 7 |
from unittest.mock import Mock |
| 8 |
|
| 9 |
from imapclient.exceptions import InvalidCriteriaError |
| 10 |
from imapclient.imapclient import _quoted |
| 11 |
|
| 12 |
from .imapclient_test import IMAPClientTest |
| 13 |
|
| 14 |
|
| 15 |
class TestSearchBase(IMAPClientTest): |
| 16 |
def setUp(self): |
| 17 |
super(TestSearchBase, self).setUp() |
| 18 |
self.client._raw_command_untagged = Mock() |
| 19 |
self.client._raw_command_untagged.return_value = [b"1 2 44"] |
| 20 |
|
| 21 |
def check_call(self, expected_args): |
| 22 |
self.client._raw_command_untagged.assert_called_once_with( |
| 23 |
b"SEARCH", expected_args |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
class TestSearch(TestSearchBase): |
| 28 |
def test_bytes_criteria(self): |
| 29 |
result = self.client.search([b"FOO", b"BAR"]) |
| 30 |
|
| 31 |
self.check_call([b"FOO", b"BAR"]) |
| 32 |
self.assertEqual(result, [1, 2, 44]) |
| 33 |
self.assertEqual(result.modseq, None) |
| 34 |
|
| 35 |
def test_bytes_criteria_with_charset(self): |
| 36 |
self.client.search([b"FOO", b"BAR"], "utf-92") |
| 37 |
|
| 38 |
self.check_call([b"CHARSET", b"utf-92", b"FOO", b"BAR"]) |
| 39 |
|
| 40 |
def test_unicode_criteria(self): |
| 41 |
result = self.client.search(["FOO", "BAR"]) |
| 42 |
|
| 43 |
# Default conversion using us-ascii. |
| 44 |
self.check_call([b"FOO", b"BAR"]) |
| 45 |
self.assertEqual(result, [1, 2, 44]) |
| 46 |
self.assertEqual(result.modseq, None) |
| 47 |
|
| 48 |
def test_unicode_criteria_with_charset(self): |
| 49 |
self.client.search(["FOO", "\u2639"], "utf-8") |
| 50 |
self.check_call([b"CHARSET", b"utf-8", b"FOO", _quoted(b"\xe2\x98\xb9")]) |
| 51 |
|
| 52 |
def test_with_date(self): |
| 53 |
self.client.search(["SINCE", date(2005, 4, 3)]) |
| 54 |
self.check_call([b"SINCE", b"03-Apr-2005"]) |
| 55 |
|
| 56 |
def test_with_datetime(self): |
| 57 |
self.client.search(["SINCE", datetime(2005, 4, 3, 2, 1, 0)]) |
| 58 |
self.check_call([b"SINCE", b"03-Apr-2005"]) # Time part is ignored |
| 59 |
|
| 60 |
def test_quoting(self): |
| 61 |
self.client.search(["TEXT", "foo bar"]) |
| 62 |
self.check_call([b"TEXT", _quoted(b'"foo bar"')]) |
| 63 |
|
| 64 |
def test_zero_length_quoting(self): |
| 65 |
# Zero-length strings should be quoted |
| 66 |
self.client.search(["HEADER", "List-Id", ""]) |
| 67 |
self.check_call([b"HEADER", b"List-Id", b'""']) |
| 68 |
|
| 69 |
def test_no_results(self): |
| 70 |
self.client._raw_command_untagged.return_value = [None] |
| 71 |
|
| 72 |
result = self.client.search(["FOO"]) |
| 73 |
self.assertEqual(result, []) |
| 74 |
self.assertEqual(result.modseq, None) |
| 75 |
|
| 76 |
def test_modseq(self): |
| 77 |
self.client._raw_command_untagged.return_value = [b"1 2 (MODSEQ 51101)"] |
| 78 |
|
| 79 |
result = self.client.search(["MODSEQ", "40000"]) |
| 80 |
|
| 81 |
self.check_call([b"MODSEQ", b"40000"]) |
| 82 |
self.assertEqual(result, [1, 2]) |
| 83 |
self.assertEqual(result.modseq, 51101) |
| 84 |
|
| 85 |
def test_nested_empty(self): |
| 86 |
self.assertRaises(InvalidCriteriaError, self.client.search, [[]]) |
| 87 |
|
| 88 |
def test_single(self): |
| 89 |
self.client.search([["FOO"]]) |
| 90 |
self.check_call([b"(FOO)"]) |
| 91 |
|
| 92 |
def test_nested(self): |
| 93 |
self.client.search(["NOT", ["SUBJECT", "topic", "TO", "[email protected]"]]) |
| 94 |
self.check_call([b"NOT", b"(SUBJECT", b"topic", b"TO", b"[email protected])"]) |
| 95 |
|
| 96 |
def test_nested_multiple(self): |
| 97 |
self.client.search(["NOT", ["OR", ["A", "x", "B", "y"], ["C", "z"]]]) |
| 98 |
self.check_call([b"NOT", b"(OR", b"(A", b"x", b"B", b"y)", b"(C", b"z))"]) |
| 99 |
|
| 100 |
def test_nested_tuple(self): |
| 101 |
self.client.search(["NOT", ("SUBJECT", "topic", "TO", "[email protected]")]) |
| 102 |
self.check_call([b"NOT", b"(SUBJECT", b"topic", b"TO", b"[email protected])"]) |
| 103 |
|
| 104 |
def test_search_custom_exception_with_invalid_list(self): |
| 105 |
def search_bad_command_exp(*args, **kwargs): |
| 106 |
raise imaplib.IMAP4.error( |
| 107 |
'SEARCH command error: BAD ["Unknown argument NOT DELETED"]' |
| 108 |
) |
| 109 |
|
| 110 |
self.client._raw_command_untagged.side_effect = search_bad_command_exp |
| 111 |
|
| 112 |
with self.assertRaises(imaplib.IMAP4.error) as cm: |
| 113 |
self.client.search(["NOT DELETED"]) |
| 114 |
self.assertIn( |
| 115 |
# Python 2.x will add a `u` prefix in the list representation, so let it handle the |
| 116 |
# representation of the criteria there too... |
| 117 |
"may have been caused by a syntax error in the criteria: %s" |
| 118 |
% str(["NOT DELETED"]), |
| 119 |
str(cm.exception), |
| 120 |
) |
| 121 |
# Original exception message should be present too just in case... |
| 122 |
self.assertIn("Unknown argument NOT DELETED", str(cm.exception)) |
| 123 |
|
| 124 |
def test_search_custom_exception_with_invalid_text(self): |
| 125 |
# Check the criteria is surrounding with quotes if the user is using a plain text criteria |
| 126 |
def search_bad_command_exp2(*args, **kwargs): |
| 127 |
raise imaplib.IMAP4.error( |
| 128 |
'SEARCH command error: BAD ["Unknown argument TOO"]' |
| 129 |
) |
| 130 |
|
| 131 |
self.client._raw_command_untagged.side_effect = search_bad_command_exp2 |
| 132 |
|
| 133 |
with self.assertRaises(imaplib.IMAP4.error) as cm: |
| 134 |
self.client.search("TOO [email protected]") |
| 135 |
self.assertIn( |
| 136 |
'may have been caused by a syntax error in the criteria: "TOO [email protected]"', |
| 137 |
str(cm.exception), |
| 138 |
) |
| 139 |
self.assertIn("Unknown argument TOO", str(cm.exception)) |
| 140 |
|
| 141 |
|
| 142 |
class TestGmailSearch(TestSearchBase): |
| 143 |
def setUp(self): |
| 144 |
super(TestGmailSearch, self).setUp() |
| 145 |
self.client._cached_capabilities = [b"X-GM-EXT-1"] |
| 146 |
|
| 147 |
def test_bytes_query(self): |
| 148 |
result = self.client.gmail_search(b"foo bar") |
| 149 |
|
| 150 |
self.check_call([b"CHARSET", b"UTF-8", b"X-GM-RAW", b'"foo bar"']) |
| 151 |
self.assertEqual(result, [1, 2, 44]) |
| 152 |
|
| 153 |
def test_bytes_query_with_charset(self): |
| 154 |
result = self.client.gmail_search(b"foo bar", "utf-42") |
| 155 |
|
| 156 |
self.check_call([b"CHARSET", b"utf-42", b"X-GM-RAW", b'"foo bar"']) |
| 157 |
self.assertEqual(result, [1, 2, 44]) |
| 158 |
|
| 159 |
def test_unicode_criteria_with_charset(self): |
| 160 |
self.client.gmail_search("foo \u2639", "utf-8") |
| 161 |
|
| 162 |
self.check_call( |
| 163 |
[b"CHARSET", b"utf-8", b"X-GM-RAW", _quoted(b'"foo \xe2\x98\xb9"')] |
| 164 |
) |