| 1 |
import asyncio |
| 2 |
import logging |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
from aioimaplib import aioimaplib |
| 7 |
from aioimaplib.aioimaplib import Abort |
| 8 |
from aioimaplib.imap_testing_server import ImapProtocol, ServerState, MockImapServer |
| 9 |
from tests.server_fixture import with_server, login_user_async, main_test |
| 10 |
|
| 11 |
aioimaplib.log.setLevel(logging.WARNING) |
| 12 |
sh = logging.StreamHandler() |
| 13 |
sh.setLevel(logging.INFO) |
| 14 |
sh.setFormatter(logging.Formatter("%(asctime)s %(levelname)s [%(module)s:%(lineno)d] %(message)s")) |
| 15 |
aioimaplib.log.addHandler(sh) |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.skip(reason="it passes alone but blocks the other tests in this module") |
| 19 |
@pytest.mark.asyncio() |
| 20 |
async def test_capabilities_server_not_compliant(event_loop): |
| 21 |
class NotCompliantProtocol(ImapProtocol): |
| 22 |
def capability(self, tag, *args): |
| 23 |
"""should send CAPABILITY IMAP4rev1 YESAUTH """ |
| 24 |
self.send_untagged_line('CAPABILITY YESAUTH') |
| 25 |
self.send_tagged_line(tag, 'OK Pre-login capabilities listed, post-login capabilities have more') |
| 26 |
|
| 27 |
class NotCompliantServer(MockImapServer): |
| 28 |
def run_server(self, host='127.0.0.1', port=1143, fetch_chunk_size=0, ssl_context=None): |
| 29 |
return event_loop.create_server(lambda: NotCompliantProtocol(ServerState(), loop=self.loop), host=host, port=port, ssl=None) |
| 30 |
|
| 31 |
imap_srv = NotCompliantServer(loop=event_loop) |
| 32 |
srv = await imap_srv.run_server(port=12345) |
| 33 |
main_server_future = asyncio.ensure_future(main_test(server=srv, ssl_context=None)) |
| 34 |
|
| 35 |
with pytest.raises(asyncio.TimeoutError): |
| 36 |
with pytest.raises(aioimaplib.Error) as expected: |
| 37 |
await login_user_async('user', 'pass', timeout=0.1) |
| 38 |
|
| 39 |
assert expected == 'server not IMAP4 compliant' |
| 40 |
|
| 41 |
|
| 42 |
@pytest.mark.asyncio() |
| 43 |
async def test_capabilities(with_server): |
| 44 |
imap_client = aioimaplib.IMAP4(port=12345, loop=asyncio.get_running_loop()) |
| 45 |
await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) |
| 46 |
|
| 47 |
assert 'IMAP4REV1' == imap_client.protocol.imap_version |
| 48 |
assert {'IMAP4rev1', 'YESAUTH'} == imap_client.protocol.capabilities |
| 49 |
assert imap_client.has_capability('YESAUTH') |
| 50 |
|
| 51 |
|
| 52 |
@pytest.mark.parametrize("with_server", [''], indirect=True) # '' = no capabilities |
| 53 |
@pytest.mark.asyncio() |
| 54 |
async def test_idle_messages_without_idle_capability_abort_command(with_server): |
| 55 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 56 |
with pytest.raises(Abort): |
| 57 |
await asyncio.wait_for(imap_client.idle(), timeout=2) |
| 58 |
|
| 59 |
|
| 60 |
@pytest.mark.parametrize("with_server", [''], indirect=True) # '' = no capabilities |
| 61 |
@pytest.mark.asyncio() |
| 62 |
async def test_expunge_messages_without_uidplus_capability_abort_command(with_server): |
| 63 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 64 |
with pytest.raises(Abort): |
| 65 |
await imap_client.uid('expunge', '1:1') |
| 66 |
|
| 67 |
|
| 68 |
@pytest.mark.parametrize("with_server", [''], indirect=True) # '' = no capabilities |
| 69 |
@pytest.mark.asyncio() |
| 70 |
async def test_move_without_move_capability_abort_command(with_server): |
| 71 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 72 |
with pytest.raises(Abort): |
| 73 |
await imap_client.move('1:1', 'Trash') |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.parametrize("with_server", [''], indirect=True) # '' = no capabilities |
| 77 |
@pytest.mark.asyncio() |
| 78 |
async def test_uidmove_without_move_capability_abort_command(with_server): |
| 79 |
imap_client = await login_user_async('user', 'pass', select=True) |
| 80 |
with pytest.raises(Abort): |
| 81 |
await imap_client.uid('move', '1:1', 'Trash') |
| 82 |
|
| 83 |
|
| 84 |
@pytest.mark.parametrize("with_server", [''], indirect=True) # '' = no capabilities |
| 85 |
@pytest.mark.asyncio() |
| 86 |
async def test_enable_without_enable_capability_abort_command(with_server): |
| 87 |
imap_client = await login_user_async('user', 'pass') |
| 88 |
with pytest.raises(Abort): |
| 89 |
await imap_client.enable('CAPABILITY') |
| 90 |
|
| 91 |
|
| 92 |
@pytest.mark.parametrize("with_server", [''], indirect=True) # '' = no capabilities |
| 93 |
@pytest.mark.asyncio() |
| 94 |
async def test_namespace_without_namespace_capability_abort_command(with_server): |
| 95 |
imap_client = await login_user_async('user', 'pass') |
| 96 |
with pytest.raises(Abort): |
| 97 |
await imap_client.namespace() |