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