| 1 |
# Copyright (c) 2014, Menno Smits |
| 2 |
# Released subject to the New BSD License |
| 3 |
# Please see http://en.wikipedia.org/wiki/BSD_licenses |
| 4 |
|
| 5 |
import io |
| 6 |
import itertools |
| 7 |
import logging |
| 8 |
import socket |
| 9 |
import sys |
| 10 |
import warnings |
| 11 |
from datetime import datetime |
| 12 |
from select import POLLIN |
| 13 |
from unittest.mock import Mock, patch, sentinel |
| 14 |
|
| 15 |
from imapclient.exceptions import CapabilityError, IMAPClientError, ProtocolError |
| 16 |
from imapclient.fixed_offset import FixedOffset |
| 17 |
from imapclient.imapclient import ( |
| 18 |
_literal, |
| 19 |
_parse_quota, |
| 20 |
IMAPlibLoggerAdapter, |
| 21 |
MailboxQuotaRoots, |
| 22 |
Quota, |
| 23 |
require_capability, |
| 24 |
) |
| 25 |
from imapclient.testable_imapclient import TestableIMAPClient as IMAPClient |
| 26 |
|
| 27 |
from .imapclient_test import IMAPClientTest |
| 28 |
|
| 29 |
|
| 30 |
class TestListFolders(IMAPClientTest): |
| 31 |
def test_list_folders(self): |
| 32 |
self.client._imap._simple_command.return_value = ("OK", [b"something"]) |
| 33 |
self.client._imap._untagged_response.return_value = ( |
| 34 |
"LIST", |
| 35 |
sentinel.folder_data, |
| 36 |
) |
| 37 |
self.client._proc_folder_list = Mock(return_value=sentinel.folder_list) |
| 38 |
|
| 39 |
folders = self.client.list_folders("foo", "bar") |
| 40 |
|
| 41 |
self.client._imap._simple_command.assert_called_once_with( |
| 42 |
"LIST", b'"foo"', b'"bar"' |
| 43 |
) |
| 44 |
self.assertEqual( |
| 45 |
self.client._proc_folder_list.call_args, ((sentinel.folder_data,), {}) |
| 46 |
) |
| 47 |
self.assertTrue(folders is sentinel.folder_list) |
| 48 |
|
| 49 |
def test_list_sub_folders(self): |
| 50 |
self.client._imap._simple_command.return_value = ("OK", [b"something"]) |
| 51 |
self.client._imap._untagged_response.return_value = ( |
| 52 |
"LSUB", |
| 53 |
sentinel.folder_data, |
| 54 |
) |
| 55 |
self.client._proc_folder_list = Mock(return_value=sentinel.folder_list) |
| 56 |
|
| 57 |
folders = self.client.list_sub_folders("foo", "bar") |
| 58 |
|
| 59 |
self.client._imap._simple_command.assert_called_once_with( |
| 60 |
"LSUB", b'"foo"', b'"bar"' |
| 61 |
) |
| 62 |
self.assertEqual( |
| 63 |
self.client._proc_folder_list.call_args, ((sentinel.folder_data,), {}) |
| 64 |
) |
| 65 |
self.assertTrue(folders is sentinel.folder_list) |
| 66 |
|
| 67 |
def test_list_folders_NO(self): |
| 68 |
self.client._imap._simple_command.return_value = ("NO", [b"badness"]) |
| 69 |
self.assertRaises(IMAPClientError, self.client.list_folders) |
| 70 |
|
| 71 |
def test_list_sub_folders_NO(self): |
| 72 |
self.client._imap._simple_command.return_value = ("NO", [b"badness"]) |
| 73 |
self.assertRaises(IMAPClientError, self.client.list_folders) |
| 74 |
|
| 75 |
def test_utf7_decoding(self): |
| 76 |
self.client._imap._simple_command.return_value = ("OK", [b"something"]) |
| 77 |
self.client._imap._untagged_response.return_value = ( |
| 78 |
"LIST", |
| 79 |
[ |
| 80 |
b'(\\HasNoChildren) "/" "A"', |
| 81 |
b'(\\HasNoChildren) "/" "Hello&AP8-world"', |
| 82 |
], |
| 83 |
) |
| 84 |
|
| 85 |
folders = self.client.list_folders("foo", "bar") |
| 86 |
|
| 87 |
self.client._imap._simple_command.assert_called_once_with( |
| 88 |
"LIST", b'"foo"', b'"bar"' |
| 89 |
) |
| 90 |
self.assertEqual( |
| 91 |
folders, |
| 92 |
[ |
| 93 |
((b"\\HasNoChildren",), b"/", "A"), |
| 94 |
((b"\\HasNoChildren",), b"/", "Hello\xffworld"), |
| 95 |
], |
| 96 |
) |
| 97 |
|
| 98 |
def test_folder_encode_off(self): |
| 99 |
self.client.folder_encode = False |
| 100 |
self.client._imap._simple_command.return_value = ("OK", [b"something"]) |
| 101 |
self.client._imap._untagged_response.return_value = ( |
| 102 |
"LIST", |
| 103 |
[ |
| 104 |
b'(\\HasNoChildren) "/" "A"', |
| 105 |
b'(\\HasNoChildren) "/" "Hello&AP8-world"', |
| 106 |
], |
| 107 |
) |
| 108 |
|
| 109 |
folders = self.client.list_folders("foo", "bar") |
| 110 |
|
| 111 |
self.client._imap._simple_command.assert_called_once_with( |
| 112 |
"LIST", '"foo"', '"bar"' |
| 113 |
) |
| 114 |
self.assertEqual( |
| 115 |
folders, |
| 116 |
[ |
| 117 |
((b"\\HasNoChildren",), b"/", b"A"), |
| 118 |
((b"\\HasNoChildren",), b"/", b"Hello&AP8-world"), |
| 119 |
], |
| 120 |
) |
| 121 |
|
| 122 |
def test_simple(self): |
| 123 |
folders = self.client._proc_folder_list( |
| 124 |
[ |
| 125 |
b'(\\HasNoChildren) "/" "A"', |
| 126 |
b'(\\HasNoChildren) "/" "Foo Bar"', |
| 127 |
] |
| 128 |
) |
| 129 |
self.assertEqual( |
| 130 |
folders, |
| 131 |
[ |
| 132 |
( |
| 133 |
(b"\\HasNoChildren",), |
| 134 |
b"/", |
| 135 |
"A", |
| 136 |
), |
| 137 |
((b"\\HasNoChildren",), b"/", "Foo Bar"), |
| 138 |
], |
| 139 |
) |
| 140 |
|
| 141 |
def test_without_quotes(self): |
| 142 |
folders = self.client._proc_folder_list( |
| 143 |
[ |
| 144 |
b'(\\HasNoChildren) "/" A', |
| 145 |
b'(\\HasNoChildren) "/" B', |
| 146 |
b'(\\HasNoChildren) "/" C', |
| 147 |
] |
| 148 |
) |
| 149 |
self.assertEqual( |
| 150 |
folders, |
| 151 |
[ |
| 152 |
((b"\\HasNoChildren",), b"/", "A"), |
| 153 |
((b"\\HasNoChildren",), b"/", "B"), |
| 154 |
((b"\\HasNoChildren",), b"/", "C"), |
| 155 |
], |
| 156 |
) |
| 157 |
|
| 158 |
def test_unquoted_numeric_folder_name(self): |
| 159 |
# Some IMAP implementations do this |
| 160 |
folders = self.client._proc_folder_list([b'(\\HasNoChildren) "/" 123']) |
| 161 |
self.assertEqual(folders, [((b"\\HasNoChildren",), b"/", "123")]) |
| 162 |
|
| 163 |
def test_unquoted_numeric_folder_name_parsed_as_long(self): |
| 164 |
# big enough numeric values might get parsed as longs |
| 165 |
folder_name = str(sys.maxsize + 1) |
| 166 |
folders = self.client._proc_folder_list( |
| 167 |
[b'(\\HasNoChildren) "/" ' + folder_name.encode("ascii")] |
| 168 |
) |
| 169 |
self.assertEqual(folders, [((b"\\HasNoChildren",), b"/", folder_name)]) |
| 170 |
|
| 171 |
def test_mixed(self): |
| 172 |
folders = self.client._proc_folder_list( |
| 173 |
[ |
| 174 |
b'(\\HasNoChildren) "/" Alpha', |
| 175 |
b'(\\HasNoChildren) "/" "Foo Bar"', |
| 176 |
b'(\\HasNoChildren) "/" C', |
| 177 |
] |
| 178 |
) |
| 179 |
self.assertEqual( |
| 180 |
folders, |
| 181 |
[ |
| 182 |
((b"\\HasNoChildren",), b"/", "Alpha"), |
| 183 |
((b"\\HasNoChildren",), b"/", "Foo Bar"), |
| 184 |
((b"\\HasNoChildren",), b"/", "C"), |
| 185 |
], |
| 186 |
) |
| 187 |
|
| 188 |
def test_funky_characters(self): |
| 189 |
folders = self.client._proc_folder_list( |
| 190 |
[ |
| 191 |
(b'(\\NoInferiors \\UnMarked) "/" {5}', "bang\xff"), |
| 192 |
b"", |
| 193 |
b'(\\HasNoChildren \\UnMarked) "/" "INBOX"', |
| 194 |
] |
| 195 |
) |
| 196 |
self.assertEqual( |
| 197 |
folders, |
| 198 |
[ |
| 199 |
((b"\\NoInferiors", b"\\UnMarked"), b"/", "bang\xff"), |
| 200 |
((b"\\HasNoChildren", b"\\UnMarked"), b"/", "INBOX"), |
| 201 |
], |
| 202 |
) |
| 203 |
|
| 204 |
def test_quoted_specials(self): |
| 205 |
folders = self.client._proc_folder_list( |
| 206 |
[ |
| 207 |
rb'(\HasNoChildren) "/" "Test \"Folder\""', |
| 208 |
rb'(\HasNoChildren) "/" "Left\"Right"', |
| 209 |
rb'(\HasNoChildren) "/" "Left\\Right"', |
| 210 |
rb'(\HasNoChildren) "/" "\"Left Right\""', |
| 211 |
rb'(\HasNoChildren) "/" "\"Left\\Right\""', |
| 212 |
] |
| 213 |
) |
| 214 |
self.assertEqual( |
| 215 |
folders, |
| 216 |
[ |
| 217 |
((b"\\HasNoChildren",), b"/", 'Test "Folder"'), |
| 218 |
((b"\\HasNoChildren",), b"/", 'Left"Right'), |
| 219 |
((b"\\HasNoChildren",), b"/", r"Left\Right"), |
| 220 |
((b"\\HasNoChildren",), b"/", r'"Left Right"'), |
| 221 |
((b"\\HasNoChildren",), b"/", r'"Left\Right"'), |
| 222 |
], |
| 223 |
) |
| 224 |
|
| 225 |
def test_empty_response(self): |
| 226 |
self.assertEqual(self.client._proc_folder_list([None]), []) |
| 227 |
|
| 228 |
def test_blanks(self): |
| 229 |
folders = self.client._proc_folder_list( |
| 230 |
["", None, rb'(\HasNoChildren) "/" "last"'] |
| 231 |
) |
| 232 |
self.assertEqual(folders, [((rb"\HasNoChildren",), b"/", "last")]) |
| 233 |
|
| 234 |
|
| 235 |
class TestFindSpecialFolder(IMAPClientTest): |
| 236 |
def test_find_special_folder_with_special_use(self): |
| 237 |
self.client._cached_capabilities = (b"SPECIAL-USE",) |
| 238 |
self.client._imap._simple_command.return_value = ("OK", [b"something"]) |
| 239 |
self.client._imap._untagged_response.return_value = ( |
| 240 |
"LIST", |
| 241 |
[ |
| 242 |
b'(\\HasNoChildren) "/" "INBOX"', |
| 243 |
b'(\\HasNoChildren \\Sent) "/" "Sent"', |
| 244 |
], |
| 245 |
) |
| 246 |
|
| 247 |
folder = self.client.find_special_folder(b"\\Sent") |
| 248 |
|
| 249 |
self.assertEqual(folder, "Sent") |
| 250 |
|
| 251 |
def test_find_special_folder_with_special_use_single_flag(self): |
| 252 |
self.client._cached_capabilities = (b"SPECIAL-USE",) |
| 253 |
self.client._imap._simple_command.return_value = ("OK", [b"something"]) |
| 254 |
self.client._imap._untagged_response.return_value = ( |
| 255 |
"LIST", |
| 256 |
[ |
| 257 |
b'(\\HasNoChildren) "/" "INBOX"', |
| 258 |
b'(\\Sent) "/" "Sent"', |
| 259 |
], |
| 260 |
) |
| 261 |
|
| 262 |
folder = self.client.find_special_folder(b"\\Sent") |
| 263 |
|
| 264 |
self.assertEqual(folder, "Sent") |
| 265 |
|
| 266 |
def test_find_special_folder_without_special_use_nor_namespace(self): |
| 267 |
self.client._cached_capabilities = (b"FOO",) |
| 268 |
self.client._imap._simple_command.return_value = ("OK", [b"something"]) |
| 269 |
self.client._imap._untagged_response.return_value = ( |
| 270 |
"LIST", |
| 271 |
[ |
| 272 |
b'(\\HasNoChildren) "/" "Sent Items"', |
| 273 |
], |
| 274 |
) |
| 275 |
|
| 276 |
folder = self.client.find_special_folder(b"\\Sent") |
| 277 |
|
| 278 |
self.assertEqual(folder, "Sent Items") |
| 279 |
|
| 280 |
|
| 281 |
class TestSelectFolder(IMAPClientTest): |
| 282 |
def test_normal(self): |
| 283 |
self.client._command_and_check = Mock() |
| 284 |
self.client._imap.untagged_responses = { |
| 285 |
b"exists": [b"3"], |
| 286 |
b"FLAGS": [rb"(\Flagged \Deleted abc [foo]/bar def)"], |
| 287 |
b"HIGHESTMODSEQ": [b"127110"], |
| 288 |
b"OK": [ |
| 289 |
rb"[PERMANENTFLAGS (\Flagged \Deleted abc [foo]/bar def \*)] Flags permitted.", |
| 290 |
b"[UIDVALIDITY 631062293] UIDs valid.", |
| 291 |
b"[UIDNEXT 1281] Predicted next UID.", |
| 292 |
b"[HIGHESTMODSEQ 127110]", |
| 293 |
], |
| 294 |
b"PERMANENTFLAGS": [rb"(\Flagged \Deleted abc [foo"], |
| 295 |
b"READ-WRITE": [b""], |
| 296 |
b"RECENT": [b"0"], |
| 297 |
b"UIDNEXT": [b"1281"], |
| 298 |
b"UIDVALIDITY": [b"631062293"], |
| 299 |
b"OTHER": [b"blah"], |
| 300 |
} |
| 301 |
|
| 302 |
result = self.client.select_folder(b"folder_name", sentinel.readonly) |
| 303 |
|
| 304 |
self.client._command_and_check.assert_called_once_with( |
| 305 |
"select", b'"folder_name"', sentinel.readonly |
| 306 |
) |
| 307 |
self.maxDiff = 99999 |
| 308 |
self.assertEqual( |
| 309 |
result, |
| 310 |
{ |
| 311 |
b"EXISTS": 3, |
| 312 |
b"RECENT": 0, |
| 313 |
b"UIDNEXT": 1281, |
| 314 |
b"UIDVALIDITY": 631062293, |
| 315 |
b"HIGHESTMODSEQ": 127110, |
| 316 |
b"FLAGS": (rb"\Flagged", rb"\Deleted", b"abc", b"[foo]/bar", b"def"), |
| 317 |
b"PERMANENTFLAGS": ( |
| 318 |
rb"\Flagged", |
| 319 |
rb"\Deleted", |
| 320 |
b"abc", |
| 321 |
b"[foo]/bar", |
| 322 |
b"def", |
| 323 |
rb"\*", |
| 324 |
), |
| 325 |
b"READ-WRITE": True, |
| 326 |
b"OTHER": [b"blah"], |
| 327 |
}, |
| 328 |
) |
| 329 |
|
| 330 |
def test_unselect(self): |
| 331 |
self.client._cached_capabilities = [b"UNSELECT"] |
| 332 |
self.client._imap._simple_command.return_value = ("OK", ["Unselect completed."]) |
| 333 |
# self.client._imap._untagged_response.return_value = ( |
| 334 |
# b'OK', [b'("name" "GImap" "vendor" "Google, Inc.")']) |
| 335 |
|
| 336 |
result = self.client.unselect_folder() |
| 337 |
self.assertEqual(result, "Unselect completed.") |
| 338 |
self.client._imap._simple_command.assert_called_with("UNSELECT") |
| 339 |
|
| 340 |
|
| 341 |
class TestAppend(IMAPClientTest): |
| 342 |
def test_without_msg_time(self): |
| 343 |
self.client._imap.append.return_value = ("OK", [b"Good"]) |
| 344 |
msg = "hi" |
| 345 |
|
| 346 |
self.client.append("foobar", msg, ["FLAG", "WAVE"], None) |
| 347 |
|
| 348 |
self.client._imap.append.assert_called_with( |
| 349 |
b'"foobar"', "(FLAG WAVE)", None, b"hi" |
| 350 |
) |
| 351 |
|
| 352 |
@patch("imapclient.imapclient.datetime_to_INTERNALDATE") |
| 353 |
def test_with_msg_time(self, datetime_to_INTERNALDATE): |
| 354 |
datetime_to_INTERNALDATE.return_value = "somedate" |
| 355 |
self.client._imap.append.return_value = ("OK", [b"Good"]) |
| 356 |
msg = b"bye" |
| 357 |
|
| 358 |
self.client.append( |
| 359 |
"foobar", |
| 360 |
msg, |
| 361 |
["FLAG", "WAVE"], |
| 362 |
datetime(2009, 4, 5, 11, 0, 5, 0, FixedOffset(2 * 60)), |
| 363 |
) |
| 364 |
|
| 365 |
self.assertTrue(datetime_to_INTERNALDATE.called) |
| 366 |
self.client._imap.append.assert_called_with( |
| 367 |
b'"foobar"', "(FLAG WAVE)", '"somedate"', msg |
| 368 |
) |
| 369 |
|
| 370 |
def test_multiappend(self): |
| 371 |
self.client._cached_capabilities = (b"MULTIAPPEND",) |
| 372 |
self.client._raw_command = Mock() |
| 373 |
self.client.multiappend("foobar", ["msg1", "msg2"]) |
| 374 |
|
| 375 |
self.client._raw_command.assert_called_once_with( |
| 376 |
b"APPEND", [b'"foobar"', b"msg1", b"msg2"], uid=False |
| 377 |
) |
| 378 |
|
| 379 |
def test_multiappend_with_flags_and_internaldate(self): |
| 380 |
self.client._cached_capabilities = (b"MULTIAPPEND",) |
| 381 |
self.client._raw_command = Mock() |
| 382 |
self.client.multiappend( |
| 383 |
"foobar", |
| 384 |
[ |
| 385 |
{ |
| 386 |
"msg": "msg1", |
| 387 |
"flags": ["FLAG", "WAVE"], |
| 388 |
"date": datetime(2009, 4, 5, 11, 0, 5, 0, FixedOffset(2 * 60)), |
| 389 |
}, |
| 390 |
{ |
| 391 |
"msg": "msg2", |
| 392 |
"flags": ["FLAG", "WAVE"], |
| 393 |
}, |
| 394 |
{ |
| 395 |
"msg": "msg3", |
| 396 |
"date": datetime(2009, 4, 5, 11, 0, 5, 0, FixedOffset(2 * 60)), |
| 397 |
}, |
| 398 |
], |
| 399 |
) |
| 400 |
|
| 401 |
self.client._raw_command.assert_called_once_with( |
| 402 |
b"APPEND", |
| 403 |
[ |
| 404 |
b'"foobar"', |
| 405 |
b"(FLAG WAVE)", |
| 406 |
b'"05-Apr-2009 11:00:05 +0200"', |
| 407 |
_literal(b"msg1"), |
| 408 |
b"(FLAG WAVE)", |
| 409 |
_literal(b"msg2"), |
| 410 |
b'"05-Apr-2009 11:00:05 +0200"', |
| 411 |
_literal(b"msg3"), |
| 412 |
], |
| 413 |
uid=False, |
| 414 |
) |
| 415 |
|
| 416 |
|
| 417 |
class TestAclMethods(IMAPClientTest): |
| 418 |
def setUp(self): |
| 419 |
super(TestAclMethods, self).setUp() |
| 420 |
self.client._cached_capabilities = [b"ACL"] |
| 421 |
|
| 422 |
def test_getacl(self): |
| 423 |
self.client._imap.getacl.return_value = ( |
| 424 |
"OK", |
| 425 |
[b"INBOX Fred rwipslda Sally rwip"], |
| 426 |
) |
| 427 |
acl = self.client.getacl("INBOX") |
| 428 |
self.assertSequenceEqual(acl, [(b"Fred", b"rwipslda"), (b"Sally", b"rwip")]) |
| 429 |
|
| 430 |
def test_setacl(self): |
| 431 |
self.client._imap.setacl.return_value = ("OK", [b"SETACL done"]) |
| 432 |
|
| 433 |
response = self.client.setacl("folder", sentinel.who, sentinel.what) |
| 434 |
|
| 435 |
self.client._imap.setacl.assert_called_with( |
| 436 |
b'"folder"', sentinel.who, sentinel.what |
| 437 |
) |
| 438 |
self.assertEqual(response, b"SETACL done") |
| 439 |
|
| 440 |
|
| 441 |
class TestQuota(IMAPClientTest): |
| 442 |
def setUp(self): |
| 443 |
super(TestQuota, self).setUp() |
| 444 |
self.client._cached_capabilities = [b"QUOTA"] |
| 445 |
|
| 446 |
def test_parse_quota(self): |
| 447 |
self.assertEqual(_parse_quota([]), []) |
| 448 |
self.assertEqual( |
| 449 |
_parse_quota([b'"User quota" (STORAGE 586720 4882812)']), |
| 450 |
[Quota("User quota", "STORAGE", 586720, 4882812)], |
| 451 |
) |
| 452 |
self.assertEqual( |
| 453 |
_parse_quota( |
| 454 |
[ |
| 455 |
b'"User quota" (STORAGE 586720 4882812)', |
| 456 |
b'"Global quota" (MESSAGES 42 1000)', |
| 457 |
] |
| 458 |
), |
| 459 |
[ |
| 460 |
Quota("User quota", "STORAGE", 586720, 4882812), |
| 461 |
Quota("Global quota", "MESSAGES", 42, 1000), |
| 462 |
], |
| 463 |
) |
| 464 |
self.assertEqual( |
| 465 |
_parse_quota( |
| 466 |
[ |
| 467 |
b'"User quota" (STORAGE 586720 4882812 MESSAGES 42 1000)', |
| 468 |
] |
| 469 |
), |
| 470 |
[ |
| 471 |
Quota("User quota", "STORAGE", 586720, 4882812), |
| 472 |
Quota("User quota", "MESSAGES", 42, 1000), |
| 473 |
], |
| 474 |
) |
| 475 |
|
| 476 |
def test__get_quota(self): |
| 477 |
self.client._command_and_check = Mock() |
| 478 |
self.client._command_and_check.return_value = [ |
| 479 |
b'"User quota" (MESSAGES 42 1000)' |
| 480 |
] |
| 481 |
|
| 482 |
quotas = self.client._get_quota("foo") |
| 483 |
|
| 484 |
self.client._command_and_check.assert_called_once_with("getquota", '"foo"') |
| 485 |
self.assertEqual(quotas, [Quota("User quota", "MESSAGES", 42, 1000)]) |
| 486 |
|
| 487 |
def test_set_quota(self): |
| 488 |
self.client._raw_command_untagged = Mock() |
| 489 |
self.client._raw_command_untagged.return_value = [ |
| 490 |
b'"User quota" (STORAGE 42 1000 MESSAGES 42 1000)' |
| 491 |
] |
| 492 |
quotas = [ |
| 493 |
Quota("User quota", "STORAGE", 42, 1000), |
| 494 |
Quota("User quota", "MESSAGES", 42, 1000), |
| 495 |
] |
| 496 |
resp = self.client.set_quota(quotas) |
| 497 |
|
| 498 |
self.client._raw_command_untagged.assert_called_once_with( |
| 499 |
b"SETQUOTA", |
| 500 |
[b'"User quota"', b"(STORAGE 1000 MESSAGES 1000)"], |
| 501 |
uid=False, |
| 502 |
response_name="QUOTA", |
| 503 |
) |
| 504 |
self.assertListEqual(resp, quotas) |
| 505 |
|
| 506 |
def test_get_quota_root(self): |
| 507 |
self.client._raw_command_untagged = Mock() |
| 508 |
self.client._raw_command_untagged.return_value = [b'"INBOX" "User quota"'] |
| 509 |
self.client._imap.untagged_responses = dict() |
| 510 |
|
| 511 |
resp = self.client.get_quota_root("INBOX") |
| 512 |
|
| 513 |
self.client._raw_command_untagged.assert_called_once_with( |
| 514 |
b"GETQUOTAROOT", b"INBOX", uid=False, response_name="QUOTAROOT" |
| 515 |
) |
| 516 |
expected = (MailboxQuotaRoots("INBOX", ["User quota"]), list()) |
| 517 |
self.assertTupleEqual(resp, expected) |
| 518 |
|
| 519 |
resp = self.client.get_quota("INBOX") |
| 520 |
self.assertEqual(resp, []) |
| 521 |
|
| 522 |
|
| 523 |
class TestIdleAndNoop(IMAPClientTest): |
| 524 |
def setUp(self): |
| 525 |
super(TestIdleAndNoop, self).setUp() |
| 526 |
self.client._cached_capabilities = [b"IDLE"] |
| 527 |
|
| 528 |
def assert_sock_select_calls(self, sock): |
| 529 |
self.assertListEqual( |
| 530 |
sock.method_calls, |
| 531 |
[ |
| 532 |
("settimeout", (None,), {}), |
| 533 |
("setblocking", (0,), {}), |
| 534 |
("setblocking", (1,), {}), |
| 535 |
("settimeout", (None,), {}), |
| 536 |
], |
| 537 |
) |
| 538 |
|
| 539 |
def assert_sock_poll_calls(self, sock): |
| 540 |
self.assertListEqual( |
| 541 |
sock.method_calls, |
| 542 |
[ |
| 543 |
("settimeout", (None,), {}), |
| 544 |
("setblocking", (0,), {}), |
| 545 |
("fileno", (), {}), |
| 546 |
("setblocking", (1,), {}), |
| 547 |
("settimeout", (None,), {}), |
| 548 |
], |
| 549 |
) |
| 550 |
|
| 551 |
def test_idle(self): |
| 552 |
self.client._imap._command.return_value = sentinel.tag |
| 553 |
self.client._imap._get_response.return_value = None |
| 554 |
|
| 555 |
self.client.idle() |
| 556 |
|
| 557 |
self.client._imap._command.assert_called_with("IDLE") |
| 558 |
self.assertEqual(self.client._idle_tag, sentinel.tag) |
| 559 |
|
| 560 |
@patch("imapclient.imapclient.POLL_SUPPORT", False) |
| 561 |
@patch("imapclient.imapclient.select.select") |
| 562 |
def test_idle_check_blocking(self, mock_select): |
| 563 |
mock_sock = Mock() |
| 564 |
self.client._imap.sock = self.client._imap.sslobj = mock_sock |
| 565 |
mock_select.return_value = ([True], [], []) |
| 566 |
counter = itertools.count() |
| 567 |
|
| 568 |
def fake_get_line(): |
| 569 |
count = next(counter) |
| 570 |
if count == 0: |
| 571 |
return b"* 1 EXISTS" |
| 572 |
elif count == 1: |
| 573 |
return b"* 0 EXPUNGE" |
| 574 |
else: |
| 575 |
raise socket.timeout |
| 576 |
|
| 577 |
self.client._imap._get_line = fake_get_line |
| 578 |
|
| 579 |
responses = self.client.idle_check() |
| 580 |
|
| 581 |
mock_select.assert_called_once_with([mock_sock], [], [], None) |
| 582 |
self.assert_sock_select_calls(mock_sock) |
| 583 |
self.assertListEqual([(1, b"EXISTS"), (0, b"EXPUNGE")], responses) |
| 584 |
|
| 585 |
@patch("imapclient.imapclient.POLL_SUPPORT", False) |
| 586 |
@patch("imapclient.imapclient.select.select") |
| 587 |
def test_idle_check_timeout(self, mock_select): |
| 588 |
mock_sock = Mock() |
| 589 |
self.client._imap.sock = self.client._imap.sslobj = mock_sock |
| 590 |
mock_select.return_value = ([], [], []) |
| 591 |
|
| 592 |
responses = self.client.idle_check(timeout=0.5) |
| 593 |
|
| 594 |
mock_select.assert_called_once_with([mock_sock], [], [], 0.5) |
| 595 |
self.assert_sock_select_calls(mock_sock) |
| 596 |
self.assertListEqual([], responses) |
| 597 |
|
| 598 |
@patch("imapclient.imapclient.POLL_SUPPORT", False) |
| 599 |
@patch("imapclient.imapclient.select.select") |
| 600 |
def test_idle_check_with_data(self, mock_select): |
| 601 |
mock_sock = Mock() |
| 602 |
self.client._imap.sock = self.client._imap.sslobj = mock_sock |
| 603 |
mock_select.return_value = ([True], [], []) |
| 604 |
counter = itertools.count() |
| 605 |
|
| 606 |
def fake_get_line(): |
| 607 |
count = next(counter) |
| 608 |
if count == 0: |
| 609 |
return b"* 99 EXISTS" |
| 610 |
else: |
| 611 |
raise socket.timeout |
| 612 |
|
| 613 |
self.client._imap._get_line = fake_get_line |
| 614 |
|
| 615 |
responses = self.client.idle_check() |
| 616 |
|
| 617 |
mock_select.assert_called_once_with([mock_sock], [], [], None) |
| 618 |
self.assert_sock_select_calls(mock_sock) |
| 619 |
self.assertListEqual([(99, b"EXISTS")], responses) |
| 620 |
|
| 621 |
@patch("imapclient.imapclient.POLL_SUPPORT", True) |
| 622 |
@patch("imapclient.imapclient.select.poll") |
| 623 |
def test_idle_check_blocking_poll(self, mock_poll_module): |
| 624 |
mock_sock = Mock(fileno=Mock(return_value=1)) |
| 625 |
self.client._imap.sock = self.client._imap.sslobj = mock_sock |
| 626 |
|
| 627 |
mock_poller = Mock(poll=Mock(return_value=[(1, POLLIN)])) |
| 628 |
mock_poll_module.return_value = mock_poller |
| 629 |
counter = itertools.count() |
| 630 |
|
| 631 |
def fake_get_line(): |
| 632 |
count = next(counter) |
| 633 |
if count == 0: |
| 634 |
return b"* 1 EXISTS" |
| 635 |
elif count == 1: |
| 636 |
return b"* 0 EXPUNGE" |
| 637 |
else: |
| 638 |
raise socket.timeout |
| 639 |
|
| 640 |
self.client._imap._get_line = fake_get_line |
| 641 |
|
| 642 |
responses = self.client.idle_check() |
| 643 |
|
| 644 |
assert mock_poll_module.call_count == 1 |
| 645 |
mock_poller.register.assert_called_once_with(1, POLLIN) |
| 646 |
mock_poller.poll.assert_called_once_with(None) |
| 647 |
self.assert_sock_poll_calls(mock_sock) |
| 648 |
self.assertListEqual([(1, b"EXISTS"), (0, b"EXPUNGE")], responses) |
| 649 |
|
| 650 |
@patch("imapclient.imapclient.POLL_SUPPORT", True) |
| 651 |
@patch("imapclient.imapclient.select.poll") |
| 652 |
def test_idle_check_timeout_poll(self, mock_poll_module): |
| 653 |
mock_sock = Mock(fileno=Mock(return_value=1)) |
| 654 |
self.client._imap.sock = self.client._imap.sslobj = mock_sock |
| 655 |
|
| 656 |
mock_poller = Mock(poll=Mock(return_value=[])) |
| 657 |
mock_poll_module.return_value = mock_poller |
| 658 |
|
| 659 |
responses = self.client.idle_check(timeout=0.5) |
| 660 |
|
| 661 |
assert mock_poll_module.call_count == 1 |
| 662 |
mock_poller.register.assert_called_once_with(1, POLLIN) |
| 663 |
mock_poller.poll.assert_called_once_with(500) |
| 664 |
self.assert_sock_poll_calls(mock_sock) |
| 665 |
self.assertListEqual([], responses) |
| 666 |
|
| 667 |
@patch("imapclient.imapclient.POLL_SUPPORT", True) |
| 668 |
@patch("imapclient.imapclient.select.poll") |
| 669 |
def test_idle_check_with_data_poll(self, mock_poll_module): |
| 670 |
mock_sock = Mock(fileno=Mock(return_value=1)) |
| 671 |
self.client._imap.sock = self.client._imap.sslobj = mock_sock |
| 672 |
|
| 673 |
mock_poller = Mock(poll=Mock(return_value=[(1, POLLIN)])) |
| 674 |
mock_poll_module.return_value = mock_poller |
| 675 |
counter = itertools.count() |
| 676 |
|
| 677 |
def fake_get_line(): |
| 678 |
count = next(counter) |
| 679 |
if count == 0: |
| 680 |
return b"* 99 EXISTS" |
| 681 |
else: |
| 682 |
raise socket.timeout |
| 683 |
|
| 684 |
self.client._imap._get_line = fake_get_line |
| 685 |
|
| 686 |
responses = self.client.idle_check() |
| 687 |
|
| 688 |
assert mock_poll_module.call_count == 1 |
| 689 |
mock_poller.register.assert_called_once_with(1, POLLIN) |
| 690 |
mock_poller.poll.assert_called_once_with(None) |
| 691 |
self.assert_sock_poll_calls(mock_sock) |
| 692 |
self.assertListEqual([(99, b"EXISTS")], responses) |
| 693 |
|
| 694 |
def test_idle_done(self): |
| 695 |
self.client._idle_tag = sentinel.tag |
| 696 |
|
| 697 |
mockSend = Mock() |
| 698 |
self.client._imap.send = mockSend |
| 699 |
mockConsume = Mock(return_value=sentinel.out) |
| 700 |
self.client._consume_until_tagged_response = mockConsume |
| 701 |
|
| 702 |
result = self.client.idle_done() |
| 703 |
|
| 704 |
mockSend.assert_called_with(b"DONE\r\n") |
| 705 |
mockConsume.assert_called_with(sentinel.tag, "IDLE") |
| 706 |
self.assertEqual(result, sentinel.out) |
| 707 |
|
| 708 |
def test_noop(self): |
| 709 |
mockCommand = Mock(return_value=sentinel.tag) |
| 710 |
self.client._imap._command = mockCommand |
| 711 |
mockConsume = Mock(return_value=sentinel.out) |
| 712 |
self.client._consume_until_tagged_response = mockConsume |
| 713 |
|
| 714 |
result = self.client.noop() |
| 715 |
|
| 716 |
mockCommand.assert_called_with("NOOP") |
| 717 |
mockConsume.assert_called_with(sentinel.tag, "NOOP") |
| 718 |
self.assertEqual(result, sentinel.out) |
| 719 |
|
| 720 |
def test_consume_until_tagged_response(self): |
| 721 |
client = self.client |
| 722 |
client._imap.tagged_commands = {sentinel.tag: None} |
| 723 |
|
| 724 |
counter = itertools.count() |
| 725 |
|
| 726 |
def fake_get_response(): |
| 727 |
count = next(counter) |
| 728 |
if count == 0: |
| 729 |
return b"* 99 EXISTS" |
| 730 |
client._imap.tagged_commands[sentinel.tag] = ("OK", [b"Idle done"]) |
| 731 |
|
| 732 |
client._imap._get_response = fake_get_response |
| 733 |
|
| 734 |
text, responses = client._consume_until_tagged_response(sentinel.tag, b"IDLE") |
| 735 |
self.assertEqual(client._imap.tagged_commands, {}) |
| 736 |
self.assertEqual(text, b"Idle done") |
| 737 |
self.assertListEqual([(99, b"EXISTS")], responses) |
| 738 |
|
| 739 |
|
| 740 |
class TestDebugLogging(IMAPClientTest): |
| 741 |
def test_IMAP_is_patched(self): |
| 742 |
# Remove all logging handlers so that the order of tests does not |
| 743 |
# prevent basicConfig from being executed |
| 744 |
for handler in logging.root.handlers[:]: |
| 745 |
logging.root.removeHandler(handler) |
| 746 |
log_stream = io.StringIO() |
| 747 |
logging.basicConfig(stream=log_stream, level=logging.DEBUG) |
| 748 |
|
| 749 |
self.client._imap._mesg("two") |
| 750 |
self.assertIn("DEBUG:imapclient.imaplib:two", log_stream.getvalue()) |
| 751 |
|
| 752 |
def test_redacted_password(self): |
| 753 |
logger_mock = Mock() |
| 754 |
logger_mock.manager.disable = logging.DEBUG |
| 755 |
logger_mock.getEffectiveLevel.return_value = logging.DEBUG |
| 756 |
|
| 757 |
adapter = IMAPlibLoggerAdapter(logger_mock, dict()) |
| 758 |
adapter.info("""> b'ICHH1 LOGIN [email protected] "secret"'""") |
| 759 |
if sys.version_info >= (3, 6, 4): |
| 760 |
# LoggerAdapter in Python 3.6.4+ calls logger.log() |
| 761 |
logger_mock.log.assert_called_once_with( |
| 762 |
logging.INFO, "> b'ICHH1 LOGIN **REDACTED**", extra={} |
| 763 |
) |
| 764 |
else: |
| 765 |
# LoggerAdapter in Python 3.4 to 3.6 calls logger._log() |
| 766 |
logger_mock._log.assert_called_once_with( |
| 767 |
logging.INFO, "> b'ICHH1 LOGIN **REDACTED**", (), extra={} |
| 768 |
) |
| 769 |
|
| 770 |
|
| 771 |
class TestTimeNormalisation(IMAPClientTest): |
| 772 |
def test_default(self): |
| 773 |
self.assertTrue(self.client.normalise_times) |
| 774 |
|
| 775 |
@patch("imapclient.imapclient.parse_fetch_response") |
| 776 |
def test_pass_through(self, parse_fetch_response): |
| 777 |
self.client._imap._command_complete.return_value = ("OK", sentinel.data) |
| 778 |
self.client._imap._untagged_response.return_value = ("OK", sentinel.fetch_data) |
| 779 |
self.client.use_uid = sentinel.use_uid |
| 780 |
|
| 781 |
def check(expected): |
| 782 |
self.client.fetch(22, ["SOMETHING"]) |
| 783 |
parse_fetch_response.assert_called_with( |
| 784 |
sentinel.fetch_data, expected, sentinel.use_uid |
| 785 |
) |
| 786 |
|
| 787 |
self.client.normalise_times = True |
| 788 |
check(True) |
| 789 |
|
| 790 |
self.client.normalise_times = False |
| 791 |
check(False) |
| 792 |
|
| 793 |
|
| 794 |
class TestNamespace(IMAPClientTest): |
| 795 |
def setUp(self): |
| 796 |
super(TestNamespace, self).setUp() |
| 797 |
self.client._cached_capabilities = [b"NAMESPACE"] |
| 798 |
|
| 799 |
def set_return(self, value): |
| 800 |
self.client._imap.namespace.return_value = ("OK", [value]) |
| 801 |
|
| 802 |
def test_simple(self): |
| 803 |
self.set_return(b'(("FOO." "/")) NIL NIL') |
| 804 |
self.assertEqual(self.client.namespace(), ((("FOO.", "/"),), None, None)) |
| 805 |
|
| 806 |
def test_folder_decoding(self): |
| 807 |
self.set_return(b'(("&AP8-." "/")) NIL NIL') |
| 808 |
self.assertEqual(self.client.namespace(), ((("\xff.", "/"),), None, None)) |
| 809 |
|
| 810 |
def test_without_folder_decoding(self): |
| 811 |
self.set_return(b'(("&AP8-." "/")) NIL NIL') |
| 812 |
self.client.folder_encode = False |
| 813 |
self.assertEqual(self.client.namespace(), (((b"&AP8-.", "/"),), None, None)) |
| 814 |
|
| 815 |
def test_other_only(self): |
| 816 |
self.set_return(b'NIL NIL (("" "."))') |
| 817 |
self.assertEqual(self.client.namespace(), (None, None, (("", "."),))) |
| 818 |
|
| 819 |
def test_complex(self): |
| 820 |
self.set_return( |
| 821 |
b'(("" "/")) ' |
| 822 |
b'(("~" "/")) ' |
| 823 |
b'(("#shared/" "/") ("#public/" "/")("#ftp/" "/")("#news." "."))' |
| 824 |
) |
| 825 |
self.assertEqual( |
| 826 |
self.client.namespace(), |
| 827 |
( |
| 828 |
(("", "/"),), |
| 829 |
(("~", "/"),), |
| 830 |
(("#shared/", "/"), ("#public/", "/"), ("#ftp/", "/"), ("#news.", ".")), |
| 831 |
), |
| 832 |
) |
| 833 |
|
| 834 |
|
| 835 |
class TestCapabilities(IMAPClientTest): |
| 836 |
def test_preauth(self): |
| 837 |
self.client._imap.capabilities = ("FOO", "BAR") |
| 838 |
self.client._imap.untagged_responses = {} |
| 839 |
|
| 840 |
self.assertEqual(self.client.capabilities(), (b"FOO", b"BAR")) |
| 841 |
|
| 842 |
def test_server_returned_capability_after_auth(self): |
| 843 |
self.client._imap.capabilities = (b"FOO",) |
| 844 |
self.client._imap.untagged_responses = {"CAPABILITY": [b"FOO MORE"]} |
| 845 |
|
| 846 |
self.assertEqual(self.client._cached_capabilities, None) |
| 847 |
self.assertEqual(self.client.capabilities(), (b"FOO", b"MORE")) |
| 848 |
self.assertEqual(self.client._cached_capabilities, (b"FOO", b"MORE")) |
| 849 |
self.assertEqual(self.client._imap.untagged_responses, {}) |
| 850 |
|
| 851 |
def test_caching(self): |
| 852 |
self.client._imap.capabilities = ("FOO",) |
| 853 |
self.client._imap.untagged_responses = {} |
| 854 |
self.client._cached_capabilities = (b"FOO", b"MORE") |
| 855 |
|
| 856 |
self.assertEqual(self.client.capabilities(), (b"FOO", b"MORE")) |
| 857 |
|
| 858 |
def test_post_auth_request(self): |
| 859 |
self.client._imap.capabilities = ("FOO",) |
| 860 |
self.client._imap.untagged_responses = {} |
| 861 |
self.client._imap.state = "SELECTED" |
| 862 |
self.client._imap.capability.return_value = ("OK", [b"FOO BAR"]) |
| 863 |
|
| 864 |
self.assertEqual(self.client.capabilities(), (b"FOO", b"BAR")) |
| 865 |
self.assertEqual(self.client._cached_capabilities, (b"FOO", b"BAR")) |
| 866 |
|
| 867 |
def test_with_starttls(self): |
| 868 |
# Initial connection |
| 869 |
self.client._imap.capabilities = ("FOO",) |
| 870 |
self.client._imap.untagged_responses = {} |
| 871 |
self.client._imap.state = "NONAUTH" |
| 872 |
self.assertEqual(self.client.capabilities(), (b"FOO",)) |
| 873 |
|
| 874 |
# Now do STARTTLS; capabilities change and should be reported. |
| 875 |
self.client._starttls_done = True |
| 876 |
self.client._imap.capability.return_value = ("OK", [b"FOO BAR"]) |
| 877 |
self.assertEqual(self.client.capabilities(), (b"FOO", b"BAR")) |
| 878 |
|
| 879 |
# Login done; capabilities change again. |
| 880 |
self.client._imap.state = "AUTH" |
| 881 |
self.client._imap.capability.return_value = ("OK", [b"FOO BAR QUX"]) |
| 882 |
self.assertEqual(self.client.capabilities(), (b"FOO", b"BAR", b"QUX")) |
| 883 |
|
| 884 |
def test_has_capability(self): |
| 885 |
self.client._cached_capabilities = (b"FOO", b"MORE") |
| 886 |
|
| 887 |
self.assertTrue(self.client.has_capability(b"FOO")) |
| 888 |
self.assertTrue(self.client.has_capability(b"foo")) |
| 889 |
self.assertFalse(self.client.has_capability(b"BAR")) |
| 890 |
|
| 891 |
self.assertTrue(self.client.has_capability("FOO")) |
| 892 |
self.assertTrue(self.client.has_capability("foo")) |
| 893 |
self.assertFalse(self.client.has_capability("BAR")) |
| 894 |
|
| 895 |
def test_decorator(self): |
| 896 |
class Foo(object): |
| 897 |
def has_capability(self, capability): |
| 898 |
if capability == "TRUE": |
| 899 |
return True |
| 900 |
return False |
| 901 |
|
| 902 |
@require_capability("TRUE") |
| 903 |
def yes(self): |
| 904 |
return True |
| 905 |
|
| 906 |
@require_capability("FALSE") |
| 907 |
def no(self): |
| 908 |
return False |
| 909 |
|
| 910 |
foo = Foo() |
| 911 |
self.assertTrue(foo.yes()) |
| 912 |
self.assertRaises(CapabilityError, foo.no) |
| 913 |
|
| 914 |
|
| 915 |
class TestId(IMAPClientTest): |
| 916 |
def setUp(self): |
| 917 |
super(TestId, self).setUp() |
| 918 |
self.client._cached_capabilities = [b"ID"] |
| 919 |
|
| 920 |
def test_id(self): |
| 921 |
self.client._imap._simple_command.return_value = ("OK", [b"Success"]) |
| 922 |
self.client._imap._untagged_response.return_value = ( |
| 923 |
b"OK", |
| 924 |
[b'("name" "GImap" "vendor" "Google, Inc.")'], |
| 925 |
) |
| 926 |
|
| 927 |
id_response = self.client.id_({"name": "IMAPClient"}) |
| 928 |
self.client._imap._simple_command.assert_called_with( |
| 929 |
"ID", '("name" "IMAPClient")' |
| 930 |
) |
| 931 |
|
| 932 |
self.assertSequenceEqual( |
| 933 |
id_response, ((b"name", b"GImap", b"vendor", b"Google, Inc."),) |
| 934 |
) |
| 935 |
|
| 936 |
def test_no_support(self): |
| 937 |
self.client._cached_capabilities = (b"IMAP4rev1",) |
| 938 |
self.assertRaises(CapabilityError, self.client.id_) |
| 939 |
|
| 940 |
def test_invalid_parameters(self): |
| 941 |
self.assertRaises(TypeError, self.client.id_, "bananarama") |
| 942 |
|
| 943 |
|
| 944 |
class TestRawCommand(IMAPClientTest): |
| 945 |
def setUp(self): |
| 946 |
super(TestRawCommand, self).setUp() |
| 947 |
self.client._imap._get_response.return_value = None |
| 948 |
self.client._imap._command_complete.return_value = ("OK", ["done"]) |
| 949 |
self.client._cached_capabilities = () |
| 950 |
|
| 951 |
def check(self, command, args, expected): |
| 952 |
typ, data = self.client._raw_command(command, args) |
| 953 |
self.assertEqual(typ, "OK") |
| 954 |
self.assertEqual(data, ["done"]) |
| 955 |
self.assertEqual(self.client._imap.sent, expected) |
| 956 |
|
| 957 |
def test_plain(self): |
| 958 |
self.check( |
| 959 |
b"search", |
| 960 |
[b"ALL"], |
| 961 |
b"tag UID SEARCH ALL\r\n", |
| 962 |
) |
| 963 |
|
| 964 |
def test_not_uid(self): |
| 965 |
self.client.use_uid = False |
| 966 |
self.check( |
| 967 |
b"search", |
| 968 |
[b"ALL"], |
| 969 |
b"tag SEARCH ALL\r\n", |
| 970 |
) |
| 971 |
|
| 972 |
def test_literal_at_end(self): |
| 973 |
self.check( |
| 974 |
b"search", |
| 975 |
[b"TEXT", b"\xfe\xff"], |
| 976 |
b"tag UID SEARCH TEXT {2}\r\n" b"\xfe\xff\r\n", |
| 977 |
) |
| 978 |
|
| 979 |
def test_embedded_literal(self): |
| 980 |
self.check( |
| 981 |
b"search", |
| 982 |
[b"TEXT", b"\xfe\xff", b"DELETED"], |
| 983 |
b"tag UID SEARCH TEXT {2}\r\n" b"\xfe\xff DELETED\r\n", |
| 984 |
) |
| 985 |
|
| 986 |
def test_multiple_literals(self): |
| 987 |
self.check( |
| 988 |
b"search", |
| 989 |
[b"TEXT", b"\xfe\xff", b"TEXT", b"\xcc"], |
| 990 |
b"tag UID SEARCH TEXT {2}\r\n" b"\xfe\xff TEXT {1}\r\n" b"\xcc\r\n", |
| 991 |
) |
| 992 |
|
| 993 |
def test_literal_plus(self): |
| 994 |
self.client._cached_capabilities = (b"LITERAL+",) |
| 995 |
|
| 996 |
typ, data = self.client._raw_command( |
| 997 |
b"APPEND", [b"\xff", _literal(b"hello")], uid=False |
| 998 |
) |
| 999 |
self.assertEqual(typ, "OK") |
| 1000 |
self.assertEqual(data, ["done"]) |
| 1001 |
self.assertEqual( |
| 1002 |
self.client._imap.sent, |
| 1003 |
b"tag APPEND {1+}\r\n" b"\xff {5+}\r\n" b"hello\r\n", |
| 1004 |
) |
| 1005 |
|
| 1006 |
def test_literal_plus_multiple_literals(self): |
| 1007 |
self.client._cached_capabilities = (b"LITERAL+",) |
| 1008 |
|
| 1009 |
typ, data = self.client._raw_command( |
| 1010 |
b"APPEND", |
| 1011 |
[b"\xff", _literal(b"hello"), b"TEXT", _literal(b"test")], |
| 1012 |
uid=False, |
| 1013 |
) |
| 1014 |
self.assertEqual(typ, "OK") |
| 1015 |
self.assertEqual(data, ["done"]) |
| 1016 |
self.assertEqual( |
| 1017 |
self.client._imap.sent, |
| 1018 |
b"tag APPEND {1+}\r\n" |
| 1019 |
b"\xff {5+}\r\n" |
| 1020 |
b"hello" |
| 1021 |
b" TEXT {4+}\r\n" |
| 1022 |
b"test\r\n", |
| 1023 |
) |
| 1024 |
|
| 1025 |
def test_complex(self): |
| 1026 |
self.check( |
| 1027 |
b"search", |
| 1028 |
[b"FLAGGED", b"TEXT", b"\xfe\xff", b"TEXT", b"\xcc", b"TEXT", b"foo"], |
| 1029 |
b"tag UID SEARCH FLAGGED TEXT {2}\r\n" |
| 1030 |
b"\xfe\xff TEXT {1}\r\n" |
| 1031 |
b"\xcc TEXT foo\r\n", |
| 1032 |
) |
| 1033 |
|
| 1034 |
def test_invalid_input_type(self): |
| 1035 |
self.assertRaises(ValueError, self.client._raw_command, "foo", []) |
| 1036 |
self.assertRaises(ValueError, self.client._raw_command, "foo", ["foo"]) |
| 1037 |
|
| 1038 |
def test_failed_continuation_wait(self): |
| 1039 |
self.client._imap._get_response.return_value = b"blah" |
| 1040 |
self.client._imap.tagged_commands["tag"] = ("NO", ["go away"]) |
| 1041 |
|
| 1042 |
expected_error = r"unexpected response while waiting for continuation response: \(u?'NO', \[u?'go away'\]\)" |
| 1043 |
with self.assertRaisesRegex(IMAPClient.AbortError, expected_error): |
| 1044 |
self.client._raw_command(b"FOO", [b"\xff"]) |
| 1045 |
|
| 1046 |
|
| 1047 |
class TestExpunge(IMAPClientTest): |
| 1048 |
def test_expunge(self): |
| 1049 |
mockCommand = Mock(return_value=sentinel.tag) |
| 1050 |
mockConsume = Mock(return_value=sentinel.out) |
| 1051 |
self.client._imap._command = mockCommand |
| 1052 |
self.client._consume_until_tagged_response = mockConsume |
| 1053 |
result = self.client.expunge() |
| 1054 |
mockCommand.assert_called_with("EXPUNGE") |
| 1055 |
mockConsume.assert_called_with(sentinel.tag, "EXPUNGE") |
| 1056 |
self.assertEqual(sentinel.out, result) |
| 1057 |
|
| 1058 |
def test_id_expunge(self): |
| 1059 |
self.client._imap.uid.return_value = ("OK", [None]) |
| 1060 |
self.assertEqual([None], self.client.expunge(["4", "5", "6"])) |
| 1061 |
|
| 1062 |
|
| 1063 |
class TestShutdown(IMAPClientTest): |
| 1064 |
def test_shutdown(self): |
| 1065 |
self.client.shutdown() |
| 1066 |
self.client._imap.shutdown.assert_called_once_with() |
| 1067 |
|
| 1068 |
|
| 1069 |
class TestContextManager(IMAPClientTest): |
| 1070 |
def test_context_manager(self): |
| 1071 |
with self.client as client: |
| 1072 |
self.assertIsInstance(client, IMAPClient) |
| 1073 |
|
| 1074 |
self.client._imap.logout.assert_called_once_with() |
| 1075 |
|
| 1076 |
@patch("imapclient.imapclient.logger") |
| 1077 |
def test_context_manager_fail_closing(self, mock_logger): |
| 1078 |
self.client._imap.logout.side_effect = RuntimeError("Error logout") |
| 1079 |
self.client._imap.shutdown.side_effect = RuntimeError("Error shutdown") |
| 1080 |
|
| 1081 |
with self.client as client: |
| 1082 |
self.assertIsInstance(client, IMAPClient) |
| 1083 |
|
| 1084 |
self.client._imap.logout.assert_called_once_with() |
| 1085 |
self.client._imap.shutdown.assert_called_once_with() |
| 1086 |
mock_logger.info.assert_called_once_with( |
| 1087 |
"Could not close the connection cleanly: %s", |
| 1088 |
self.client._imap.shutdown.side_effect, |
| 1089 |
) |
| 1090 |
|
| 1091 |
def test_exception_inside_context_manager(self): |
| 1092 |
with self.assertRaises(ValueError): |
| 1093 |
with self.client as _: |
| 1094 |
raise ValueError("Error raised inside the context manager") |
| 1095 |
|
| 1096 |
|
| 1097 |
class TestProtocolError(IMAPClientTest): |
| 1098 |
def test_tagged_response_with_parse_error(self): |
| 1099 |
client = self.client |
| 1100 |
client._imap.tagged_commands = {sentinel.tag: None} |
| 1101 |
client._imap._get_response = lambda: b"NOT-A-STAR 99 EXISTS" |
| 1102 |
|
| 1103 |
with self.assertRaises(ProtocolError): |
| 1104 |
client._consume_until_tagged_response(sentinel.tag, b"IDLE") |
| 1105 |
|
| 1106 |
|
| 1107 |
class TestSocket(IMAPClientTest): |
| 1108 |
def test_issues_warning_for_deprecating_sock_property(self): |
| 1109 |
mock_sock = Mock() |
| 1110 |
self.client._imap.sock = self.client._imap.sslobj = mock_sock |
| 1111 |
with warnings.catch_warnings(record=True) as warnings_caught: |
| 1112 |
warnings.simplefilter("always", DeprecationWarning) |
| 1113 |
assert self.client._sock == self.client.socket() |
| 1114 |
assert len(warnings_caught) == 1 |