| 1 |
# aioimaplib : an IMAPrev4 lib using python asyncio |
| 2 |
# Copyright (C) 2016 Bruno Thomas |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
import asyncio |
| 17 |
import functools |
| 18 |
import time |
| 19 |
from unittest.mock import Mock |
| 20 |
|
| 21 |
from imaplib2 import imaplib2 |
| 22 |
from aioimaplib.imap_testing_server import Mail, IDLE, SELECTED |
| 23 |
from tests.server_fixture import with_server, login_user |
| 24 |
import pytest |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.flaky(reruns=5) |
| 28 |
@pytest.mark.asyncio() |
| 29 |
async def test_idle(with_server): |
| 30 |
imap_client = await login_user('user', 'pass', select=True, lib=imaplib2.IMAP4) |
| 31 |
idle_callback = Mock() |
| 32 |
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.idle, callback=idle_callback)) |
| 33 |
await asyncio.wait_for(with_server.get_connection('user').wait(IDLE), 1) |
| 34 |
|
| 35 |
asyncio.get_running_loop().run_in_executor(None, functools.partial(with_server.receive, |
| 36 |
Mail.create(to=['user'], mail_from='me', subject='hello'))) |
| 37 |
|
| 38 |
await asyncio.wait_for(with_server.get_connection('user').wait(SELECTED), 5) |
| 39 |
time.sleep(0.2) # eurk hate sleeps but I don't know how to wait for the lib to receive end of IDLE |
| 40 |
idle_callback.assert_called_once() |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.asyncio() |
| 44 |
async def test_login_twice(with_server): |
| 45 |
with pytest.raises(imaplib2.IMAP4.error) as expected: |
| 46 |
imap_client = await login_user('user', 'pass', lib=imaplib2.IMAP4) |
| 47 |
|
| 48 |
await asyncio.wait_for( |
| 49 |
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.login, 'user', 'pass')), 1) |
| 50 |
|
| 51 |
assert expected == 'command LOGIN illegal in state AUTH' |