Skip to content

Commit

Permalink
refactor: rename imapserver into imap-testing-server (more explicit)
Browse files Browse the repository at this point in the history
  • Loading branch information
bamthomas committed Jan 11, 2025
1 parent f357c18 commit 9b1d64a
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -806,8 +806,8 @@ async def async_main(**kwargs):
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
prog='imapserver',
description='Small python asyncio IMAP testing server.',
prog='imap-testing-server',
description='Small python asyncio IMAP testing server from aioimaplib.',
epilog='')
parser.add_argument('--port', help='tcp port of the server', default=1143)
parser.add_argument('--host', help='host of the server', default='127.0.0.1')
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
imapserver = "aioimaplib.imapserver:main"
imap-testing-server = "aioimaplib.imap_testing_server:main"
2 changes: 1 addition & 1 deletion tests/server_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest

from aioimaplib import aioimaplib
from aioimaplib.imapserver import MockImapServer
from aioimaplib.imap_testing_server import MockImapServer
from tests.ssl_cert import create_temp_self_signed_cert


Expand Down
2 changes: 1 addition & 1 deletion tests/test_acceptance_aioimaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import pytest

from aioimaplib.imapserver import Mail
from aioimaplib.imap_testing_server import Mail
from tests.server_fixture import with_server, login_user_async


Expand Down
8 changes: 4 additions & 4 deletions tests/test_aioimaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

from aioimaplib import aioimaplib, extract_exists, STOP_WAIT_SERVER_PUSH, FetchCommand, IdleCommand
from aioimaplib.aioimaplib import Commands, IMAP4ClientProtocol, Command, Abort
from aioimaplib import imapserver
from aioimaplib.imapserver import Mail
from aioimaplib import imap_testing_server
from aioimaplib.imap_testing_server import Mail
from tests.server_fixture import with_ssl_server, with_ssl, with_server, login_user_async, create_server

aioimaplib.log.setLevel(logging.WARNING)
Expand Down Expand Up @@ -610,11 +610,11 @@ async def test_check(with_server):
@pytest.mark.asyncio()
async def test_close(with_server):
imap_client = await login_user_async('user', 'pass', select=True)
assert imapserver.SELECTED == with_server.get_connection('user').state
assert imap_testing_server.SELECTED == with_server.get_connection('user').state

assert ('OK', [b'CLOSE completed.']) == (await imap_client.close())

assert imapserver.AUTH == with_server.get_connection('user').state
assert imap_testing_server.AUTH == with_server.get_connection('user').state


@pytest.mark.asyncio()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_aioimaplib_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from aioimaplib import aioimaplib
from aioimaplib.aioimaplib import Abort
from aioimaplib.imapserver import ImapProtocol, ServerState, MockImapServer
from aioimaplib.imap_testing_server import ImapProtocol, ServerState, MockImapServer
from tests.server_fixture import with_server, login_user_async, main_test

aioimaplib.log.setLevel(logging.WARNING)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_aioimaplib_clocked.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from aioimaplib import Command, CommandTimeout, STOP_WAIT_SERVER_PUSH, AioImapException, Response
from aioimaplib import imapserver
from aioimaplib.imap_testing_server import ImapProtocol
from tests.server_fixture import with_server, login_user_async, advance_time


Expand Down Expand Up @@ -152,7 +152,7 @@ async def test_idle_start__exits_queueget_with_keepalive_without_timeout_error(e
imap_client = await login_user_async('user', 'pass', select=True, loop=event_loop)

# Idle long enough for the server to issue a keep-alive
server_idle_timeout = imapserver.ImapProtocol.IDLE_STILL_HERE_PERIOD_SECONDS
server_idle_timeout = ImapProtocol.IDLE_STILL_HERE_PERIOD_SECONDS
idle_timeout = server_idle_timeout + 1
idle = await imap_client.idle_start(idle_timeout)

Expand Down
25 changes: 12 additions & 13 deletions tests/test_imapserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,63 +26,62 @@
import pytz
import sys

from aioimaplib import imapserver
from aioimaplib.imapserver import ServerState, Mail, MockImapServer, ImapProtocol, InvalidUidSet
from aioimaplib.imap_testing_server import ServerState, Mail, MockImapServer, ImapProtocol, InvalidUidSet
import pytest


class TestMailToString(unittest.TestCase):
def test_message_date_string(self):
now = pytz.timezone('Europe/Paris').localize(datetime(2016, 2, 2, 12, 13, 14, 151))

mail = imapserver.Mail.create(['user'], date=now)
mail = Mail.create(['user'], date=now)

assert mail.email.get('Date') == 'Tue, 02 Feb 2016 12:13:14 +0100'

def test_message_default_date_string_is_utc(self):
mail = imapserver.Mail.create(['user'])
mail = Mail.create(['user'])

assert mail.email.get('Date').endswith('+0000')

def test_message_title_string_without_accents_isnot_encoded(self):
now = pytz.timezone('Europe/Paris').localize(datetime(2016, 2, 2, 12, 13, 14, 151))

mail = imapserver.Mail.create(['user'], subject='subject', date=now)
mail = Mail.create(['user'], subject='subject', date=now)

assert mail.email.get('Subject') == 'subject'

def test_message_title_string_with_accents_is_base64encoded(self):
mail = imapserver.Mail.create(['user'], subject='Classé ?')
mail = Mail.create(['user'], subject='Classé ?')

assert '=?utf-8?b?Q2xhc3PDqSA/?=' in mail.as_string()

def test_message_quoted_printable(self):
mail = imapserver.Mail.create(['user'], content='Bonjour à vous', quoted_printable=True)
mail = Mail.create(['user'], content='Bonjour à vous', quoted_printable=True)

assert 'Bonjour =C3=A0 vous' in mail.as_string(), '"=C3=A0" not found in %s' % mail.as_string()

def test_message_not_quoted_printable(self):
mail = imapserver.Mail.create(['user'], subject='élo ?', content='Bonjour à vous').as_bytes()
mail = Mail.create(['user'], subject='élo ?', content='Bonjour à vous').as_bytes()

m = email.message_from_bytes(mail)
assert 'Bonjour à vous' == m.get_payload(decode=True).decode()

def test_header_encode_to(self):
mail = imapserver.Mail.create(['Zébulon Durand <[email protected]>'], mail_from='[email protected]', subject='subject')
mail = Mail.create(['Zébulon Durand <[email protected]>'], mail_from='[email protected]', subject='subject')

assert '=?utf-8?q?Z=C3=A9bulon_Durand_=3Czeb=40zebulon=2Eio=3E?=' in mail.as_string(), 'expected string not found in :%s\n' % mail.as_string()

def test_mail_from(self):
mail = imapserver.Mail.create(['user'], subject='subject')
mail = Mail.create(['user'], subject='subject')
assert mail.email.get('From') == ''

mail = imapserver.Mail.create(['user'], mail_from='<test@test>', subject='subject')
mail = Mail.create(['user'], mail_from='<test@test>', subject='subject')
assert mail.email.get('From') == '<test@test>'

mail = imapserver.Mail.create(['user'], mail_from='test@test', subject='subject')
mail = Mail.create(['user'], mail_from='test@test', subject='subject')
assert mail.email.get('From') == '<test@test>'

mail = imapserver.Mail.create(['user'], mail_from='Test <test@test>', subject='subject')
mail = Mail.create(['user'], mail_from='Test <test@test>', subject='subject')
assert mail.email.get('From') == 'Test <test@test>'

def test_build_sequence_range(self):
Expand Down
15 changes: 7 additions & 8 deletions tests/test_imapserver_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import pytest
from pytz import utc

from aioimaplib import imapserver
from aioimaplib.imapserver import Mail
from aioimaplib.imap_testing_server import Mail, AUTH, SELECTED, LOGOUT
from tests.server_fixture import with_server, login_user, with_ssl, with_ssl_server


Expand All @@ -47,7 +46,7 @@ async def test_server_login(with_server):

assert 'OK' == result
assert [b'LOGIN completed'] == data
assert imapserver.AUTH == with_server.get_connection('user').state
assert AUTH == with_server.get_connection('user').state


@pytest.mark.asyncio
Expand All @@ -59,7 +58,7 @@ async def test_select_no_messages_in_mailbox(with_server):

assert 'OK' == result
assert [b'0'] == data
assert imapserver.SELECTED == with_server.get_connection('user@mail').state
assert SELECTED == with_server.get_connection('user@mail').state


@pytest.mark.asyncio
Expand Down Expand Up @@ -95,7 +94,7 @@ async def test_examine_no_messages_in_mailbox(with_server):
assert ('OK', [b'0']) == (await asyncio.wait_for(
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.select, readonly=True)), 1))

assert imapserver.AUTH == with_server.get_connection('user').state
assert AUTH == with_server.get_connection('user').state


@pytest.mark.asyncio
Expand Down Expand Up @@ -383,12 +382,12 @@ async def test_subscribe_unsubscribe_lsub(with_server):
@pytest.mark.asyncio
async def test_close(with_server):
imap_client = await login_user('user', 'pass', select=True)
assert imapserver.SELECTED == with_server.get_connection('user').state
assert SELECTED == with_server.get_connection('user').state

assert ('OK', [b'CLOSE completed.']) == \
(await asyncio.wait_for(asyncio.get_running_loop().run_in_executor(None, imap_client.close), 1))

assert imapserver.AUTH == with_server.get_connection('user').state
assert AUTH == with_server.get_connection('user').state


@pytest.mark.asyncio
Expand Down Expand Up @@ -480,7 +479,7 @@ async def test_logout(with_server):

assert 'BYE' == result # uhh ?
assert [b'Logging out'] == data
assert imapserver.LOGOUT == with_server.get_connection('user').state
assert LOGOUT == with_server.get_connection('user').state


@pytest.mark.asyncio
Expand Down
7 changes: 3 additions & 4 deletions tests/test_imapserver_imaplib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

from imaplib2 import imaplib2
from mock import Mock
from aioimaplib import imapserver
from aioimaplib.imapserver import Mail
from aioimaplib.imap_testing_server import Mail, IDLE, SELECTED
from tests.server_fixture import with_server, login_user
import pytest

Expand All @@ -33,12 +32,12 @@ async def test_idle(with_server):
imap_client = await login_user('user', 'pass', select=True, lib=imaplib2.IMAP4)
idle_callback = Mock()
asyncio.get_running_loop().run_in_executor(None, functools.partial(imap_client.idle, callback=idle_callback))
await asyncio.wait_for(with_server.get_connection('user').wait(imapserver.IDLE), 1)
await asyncio.wait_for(with_server.get_connection('user').wait(IDLE), 1)

asyncio.get_running_loop().run_in_executor(None, functools.partial(with_server.receive,
Mail.create(to=['user'], mail_from='me', subject='hello')))

await asyncio.wait_for(with_server.get_connection('user').wait(imapserver.SELECTED), 5)
await asyncio.wait_for(with_server.get_connection('user').wait(SELECTED), 5)
time.sleep(0.2) # eurk hate sleeps but I don't know how to wait for the lib to receive end of IDLE
idle_callback.assert_called_once()

Expand Down

0 comments on commit 9b1d64a

Please sign in to comment.