| 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 logging |
| 19 |
import ssl |
| 20 |
import unittest |
| 21 |
from datetime import datetime, timedelta |
| 22 |
from unittest.mock import call, MagicMock |
| 23 |
|
| 24 |
import pytest |
| 25 |
from pytz import utc |
| 26 |
|
| 27 |
from aioimaplib import aioimaplib, extract_exists, STOP_WAIT_SERVER_PUSH, FetchCommand, IdleCommand |
| 28 |
from aioimaplib.aioimaplib import Commands, IMAP4ClientProtocol, Command, Abort |
| 29 |
from aioimaplib import imap_testing_server |
| 30 |
from aioimaplib.imap_testing_server import Mail |
| 31 |
from tests.server_fixture import with_ssl_server, with_ssl, with_server, login_user_async, create_server |
| 32 |
|
| 33 |
aioimaplib.log.setLevel(logging.WARNING) |
| 34 |
sh = logging.StreamHandler() |
| 35 |
sh.setLevel(logging.INFO) |
| 36 |
sh.setFormatter(logging.Formatter("%(asctime)s %(levelname)s [%(module)s:%(lineno)d] %(message)s")) |
| 37 |
aioimaplib.log.addHandler(sh) |
| 38 |
|
| 39 |
|
| 40 |
class TestAioimaplibUtils(unittest.TestCase): |
| 41 |
def setUp(self): |
| 42 |
self.imap_protocol = IMAP4ClientProtocol(None) |
| 43 |
self.imap_protocol._handle_line = MagicMock(return_value=None) |
| 44 |
aioimaplib.get_running_loop = asyncio.new_event_loop # monkey patch to avoid Exception "No running loop" |
| 45 |
|
| 46 |
def test_split_responses_no_data(self): |
| 47 |
self.imap_protocol.data_received(b'') |
| 48 |
self.imap_protocol._handle_line.assert_not_called() |
| 49 |
|
| 50 |
def test_split_responses_regular_lines(self): |
| 51 |
self.imap_protocol.data_received(b'* BYE Logging out\r\nCAPB2 OK LOGOUT completed\r\n') |
| 52 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* BYE Logging out', None), call(b'CAPB2 OK LOGOUT completed', None)]) |
| 53 |
|
| 54 |
def test_split_responses_with_message_data(self): |
| 55 |
cmd = Command('FETCH', 'TAG') |
| 56 |
self.imap_protocol._handle_line = MagicMock(return_value=cmd) |
| 57 |
self.imap_protocol.data_received(b'* 1 FETCH (UID 1 RFC822 {26}\r\n...\r\n(mail content)\r\n...\r\n)\r\n' |
| 58 |
b'TAG OK FETCH completed.\r\n') |
| 59 |
|
| 60 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* 1 FETCH (UID 1 RFC822 {26}', None)]) |
| 61 |
self.imap_protocol._handle_line.assert_has_calls([call(b')', cmd)]) |
| 62 |
self.imap_protocol._handle_line.assert_has_calls([call(b'TAG OK FETCH completed.', None)]) |
| 63 |
assert [b'...\r\n(mail content)\r\n...\r\n'] == cmd.response.lines |
| 64 |
|
| 65 |
def test_split_responses_with_two_messages_data(self): |
| 66 |
cmd = Command('FETCH', 'TAG') |
| 67 |
self.imap_protocol._handle_line = MagicMock(return_value=cmd) |
| 68 |
self.imap_protocol.data_received(b'* 3 FETCH (UID 3 RFC822 {6}\r\nmail 1)\r\n' |
| 69 |
b'* 4 FETCH (UID 4 RFC822 {6}\r\nmail 2)\r\n' |
| 70 |
b'TAG OK FETCH completed.\r\n') |
| 71 |
|
| 72 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* 3 FETCH (UID 3 RFC822 {6}', None), |
| 73 |
call(b')', cmd), |
| 74 |
call(b'* 4 FETCH (UID 4 RFC822 {6}', None), |
| 75 |
call(b')', cmd), |
| 76 |
call(b'TAG OK FETCH completed.', None)]) |
| 77 |
assert [b'mail 1', b'mail 2'] == cmd.response.lines |
| 78 |
|
| 79 |
def test_split_responses_with_flag_fetch_message_data(self): |
| 80 |
self.imap_protocol.data_received(b'* 1 FETCH (UID 10 FLAGS (FOO))\r\n' |
| 81 |
b'* 1 FETCH (UID 15 FLAGS (BAR))\r\n' |
| 82 |
b'TAG OK STORE completed.\r\n') |
| 83 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* 1 FETCH (UID 10 FLAGS (FOO))', None), |
| 84 |
call(b'* 1 FETCH (UID 15 FLAGS (BAR))', None), |
| 85 |
call(b'TAG OK STORE completed.', None)]) |
| 86 |
|
| 87 |
def test_split_responses_with_message_data_expunge(self): |
| 88 |
self.imap_protocol.data_received(b'* 123 EXPUNGE\r\nTAG OK SELECT completed.\r\n') |
| 89 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* 123 EXPUNGE', None), |
| 90 |
call(b'TAG OK SELECT completed.', None)]) |
| 91 |
|
| 92 |
def test_incomplete_line_with_literal_fetch(self): |
| 93 |
cmd = Command('FETCH', 'TAG') |
| 94 |
self.imap_protocol._handle_line = MagicMock(return_value=cmd) |
| 95 |
self.imap_protocol.data_received(b'* 12 FETCH (BODY[HEADER] {4}\r\nyo\r\n)\r\n* 13 FETCH (BODY[') |
| 96 |
self.imap_protocol.data_received(b'HEADER] {5}\r\nyo2\r\n)\r\nTAG OK STORE completed.\r\n') |
| 97 |
|
| 98 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* 12 FETCH (BODY[HEADER] {4}', None), call(b')', cmd)]) |
| 99 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* 13 FETCH (BODY[HEADER] {5}', None), |
| 100 |
call(b')', cmd), |
| 101 |
call(b'TAG OK STORE completed.', None)]) |
| 102 |
assert [b'yo\r\n', b'yo2\r\n'] == cmd.response.lines |
| 103 |
|
| 104 |
def test_incomplete_lines_during_literal(self): |
| 105 |
cmd = Command('LIST', 'TAG') |
| 106 |
self.imap_protocol._handle_line = MagicMock(return_value=cmd) |
| 107 |
|
| 108 |
self.imap_protocol.data_received(b'* LIST () "/" {11}\r\nfoo/') |
| 109 |
self.imap_protocol.data_received(b'bar/') |
| 110 |
self.imap_protocol.data_received(b'baz\r\n* LIST () "/" qux\r\nTAG OK LIST completed\r\n') |
| 111 |
|
| 112 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* LIST () "/" {11}', None)]) |
| 113 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* LIST () "/" qux', None), |
| 114 |
call(b'TAG OK LIST completed', None)]) |
| 115 |
assert [b'foo/bar/baz'] == cmd.response.lines |
| 116 |
|
| 117 |
def test_incomplete_line_during_literal_no_cmd_found(self): |
| 118 |
self.imap_protocol.data_received(b'* LIST () "/" {7}\r\nfoo/') |
| 119 |
self.imap_protocol.data_received(b'bar\r\nTAG OK LIST completed\r\n') |
| 120 |
|
| 121 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* LIST () "/" {7}', None)]) |
| 122 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* LIST () "/" {7}', None), |
| 123 |
call(b'', Command('NIL', 'unused')), |
| 124 |
call(b'TAG OK LIST completed', None)]) |
| 125 |
|
| 126 |
def test_line_with_literal_no_cmd_found_no_AttributeError_thrown(self): |
| 127 |
self.imap_protocol.data_received(b'* 3 FETCH (UID 12 RFC822 {4}\r\nmail)\r\n' |
| 128 |
b'TAG OK FETCH completed.\r\n') |
| 129 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* 3 FETCH (UID 12 RFC822 {4}', None), |
| 130 |
call(b')', Command('NIL', 'unused')), |
| 131 |
call(b'TAG OK FETCH completed.', None)]) |
| 132 |
|
| 133 |
def test_line_with_attachment_literals(self): |
| 134 |
cmd = Command('FETCH', 'TAG') |
| 135 |
self.imap_protocol._handle_line = MagicMock(return_value=cmd) |
| 136 |
|
| 137 |
self.imap_protocol.data_received(b'* 46 FETCH (UID 46 FLAGS () BODYSTRUCTURE (' |
| 138 |
b'("text" "calendar" ("charset" "UTF-8" "name" {16}\r\nG\xe9n\xe9ration 3.ics)' |
| 139 |
b' "<mqwssinzuqvhkzlnhlcq>" NIL "quoted-printable" 365 14 NIL ' |
| 140 |
b'("attachment" ("filename" {16}\r\nG\xe9n\xe9ration 3.ics)))\r\n') |
| 141 |
|
| 142 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* 46 FETCH (UID 46 FLAGS () BODYSTRUCTURE (' |
| 143 |
b'("text" "calendar" ("charset" "UTF-8" "name" {16}', None), |
| 144 |
call(b') "<mqwssinzuqvhkzlnhlcq>" NIL "quoted-printable" 365 14 NIL ' |
| 145 |
b'("attachment" ("filename" {16}', cmd), |
| 146 |
call(b')))', cmd)]) |
| 147 |
assert [b'G\xe9n\xe9ration 3.ics', b'G\xe9n\xe9ration 3.ics'] == cmd.response.lines |
| 148 |
|
| 149 |
def test_incomplete_line_followed_by_incomplete_literal(self): |
| 150 |
cmd = Command('FETCH', 'TAG') |
| 151 |
self.imap_protocol._handle_line = MagicMock(return_value=cmd) |
| 152 |
|
| 153 |
self.imap_protocol.data_received(b'* 2 FETCH (') |
| 154 |
self.imap_protocol.data_received(b'FLAGS () UID 160016 BODY[] {10}\r\non the ') |
| 155 |
self.imap_protocol.data_received(b'dot)\r\nTAG OK FETCH completed\r\n') |
| 156 |
|
| 157 |
self.imap_protocol._handle_line.assert_has_calls([call(b'* 2 FETCH (FLAGS () UID 160016 BODY[] {10}', None), |
| 158 |
call(b')', cmd), call(b'TAG OK FETCH completed', None)]) |
| 159 |
assert [b'on the dot'] == cmd.response.lines |
| 160 |
|
| 161 |
# cf 1st FETCH in https://tools.ietf.org/html/rfc3501#section-8 example |
| 162 |
def test_incomplete_fetch_message_attributes_without_literal(self): |
| 163 |
cmd = FetchCommand('TAG') |
| 164 |
self.imap_protocol._handle_line = MagicMock(return_value=cmd) |
| 165 |
|
| 166 |
line = b'* 12 FETCH (FLAGS (\Seen) BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 3028 \r\n' |
| 167 |
cmd.append_to_resp(line) |
| 168 |
self.imap_protocol.data_received(line) |
| 169 |
line = b'92))\r\nTAG OK FETCH completed\r\n' |
| 170 |
cmd.append_to_resp(line) |
| 171 |
self.imap_protocol.data_received(line) |
| 172 |
|
| 173 |
self.imap_protocol._handle_line.assert_has_calls( |
| 174 |
[call(b'* 12 FETCH (FLAGS (\Seen) BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 3028 ', None), |
| 175 |
call(b'92))', cmd), call(b'TAG OK FETCH completed', None)]) |
| 176 |
|
| 177 |
def test_incomplete_fetch_with_incomplete_line(self): |
| 178 |
cmd = FetchCommand('TAG') |
| 179 |
self.imap_protocol._handle_line = MagicMock(return_value=cmd) |
| 180 |
|
| 181 |
self.imap_protocol.data_received(b'* 21 FETCH (FLAGS (\Seen) BODY[] {16}\r\nincomplete fetch') |
| 182 |
self.imap_protocol.data_received(b')\r\nTAG OK FETCH completed\r\n') |
| 183 |
|
| 184 |
self.imap_protocol._handle_line.assert_has_calls( |
| 185 |
[call(b'* 21 FETCH (FLAGS (\Seen) BODY[] {16}', None), |
| 186 |
call(b')', cmd), call(b'TAG OK FETCH completed', None)]) |
| 187 |
|
| 188 |
def test_command_repr(self): |
| 189 |
assert 'tag NAME' == str(Command('NAME', 'tag')) |
| 190 |
assert 'tag NAME arg1 arg2' == str(Command('NAME', 'tag', 'arg1', 'arg2')) |
| 191 |
assert 'tag UID NAME arg' == str(Command('NAME', 'tag', 'arg', prefix='UID')) |
| 192 |
assert 'tag UID NAME' == str(Command('NAME', 'tag', prefix='UID')) |
| 193 |
|
| 194 |
|
| 195 |
class TestDataReceived(unittest.TestCase): |
| 196 |
def setUp(self): |
| 197 |
self.imap_protocol = IMAP4ClientProtocol(None) |
| 198 |
aioimaplib.get_running_loop = asyncio.new_event_loop # monkey patch to avoid Exception "No running loop" |
| 199 |
|
| 200 |
def test_when_idle_continuation_line_in_same_dataframe_as_status_update(self): |
| 201 |
queue = asyncio.Queue() |
| 202 |
cmd = IdleCommand('TAG', queue) |
| 203 |
self.imap_protocol.pending_sync_command = cmd |
| 204 |
self.imap_protocol.data_received(b'+ idling\r\n* 1 EXISTS\r\n* 1 RECENT\r\n') |
| 205 |
|
| 206 |
assert self.imap_protocol._idle_event.is_set() |
| 207 |
assert [b'1 EXISTS', b'1 RECENT'] == queue.get_nowait() |
| 208 |
|
| 209 |
|
| 210 |
class TestFetchWaitsForAllMessageAttributes(unittest.TestCase): |
| 211 |
def setUp(self): |
| 212 |
aioimaplib.get_running_loop = asyncio.new_event_loop # monkey patch to avoid Exception "No running loop" |
| 213 |
|
| 214 |
def test_empty_fetch(self): |
| 215 |
assert not FetchCommand('TAG').wait_data() |
| 216 |
|
| 217 |
def test_simple_fetch(self): |
| 218 |
fetch = FetchCommand('TAG') |
| 219 |
fetch.append_to_resp(b'12 FETCH (FLAGS (\Seen))') |
| 220 |
|
| 221 |
assert not fetch.wait_data() |
| 222 |
|
| 223 |
def test_simple_fetch_with_two_lines(self): |
| 224 |
fetch = FetchCommand('TAG') |
| 225 |
fetch.append_to_resp(b'12 FETCH (FLAGS (\Seen) BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 3028') |
| 226 |
assert fetch.wait_data() |
| 227 |
|
| 228 |
fetch.append_to_resp(b'92))') |
| 229 |
assert not fetch.wait_data() |
| 230 |
|
| 231 |
def test_fetch_with_literal(self): |
| 232 |
fetch = FetchCommand('TAG') |
| 233 |
fetch.append_to_resp(b'12 FETCH (FLAGS () BODY[] {13}') |
| 234 |
fetch.begin_literal_data(13, b'literal (data') |
| 235 |
fetch.append_to_resp(b')') |
| 236 |
|
| 237 |
assert not fetch.wait_data() |
| 238 |
|
| 239 |
def test_fetch_only_the_last_message_data(self): |
| 240 |
fetch = FetchCommand('TAG') |
| 241 |
fetch.append_to_resp(b'12 FETCH (FLAGS (\Seen)') # not closed on purpose |
| 242 |
assert fetch.wait_data() |
| 243 |
|
| 244 |
fetch.append_to_resp(b'13 FETCH (FLAGS (\Seen)') |
| 245 |
assert fetch.wait_data() |
| 246 |
|
| 247 |
fetch.append_to_resp(b')') |
| 248 |
assert not fetch.wait_data() |
| 249 |
|
| 250 |
|
| 251 |
@pytest.mark.asyncio() |
| 252 |
async def test_login(with_server): |
| 253 |
imap_client = aioimaplib.IMAP4(port=12345, loop=asyncio.get_running_loop(), timeout=3) |
| 254 |
await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) |
| 255 |
|
| 256 |
result, data = await imap_client.login('user', 'password') |
| 257 |
|
| 258 |
assert aioimaplib.AUTH == imap_client.protocol.state |
| 259 |
assert 'OK' == result |
| 260 |
assert b'LOGIN completed' == data[-1] |
| 261 |
assert imap_client.has_capability('IDLE') |
| 262 |
assert imap_client.has_capability('UIDPLUS') |
| 263 |
|
| 264 |
|
| 265 |
@pytest.mark.asyncio() |
| 266 |
async def test_xoauth2(with_server): |
| 267 |
imap_client = aioimaplib.IMAP4(port=12345, loop=asyncio.get_running_loop(), timeout=3) |
| 268 |
await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) |
| 269 |
|
| 270 |
result, data = await imap_client.xoauth2('user', 'myspecialtoken') |
| 271 |
|
| 272 |
assert aioimaplib.AUTH == imap_client.protocol.state |
| 273 |
assert 'OK' == result |
| 274 |
assert b'AUTHENTICATE completed' == data[-1] |
| 275 |
|
| 276 |
|
| 277 |
@pytest.mark.asyncio() |
| 278 |
async def test_login_with_special_characters(with_server): |
| 279 |
imap_client = aioimaplib.IMAP4(port=12345, loop=asyncio.get_running_loop(), timeout=3) |
| 280 |
await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) |
| 281 |
|
| 282 |
result, data = await imap_client.login('user', 'pass"word') |
| 283 |
|
| 284 |
assert aioimaplib.AUTH == imap_client.protocol.state |
| 285 |
assert 'OK' == result |
| 286 |
assert b'LOGIN completed' == data[-1] |
| 287 |
|
| 288 |
|
| 289 |
@pytest.mark.asyncio() |
| 290 |
async def test_login_twice(with_server): |
| 291 |
with pytest.raises(aioimaplib.Error) as expected: |
| 292 |
imap_client = await login_user_async('user', 'pass') |
| 293 |
|
| 294 |
await imap_client.login('user', 'password') |
| 295 |
|
| 296 |
assert expected == 'command LOGIN illegal in state AUTH' |
| 297 |
|
| 298 |
|
| 299 |
@pytest.mark.asyncio() |
| 300 |
async def test_logout(with_server): |
| 301 |
imap_client = await login_user_async('user', 'pass') |
| 302 |
|
| 303 |
result, data = await imap_client.logout() |
| 304 |
|
| 305 |
assert 'OK' == result |
| 306 |
assert [b'BYE Logging out', b'LOGOUT completed'] == data |
| 307 |
assert aioimaplib.LOGOUT == imap_client.protocol.state |
| 308 |
|
| 309 |
|
| 310 |
@pytest.mark.asyncio() |
| 311 |
async def test_select_no_messages(with_server): |
| 312 |
imap_client = await login_user_async('user', 'pass') |
| 313 |
|
| 314 |
resp = await imap_client.select() |
| 315 |
|
| 316 |
assert 'OK' == resp[0] |
| 317 |
assert 0 == extract_exists(resp) |
| 318 |
assert aioimaplib.SELECTED == imap_client.protocol.state |
| 319 |
|
| 320 |
|
| 321 |
@pytest.mark.asyncio() |
| 322 |
async def test_examine_no_messages(with_server): |
| 323 |
imap_client = await login_user_async('user', 'pass') |
| 324 |
|
| 325 |
assert 0 == extract_exists((await imap_client.examine())) |
| 326 |
|
| 327 |
assert aioimaplib.AUTH == imap_client.protocol.state |
| 328 |
|
| 329 |
|
| 330 |
@pytest.mark.asyncio() |
| 331 |
async def test_search_two_messages(with_server): |
| 332 |
with_server.receive(Mail.create(['user'])) |
| 333 |
with_server.receive(Mail.create(['user'])) |
| 334 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 335 |
|
| 336 |
result, data = await imap_client.search('ALL') |
| 337 |
|
| 338 |
assert 'OK' == result |
| 339 |
assert b'1 2' == data[0] |
| 340 |
|
| 341 |
|
| 342 |
@pytest.mark.asyncio() |
| 343 |
async def test_search_messages(with_server): |
| 344 |
"""Increase compatibility with https://docs.python.org/3/library/imaplib.html#imap4-example.""" |
| 345 |
with_server.receive(Mail.create(['user'])) |
| 346 |
with_server.receive(Mail.create(['user'])) |
| 347 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 348 |
|
| 349 |
# E.g. typ, data = M.search(None, 'ALL') |
| 350 |
result, data = await imap_client.search(None, 'ALL') |
| 351 |
|
| 352 |
assert 'OK' == result |
| 353 |
assert b'1 2' == data[0] |
| 354 |
|
| 355 |
|
| 356 |
@pytest.mark.asyncio() |
| 357 |
async def test_uid_with_illegal_command(with_server): |
| 358 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 359 |
|
| 360 |
for command in {'COPY', 'FETCH', 'STORE', 'EXPUNGE', 'MOVE'}.symmetric_difference(Commands.keys()): |
| 361 |
with pytest.raises(aioimaplib.Abort) as expected: |
| 362 |
await imap_client.uid(command) |
| 363 |
|
| 364 |
assert ('command UID only possible with COPY, FETCH, EXPUNGE (w/UIDPLUS) or STORE (was %s)' % command) in str(expected) |
| 365 |
|
| 366 |
|
| 367 |
@pytest.mark.asyncio() |
| 368 |
async def test_search_three_messages_by_uid(with_server): |
| 369 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 370 |
with_server.receive(Mail.create(['user'])) # id=1 uid=1 |
| 371 |
with_server.receive(Mail.create(['user']), mailbox='OTHER_MAILBOX') # id=1 uid=1 |
| 372 |
with_server.receive(Mail.create(['user'])) # id=2 uid=2 |
| 373 |
|
| 374 |
assert b'1 2' == (await imap_client.search('ALL')).lines[0] |
| 375 |
assert b'1 2' == (await imap_client.uid_search('ALL')).lines[0] |
| 376 |
|
| 377 |
await imap_client.select('OTHER_MAILBOX') |
| 378 |
assert b'1' == (await imap_client.uid_search('ALL')).lines[0] |
| 379 |
|
| 380 |
|
| 381 |
@pytest.mark.asyncio() |
| 382 |
async def test_fetch(with_server): |
| 383 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 384 |
mail = Mail.create(['user'], mail_from='me', subject='hello', |
| 385 |
content='pleased to meet you, wont you guess my name ?') |
| 386 |
with_server.receive(mail) |
| 387 |
|
| 388 |
result, data = await imap_client.fetch('1', '(RFC822)') |
| 389 |
content = mail.as_bytes() |
| 390 |
|
| 391 |
assert 'OK' == result |
| 392 |
assert [ |
| 393 |
b'1 FETCH (RFC822 {%d}' % len(content), content, b')', |
| 394 |
b'FETCH completed.' |
| 395 |
] == data |
| 396 |
|
| 397 |
|
| 398 |
@pytest.mark.asyncio() |
| 399 |
async def test_fetch_by_uid_without_body(with_server): |
| 400 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 401 |
mail = Mail.create(['user'], mail_from='me', subject='hello', |
| 402 |
content='pleased to meet you, wont you guess my name ?') |
| 403 |
with_server.receive(mail) |
| 404 |
|
| 405 |
response = (await imap_client.uid('fetch', '1', '(UID FLAGS)')) |
| 406 |
|
| 407 |
assert 'OK' == response.result |
| 408 |
assert b'1 FETCH (UID 1 FLAGS ())' == response.lines[0] |
| 409 |
|
| 410 |
|
| 411 |
@pytest.mark.asyncio() |
| 412 |
async def test_fetch_by_uid(with_server): |
| 413 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 414 |
mail = Mail.create(['user'], mail_from='me', subject='hello', |
| 415 |
content='pleased to meet you, wont you guess my name ?') |
| 416 |
with_server.receive(mail) |
| 417 |
|
| 418 |
response = (await imap_client.uid('fetch', '1', '(RFC822)')) |
| 419 |
assert 'OK' == response.result |
| 420 |
assert mail.as_bytes() == response.lines[1] |
| 421 |
|
| 422 |
|
| 423 |
@pytest.mark.asyncio() |
| 424 |
async def test_idle(with_server): |
| 425 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 426 |
|
| 427 |
idle = await imap_client.idle_start(timeout=0.3) |
| 428 |
with_server.receive(Mail.create(to=['user'], mail_from='me', subject='hello')) |
| 429 |
|
| 430 |
assert [b'1 EXISTS', b'1 RECENT'] == (await imap_client.wait_server_push()) |
| 431 |
|
| 432 |
imap_client.idle_done() |
| 433 |
assert ('OK', [b'IDLE terminated']) == (await asyncio.wait_for(idle, 1)) |
| 434 |
|
| 435 |
assert imap_client._idle_waiter._cancelled |
| 436 |
with pytest.raises(asyncio.TimeoutError): |
| 437 |
await imap_client.wait_server_push(timeout=0.1) |
| 438 |
|
| 439 |
|
| 440 |
@pytest.mark.asyncio() |
| 441 |
async def test_idle_loop(with_server): |
| 442 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 443 |
|
| 444 |
idle = await imap_client.idle_start(timeout=0.3) |
| 445 |
with_server.receive(Mail.create(to=['user'], mail_from='me', subject='hello')) |
| 446 |
|
| 447 |
data = list() |
| 448 |
while imap_client.has_pending_idle(): |
| 449 |
data.append((await imap_client.wait_server_push())) |
| 450 |
if data[-1] == STOP_WAIT_SERVER_PUSH: |
| 451 |
imap_client.idle_done() |
| 452 |
await asyncio.wait_for(idle, 1) |
| 453 |
|
| 454 |
assert [[b'1 EXISTS', b'1 RECENT'], STOP_WAIT_SERVER_PUSH] == data |
| 455 |
|
| 456 |
|
| 457 |
@pytest.mark.asyncio() |
| 458 |
async def test_idle_stop(with_server): |
| 459 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 460 |
idle = await imap_client.idle_start() |
| 461 |
|
| 462 |
assert (await imap_client.stop_wait_server_push()) |
| 463 |
|
| 464 |
assert STOP_WAIT_SERVER_PUSH == (await imap_client.wait_server_push()) |
| 465 |
|
| 466 |
imap_client.idle_done() |
| 467 |
await asyncio.wait_for(idle, 1) |
| 468 |
|
| 469 |
|
| 470 |
@pytest.mark.asyncio() |
| 471 |
async def test_idle_stop_does_nothing_if_no_pending_idle(with_server): |
| 472 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 473 |
|
| 474 |
assert not (await imap_client.stop_wait_server_push()) |
| 475 |
|
| 476 |
|
| 477 |
@pytest.mark.asyncio() |
| 478 |
async def test_idle_error_response(with_server): |
| 479 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 480 |
|
| 481 |
conn = with_server.get_connection('user') |
| 482 |
def idle_error(tag, *args): |
| 483 |
conn.error(tag, "Error initiating IDLE") |
| 484 |
conn.idle = idle_error |
| 485 |
|
| 486 |
with pytest.raises(Abort): |
| 487 |
await imap_client.idle_start() |
| 488 |
|
| 489 |
|
| 490 |
@pytest.mark.asyncio() |
| 491 |
async def test_store_and_search_by_keyword(with_server): |
| 492 |
with_server.receive(Mail.create(['user'])) |
| 493 |
with_server.receive(Mail.create(['user'])) |
| 494 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 495 |
assert b'' == (await imap_client.uid_search('KEYWORD FOO', charset=None)).lines[0] |
| 496 |
|
| 497 |
assert 'OK' == (await imap_client.uid('store', '1', '+FLAGS (FOO)')).result |
| 498 |
|
| 499 |
assert b'1' == (await imap_client.uid_search('KEYWORD FOO', charset=None)).lines[0] |
| 500 |
assert b'2' == (await imap_client.uid_search('UNKEYWORD FOO', charset=None)).lines[0] |
| 501 |
|
| 502 |
|
| 503 |
@pytest.mark.asyncio() |
| 504 |
async def test_expunge_messages(with_server): |
| 505 |
with_server.receive(Mail.create(['user'])) |
| 506 |
with_server.receive(Mail.create(['user'])) |
| 507 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 508 |
|
| 509 |
assert ('OK', [b'1 EXPUNGE', b'2 EXPUNGE', b'EXPUNGE completed.']) == (await imap_client.expunge()) |
| 510 |
|
| 511 |
assert 0 == extract_exists((await imap_client.select())) |
| 512 |
|
| 513 |
|
| 514 |
@pytest.mark.asyncio() |
| 515 |
async def test_copy_messages(with_server): |
| 516 |
with_server.receive(Mail.create(['user'])) |
| 517 |
with_server.receive(Mail.create(['user'])) |
| 518 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 519 |
|
| 520 |
result, _ = await imap_client.copy('1', '2', 'MAILBOX') |
| 521 |
assert 'OK' == result |
| 522 |
|
| 523 |
assert 2 == extract_exists((await imap_client.select('MAILBOX'))) |
| 524 |
|
| 525 |
|
| 526 |
@pytest.mark.asyncio() |
| 527 |
async def test_copy_messages_by_uid(with_server): |
| 528 |
with_server.receive(Mail.create(['user'])) |
| 529 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 530 |
|
| 531 |
result, _ = await imap_client.uid('copy', '1', 'MAILBOX') |
| 532 |
assert 'OK' == result |
| 533 |
|
| 534 |
assert 1 == extract_exists((await imap_client.select('MAILBOX'))) |
| 535 |
|
| 536 |
|
| 537 |
@pytest.mark.asyncio() |
| 538 |
async def test_concurrency_1_executing_sync_commands_sequentially(with_server): |
| 539 |
imap_client = await login_user_async('user', 'pass') |
| 540 |
|
| 541 |
f1 = asyncio.ensure_future(imap_client.examine('INBOX')) |
| 542 |
f2 = asyncio.ensure_future(imap_client.examine('MAILBOX')) |
| 543 |
|
| 544 |
await asyncio.wait([f1, f2]) |
| 545 |
assert f1.exception() is None |
| 546 |
assert f2.exception() is None |
| 547 |
|
| 548 |
|
| 549 |
@pytest.mark.asyncio() |
| 550 |
async def test_concurrency_2_executing_same_async_commands_sequentially(with_server): |
| 551 |
with_server.receive(Mail.create(['user'])) |
| 552 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 553 |
|
| 554 |
f1 = asyncio.ensure_future(imap_client.fetch('1', '(RFC822)')) |
| 555 |
f2 = asyncio.ensure_future(imap_client.fetch('1', '(RFC822)')) |
| 556 |
|
| 557 |
await asyncio.wait([f1, f2]) |
| 558 |
assert f1.exception() is None |
| 559 |
assert f2.exception() is None |
| 560 |
|
| 561 |
|
| 562 |
@pytest.mark.asyncio() |
| 563 |
async def test_concurrency_3_executing_async_commands_in_parallel(with_server): |
| 564 |
# cf valid example in https://tools.ietf.org/html/rfc3501#section-5.5 |
| 565 |
with_server.receive(Mail.create(['user'])) |
| 566 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 567 |
|
| 568 |
store = asyncio.ensure_future(imap_client.store('1', '+FLAGS (FOO)')) |
| 569 |
copy = asyncio.ensure_future(imap_client.copy('1', 'MBOX')) |
| 570 |
expunge = asyncio.ensure_future(imap_client.expunge()) |
| 571 |
|
| 572 |
await asyncio.wait([store, copy, expunge]) |
| 573 |
assert 0 == extract_exists((await imap_client.select())) |
| 574 |
assert 1 == extract_exists((await imap_client.select('MBOX'))) |
| 575 |
assert b'1' == (await imap_client.search('KEYWORD FOO', charset=None)).lines[0] |
| 576 |
|
| 577 |
|
| 578 |
@pytest.mark.asyncio() |
| 579 |
async def test_concurrency_4_sync_command_waits_for_async_commands_to_finish(with_server): |
| 580 |
with_server.receive(Mail.create(['user'])) |
| 581 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 582 |
|
| 583 |
asyncio.ensure_future(imap_client.copy('1', 'MBOX')) |
| 584 |
asyncio.ensure_future(imap_client.expunge()) |
| 585 |
examine = asyncio.ensure_future(imap_client.examine('MBOX')) |
| 586 |
|
| 587 |
assert 1 == extract_exists((await asyncio.wait_for(examine, 1))) |
| 588 |
|
| 589 |
|
| 590 |
@pytest.mark.asyncio() |
| 591 |
async def test_noop(with_server): |
| 592 |
imap_client = await login_user_async('user', 'pass') |
| 593 |
assert ('OK', [b'NOOP completed.']) == (await imap_client.noop()) |
| 594 |
|
| 595 |
|
| 596 |
@pytest.mark.asyncio() |
| 597 |
async def test_noop_with_untagged_data(with_server): |
| 598 |
imap_client = await login_user_async('user', 'pass') |
| 599 |
with_server.receive(Mail.create(['user'])) |
| 600 |
|
| 601 |
assert ('OK', [b'1 EXISTS', b'1 RECENT', b'NOOP completed.']) == (await imap_client.noop()) |
| 602 |
|
| 603 |
|
| 604 |
@pytest.mark.asyncio() |
| 605 |
async def test_check(with_server): |
| 606 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 607 |
assert ('OK', [b'CHECK completed.']) == (await imap_client.check()) |
| 608 |
|
| 609 |
|
| 610 |
@pytest.mark.asyncio() |
| 611 |
async def test_close(with_server): |
| 612 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 613 |
assert imap_testing_server.SELECTED == with_server.get_connection('user').state |
| 614 |
|
| 615 |
assert ('OK', [b'CLOSE completed.']) == (await imap_client.close()) |
| 616 |
|
| 617 |
assert imap_testing_server.AUTH == with_server.get_connection('user').state |
| 618 |
|
| 619 |
|
| 620 |
@pytest.mark.asyncio() |
| 621 |
async def test_status(with_server): |
| 622 |
imap_client = await login_user_async('user', 'pass') |
| 623 |
|
| 624 |
assert b'INBOX (MESSAGES 0 UIDNEXT 1)' == \ |
| 625 |
(await imap_client.status('INBOX', '(MESSAGES UIDNEXT)')).lines[0] |
| 626 |
|
| 627 |
|
| 628 |
@pytest.mark.asyncio() |
| 629 |
async def test_subscribe_unsubscribe_lsub(with_server): |
| 630 |
imap_client = await login_user_async('user', 'pass') |
| 631 |
|
| 632 |
assert ('OK', [b'SUBSCRIBE completed.']) == (await imap_client.subscribe('#fr.soc.feminisme')) |
| 633 |
assert ('OK', [b'() "." #fr.soc.feminisme', b'LSUB completed.']) == \ |
| 634 |
(await imap_client.lsub('#fr.', 'soc.*')) |
| 635 |
assert ('OK', [b'UNSUBSCRIBE completed.']) == (await imap_client.unsubscribe('#fr.soc.feminisme')) |
| 636 |
assert ('OK', [b'LSUB completed.']) == (await imap_client.lsub('#fr', '.*')) |
| 637 |
|
| 638 |
|
| 639 |
@pytest.mark.asyncio() |
| 640 |
async def test_create_delete_mailbox(with_server): |
| 641 |
imap_client = await login_user_async('user', 'pass') |
| 642 |
assert 'NO' == (await imap_client.status('MBOX', '(MESSAGES)')).result |
| 643 |
|
| 644 |
assert ('OK', [b'CREATE completed.']) == (await imap_client.create('MBOX')) |
| 645 |
assert 'OK' == (await imap_client.status('MBOX', '(MESSAGES)')).result |
| 646 |
|
| 647 |
assert ('OK', [b'DELETE completed.']) == (await imap_client.delete('MBOX')) |
| 648 |
assert 'NO' == (await imap_client.status('MBOX', '(MESSAGES)')).result |
| 649 |
|
| 650 |
|
| 651 |
@pytest.mark.asyncio() |
| 652 |
async def test_rename_mailbox(with_server): |
| 653 |
imap_client = await login_user_async('user', 'pass') |
| 654 |
assert 'NO' == (await imap_client.status('MBOX', '(MESSAGES)')).result |
| 655 |
|
| 656 |
assert ('OK', [b'RENAME completed.']) == (await imap_client.rename('INBOX', 'MBOX')) |
| 657 |
|
| 658 |
assert 'OK' == (await imap_client.status('MBOX', '(MESSAGES)')).result |
| 659 |
|
| 660 |
|
| 661 |
@pytest.mark.asyncio() |
| 662 |
async def test_list(with_server): |
| 663 |
imap_client = await login_user_async('user', 'pass') |
| 664 |
assert ('OK', [b'() "/" Drafts', b'() "/" INBOX', b'() "/" Sent', b'() "/" Trash', |
| 665 |
b'LIST completed.']) == (await imap_client.list('""', '.*')) |
| 666 |
|
| 667 |
await imap_client.create('MYBOX') |
| 668 |
assert ('OK', [b'() "/" Drafts', b'() "/" INBOX', b'() "/" MYBOX', b'() "/" Sent', b'() "/" Trash', |
| 669 |
b'LIST completed.']) == \ |
| 670 |
(await imap_client.list('""', '.*')) |
| 671 |
|
| 672 |
|
| 673 |
@pytest.mark.asyncio() |
| 674 |
async def test_get_quotaroot(with_server): |
| 675 |
imap_client = await login_user_async('user', 'pass') |
| 676 |
with_server.receive(Mail.create(['user'])) |
| 677 |
|
| 678 |
response = await imap_client.getquotaroot('INBOX') |
| 679 |
|
| 680 |
assert response.lines == [b'INBOX (STORAGE 292 5000)', b'GETQUOTAROOT completed.'] |
| 681 |
|
| 682 |
|
| 683 |
@pytest.mark.asyncio() |
| 684 |
async def test_append(with_server): |
| 685 |
imap_client = await login_user_async('user@mail', 'pass') |
| 686 |
assert 0 == extract_exists((await imap_client.examine('INBOX'))) |
| 687 |
|
| 688 |
msg = Mail.create(['user@mail'], subject='append msg', content='do you see me ?') |
| 689 |
response = await imap_client.append(msg.as_bytes(), mailbox='INBOX', flags='FOO BAR', |
| 690 |
date=datetime.now(tz=utc), ) |
| 691 |
assert 'OK' == response.result |
| 692 |
assert b'1] APPEND completed' in response.lines[0] |
| 693 |
|
| 694 |
assert 1 == extract_exists((await imap_client.examine('INBOX'))) |
| 695 |
|
| 696 |
|
| 697 |
@pytest.mark.asyncio() |
| 698 |
async def test_rfc5032_within(with_server): |
| 699 |
with_server.receive(Mail.create(['user'], date=datetime.now(tz=utc) - timedelta(seconds=84600 * 3))) # 1 |
| 700 |
with_server.receive(Mail.create(['user'], date=datetime.now(tz=utc) - timedelta(seconds=84600))) # 2 |
| 701 |
with_server.receive(Mail.create(['user'])) # 3 |
| 702 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 703 |
|
| 704 |
assert b'1' == (await imap_client.search('OLDER', '84700')).lines[0] |
| 705 |
assert b'2 3' == (await imap_client.search('YOUNGER', '84700')).lines[0] |
| 706 |
|
| 707 |
|
| 708 |
@pytest.mark.asyncio() |
| 709 |
async def test_rfc4315_uidplus_expunge(with_server): |
| 710 |
with_server.receive(Mail.create(['user'])) |
| 711 |
with_server.receive(Mail.create(['user'])) |
| 712 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 713 |
|
| 714 |
assert ('OK', [b'1 EXPUNGE', b'UID EXPUNGE completed.']) == (await imap_client.uid('expunge', '1:1')) |
| 715 |
|
| 716 |
assert 1 == extract_exists((await imap_client.select())) |
| 717 |
|
| 718 |
|
| 719 |
@pytest.mark.asyncio() |
| 720 |
async def test_rfc6851_move(with_server): |
| 721 |
with_server.receive(Mail.create(['user'])) |
| 722 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 723 |
uidvalidity = with_server.get_connection('user').uidvalidity |
| 724 |
|
| 725 |
assert ('OK', [b'OK [COPYUID %d 1:1 1:1]' % uidvalidity, b'1 EXPUNGE', b'Done']) == \ |
| 726 |
(await imap_client.move('1:1', 'Trash')) |
| 727 |
|
| 728 |
assert 0 == extract_exists((await imap_client.select())) |
| 729 |
assert 1 == extract_exists((await imap_client.select('Trash'))) |
| 730 |
|
| 731 |
|
| 732 |
@pytest.mark.asyncio() |
| 733 |
async def test_rfc6851_uidmove(with_server): |
| 734 |
with_server.receive(Mail.create(['user'])) |
| 735 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 736 |
uidvalidity = with_server.get_connection('user').uidvalidity |
| 737 |
|
| 738 |
assert ('OK', [b'OK [COPYUID %d 1:1 1:1]' % uidvalidity, b'1 EXPUNGE', b'Done']) == \ |
| 739 |
(await imap_client.uid('move', '1:1', 'Trash')) |
| 740 |
|
| 741 |
assert 0 == extract_exists((await imap_client.select())) |
| 742 |
assert 1 == extract_exists((await imap_client.select('Trash'))) |
| 743 |
|
| 744 |
|
| 745 |
@pytest.mark.asyncio() |
| 746 |
async def test_rfc5161_enable(with_server): |
| 747 |
imap_client = await login_user_async('user', 'pass') |
| 748 |
|
| 749 |
assert ('OK', [b'X-GOOD-IDEA CONDSTORE enabled']) == \ |
| 750 |
(await imap_client.enable('X-GOOD-IDEA CONDSTORE')) |
| 751 |
|
| 752 |
|
| 753 |
@pytest.mark.asyncio() |
| 754 |
async def test_rfc2342_namespace(with_server): |
| 755 |
imap_client = await login_user_async('user', 'pass') |
| 756 |
response = await imap_client.namespace() |
| 757 |
|
| 758 |
assert ('OK', [b'(("" "/")) NIL NIL', b'NAMESPACE command completed']) == response |
| 759 |
|
| 760 |
|
| 761 |
@pytest.mark.asyncio() |
| 762 |
async def test_rfc2971_id(with_server): |
| 763 |
imap_client = await login_user_async('user', 'pass') |
| 764 |
response = await imap_client.id() |
| 765 |
assert ('OK', [b'ID command completed']) == response |
| 766 |
|
| 767 |
|
| 768 |
@pytest.mark.asyncio() |
| 769 |
async def test_callback_is_called_when_connection_is_lost(event_loop): |
| 770 |
imapserver = create_server(None, event_loop) |
| 771 |
srv = await imapserver.run_server(host='127.0.0.1', port=12345, fetch_chunk_size=64, ssl_context=None) |
| 772 |
async with srv: |
| 773 |
await srv.start_serving() |
| 774 |
queue = asyncio.Queue() |
| 775 |
imap_client = aioimaplib.IMAP4(port=12345, loop=event_loop, timeout=3, |
| 776 |
conn_lost_cb=lambda m: queue.put_nowait('called with %s' % m)) |
| 777 |
await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) |
| 778 |
await imap_client.login('login', 'password') |
| 779 |
|
| 780 |
imapserver.reset() |
| 781 |
srv.close() |
| 782 |
await asyncio.wait_for(srv.wait_closed(), 1) |
| 783 |
|
| 784 |
assert 'called with None' == (await asyncio.wait_for(queue.get(), timeout=2)) |
| 785 |
|
| 786 |
|
| 787 |
@pytest.mark.asyncio() |
| 788 |
async def test_client_can_connect_to_server_over_ssl(with_ssl, with_ssl_server): |
| 789 |
ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=with_ssl[1]) |
| 790 |
imap_client = aioimaplib.IMAP4_SSL(port=12345, loop=asyncio.get_running_loop(), ssl_context=ssl_context) |
| 791 |
await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) |
| 792 |
|
| 793 |
assert 'IMAP4REV1' == imap_client.protocol.imap_version |
| 794 |
assert {'IMAP4rev1', 'YESAUTH'} == imap_client.protocol.capabilities |
| 795 |
assert imap_client.has_capability('YESAUTH') |