Skip to content

Commit

Permalink
Merge pull request #57 from oleksandr-mazur/master
Browse files Browse the repository at this point in the history
Fixed AllowedVersions check for all versions
  • Loading branch information
bamthomas committed Apr 9, 2021
2 parents e43e5c9 + 3a41957 commit c59cf4f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
8 changes: 4 additions & 4 deletions aioimaplib/aioimaplib.py
Expand Up @@ -568,11 +568,11 @@ def capability(self):

capability_list = response.lines[0].split()
self.capabilities = set(capability_list)
version = capability_list[0].upper()
if version not in AllowedVersions:
try:
self.imap_version = list(
filter(lambda x: x.upper() in AllowedVersions, capability_list)).pop().upper()
except IndexError:
raise Error('server not IMAP4 compliant')
else:
self.imap_version = version

@asyncio.coroutine
def append(self, message_bytes, mailbox='INBOX', flags=None, date=None, timeout=None):
Expand Down
52 changes: 51 additions & 1 deletion aioimaplib/tests/test_aioimaplib.py
Expand Up @@ -29,7 +29,7 @@
TWENTY_NINE_MINUTES, STOP_WAIT_SERVER_PUSH, FetchCommand, IdleCommand
from aioimaplib.aioimaplib import Commands, IMAP4ClientProtocol, Command, Response, Abort, AioImapException
from aioimaplib.tests import imapserver
from aioimaplib.tests.imapserver import Mail
from aioimaplib.tests.imapserver import Mail, MockImapServer, ImapProtocol
from aioimaplib.tests.ssl_cert import create_temp_self_signed_cert
from aioimaplib.tests.test_imapserver import WithImapServer

Expand Down Expand Up @@ -343,6 +343,56 @@ def login_user(self, login, password, select=False, lib=aioimaplib.IMAP4):
return imap_client


class AllowedVersionsImapProtocol(ImapProtocol):
def capability(self, tag, *args):
"""No sent IMAP4rev1"""
self.send_untagged_line('CAPABILITY YESAUTH')
self.send_tagged_line(tag, 'OK Pre-login capabilities listed, post-login capabilities have more')


class AllowedVersionsMockImapServer(MockImapServer):
def run_server(self, host='127.0.0.1', port=1143, fetch_chunk_size=0, ssl_context=None):
def create_protocol():
protocol = AllowedVersionsImapProtocol(self._server_state, fetch_chunk_size, self.capabilities, self.loop)
self._connections.append(protocol)
return protocol

server = self.loop.create_server(create_protocol, host, port, ssl=ssl_context)
return self.loop.run_until_complete(server)



class AllowedVersionsAioWithImapServer(AioWithImapServer):
def _init_server(self, loop, capabilities=None, ssl_context=None):
self.loop = loop
if capabilities is not None:
self.imapserver = AllowedVersionsMockImapServer(loop=loop, capabilities=capabilities)
else:
self.imapserver = AllowedVersionsMockImapServer(loop=loop)
self.server = self.imapserver.run_server(
host='127.0.0.1', port=12345, fetch_chunk_size=64, ssl_context=ssl_context
)



class TestAioimaplibAllowedVersions(AllowedVersionsAioWithImapServer, asynctest.TestCase):
def setUp(self):
self._init_server(self.loop)

@asyncio.coroutine
def tearDown(self):
yield from self._shutdown_server()

@asyncio.coroutine
def test_capabilities_allowed_versions(self):
with self.assertRaises(asyncio.TimeoutError):
with self.assertRaises(aioimaplib.Error) as expected:
yield from self.login_user('user', 'pass')

self.assertEqual(expected.exception.args, ('server not IMAP4 compliant',))



class TestAioimaplib(AioWithImapServer, asynctest.TestCase):
def setUp(self):
self._init_server(self.loop)
Expand Down

0 comments on commit c59cf4f

Please sign in to comment.