1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
|
# -*- coding: utf-8 -*-
# aioimaplib : an IMAPrev4 lib using python asyncio
# Copyright (C) 2016 Bruno Thomas
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio
import email
import functools
import imaplib
import ssl
from datetime import datetime, timedelta
import pytest
from pytz import utc
from aioimaplib.imap_testing_server import Mail, AUTH, SELECTED, LOGOUT
from tests.server_fixture import with_server, login_user, with_ssl, with_ssl_server
@pytest.mark.asyncio
async def test_server_greetings_and_capabilities(with_server):
pending_imap = asyncio.get_running_loop().run_in_executor(None, functools.partial(imaplib.IMAP4, host='127.0.0.1', port=12345))
imap_client = await asyncio.wait_for(pending_imap, 1)
assert 'NONAUTH' == imap_client.state
@pytest.mark.asyncio
async def test_server_login(with_server):
pending_imap = asyncio.get_running_loop().run_in_executor(None, functools.partial(imaplib.IMAP4, host='127.0.0.1', port=12345))
imap_client = await asyncio.wait_for(pending_imap, 1)
pending_login = asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.login, 'user', 'pass'))
result, data = await asyncio.wait_for(pending_login, 1)
assert 'OK' == result
assert [b'LOGIN completed'] == data
assert AUTH == with_server.get_connection('user').state
@pytest.mark.asyncio
async def test_select_no_messages_in_mailbox(with_server):
imap_client = await login_user('user@mail', 'pass')
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select)), 1)
assert 'OK' == result
assert [b'0'] == data
assert SELECTED == with_server.get_connection('user@mail').state
@pytest.mark.asyncio
async def test_select_one_message_in_mailbox(with_server):
with_server.receive(Mail.create(to=['user'], mail_from='me', subject='hello'))
imap_client = await login_user('user', 'pass')
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select)), 1)
assert 'OK' == result
assert [b'1'] == data
@pytest.mark.asyncio
async def test_select_one_message_in_INBOX_zero_in_OTHER(with_server):
with_server.receive(Mail.create(to=['user'], mail_from='me', subject='hello'))
imap_client = await login_user('user', 'pass')
_, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select)), 1)
assert [b'1'] == data
_, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select, 'OTHER')), 1)
assert [b'0'] == data
@pytest.mark.asyncio
async def test_examine_no_messages_in_mailbox(with_server):
imap_client = await login_user('user', 'pass')
assert ('OK', [b'0']) == (await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select, readonly=True)), 1))
assert AUTH == with_server.get_connection('user').state
@pytest.mark.asyncio
async def test_search_by_uid_two_messages(with_server):
with_server.receive(Mail.create(['user']))
with_server.receive(Mail.create(['user']))
imap_client = await login_user('user', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'search', 'utf-8', 'ALL')), 1)
assert 'OK' == result
assert [b'1 2'] == data
@pytest.mark.asyncio
async def test_search_by_uid_one_message_two_recipients(with_server):
with_server.receive(Mail.create(['user1', 'user2']))
imap_client = await login_user('user1', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'ALL')), 1)
assert 'OK' == result
assert [b'1'] == data
imap_client = await login_user('user2', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'ALL')), 1)
assert 'OK' == result
assert [b'1'] == data
@pytest.mark.asyncio
async def test_fetch_one_message_by_uid(with_server):
mail = Mail.create(['user'], mail_from='me', subject='hello', content='pleased to meet you, wont you guess my name ?')
with_server.receive(mail)
imap_client = await login_user('user', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(RFC822)')), 1)
assert 'OK' == result
assert [(b'1 (UID 1 RFC822 {360}', mail.as_bytes()), b')'] == data
@pytest.mark.asyncio
async def test_fetch_bad_range(with_server):
imap_client = await login_user('user', 'pass', select=True)
with pytest.raises(Exception) as expected:
await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '0:*', '(RFC822)')), 1)
assert 'UID command error: BAD [b\'Error in IMAP command: Invalid uidset\']' == str(expected)
with pytest.raises(Exception) as expected:
await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '2:0', '(RFC822)')), 1)
assert 'UID command error: BAD [b\'Error in IMAP command: Invalid uidset\']' == str(expected)
@pytest.mark.asyncio
async def test_fetch_one_message_by_uid_with_bodypeek(with_server):
mail = Mail.create(['user'], mail_from='me', subject='hello', content='this mail is still unread')
with_server.receive(mail)
imap_client = await login_user('user', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(UID BODY.PEEK[])')), 1)
assert 'OK' == result
assert [(b'1 (UID 1 BODY.PEEK[] {340}', mail.as_bytes()), b')'] == data
@pytest.mark.asyncio
async def test_fetch_one_messages_by_uid_without_body(with_server):
mail = Mail.create(['user'], mail_from='me', subject='hello', content='whatever')
with_server.receive(mail)
imap_client = await login_user('user', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(UID FLAGS)')), 1)
assert 'OK' == result
assert [(b'1 (UID 1 FLAGS ())')] == data
@pytest.mark.asyncio
async def test_fetch_one_messages_by_id_without_body(with_server):
mail = Mail.create(['user'], mail_from='me', subject='hello', content='whatever')
with_server.receive(mail)
imap_client = await login_user('user', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.fetch, '1', '(UID FLAGS)')), 1)
assert [(b'1 (UID 1 FLAGS ())')] == data
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.fetch, '1', '(FLAGS)')), 1)
assert [(b'1 (FLAGS ())')] == data
@pytest.mark.asyncio
async def test_fetch_messages_by_uid_range(with_server):
mail = Mail.create(['user'], mail_from='me', subject='hello', content='whatever')
with_server.receive(mail)
imap_client = await login_user('user', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1:1', '(FLAGS)')), 1)
assert [(b'1 (UID 1 FLAGS ())')] == data
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.fetch, '1:*', '(UID FLAGS)')), 1)
assert [(b'1 (UID 1 FLAGS ())')] == data
@pytest.mark.asyncio
async def test_fetch_one_messages_by_uid_encoding_cp1252(with_server):
with_server.receive(Mail.create(['user'], mail_from='me', subject='hello', content='maître', encoding='cp1252'))
imap_client = await login_user('user', 'pass', select=True)
_, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(RFC822)')), 1)
mail_content = data[0][1]
assert b'charset="cp1252"' in mail_content
assert b'ma\xeetre' in mail_content
assert 'maître' == email.message_from_bytes(mail_content).get_payload().strip()
@pytest.mark.asyncio
async def test_fetch_one_messages_out_of_two(with_server):
with_server.receive(Mail.create(['user'], mail_from='me', subject='hello', content='maître'))
with_server.receive(Mail.create(['user'], mail_from='you', subject='yo', content='bro'))
imap_client = await login_user('user', 'pass', select=True)
_, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(RFC822)')), 1)
assert 2 == len(data)
@pytest.mark.asyncio
async def test_fetch_one_message_with_headers(with_server):
with_server.receive(Mail.create(['user'], mail_from='me', subject='hello', content='maître'))
imap_client = await login_user('user', 'pass', select=True)
_, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(BODY.PEEK[HEADER.FIELDS (Content-type From)])')), 1)
assert b'1 (UID 1 BODY[HEADER.FIELDS (Content-type From)] {57}' == data[0][0]
assert b'Content-type: text/plain; charset="utf-8"\r\nFrom: <me>\r\n\r\n' == data[0][1]
@pytest.mark.asyncio
async def test_store(with_server):
with_server.receive(Mail.create(['user']))
imap_client = await login_user('user', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'store', '1', '+FLAGS.SILENT (\Seen \Answered)')), 1)
assert 'OK' == result
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', 'UID (FLAGS)')), 1)
assert 'OK' == result
assert [b'1 (UID 1 FLAGS (\Seen \Answered))'] == data
@pytest.mark.asyncio
async def test_store_and_search_by_keyword(with_server):
with_server.receive(Mail.create(['user']))
with_server.receive(Mail.create(['user']))
imap_client = await login_user('user', 'pass', select=True)
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'KEYWORD FOO')), 1)
assert 'OK' == result
assert [b''] == data
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'store', '1', '+FLAGS (FOO)')), 1)
assert 'OK' == result
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'KEYWORD FOO')), 1)
assert 'OK' == result
assert [b'1'] == data
result, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'UNKEYWORD FOO')), 1)
assert 'OK' == result
assert [b'2'] == data
@pytest.mark.asyncio
async def test_search_by_uid_range(with_server):
with_server.receive(Mail.create(['user']))
with_server.receive(Mail.create(['user']))
imap_client = await login_user('user', 'pass', select=True)
_, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'search', None, '1:2')), 1)
assert [b'1 2'] == data
_, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'search', None, '1:*')), 1)
assert [b'1 2'] == data
_, data = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.uid, 'search', None, '1:1')), 1)
assert [b'1'] == data
@pytest.mark.asyncio
async def test_expunge_messages(with_server):
with_server.receive(Mail.create(['user']))
with_server.receive(Mail.create(['user']))
imap_client = await login_user('user', 'pass', select=True)
await asyncio.wait_for(asyncio.get_running_loop().run_in_executor(None, imap_client.expunge), 1)
assert ('OK', [b'0']) == (await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select)), 1))
@pytest.mark.asyncio
async def test_noop(with_server):
imap_client = await login_user('user', 'pass', select=True)
assert ('OK', [b'NOOP completed.']) == \
(await asyncio.wait_for(asyncio.get_running_loop().run_in_executor(None, imap_client.noop), 1))
@pytest.mark.asyncio
async def test_check(with_server):
imap_client = await login_user('user', 'pass', select=True)
assert ('OK', [b'CHECK completed.']) == \
(await asyncio.wait_for(asyncio.get_running_loop().run_in_executor(None, imap_client.check), 1))
@pytest.mark.asyncio
async def test_status(with_server):
imap_client = await login_user('user', 'pass')
assert ('OK', [b'INBOX (MESSAGES 0 UIDNEXT 1)']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.status, 'INBOX',
'(MESSAGES UIDNEXT)')), 1))
@pytest.mark.asyncio
async def test_subscribe_unsubscribe_lsub(with_server):
imap_client = await login_user('user', 'pass')
assert ('OK', [b'SUBSCRIBE completed.']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(
imap_client.subscribe, '#fr.soc.feminisme')), 1))
assert ('OK', [b'() "." #fr.soc.feminisme']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(
imap_client.lsub, '#fr', 'soc.*')), 1))
assert ('OK', [b'UNSUBSCRIBE completed.']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(
imap_client.unsubscribe, '#fr.soc.feminisme')), 1))
assert ('OK', [None]) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(
imap_client.lsub, '#fr', '.*')), 1))
@pytest.mark.asyncio
async def test_close(with_server):
imap_client = await login_user('user', 'pass', select=True)
assert SELECTED == with_server.get_connection('user').state
assert ('OK', [b'CLOSE completed.']) == \
(await asyncio.wait_for(asyncio.get_running_loop().run_in_executor(None, imap_client.close), 1))
assert AUTH == with_server.get_connection('user').state
@pytest.mark.asyncio
async def test_copy_messages(with_server):
with_server.receive(Mail.create(['user']))
with_server.receive(Mail.create(['user']))
imap_client = await login_user('user', 'pass', select=True)
result, _ = await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.copy, '1 2', 'MAILBOX')), 20)
assert 'OK' == result
assert ('OK', [b'2']) == (await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select, 'MAILBOX')), 20))
@pytest.mark.asyncio
async def test_create_delete_mailbox(with_server):
imap_client = await login_user('user', 'pass')
assert ('NO', [b'STATUS completed.']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))
assert ('OK', [b'CREATE completed.']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.create, 'MBOX')), 1))
assert ('OK', [b'MBOX (MESSAGES 0)']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))
assert ('OK', [b'DELETE completed.']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.delete, 'MBOX')), 1))
assert ('NO', [b'STATUS completed.']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))
@pytest.mark.asyncio
async def test_rename_mailbox(with_server):
with_server.receive(Mail.create(['user']))
imap_client = await login_user('user', 'pass')
assert ('NO', [b'STATUS completed.']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))
assert ('OK', [b'RENAME completed.']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.rename, 'INBOX', 'MBOX')), 1))
assert ('OK', [b'MBOX (MESSAGES 1)']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))
@pytest.mark.asyncio
async def test_list(with_server):
imap_client = await login_user('user', 'pass')
assert ('OK', [b'() "/" Drafts', b'() "/" INBOX', b'() "/" Sent', b'() "/" Trash']) == \
(await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.list, '""', '*')), 1))
@pytest.mark.asyncio
async def test_append(with_server):
imap_client = await login_user('user@mail', 'pass')
assert ('OK', [b'0']) == (await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select, 'INBOX', readonly=True)), 2))
msg = Mail.create(['user@mail'], subject='append msg', content='do you see me ?')
assert 'OK' == (await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(
imap_client.append, 'INBOX', 'FOO BAR', datetime.now(tz=utc), msg.as_bytes())), 2))[0]
assert ('OK', [b'1']) == (await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select, 'INBOX', readonly=True)), 2))
@pytest.mark.asyncio
async def test_logout(with_server):
imap_client = await login_user('user', 'pass')
result, data = await asyncio.wait_for(asyncio.get_running_loop().run_in_executor(None, imap_client.logout), 1)
assert 'BYE' == result # uhh ?
assert [b'Logging out'] == data
assert LOGOUT == with_server.get_connection('user').state
@pytest.mark.asyncio
async def test_rfc5032_within(with_server):
with_server.receive(Mail.create(['user'], date=datetime.now(tz=utc) - timedelta(seconds=84600 * 3))) # 1
with_server.receive(Mail.create(['user'], date=datetime.now(tz=utc) - timedelta(seconds=84600))) # 2
with_server.receive(Mail.create(['user'])) # 3
imap_client = await login_user('user', 'pass', select=True)
assert [b'2 3'] == (await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.search, 'utf-8', 'YOUNGER', '84700')), 1))[1]
assert [b'1'] == (await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.search, 'utf-8', 'OLDER', '84700')), 1))[1]
@pytest.mark.asyncio
async def test_getquotaroot(with_server):
imap_client = await login_user('user', 'pass')
with_server.receive(Mail.create(['user']))
assert ('OK', [[b'INBOX INBOX'], [b'INBOX (STORAGE 292 5000)']]) == \
(await asyncio.wait_for(asyncio.get_running_loop().run_in_executor(None,
functools.partial(imap_client.getquotaroot, 'INBOX')), 1))
@pytest.mark.asyncio()
async def test_client_can_connect_to_server_over_ssl(with_ssl, with_ssl_server):
ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=with_ssl[1])
pending_imap = asyncio.get_running_loop().run_in_executor(None, functools.partial(
imaplib.IMAP4_SSL,
host='127.0.0.1',
port=12345,
ssl_context=ssl_context)
)
imap_client = await asyncio.wait_for(pending_imap, 1)
assert 'NONAUTH' == imap_client.state
|