| 1 |
import asyncio |
| 2 |
import functools |
| 3 |
import imaplib |
| 4 |
import ssl |
| 5 |
from email.charset import add_charset, SHORTEST |
| 6 |
from ssl import SSLContext |
| 7 |
from typing import Optional |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from aioimaplib import aioimaplib |
| 12 |
from aioimaplib.imap_testing_server import MockImapServer |
| 13 |
from tests.ssl_cert import create_temp_self_signed_cert |
| 14 |
|
| 15 |
|
| 16 |
async def main_test(server: MockImapServer, ssl_context: Optional[SSLContext]): |
| 17 |
srv = await server.run_server(host='127.0.0.1', port=12345, fetch_chunk_size=64, ssl_context=ssl_context) |
| 18 |
async with srv: |
| 19 |
try: |
| 20 |
await srv.serve_forever() |
| 21 |
except Exception: |
| 22 |
srv.shutdown() |
| 23 |
|
| 24 |
|
| 25 |
@pytest.fixture() |
| 26 |
def with_ssl(): |
| 27 |
_cert_file, _cert_key = create_temp_self_signed_cert() |
| 28 |
ssl_context_server = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) |
| 29 |
ssl_context_server.load_cert_chain(_cert_file, _cert_key) |
| 30 |
return ssl_context_server, _cert_file |
| 31 |
|
| 32 |
|
| 33 |
@pytest.fixture |
| 34 |
def advance_time(event_loop, request): |
| 35 |
return EventLoopClockAdvancer(event_loop) |
| 36 |
|
| 37 |
|
| 38 |
@pytest.fixture() |
| 39 |
def with_server(event_loop, request): |
| 40 |
capabilities = request.param if hasattr(request, 'param') else None # |
| 41 |
imapserver = create_server(capabilities, event_loop) |
| 42 |
main_server_future = asyncio.ensure_future(main_test(server=imapserver, ssl_context=None)) |
| 43 |
event_loop.run_until_complete(asyncio.sleep(0.01)) |
| 44 |
try: |
| 45 |
yield imapserver |
| 46 |
finally: |
| 47 |
imapserver.reset() |
| 48 |
main_server_future.cancel() |
| 49 |
|
| 50 |
|
| 51 |
def create_server(capabilities, event_loop): |
| 52 |
add_charset('utf-8', SHORTEST, None, 'utf-8') |
| 53 |
add_charset('cp1252', SHORTEST, None, 'cp1252') |
| 54 |
if capabilities is not None: |
| 55 |
imapserver = MockImapServer(loop=event_loop, capabilities=capabilities) |
| 56 |
else: |
| 57 |
imapserver = MockImapServer(loop=event_loop) |
| 58 |
return imapserver |
| 59 |
|
| 60 |
|
| 61 |
@pytest.fixture() |
| 62 |
def with_ssl_server(event_loop, with_ssl): |
| 63 |
add_charset('utf-8', SHORTEST, None, 'utf-8') |
| 64 |
add_charset('cp1252', SHORTEST, None, 'cp1252') |
| 65 |
imapserver = MockImapServer(loop=event_loop) |
| 66 |
cancel_handle = asyncio.ensure_future(main_test(server=imapserver, ssl_context=with_ssl[0])) |
| 67 |
event_loop.run_until_complete(asyncio.sleep(0.01)) |
| 68 |
|
| 69 |
try: |
| 70 |
yield imapserver |
| 71 |
finally: |
| 72 |
imapserver.reset() |
| 73 |
cancel_handle.cancel() |
| 74 |
|
| 75 |
|
| 76 |
async def login_user(login, password, select=False, lib=imaplib.IMAP4, loop=None): |
| 77 |
args = {'host': '127.0.0.1', 'port': 12345} if loop is None else {'host': '127.0.0.1', 'port': 12345, 'loop': loop} |
| 78 |
imap_client = await asyncio.wait_for( |
| 79 |
asyncio.get_running_loop().run_in_executor(None, functools.partial(lib, **args)), 1) |
| 80 |
|
| 81 |
await asyncio.wait_for( |
| 82 |
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.login, login, password)), 1) |
| 83 |
|
| 84 |
if select: |
| 85 |
await asyncio.wait_for( |
| 86 |
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select)), 1) |
| 87 |
|
| 88 |
return imap_client |
| 89 |
|
| 90 |
|
| 91 |
async def login_user_async(login, password, select=False, loop=None, timeout=1): |
| 92 |
test_loop = loop if loop is not None else asyncio.get_running_loop() |
| 93 |
imap_client = aioimaplib.IMAP4(host='127.0.0.1', port=12345, loop=test_loop, timeout=timeout) |
| 94 |
await imap_client.wait_hello_from_server() |
| 95 |
await imap_client.login(login, password) |
| 96 |
if select: |
| 97 |
await imap_client.select() |
| 98 |
return imap_client |
| 99 |
|
| 100 |
|
| 101 |
class EventLoopClockAdvancer: |
| 102 |
""" |
| 103 |
from https://github.com/pytest-dev/pytest-asyncio/pull/113 |
| 104 |
A helper object that when called will advance the event loop's time. If the |
| 105 |
call is awaited, the caller task will wait an iteration for the update to |
| 106 |
wake up any awaiting handlers. |
| 107 |
""" |
| 108 |
|
| 109 |
__slots__ = ("offset", "loop", "sleep_duration", "_base_time") |
| 110 |
|
| 111 |
def __init__(self, loop, sleep_duration=1e-6): |
| 112 |
self.offset = 0.0 |
| 113 |
self._base_time = loop.time |
| 114 |
self.loop = loop |
| 115 |
self.sleep_duration = sleep_duration |
| 116 |
|
| 117 |
# incorporate offset timing into the event loop |
| 118 |
self.loop.time = self.time |
| 119 |
|
| 120 |
def time(self): |
| 121 |
""" |
| 122 |
Return the time according to the event loop's clock. The time is |
| 123 |
adjusted by an offset. |
| 124 |
""" |
| 125 |
return self._base_time() + self.offset |
| 126 |
|
| 127 |
async def __call__(self, seconds): |
| 128 |
""" |
| 129 |
Advance time by a given offset in seconds. Returns an awaitable |
| 130 |
that will complete after all tasks scheduled for after advancement |
| 131 |
of time are proceeding. |
| 132 |
""" |
| 133 |
# sleep so that the loop does everything currently waiting |
| 134 |
await asyncio.sleep(self.sleep_duration) |
| 135 |
|
| 136 |
if seconds > 0: |
| 137 |
# advance the clock by the given offset |
| 138 |
self.offset += seconds |
| 139 |
|
| 140 |
# Once the clock is adjusted, new tasks may have just been |
| 141 |
# scheduled for running in the next pass through the event loop |
| 142 |
await asyncio.sleep(self.sleep_duration) |