diff --git a/aioimaplib/aioimaplib.py b/aioimaplib/aioimaplib.py index af4293e..36c4511 100644 --- a/aioimaplib/aioimaplib.py +++ b/aioimaplib/aioimaplib.py @@ -206,9 +206,8 @@ def append_to_resp(self, line, result='Pending'): self.response = Response(result, old.lines + [line]) self._reset_timer() - @asyncio.coroutine - def wait(self): - yield from self._event.wait() + async def wait(self): + await self._event.wait() if self._exception is not None: raise self._exception @@ -303,10 +302,9 @@ def __init__(self, cmd, data=b''): def change_state(coro): @functools.wraps(coro) - @asyncio.coroutine - def wrapper(self, *args, **kargs): - with (yield from self.state_condition): - res = yield from coro(self, *args, **kargs) + async def wrapper(self, *args, **kargs): + with (await self.state_condition): + res = await coro(self, *args, **kargs) log.debug('state -> %s' % self.state) self.state_condition.notify_all() return res @@ -413,26 +411,25 @@ def send(self, line): log.debug('Sending : %s' % data) self.transport.write(data) - @asyncio.coroutine - def execute(self, command): + async def execute(self, command): if self.state not in Commands.get(command.name).valid_states: raise Abort('command %s illegal in state %s' % (command.name, self.state)) if self.pending_sync_command is not None: - yield from self.pending_sync_command.wait() + await self.pending_sync_command.wait() if Commands.get(command.name).exec == Exec.is_sync: if self.pending_async_commands: - yield from self.wait_async_pending_commands() + await self.wait_async_pending_commands() self.pending_sync_command = command else: if self.pending_async_commands.get(command.untagged_resp_name) is not None: - yield from self.pending_async_commands[command.untagged_resp_name].wait() + await self.pending_async_commands[command.untagged_resp_name].wait() self.pending_async_commands[command.untagged_resp_name] = command self.send(str(command)) try: - yield from command.wait() + await command.wait() except CommandTimeout: if Commands.get(command.name).exec == Exec.is_sync: self.pending_sync_command = None @@ -443,20 +440,18 @@ def execute(self, command): return command.response @change_state - @asyncio.coroutine - def welcome(self, command): + async def welcome(self, command): if 'PREAUTH' in command: self.state = AUTH elif 'OK' in command: self.state = NONAUTH else: raise Error(command) - yield from self.capability() + await self.capability() @change_state - @asyncio.coroutine - def login(self, user, password): - response = yield from self.execute( + async def login(self, user, password): + response = await self.execute( Command('LOGIN', self.new_tag(), user, '%s' % quoted(password), loop=self.loop)) if 'OK' == response.result: @@ -467,17 +462,15 @@ def login(self, user, password): return response @change_state - @asyncio.coroutine - def logout(self): - response = (yield from self.execute(Command('LOGOUT', self.new_tag(), loop=self.loop))) + async def logout(self): + response = (await self.execute(Command('LOGOUT', self.new_tag(), loop=self.loop))) if 'OK' == response.result: self.state = LOGOUT return response @change_state - @asyncio.coroutine - def select(self, mailbox='INBOX'): - response = yield from self.execute( + async def select(self, mailbox='INBOX'): + response = await self.execute( Command('SELECT', self.new_tag(), mailbox, loop=self.loop)) if 'OK' == response.result: @@ -485,18 +478,16 @@ def select(self, mailbox='INBOX'): return response @change_state - @asyncio.coroutine - def close(self): - response = yield from self.execute(Command('CLOSE', self.new_tag(), loop=self.loop)) + async def close(self): + response = await self.execute(Command('CLOSE', self.new_tag(), loop=self.loop)) if response.result == 'OK': self.state = AUTH return response - @asyncio.coroutine - def idle(self): + async def idle(self): if 'IDLE' not in self.capabilities: raise Abort('server has not IDLE capability') - return (yield from self.execute(IdleCommand(self.new_tag(), self.idle_queue, loop=self.loop))) + return await self.execute(IdleCommand(self.new_tag(), self.idle_queue, loop=self.loop)) def has_pending_idle_command(self): return self.pending_sync_command is not None and self.pending_sync_command.name == 'IDLE' @@ -504,67 +495,59 @@ def has_pending_idle_command(self): def idle_done(self): self.send('DONE') - @asyncio.coroutine - def search(self, *criteria, charset='utf-8', by_uid=False): + async def search(self, *criteria, charset='utf-8', by_uid=False): args = ('CHARSET', charset) + criteria if charset is not None else criteria prefix = 'UID' if by_uid else '' - return (yield from self.execute( - Command('SEARCH', self.new_tag(), *args, prefix=prefix, loop=self.loop))) + return await self.execute( + Command('SEARCH', self.new_tag(), *args, prefix=prefix, loop=self.loop)) - @asyncio.coroutine - def fetch(self, message_set, message_parts, by_uid=False, timeout=None): - return (yield from self.execute( + async def fetch(self, message_set, message_parts, by_uid=False, timeout=None): + return await self.execute( FetchCommand(self.new_tag(), message_set, message_parts, - prefix='UID' if by_uid else '', loop=self.loop, timeout=timeout))) + prefix='UID' if by_uid else '', loop=self.loop, timeout=timeout)) - @asyncio.coroutine - def store(self, *args, by_uid=False): - return (yield from self.execute( + async def store(self, *args, by_uid=False): + return await self.execute( Command('STORE', self.new_tag(), *args, - prefix='UID' if by_uid else '', untagged_resp_name='FETCH', loop=self.loop))) + prefix='UID' if by_uid else '', untagged_resp_name='FETCH', loop=self.loop)) - @asyncio.coroutine - def expunge(self, *args, by_uid=False): - return (yield from self.execute( + async def expunge(self, *args, by_uid=False): + return await self.execute( Command('EXPUNGE', self.new_tag(), *args, - prefix='UID' if by_uid else '', loop=self.loop))) + prefix='UID' if by_uid else '', loop=self.loop)) - @asyncio.coroutine - def uid(self, command, *criteria, timeout=None): + async def uid(self, command, *criteria, timeout=None): if self.state not in Commands.get('UID').valid_states: raise Abort('command UID illegal in state %s' % self.state) if command.upper() == 'FETCH': - return (yield from self.fetch(criteria[0], criteria[1], by_uid=True, timeout=timeout)) + return await self.fetch(criteria[0], criteria[1], by_uid=True, timeout=timeout) if command.upper() == 'STORE': - return (yield from self.store(*criteria, by_uid=True)) + return await self.store(*criteria, by_uid=True) if command.upper() == 'COPY': - return (yield from self.copy(*criteria, by_uid=True)) + return await self.copy(*criteria, by_uid=True) if command.upper() == 'MOVE': - return (yield from self.move(*criteria, by_uid=True)) + return await self.move(*criteria, by_uid=True) if command.upper() == 'EXPUNGE': if 'UIDPLUS' not in self.capabilities: raise Abort('EXPUNGE with uids is only valid with UIDPLUS capability. UIDPLUS not in (%s)' % self.capabilities) - return (yield from self.expunge(*criteria, by_uid=True)) + return await self.expunge(*criteria, by_uid=True) raise Abort('command UID only possible with COPY, FETCH, EXPUNGE (w/UIDPLUS) or STORE (was %s)' % command.upper()) - @asyncio.coroutine - def copy(self, *args, by_uid=False): - return (yield from self.execute( + async def copy(self, *args, by_uid=False): + return (await self.execute( Command('COPY', self.new_tag(), *args, prefix='UID' if by_uid else '', loop=self.loop))) - @asyncio.coroutine - def move(self, uid_set, mailbox, by_uid=False): + async def move(self, uid_set, mailbox, by_uid=False): if 'MOVE' not in self.capabilities: raise Abort('server has not MOVE capability') - return (yield from self.execute( + return (await self.execute( Command('MOVE', self.new_tag(), uid_set, mailbox, prefix='UID' if by_uid else '', loop=self.loop))) - @asyncio.coroutine - def capability(self): - response = yield from self.execute(Command('CAPABILITY', self.new_tag(), loop=self.loop)) + async def capability(self): + response = await self.execute(Command('CAPABILITY', self.new_tag(), loop=self.loop)) capability_list = response.lines[0].split() self.capabilities = set(capability_list) @@ -574,8 +557,7 @@ def capability(self): except IndexError: raise Error('server not IMAP4 compliant') - @asyncio.coroutine - def append(self, message_bytes, mailbox='INBOX', flags=None, date=None, timeout=None): + async def append(self, message_bytes, mailbox='INBOX', flags=None, date=None, timeout=None): args = [mailbox] if flags is not None: if (flags[0], flags[-1]) != ('(', ')'): @@ -586,37 +568,32 @@ def append(self, message_bytes, mailbox='INBOX', flags=None, date=None, timeout= args.append(time2internaldate(date)) args.append('{%s}' % len(message_bytes)) self.literal_data = message_bytes - return (yield from self.execute(Command('APPEND', self.new_tag(), *args, loop=self.loop, timeout=timeout))) + return await self.execute(Command('APPEND', self.new_tag(), *args, loop=self.loop, timeout=timeout)) - @asyncio.coroutine - def id(self, **kwargs): + async def id(self, **kwargs): args = arguments_rfs2971(**kwargs) - return (yield from self.execute(Command('ID', self.new_tag(), *args, loop=self.loop))) + return await self.execute(Command('ID', self.new_tag(), *args, loop=self.loop)) simple_commands = {'NOOP', 'CHECK', 'STATUS', 'CREATE', 'DELETE', 'RENAME', 'SUBSCRIBE', 'UNSUBSCRIBE', 'LSUB', 'LIST', 'EXAMINE', 'ENABLE'} - @asyncio.coroutine - def namespace(self): + async def namespace(self): if 'NAMESPACE' not in self.capabilities: raise Abort('server has not NAMESPACE capability') - return (yield from self.execute(Command('NAMESPACE', self.new_tag(), loop=self.loop))) + return await self.execute(Command('NAMESPACE', self.new_tag(), loop=self.loop)) - @asyncio.coroutine - def simple_command(self, name, *args): + async def simple_command(self, name, *args): if name not in self.simple_commands: raise NotImplementedError('simple command only available for %s' % self.simple_commands) - return (yield from self.execute(Command(name, self.new_tag(), *args, loop=self.loop))) + return await self.execute(Command(name, self.new_tag(), *args, loop=self.loop)) - @asyncio.coroutine - def wait_async_pending_commands(self): - yield from asyncio.wait([asyncio.ensure_future(cmd.wait()) for cmd in self.pending_async_commands.values()]) + async def wait_async_pending_commands(self): + await asyncio.wait([asyncio.ensure_future(cmd.wait()) for cmd in self.pending_async_commands.values()]) - @asyncio.coroutine - def wait(self, state_regexp): + async def wait(self, state_regexp): state_re = re.compile(state_regexp) - with (yield from self.state_condition): - yield from self.state_condition.wait_for(lambda: state_re.match(self.state)) + with (await self.state_condition): + await self.state_condition.wait_for(lambda: state_re.match(self.state)) def _untagged_response(self, line): line = line.replace('* ', '') @@ -705,153 +682,120 @@ def create_client(self, host, port, loop, conn_lost_cb=None, ssl_context=None): def get_state(self): return self.protocol.state - @asyncio.coroutine - def wait_hello_from_server(self): - yield from asyncio.wait_for(self.protocol.wait('AUTH|NONAUTH'), self.timeout) + async def wait_hello_from_server(self): + await asyncio.wait_for(self.protocol.wait('AUTH|NONAUTH'), self.timeout) - @asyncio.coroutine - def login(self, user, password): - return (yield from asyncio.wait_for(self.protocol.login(user, password), self.timeout)) + async def login(self, user, password): + return await asyncio.wait_for(self.protocol.login(user, password), self.timeout) - @asyncio.coroutine - def logout(self): - return (yield from asyncio.wait_for(self.protocol.logout(), self.timeout)) + async def logout(self): + return await asyncio.wait_for(self.protocol.logout(), self.timeout) - @asyncio.coroutine - def select(self, mailbox='INBOX'): - return (yield from asyncio.wait_for(self.protocol.select(mailbox), self.timeout)) + async def select(self, mailbox='INBOX'): + return await asyncio.wait_for(self.protocol.select(mailbox), self.timeout) - @asyncio.coroutine - def search(self, *criteria, charset='utf-8'): - return (yield from asyncio.wait_for(self.protocol.search(*criteria, charset=charset), self.timeout)) + async def search(self, *criteria, charset='utf-8'): + return await asyncio.wait_for(self.protocol.search(*criteria, charset=charset), self.timeout) - @asyncio.coroutine - def uid_search(self, *criteria, charset='utf-8'): - return ( - yield from asyncio.wait_for(self.protocol.search(*criteria, by_uid=True, charset=charset), self.timeout)) + async def uid_search(self, *criteria, charset='utf-8'): + return await asyncio.wait_for(self.protocol.search(*criteria, by_uid=True, charset=charset), self.timeout) - @asyncio.coroutine - def uid(self, command, *criteria): - return (yield from self.protocol.uid(command, *criteria, timeout=self.timeout)) + async def uid(self, command, *criteria): + return await self.protocol.uid(command, *criteria, timeout=self.timeout) - @asyncio.coroutine - def store(self, *criteria): - return (yield from asyncio.wait_for(self.protocol.store(*criteria), self.timeout)) + async def store(self, *criteria): + return await asyncio.wait_for(self.protocol.store(*criteria), self.timeout) - @asyncio.coroutine - def copy(self, *criteria): - return (yield from asyncio.wait_for(self.protocol.copy(*criteria), self.timeout)) + async def copy(self, *criteria): + return await asyncio.wait_for(self.protocol.copy(*criteria), self.timeout) - @asyncio.coroutine - def expunge(self): - return (yield from asyncio.wait_for(self.protocol.expunge(), self.timeout)) + async def expunge(self): + return await asyncio.wait_for(self.protocol.expunge(), self.timeout) - @asyncio.coroutine - def fetch(self, message_set, message_parts): - return (yield from self.protocol.fetch(message_set, message_parts, timeout=self.timeout)) + async def fetch(self, message_set, message_parts): + return await self.protocol.fetch(message_set, message_parts, timeout=self.timeout) - @asyncio.coroutine - def idle(self): - return (yield from self.protocol.idle()) + async def idle(self): + return await self.protocol.idle() def idle_done(self): if self._idle_waiter is not None: self._idle_waiter.cancel() self.protocol.idle_done() - @asyncio.coroutine - def stop_wait_server_push(self): + async def stop_wait_server_push(self): if self.protocol.has_pending_idle_command(): - yield from self.protocol.idle_queue.put(STOP_WAIT_SERVER_PUSH) + await self.protocol.idle_queue.put(STOP_WAIT_SERVER_PUSH) return True return False - @asyncio.coroutine - def wait_server_push(self, timeout=TWENTY_NINE_MINUTES): - return (yield from asyncio.wait_for(self.protocol.idle_queue.get(), timeout=timeout)) + async def wait_server_push(self, timeout=TWENTY_NINE_MINUTES): + return await asyncio.wait_for(self.protocol.idle_queue.get(), timeout=timeout) - @asyncio.coroutine - def idle_start(self, timeout=TWENTY_NINE_MINUTES): + async def idle_start(self, timeout=TWENTY_NINE_MINUTES): if self._idle_waiter is not None: self._idle_waiter.cancel() idle = asyncio.ensure_future(self.idle()) self._idle_waiter = self.protocol.loop.call_later(timeout, lambda: asyncio.ensure_future(self.stop_wait_server_push())) - yield from self.wait_server_push(self.timeout) # idling continuation + await self.wait_server_push(self.timeout) # idling continuation return idle def has_pending_idle(self): return self.protocol.has_pending_idle_command() - @asyncio.coroutine - def id(self, **kwargs): - return (yield from asyncio.wait_for(self.protocol.id(**kwargs), self.timeout)) + async def id(self, **kwargs): + return await asyncio.wait_for(self.protocol.id(**kwargs), self.timeout) - @asyncio.coroutine - def namespace(self): - return (yield from asyncio.wait_for(self.protocol.namespace(), self.timeout)) + async def namespace(self): + return await asyncio.wait_for(self.protocol.namespace(), self.timeout) - @asyncio.coroutine - def noop(self): - return (yield from asyncio.wait_for(self.protocol.simple_command('NOOP'), self.timeout)) + async def noop(self): + return await asyncio.wait_for(self.protocol.simple_command('NOOP'), self.timeout) - @asyncio.coroutine - def check(self): - return (yield from asyncio.wait_for(self.protocol.simple_command('CHECK'), self.timeout)) + async def check(self): + return await asyncio.wait_for(self.protocol.simple_command('CHECK'), self.timeout) - @asyncio.coroutine - def examine(self, mailbox='INBOX'): - return (yield from asyncio.wait_for(self.protocol.simple_command('EXAMINE', mailbox), self.timeout)) + async def examine(self, mailbox='INBOX'): + return await asyncio.wait_for(self.protocol.simple_command('EXAMINE', mailbox), self.timeout) - @asyncio.coroutine - def status(self, mailbox, names): - return (yield from asyncio.wait_for(self.protocol.simple_command('STATUS', mailbox, names), self.timeout)) + async def status(self, mailbox, names): + return await asyncio.wait_for(self.protocol.simple_command('STATUS', mailbox, names), self.timeout) - @asyncio.coroutine - def subscribe(self, mailbox): - return (yield from asyncio.wait_for(self.protocol.simple_command('SUBSCRIBE', mailbox), self.timeout)) + async def subscribe(self, mailbox): + return await asyncio.wait_for(self.protocol.simple_command('SUBSCRIBE', mailbox), self.timeout) - @asyncio.coroutine - def unsubscribe(self, mailbox): - return (yield from asyncio.wait_for(self.protocol.simple_command('UNSUBSCRIBE', mailbox), self.timeout)) + async def unsubscribe(self, mailbox): + return await asyncio.wait_for(self.protocol.simple_command('UNSUBSCRIBE', mailbox), self.timeout) - @asyncio.coroutine - def lsub(self, reference_name, mailbox_name): - return (yield from asyncio.wait_for(self.protocol.simple_command('LSUB', reference_name, mailbox_name), self.timeout)) + async def lsub(self, reference_name, mailbox_name): + return await asyncio.wait_for(self.protocol.simple_command('LSUB', reference_name, mailbox_name), self.timeout) - @asyncio.coroutine - def create(self, mailbox_name): - return (yield from asyncio.wait_for(self.protocol.simple_command('CREATE', mailbox_name), self.timeout)) + async def create(self, mailbox_name): + return await asyncio.wait_for(self.protocol.simple_command('CREATE', mailbox_name), self.timeout) - @asyncio.coroutine - def delete(self, mailbox_name): - return (yield from asyncio.wait_for(self.protocol.simple_command('DELETE', mailbox_name), self.timeout)) + async def delete(self, mailbox_name): + return await asyncio.wait_for(self.protocol.simple_command('DELETE', mailbox_name), self.timeout) - @asyncio.coroutine - def rename(self, old_mailbox_name, new_mailbox_name): - return (yield from asyncio.wait_for(self.protocol.simple_command('RENAME', old_mailbox_name, new_mailbox_name), self.timeout)) + async def rename(self, old_mailbox_name, new_mailbox_name): + return await asyncio.wait_for(self.protocol.simple_command('RENAME', old_mailbox_name, new_mailbox_name), self.timeout) - @asyncio.coroutine - def list(self, reference_name, mailbox_pattern): - return (yield from asyncio.wait_for(self.protocol.simple_command('LIST', reference_name, mailbox_pattern), self.timeout)) + async def list(self, reference_name, mailbox_pattern): + return await asyncio.wait_for(self.protocol.simple_command('LIST', reference_name, mailbox_pattern), self.timeout) - @asyncio.coroutine - def append(self, message_bytes, mailbox='INBOX', flags=None, date=None): - return (yield from self.protocol.append(message_bytes, mailbox, flags, date, timeout=self.timeout)) + async def append(self, message_bytes, mailbox='INBOX', flags=None, date=None): + return await self.protocol.append(message_bytes, mailbox, flags, date, timeout=self.timeout) - @asyncio.coroutine - def close(self): - return (yield from asyncio.wait_for(self.protocol.close(), self.timeout)) + async def close(self): + return await asyncio.wait_for(self.protocol.close(), self.timeout) - @asyncio.coroutine - def move(self, uid_set, mailbox): - return (yield from asyncio.wait_for(self.protocol.move(uid_set, mailbox), self.timeout)) + async def move(self, uid_set, mailbox): + return await asyncio.wait_for(self.protocol.move(uid_set, mailbox), self.timeout) - @asyncio.coroutine - def enable(self, capability): + async def enable(self, capability): if 'ENABLE' not in self.protocol.capabilities: raise Abort('server has not ENABLE capability') - return (yield from asyncio.wait_for(self.protocol.simple_command('ENABLE', capability), self.timeout)) + return await asyncio.wait_for(self.protocol.simple_command('ENABLE', capability), self.timeout) def has_capability(self, capability): return capability in self.protocol.capabilities @@ -885,9 +829,11 @@ def int2ap(num): val += ap[mod:mod + 1] return val + Months = ' Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ') Mon2num = {s.encode():n+1 for n, s in enumerate(Months[1:])} + def time2internaldate(date_time): """Convert date_time to IMAP4 INTERNALDATE representation. diff --git a/aioimaplib/tests/example.py b/aioimaplib/tests/example.py index 0598b15..b9aca28 100644 --- a/aioimaplib/tests/example.py +++ b/aioimaplib/tests/example.py @@ -4,36 +4,34 @@ from aioimaplib import aioimaplib -@asyncio.coroutine -def wait_for_new_message(host, user, password): +async def wait_for_new_message(host, user, password): imap_client = aioimaplib.IMAP4_SSL(host=host) - yield from imap_client.wait_hello_from_server() + await imap_client.wait_hello_from_server() - yield from imap_client.login(user, password) - yield from imap_client.select() + await imap_client.login(user, password) + await imap_client.select() - asyncio.async(imap_client.idle()) + await imap_client.idle() while True: - msg = yield from imap_client.wait_server_push() + msg = await imap_client.wait_server_push() print('--> received from server: %s' % msg) if 'EXISTS' in msg: imap_client.idle_done() break - yield from imap_client.logout() + await imap_client.logout() -@asyncio.coroutine -def fetch_mail(host, user, password): +async def fetch_mail(host, user, password): imap_client = aioimaplib.IMAP4_SSL(host=host) - yield from imap_client.wait_hello_from_server() + await imap_client.wait_hello_from_server() - yield from imap_client.login(user, password) + await imap_client.login(user, password) - response = yield from imap_client.select() + response = await imap_client.select() print('there is %s messages INBOX' % aioimaplib.extract_exists(response)) - yield from imap_client.logout() + await imap_client.logout() if __name__ == '__main__': diff --git a/aioimaplib/tests/imapserver.py b/aioimaplib/tests/imapserver.py index 40c4ecd..6bdcfbd 100644 --- a/aioimaplib/tests/imapserver.py +++ b/aioimaplib/tests/imapserver.py @@ -168,9 +168,8 @@ def _reindex(self, user, mailbox): def critical_section(next_state): - @asyncio.coroutine - def execute_section(self, state, critical_func, *args, **kwargs): - with (yield from self.state_condition): + async def execute_section(self, state, critical_func, *args, **kwargs): + with await self.state_condition: critical_func(self, *args, **kwargs) self.state = state log.debug('state -> %s' % state) @@ -310,10 +309,9 @@ def close(self, tag, *args): self.user_mailbox = None self.send_tagged_line(tag, 'OK CLOSE completed.') - @asyncio.coroutine - def wait(self, state): - with (yield from self.state_condition): - yield from self.state_condition.wait_for(lambda: self.state == state) + async def wait(self, state): + with await self.state_condition: + await self.state_condition.wait_for(lambda: self.state == state) def examine(self, tag, *args): mailbox_name = args[0] @@ -663,8 +661,7 @@ def receive(self, mail, imap_user=None, mailbox='INBOX'): uids.append(self._server_state.imap_receive(to, mail, mailbox)) return uids - @asyncio.coroutine - def wait_state(self, state, user): + async def wait_state(self, state, user): user_connections = [connection for connection in self._connections if connection.user_login == user] if len(user_connections) == 0: other_users = list(map(lambda c: c.user_login, self._connections)) @@ -672,7 +669,7 @@ def wait_state(self, state, user): if len(user_connections) > 1: raise ValueError("wait_state can't handle %d connections for user %s" % (len(user_connections), user)) - yield from user_connections[0].wait(state) + await user_connections[0].wait(state) def get_connection(self, user): return self._server_state.get_connection(user) @@ -760,6 +757,7 @@ def create(to, mail_from='', subject='', content='', return Mail(msg, date=date) + if __name__ == '__main__': loop = asyncio.get_event_loop() server = MockImapServer().run_server() diff --git a/aioimaplib/tests/test_acceptance_aioimaplib.py b/aioimaplib/tests/test_acceptance_aioimaplib.py index 733f99a..982d2fd 100644 --- a/aioimaplib/tests/test_acceptance_aioimaplib.py +++ b/aioimaplib/tests/test_acceptance_aioimaplib.py @@ -29,19 +29,17 @@ class TestAioimaplibAcceptance(AioWithImapServer, TestCase): def setUp(self): self._init_server(self.loop) - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() - @asyncio.coroutine - def test_file_with_attachement(self): + async def test_file_with_attachement(self): with open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/test_attachment.eml'), mode='br') as msg: - imap_client = yield from self.login_user('user@mail', 'pass', select=True) + imap_client = await self.login_user('user@mail', 'pass', select=True) mail = Mail(email.message_from_binary_file(msg)) self.imapserver.receive(mail, imap_user='user@mail') - result, data = yield from imap_client.fetch('1', '(RFC822)') + result, data = await imap_client.fetch('1', '(RFC822)') self.assertEqual('OK', result) self.assertEqual(['1 FETCH (RFC822 {418898}', mail.as_bytes(), ')', 'FETCH completed.'], data) diff --git a/aioimaplib/tests/test_aioimaplib.py b/aioimaplib/tests/test_aioimaplib.py index 3333a5c..85aa9f5 100644 --- a/aioimaplib/tests/test_aioimaplib.py +++ b/aioimaplib/tests/test_aioimaplib.py @@ -248,98 +248,90 @@ def test_fetch_only_the_last_message_data(self): class TestAioimaplibCommand(asynctest.ClockedTestCase): - @asyncio.coroutine - def test_command_timeout(self): + async def test_command_timeout(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=1) - yield from self.advance(2) + await self.advance(2) with self.assertRaises(AioImapException): - yield from cmd.wait() + await cmd.wait() - @asyncio.coroutine - def test_command_close_cancels_timer(self): + async def test_command_close_cancels_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=1) cmd.close('line', 'OK') - yield from self.advance(3) + await self.advance(3) - yield from cmd.wait() + await cmd.wait() self.assertEqual(Response('OK', ['line']), cmd.response) - @asyncio.coroutine - def test_command_begin_literal_data_resets_timer(self): + async def test_command_begin_literal_data_resets_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) - yield from self.advance(1) + await self.advance(1) cmd.begin_literal_data(7, b'literal') - yield from self.advance(1.9) + await self.advance(1.9) cmd.close('line', 'OK') - yield from cmd.wait() + await cmd.wait() self.assertEqual(Response('OK', [b'literal', 'line']), cmd.response) - @asyncio.coroutine - def test_command_append_data_resets_timer(self): + async def test_command_append_data_resets_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) cmd.begin_literal_data(4, b'da') - yield from self.advance(1.9) + await self.advance(1.9) cmd.append_literal_data(b'ta') - yield from self.advance(1.9) + await self.advance(1.9) cmd.close('line', 'OK') - yield from cmd.wait() + await cmd.wait() self.assertEqual(Response('OK', [b'data', 'line']), cmd.response) - @asyncio.coroutine - def test_command_append_literal_data_resets_timer(self): + async def test_command_append_literal_data_resets_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) cmd.begin_literal_data(12, b'literal') - yield from self.advance(1.9) + await self.advance(1.9) cmd.append_literal_data(b' data') - yield from self.advance(1.9) + await self.advance(1.9) cmd.close('line', 'OK') - yield from cmd.wait() + await cmd.wait() self.assertEqual(Response('OK', [b'literal data', 'line']), cmd.response) - @asyncio.coroutine - def test_command_append_to_resp_resets_timer(self): + async def test_command_append_to_resp_resets_timer(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) - yield from self.advance(1.9) + await self.advance(1.9) cmd.append_to_resp('line 1') - yield from self.advance(1.9) + await self.advance(1.9) cmd.close('line 2', 'OK') - yield from cmd.wait() + await cmd.wait() self.assertEqual(Response('OK', ['line 1', 'line 2']), cmd.response) - @asyncio.coroutine - def test_command_timeout_while_receiving_data(self): + async def test_command_timeout_while_receiving_data(self): cmd = Command('CMD', 'tag', loop=self.loop, timeout=2) - yield from self.advance(1) + await self.advance(1) cmd.begin_literal_data(12, b'literal') - yield from self.advance(3) + await self.advance(3) with self.assertRaises(AioImapException): - yield from cmd.wait() + await cmd.wait() class AioWithImapServer(WithImapServer): - @asyncio.coroutine - def login_user(self, login, password, select=False, lib=aioimaplib.IMAP4): + async def login_user(self, login, password, select=False, lib=aioimaplib.IMAP4): imap_client = lib(port=12345, loop=self.loop, timeout=3) - yield from asyncio.wait_for(imap_client.wait_hello_from_server(), 2) + await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) - yield from imap_client.login(login, password) + await imap_client.login(login, password) if select: - yield from imap_client.select() + await imap_client.select() return imap_client @@ -361,7 +353,6 @@ def create_protocol(): return self.loop.run_until_complete(server) - class AllowedVersionsAioWithImapServer(AioWithImapServer): def _init_server(self, loop, capabilities=None, ssl_context=None): self.loop = loop @@ -374,48 +365,41 @@ def _init_server(self, loop, capabilities=None, ssl_context=None): ) - class TestAioimaplibAllowedVersions(AllowedVersionsAioWithImapServer, asynctest.TestCase): def setUp(self): self._init_server(self.loop) - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() - @asyncio.coroutine - def test_capabilities_allowed_versions(self): + async 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') + await 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) - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() - @asyncio.coroutine - def test_capabilities(self): + async def test_capabilities(self): imap_client = aioimaplib.IMAP4(port=12345, loop=self.loop) - yield from asyncio.wait_for(imap_client.wait_hello_from_server(), 2) + await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) self.assertEquals('IMAP4REV1', imap_client.protocol.imap_version) self.assertEquals({'IMAP4rev1', 'YESAUTH'}, imap_client.protocol.capabilities) self.assertTrue(imap_client.has_capability('YESAUTH')) - @asyncio.coroutine - def test_login(self): + async def test_login(self): imap_client = aioimaplib.IMAP4(port=12345, loop=self.loop, timeout=3) - yield from asyncio.wait_for(imap_client.wait_hello_from_server(), 2) + await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) - result, data = yield from imap_client.login('user', 'password') + result, data = await imap_client.login('user', 'password') self.assertEquals(aioimaplib.AUTH, imap_client.protocol.state) self.assertEqual('OK', result) @@ -423,12 +407,11 @@ def test_login(self): self.assertTrue(imap_client.has_capability('IDLE')) self.assertTrue(imap_client.has_capability('UIDPLUS')) - @asyncio.coroutine - def test_login_with_special_characters(self): + async def test_login_with_special_characters(self): imap_client = aioimaplib.IMAP4(port=12345, loop=self.loop, timeout=3) - yield from asyncio.wait_for(imap_client.wait_hello_from_server(), 2) + await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) - result, data = yield from imap_client.login('user', 'pass"word') + result, data = await imap_client.login('user', 'pass"word') self.assertEquals(aioimaplib.AUTH, imap_client.protocol.state) self.assertEqual('OK', result) @@ -436,87 +419,79 @@ def test_login_with_special_characters(self): self.assertTrue(imap_client.has_capability('IDLE')) self.assertTrue(imap_client.has_capability('UIDPLUS')) - @asyncio.coroutine - def test_login_twice(self): + async def test_login_twice(self): with self.assertRaises(aioimaplib.Error) as expected: - imap_client = yield from self.login_user('user', 'pass') + imap_client = await self.login_user('user', 'pass') - yield from imap_client.login('user', 'password') + await imap_client.login('user', 'password') self.assertEqual(expected.exception.args, ('command LOGIN illegal in state AUTH',)) - @asyncio.coroutine - def test_logout(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_logout(self): + imap_client = await self.login_user('user', 'pass') - result, data = yield from imap_client.logout() + result, data = await imap_client.logout() self.assertEqual('OK', result) self.assertEqual(['BYE Logging out', 'LOGOUT completed'], data) self.assertEquals(aioimaplib.LOGOUT, imap_client.protocol.state) - @asyncio.coroutine - def test_select_no_messages(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_select_no_messages(self): + imap_client = await self.login_user('user', 'pass') - resp = yield from imap_client.select() + resp = await imap_client.select() self.assertEqual('OK', resp[0]) self.assertEqual(0, extract_exists(resp)) self.assertEquals(aioimaplib.SELECTED, imap_client.protocol.state) - @asyncio.coroutine - def test_examine_no_messages(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_examine_no_messages(self): + imap_client = await self.login_user('user', 'pass') - self.assertEquals(0, extract_exists((yield from imap_client.examine()))) + self.assertEquals(0, extract_exists((await imap_client.examine()))) self.assertEquals(aioimaplib.AUTH, imap_client.protocol.state) - @asyncio.coroutine - def test_search_two_messages(self): + async def test_search_two_messages(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, data = yield from imap_client.search('ALL') + result, data = await imap_client.search('ALL') self.assertEqual('OK', result) self.assertEqual('1 2', data[0]) - @asyncio.coroutine - def test_uid_with_illegal_command(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_uid_with_illegal_command(self): + imap_client = await self.login_user('user', 'pass', select=True) for command in {'COPY', 'FETCH', 'STORE', 'EXPUNGE', 'MOVE'}.symmetric_difference(Commands.keys()): with self.assertRaises(aioimaplib.Abort) as expected: - yield from imap_client.uid(command) + await imap_client.uid(command) self.assertEqual(expected.exception.args, ('command UID only possible with COPY, FETCH, EXPUNGE (w/UIDPLUS) or STORE (was %s)' % command,)) - @asyncio.coroutine - def test_search_three_messages_by_uid(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_search_three_messages_by_uid(self): + imap_client = await self.login_user('user', 'pass', select=True) self.imapserver.receive(Mail.create(['user'])) # id=1 uid=1 self.imapserver.receive(Mail.create(['user']), mailbox='OTHER_MAILBOX') # id=1 uid=1 self.imapserver.receive(Mail.create(['user'])) # id=2 uid=2 - self.assertEqual('1 2', (yield from imap_client.search('ALL')).lines[0]) - self.assertEqual('1 2', (yield from imap_client.uid_search('ALL')).lines[0]) + self.assertEqual('1 2', (await imap_client.search('ALL')).lines[0]) + self.assertEqual('1 2', (await imap_client.uid_search('ALL')).lines[0]) - yield from imap_client.select('OTHER_MAILBOX') - self.assertEqual('1', (yield from imap_client.uid_search('ALL')).lines[0]) + await imap_client.select('OTHER_MAILBOX') + self.assertEqual('1', (await imap_client.uid_search('ALL')).lines[0]) - @asyncio.coroutine - def test_fetch(self): + async def test_fetch(self): print('test loop %r' % self.loop) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) mail = Mail.create(['user'], mail_from='me', subject='hello', content='pleased to meet you, wont you guess my name ?') self.imapserver.receive(mail) - result, data = yield from imap_client.fetch('1', '(RFC822)') + result, data = await imap_client.fetch('1', '(RFC822)') content = mail.as_bytes() self.assertEqual('OK', result) @@ -525,320 +500,289 @@ def test_fetch(self): 'FETCH completed.' ], data) - @asyncio.coroutine - def test_fetch_by_uid_without_body(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_fetch_by_uid_without_body(self): + imap_client = await self.login_user('user', 'pass', select=True) mail = Mail.create(['user'], mail_from='me', subject='hello', content='pleased to meet you, wont you guess my name ?') self.imapserver.receive(mail) - response = (yield from imap_client.uid('fetch', '1', '(UID FLAGS)')) + response = (await imap_client.uid('fetch', '1', '(UID FLAGS)')) self.assertEqual('OK', response.result) self.assertEquals('1 FETCH (UID 1 FLAGS ())', response.lines[0]) - @asyncio.coroutine - def test_fetch_by_uid(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_fetch_by_uid(self): + imap_client = await self.login_user('user', 'pass', select=True) mail = Mail.create(['user'], mail_from='me', subject='hello', content='pleased to meet you, wont you guess my name ?') self.imapserver.receive(mail) - response = (yield from imap_client.uid('fetch', '1', '(RFC822)')) + response = (await imap_client.uid('fetch', '1', '(RFC822)')) self.assertEqual('OK', response.result) self.assertEquals(mail.as_bytes(), response.lines[1]) - @asyncio.coroutine - def test_idle(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_idle(self): + imap_client = await self.login_user('user', 'pass', select=True) - idle = yield from imap_client.idle_start(timeout=0.3) + idle = await imap_client.idle_start(timeout=0.3) self.imapserver.receive(Mail.create(to=['user'], mail_from='me', subject='hello')) - self.assertEquals(['1 EXISTS', '1 RECENT'], (yield from imap_client.wait_server_push())) + self.assertEquals(['1 EXISTS', '1 RECENT'], (await imap_client.wait_server_push())) imap_client.idle_done() - self.assertEquals(('OK', ['IDLE terminated']), (yield from asyncio.wait_for(idle, 1))) + self.assertEquals(('OK', ['IDLE terminated']), (await asyncio.wait_for(idle, 1))) self.assertTrue(imap_client._idle_waiter._cancelled) with self.assertRaises(asyncio.TimeoutError): - yield from imap_client.wait_server_push(timeout=0.1) + await imap_client.wait_server_push(timeout=0.1) - @asyncio.coroutine - def test_idle_loop(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_idle_loop(self): + imap_client = await self.login_user('user', 'pass', select=True) - idle = yield from imap_client.idle_start(timeout=0.3) + idle = await imap_client.idle_start(timeout=0.3) self.imapserver.receive(Mail.create(to=['user'], mail_from='me', subject='hello')) data = list() while imap_client.has_pending_idle(): - data.append((yield from imap_client.wait_server_push())) + data.append((await imap_client.wait_server_push())) if data[-1] == STOP_WAIT_SERVER_PUSH: imap_client.idle_done() - yield from asyncio.wait_for(idle, 1) + await asyncio.wait_for(idle, 1) self.assertEqual([['1 EXISTS', '1 RECENT'], STOP_WAIT_SERVER_PUSH], data) - @asyncio.coroutine - def test_idle_stop(self): - imap_client = yield from self.login_user('user', 'pass', select=True) - idle = yield from imap_client.idle_start() + async def test_idle_stop(self): + imap_client = await self.login_user('user', 'pass', select=True) + idle = await imap_client.idle_start() - self.assertTrue((yield from imap_client.stop_wait_server_push())) + self.assertTrue((await imap_client.stop_wait_server_push())) - self.assertEquals(STOP_WAIT_SERVER_PUSH, (yield from imap_client.wait_server_push())) + self.assertEquals(STOP_WAIT_SERVER_PUSH, (await imap_client.wait_server_push())) imap_client.idle_done() - yield from asyncio.wait_for(idle, 1) + await asyncio.wait_for(idle, 1) - @asyncio.coroutine - def test_idle_stop_does_nothing_if_no_pending_idle(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_idle_stop_does_nothing_if_no_pending_idle(self): + imap_client = await self.login_user('user', 'pass', select=True) - self.assertFalse((yield from imap_client.stop_wait_server_push())) + self.assertFalse((await imap_client.stop_wait_server_push())) - @asyncio.coroutine - def test_store_and_search_by_keyword(self): + async def test_store_and_search_by_keyword(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) - self.assertEqual('', (yield from imap_client.uid_search('KEYWORD FOO', charset=None)).lines[0]) + imap_client = await self.login_user('user', 'pass', select=True) + self.assertEqual('', (await imap_client.uid_search('KEYWORD FOO', charset=None)).lines[0]) - self.assertEquals('OK', (yield from imap_client.uid('store', '1', '+FLAGS (FOO)')).result) + self.assertEquals('OK', (await imap_client.uid('store', '1', '+FLAGS (FOO)')).result) - self.assertEqual('1', (yield from imap_client.uid_search('KEYWORD FOO', charset=None)).lines[0]) - self.assertEqual('2', (yield from imap_client.uid_search('UNKEYWORD FOO', charset=None)).lines[0]) + self.assertEqual('1', (await imap_client.uid_search('KEYWORD FOO', charset=None)).lines[0]) + self.assertEqual('2', (await imap_client.uid_search('UNKEYWORD FOO', charset=None)).lines[0]) - @asyncio.coroutine - def test_expunge_messages(self): + async def test_expunge_messages(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - self.assertEquals(('OK', ['1 EXPUNGE', '2 EXPUNGE', 'EXPUNGE completed.']), (yield from imap_client.expunge())) + self.assertEquals(('OK', ['1 EXPUNGE', '2 EXPUNGE', 'EXPUNGE completed.']), (await imap_client.expunge())) - self.assertEquals(0, extract_exists((yield from imap_client.select()))) + self.assertEquals(0, extract_exists((await imap_client.select()))) - @asyncio.coroutine - def test_copy_messages(self): + async def test_copy_messages(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, _ = yield from imap_client.copy('1', '2', 'MAILBOX') + result, _ = await imap_client.copy('1', '2', 'MAILBOX') self.assertEqual('OK', result) - self.assertEquals(2, extract_exists((yield from imap_client.select('MAILBOX')))) + self.assertEquals(2, extract_exists((await imap_client.select('MAILBOX')))) - @asyncio.coroutine - def test_copy_messages_by_uid(self): + async def test_copy_messages_by_uid(self): self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, _ = yield from imap_client.uid('copy', '1', 'MAILBOX') + result, _ = await imap_client.uid('copy', '1', 'MAILBOX') self.assertEqual('OK', result) - self.assertEquals(1, extract_exists((yield from imap_client.select('MAILBOX')))) + self.assertEquals(1, extract_exists((await imap_client.select('MAILBOX')))) - @asyncio.coroutine - def test_concurrency_1_executing_sync_commands_sequentially(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_concurrency_1_executing_sync_commands_sequentially(self): + imap_client = await self.login_user('user', 'pass') f1 = asyncio.ensure_future(imap_client.examine('INBOX')) f2 = asyncio.ensure_future(imap_client.examine('MAILBOX')) - yield from asyncio.wait([f1, f2]) + await asyncio.wait([f1, f2]) self.assertIsNone(f1.exception()) self.assertIsNone(f2.exception()) - @asyncio.coroutine - def test_concurrency_2_executing_same_async_commands_sequentially(self): + async def test_concurrency_2_executing_same_async_commands_sequentially(self): self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) f1 = asyncio.ensure_future(imap_client.fetch('1', '(RFC822)')) f2 = asyncio.ensure_future(imap_client.fetch('1', '(RFC822)')) - yield from asyncio.wait([f1, f2]) + await asyncio.wait([f1, f2]) self.assertIsNone(f1.exception()) self.assertIsNone(f2.exception()) - @asyncio.coroutine - def test_concurrency_3_executing_async_commands_in_parallel(self): + async def test_concurrency_3_executing_async_commands_in_parallel(self): # cf valid example in https://tools.ietf.org/html/rfc3501#section-5.5 self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) store = asyncio.ensure_future(imap_client.store('1', '+FLAGS (FOO)')) copy = asyncio.ensure_future(imap_client.copy('1', 'MBOX')) expunge = asyncio.ensure_future(imap_client.expunge()) - yield from asyncio.wait([store, copy, expunge]) - self.assertEquals(0, extract_exists((yield from imap_client.select()))) - self.assertEquals(1, extract_exists((yield from imap_client.select('MBOX')))) - self.assertEqual('1', (yield from imap_client.search('KEYWORD FOO', charset=None)).lines[0]) + await asyncio.wait([store, copy, expunge]) + self.assertEquals(0, extract_exists((await imap_client.select()))) + self.assertEquals(1, extract_exists((await imap_client.select('MBOX')))) + self.assertEqual('1', (await imap_client.search('KEYWORD FOO', charset=None)).lines[0]) - @asyncio.coroutine - def test_concurrency_4_sync_command_waits_for_async_commands_to_finish(self): + async def test_concurrency_4_sync_command_waits_for_async_commands_to_finish(self): self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) asyncio.ensure_future(imap_client.copy('1', 'MBOX')) asyncio.ensure_future(imap_client.expunge()) examine = asyncio.ensure_future(imap_client.examine('MBOX')) - self.assertEquals(1, extract_exists((yield from asyncio.wait_for(examine, 1)))) + self.assertEquals(1, extract_exists((await asyncio.wait_for(examine, 1)))) - @asyncio.coroutine - def test_noop(self): - imap_client = yield from self.login_user('user', 'pass') - self.assertEquals(('OK', ['NOOP completed.']), (yield from imap_client.noop())) + async def test_noop(self): + imap_client = await self.login_user('user', 'pass') + self.assertEquals(('OK', ['NOOP completed.']), (await imap_client.noop())) - @asyncio.coroutine - def test_noop_with_untagged_data(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_noop_with_untagged_data(self): + imap_client = await self.login_user('user', 'pass') self.imapserver.receive(Mail.create(['user'])) - self.assertEquals(('OK', ['1 EXISTS', '1 RECENT', 'NOOP completed.']), (yield from imap_client.noop())) + self.assertEquals(('OK', ['1 EXISTS', '1 RECENT', 'NOOP completed.']), (await imap_client.noop())) - @asyncio.coroutine - def test_check(self): - imap_client = yield from self.login_user('user', 'pass', select=True) - self.assertEquals(('OK', ['CHECK completed.']), (yield from imap_client.check())) + async def test_check(self): + imap_client = await self.login_user('user', 'pass', select=True) + self.assertEquals(('OK', ['CHECK completed.']), (await imap_client.check())) - @asyncio.coroutine - def test_close(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_close(self): + imap_client = await self.login_user('user', 'pass', select=True) self.assertEquals(imapserver.SELECTED, self.imapserver.get_connection('user').state) - self.assertEquals(('OK', ['CLOSE completed.']), (yield from imap_client.close())) + self.assertEquals(('OK', ['CLOSE completed.']), (await imap_client.close())) self.assertEquals(imapserver.AUTH, self.imapserver.get_connection('user').state) - @asyncio.coroutine - def test_status(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_status(self): + imap_client = await self.login_user('user', 'pass') self.assertEquals('INBOX (MESSAGES 0 UIDNEXT 1)', - (yield from imap_client.status('INBOX', '(MESSAGES UIDNEXT)')).lines[0]) + (await imap_client.status('INBOX', '(MESSAGES UIDNEXT)')).lines[0]) - @asyncio.coroutine - def test_subscribe_unsubscribe_lsub(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_subscribe_unsubscribe_lsub(self): + imap_client = await self.login_user('user', 'pass') - self.assertEquals(('OK', ['SUBSCRIBE completed.']), (yield from imap_client.subscribe('#fr.soc.feminisme'))) + self.assertEquals(('OK', ['SUBSCRIBE completed.']), (await imap_client.subscribe('#fr.soc.feminisme'))) self.assertEquals(('OK', ['() "." #fr.soc.feminisme', 'LSUB completed.']), - (yield from imap_client.lsub('#fr.', 'soc.*'))) - self.assertEquals(('OK', ['UNSUBSCRIBE completed.']), (yield from imap_client.unsubscribe('#fr.soc.feminisme'))) - self.assertEquals(('OK', ['LSUB completed.']), (yield from imap_client.lsub('#fr', '.*'))) + (await imap_client.lsub('#fr.', 'soc.*'))) + self.assertEquals(('OK', ['UNSUBSCRIBE completed.']), (await imap_client.unsubscribe('#fr.soc.feminisme'))) + self.assertEquals(('OK', ['LSUB completed.']), (await imap_client.lsub('#fr', '.*'))) - @asyncio.coroutine - def test_create_delete_mailbox(self): - imap_client = yield from self.login_user('user', 'pass') - self.assertEquals('NO', (yield from imap_client.status('MBOX', '(MESSAGES)')).result) + async def test_create_delete_mailbox(self): + imap_client = await self.login_user('user', 'pass') + self.assertEquals('NO', (await imap_client.status('MBOX', '(MESSAGES)')).result) - self.assertEquals(('OK', ['CREATE completed.']), (yield from imap_client.create('MBOX'))) - self.assertEquals('OK', (yield from imap_client.status('MBOX', '(MESSAGES)')).result) + self.assertEquals(('OK', ['CREATE completed.']), (await imap_client.create('MBOX'))) + self.assertEquals('OK', (await imap_client.status('MBOX', '(MESSAGES)')).result) - self.assertEquals(('OK', ['DELETE completed.']), (yield from imap_client.delete('MBOX'))) - self.assertEquals('NO', (yield from imap_client.status('MBOX', '(MESSAGES)')).result) + self.assertEquals(('OK', ['DELETE completed.']), (await imap_client.delete('MBOX'))) + self.assertEquals('NO', (await imap_client.status('MBOX', '(MESSAGES)')).result) - @asyncio.coroutine - def test_rename_mailbox(self): - imap_client = yield from self.login_user('user', 'pass') - self.assertEquals('NO', (yield from imap_client.status('MBOX', '(MESSAGES)')).result) + async def test_rename_mailbox(self): + imap_client = await self.login_user('user', 'pass') + self.assertEquals('NO', (await imap_client.status('MBOX', '(MESSAGES)')).result) - self.assertEquals(('OK', ['RENAME completed.']), (yield from imap_client.rename('INBOX', 'MBOX'))) + self.assertEquals(('OK', ['RENAME completed.']), (await imap_client.rename('INBOX', 'MBOX'))) - self.assertEquals('OK', (yield from imap_client.status('MBOX', '(MESSAGES)')).result) + self.assertEquals('OK', (await imap_client.status('MBOX', '(MESSAGES)')).result) - @asyncio.coroutine - def test_list(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_list(self): + imap_client = await self.login_user('user', 'pass') self.assertEquals(('OK', ['() "/" Drafts', '() "/" INBOX', '() "/" Sent', '() "/" Trash', - 'LIST completed.']), (yield from imap_client.list('""', '.*'))) + 'LIST completed.']), (await imap_client.list('""', '.*'))) - yield from imap_client.create('MYBOX') + await imap_client.create('MYBOX') self.assertEquals(('OK', ['() "/" Drafts', '() "/" INBOX', '() "/" MYBOX', '() "/" Sent', '() "/" Trash', 'LIST completed.']), - (yield from imap_client.list('""', '.*'))) + (await imap_client.list('""', '.*'))) - @asyncio.coroutine - def test_append(self): - imap_client = yield from self.login_user('user@mail', 'pass') - self.assertEquals(0, extract_exists((yield from imap_client.examine('INBOX')))) + async def test_append(self): + imap_client = await self.login_user('user@mail', 'pass') + self.assertEquals(0, extract_exists((await imap_client.examine('INBOX')))) msg = Mail.create(['user@mail'], subject='append msg', content='do you see me ?') - response = yield from imap_client.append(msg.as_bytes(), mailbox='INBOX', flags='FOO BAR', + response = await imap_client.append(msg.as_bytes(), mailbox='INBOX', flags='FOO BAR', date=datetime.now(tz=utc), ) self.assertEquals('OK', response.result) self.assertTrue('1] APPEND completed' in response.lines[0]) - self.assertEquals(1, extract_exists((yield from imap_client.examine('INBOX')))) + self.assertEquals(1, extract_exists((await imap_client.examine('INBOX')))) - @asyncio.coroutine - def test_rfc5032_within(self): + async def test_rfc5032_within(self): self.imapserver.receive(Mail.create(['user'], date=datetime.now(tz=utc) - timedelta(seconds=84600 * 3))) # 1 self.imapserver.receive(Mail.create(['user'], date=datetime.now(tz=utc) - timedelta(seconds=84600))) # 2 self.imapserver.receive(Mail.create(['user'])) # 3 - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - self.assertEquals('1', (yield from imap_client.search('OLDER', '84700')).lines[0]) - self.assertEquals('2 3', (yield from imap_client.search('YOUNGER', '84700')).lines[0]) + self.assertEquals('1', (await imap_client.search('OLDER', '84700')).lines[0]) + self.assertEquals('2 3', (await imap_client.search('YOUNGER', '84700')).lines[0]) - @asyncio.coroutine - def test_rfc4315_uidplus_expunge(self): + async def test_rfc4315_uidplus_expunge(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - self.assertEquals(('OK', ['1 EXPUNGE', 'UID EXPUNGE completed.']), (yield from imap_client.uid('expunge', '1:1'))) + self.assertEquals(('OK', ['1 EXPUNGE', 'UID EXPUNGE completed.']), (await imap_client.uid('expunge', '1:1'))) - self.assertEquals(1, extract_exists((yield from imap_client.select()))) + self.assertEquals(1, extract_exists((await imap_client.select()))) - @asyncio.coroutine - def test_rfc6851_move(self): + async def test_rfc6851_move(self): self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) uidvalidity = self.imapserver.get_connection('user').uidvalidity self.assertEqual(('OK', ['OK [COPYUID %d 1:1 1:1]' % uidvalidity, '1 EXPUNGE', 'Done']), - (yield from imap_client.move('1:1', 'Trash'))) + (await imap_client.move('1:1', 'Trash'))) - self.assertEquals(0, extract_exists((yield from imap_client.select()))) - self.assertEquals(1, extract_exists((yield from imap_client.select('Trash')))) + self.assertEquals(0, extract_exists((await imap_client.select()))) + self.assertEquals(1, extract_exists((await imap_client.select('Trash')))) - @asyncio.coroutine - def test_rfc6851_uidmove(self): + async def test_rfc6851_uidmove(self): self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) uidvalidity = self.imapserver.get_connection('user').uidvalidity self.assertEqual(('OK', ['OK [COPYUID %d 1:1 1:1]' % uidvalidity, '1 EXPUNGE', 'Done']), - (yield from imap_client.uid('move', '1:1', 'Trash'))) + (await imap_client.uid('move', '1:1', 'Trash'))) - self.assertEquals(0, extract_exists((yield from imap_client.select()))) - self.assertEquals(1, extract_exists((yield from imap_client.select('Trash')))) + self.assertEquals(0, extract_exists((await imap_client.select()))) + self.assertEquals(1, extract_exists((await imap_client.select('Trash')))) - @asyncio.coroutine - def test_rfc5161_enable(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_rfc5161_enable(self): + imap_client = await self.login_user('user', 'pass') self.assertEqual(('OK', ['X-GOOD-IDEA CONDSTORE enabled']), - (yield from imap_client.enable('X-GOOD-IDEA CONDSTORE'))) + (await imap_client.enable('X-GOOD-IDEA CONDSTORE'))) - @asyncio.coroutine - def test_rfc2342_namespace(self): - imap_client = yield from self.login_user('user', 'pass') - response = yield from imap_client.namespace() + async def test_rfc2342_namespace(self): + imap_client = await self.login_user('user', 'pass') + response = await imap_client.namespace() self.assertEqual(('OK', ['(("" "/")) NIL NIL', 'NAMESPACE command completed']), response) - @asyncio.coroutine - def test_rfc2971_id(self): - imap_client = yield from self.login_user('user', 'pass') - response = yield from imap_client.id() + async def test_rfc2971_id(self): + imap_client = await self.login_user('user', 'pass') + response = await imap_client.id() self.assertEqual(('OK', ['ID command completed']), response) @@ -846,45 +790,38 @@ class TestImapServerCapabilities(AioWithImapServer, asynctest.TestCase): def setUp(self): self._init_server(self.loop, capabilities='') - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() - @asyncio.coroutine - def test_idle_messages_without_idle_capability_abort_command(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_idle_messages_without_idle_capability_abort_command(self): + imap_client = await self.login_user('user', 'pass', select=True) with self.assertRaises(Abort): - yield from imap_client.idle() + await imap_client.idle() - @asyncio.coroutine - def test_expunge_messages_without_uidplus_capability_abort_command(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_expunge_messages_without_uidplus_capability_abort_command(self): + imap_client = await self.login_user('user', 'pass', select=True) with self.assertRaises(Abort): - yield from imap_client.uid('expunge', '1:1') + await imap_client.uid('expunge', '1:1') - @asyncio.coroutine - def test_move_without_move_capability_abort_command(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_move_without_move_capability_abort_command(self): + imap_client = await self.login_user('user', 'pass', select=True) with self.assertRaises(Abort): - yield from imap_client.move('1:1', 'Trash') + await imap_client.move('1:1', 'Trash') - @asyncio.coroutine - def test_uidmove_without_move_capability_abort_command(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_uidmove_without_move_capability_abort_command(self): + imap_client = await self.login_user('user', 'pass', select=True) with self.assertRaises(Abort): - yield from imap_client.uid('move', '1:1', 'Trash') + await imap_client.uid('move', '1:1', 'Trash') - @asyncio.coroutine - def test_enable_without_enable_capability_abort_command(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_enable_without_enable_capability_abort_command(self): + imap_client = await self.login_user('user', 'pass') with self.assertRaises(Abort): - yield from imap_client.enable('CAPABILITY') + await imap_client.enable('CAPABILITY') - @asyncio.coroutine - def test_namespace_without_namespace_capability_abort_command(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_namespace_without_namespace_capability_abort_command(self): + imap_client = await self.login_user('user', 'pass') with self.assertRaises(Abort): - yield from imap_client.namespace() + await imap_client.namespace() class TestAioimaplibClocked(AioWithImapServer, asynctest.ClockedTestCase): @@ -892,55 +829,51 @@ class TestAioimaplibClocked(AioWithImapServer, asynctest.ClockedTestCase): def setUp(self): self._init_server(self.loop) - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() - @asyncio.coroutine - def test_when_async_commands_timeout__they_should_be_removed_from_protocol_state(self): - imap_client = yield from self.login_user('user', 'pass', select=True) - yield from (imap_client.protocol.execute(Command( + async def test_when_async_commands_timeout__they_should_be_removed_from_protocol_state(self): + imap_client = await self.login_user('user', 'pass', select=True) + await (imap_client.protocol.execute(Command( 'DELAY', imap_client.protocol.new_tag(), '3', loop=self.loop))) noop_task = asyncio.ensure_future(imap_client.protocol.execute( Command('NOOP', imap_client.protocol.new_tag(), '', loop=self.loop, timeout=2))) - yield from self.advance(1) + await self.advance(1) self.assertEqual(1, len(imap_client.protocol.pending_async_commands)) - yield from self.advance(1.1) + await self.advance(1.1) - finished, pending = yield from asyncio.wait([noop_task], loop=self.loop) + finished, pending = await asyncio.wait([noop_task], loop=self.loop) self.assertTrue(noop_task in finished) self.assertTrue(isinstance(noop_task.exception(), CommandTimeout)) self.assertEqual(0, len(imap_client.protocol.pending_async_commands)) - @asyncio.coroutine - def test_when_sync_commands_timeout__they_should_be_removed_from_protocol_state(self): - imap_client = yield from self.login_user('user', 'pass') - yield from (imap_client.protocol.execute(Command( + async def test_when_sync_commands_timeout__they_should_be_removed_from_protocol_state(self): + imap_client = await self.login_user('user', 'pass') + await (imap_client.protocol.execute(Command( 'DELAY', imap_client.protocol.new_tag(), '3', loop=self.loop))) delay_task = asyncio.ensure_future(imap_client.protocol.execute( Command('DELAY', imap_client.protocol.new_tag(), '0', loop=self.loop, timeout=2))) - yield from self.advance(1) + await self.advance(1) self.assertIsNotNone(imap_client.protocol.pending_sync_command) - yield from self.advance(1.1) + await self.advance(1.1) - finished, pending = yield from asyncio.wait([delay_task], loop=self.loop) + finished, pending = await asyncio.wait([delay_task], loop=self.loop) self.assertTrue(delay_task in finished) self.assertTrue(isinstance(delay_task.exception(), CommandTimeout)) self.assertIsNone(imap_client.protocol.pending_sync_command) - @asyncio.coroutine - def test_idle_start__exits_queueget_without_timeout_error(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_idle_start__exits_queueget_without_timeout_error(self): + imap_client = await self.login_user('user', 'pass', select=True) idle_timeout = 5 - yield from imap_client.idle_start(idle_timeout) + await imap_client.idle_start(idle_timeout) push_task = asyncio.ensure_future(imap_client.wait_server_push(idle_timeout + 2)) - yield from self.advance(idle_timeout + 1) + await self.advance(idle_timeout + 1) - r = yield from asyncio.wait_for(push_task, 0) + r = await asyncio.wait_for(push_task, 0) self.assertEqual(STOP_WAIT_SERVER_PUSH, r) @@ -948,17 +881,16 @@ class TestAioimaplibCallback(AioWithImapServer, asynctest.TestCase): def setUp(self): self._init_server(self.loop) - @asyncio.coroutine - def test_callback_is_called_when_connection_is_lost(self): + async def test_callback_is_called_when_connection_is_lost(self): queue = asyncio.Queue() imap_client = aioimaplib.IMAP4(port=12345, loop=self.loop, timeout=3, conn_lost_cb=( lambda m: queue.put_nowait('called with %s' % m))) - yield from asyncio.wait_for(imap_client.wait_hello_from_server(), 2) - yield from imap_client.login('login', 'password') + await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) + await imap_client.login('login', 'password') - yield from self._shutdown_server() + await self._shutdown_server() - self.assertEqual('called with None', (yield from asyncio.wait_for(queue.get(), timeout=2))) + self.assertEqual('called with None', (await asyncio.wait_for(queue.get(), timeout=2))) class TestAioimaplibSSL(WithImapServer, asynctest.TestCase): @@ -976,17 +908,15 @@ def setUp(self): self._init_server(self.loop, ssl_context=ssl_context) - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() os.remove(self._cert_file) os.remove(self._cert_key) - @asyncio.coroutine - def test_client_can_connect_to_server_over_ssl(self): + async def test_client_can_connect_to_server_over_ssl(self): ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=self._cert_file) imap_client = aioimaplib.IMAP4_SSL(port=12345, loop=self.loop, ssl_context=ssl_context) - yield from asyncio.wait_for(imap_client.wait_hello_from_server(), 2) + await asyncio.wait_for(imap_client.wait_hello_from_server(), 2) self.assertEquals('IMAP4REV1', imap_client.protocol.imap_version) self.assertEquals({'IMAP4rev1', 'YESAUTH'}, imap_client.protocol.capabilities) diff --git a/aioimaplib/tests/test_imapserver.py b/aioimaplib/tests/test_imapserver.py index fbc6813..6a59a09 100644 --- a/aioimaplib/tests/test_imapserver.py +++ b/aioimaplib/tests/test_imapserver.py @@ -155,22 +155,20 @@ def _init_server(self, loop, capabilities=None, ssl_context=None): host='127.0.0.1', port=12345, fetch_chunk_size=64, ssl_context=ssl_context ) - @asyncio.coroutine - def _shutdown_server(self): + async def _shutdown_server(self): self.imapserver.reset() self.server.close() - yield from asyncio.wait_for(self.server.wait_closed(), 1) + await asyncio.wait_for(self.server.wait_closed(), 1) - @asyncio.coroutine - def login_user(self, login, password, select=False, lib=imaplib.IMAP4): - imap_client = yield from asyncio.wait_for( + async def login_user(self, login, password, select=False, lib=imaplib.IMAP4): + imap_client = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(lib, host='127.0.0.1', port=12345)), 1) - yield from asyncio.wait_for( + await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.login, login, password)), 1) if select: - yield from asyncio.wait_for( + await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select)), 1) return imap_client diff --git a/aioimaplib/tests/test_imapserver_aioimaplib.py b/aioimaplib/tests/test_imapserver_aioimaplib.py index 307cb5a..79aaeda 100644 --- a/aioimaplib/tests/test_imapserver_aioimaplib.py +++ b/aioimaplib/tests/test_imapserver_aioimaplib.py @@ -27,35 +27,32 @@ class TestAioimaplib(AioWithImapServer, asynctest.TestCase): def setUp(self): self._init_server(self.loop) - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() - @asyncio.coroutine - def test_append_too_long(self): - imap_client = yield from self.login_user('user@mail', 'pass') - self.assertEquals(0, extract_exists((yield from imap_client.examine('INBOX')))) + async def test_append_too_long(self): + imap_client = await self.login_user('user@mail', 'pass') + self.assertEquals(0, extract_exists((await imap_client.examine('INBOX')))) message_bytes = b'do you see me ?' imap_client.protocol.literal_data = message_bytes * 2 args = ['INBOX', '{%s}' % len(message_bytes)] - response = yield from imap_client.protocol.execute( + response = await imap_client.protocol.execute( Command('APPEND', imap_client.protocol.new_tag(), *args, loop=self.loop) ) self.assertEquals('BAD', response.result) self.assertTrue('expected CRLF but got' in response.lines[0]) - @asyncio.coroutine - def test_append_too_short(self): - imap_client = yield from self.login_user('user@mail', 'pass') - self.assertEquals(0, extract_exists((yield from imap_client.examine('INBOX')))) + async def test_append_too_short(self): + imap_client = await self.login_user('user@mail', 'pass') + self.assertEquals(0, extract_exists((await imap_client.examine('INBOX')))) message_bytes = b'do you see me ?' * 2 imap_client.protocol.literal_data = message_bytes[:5] args = ['INBOX', '{%s}' % len(message_bytes)] - response = yield from imap_client.protocol.execute( + response = await imap_client.protocol.execute( Command('APPEND', imap_client.protocol.new_tag(), *args, loop=self.loop) ) self.assertEquals('BAD', response.result) diff --git a/aioimaplib/tests/test_imapserver_imaplib.py b/aioimaplib/tests/test_imapserver_imaplib.py index aaf82a5..e8f303b 100644 --- a/aioimaplib/tests/test_imapserver_imaplib.py +++ b/aioimaplib/tests/test_imapserver_imaplib.py @@ -37,195 +37,179 @@ class TestImapServerWithImaplib(WithImapServer, TestCase): def setUp(self): self._init_server(self.loop) - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() def __init__(self, methodName='runTest'): super().__init__(methodName) add_charset('utf-8', SHORTEST, None, 'utf-8') add_charset('cp1252', SHORTEST, None, 'cp1252') - @asyncio.coroutine - def test_server_greetings_and_capabilities(self): + async def test_server_greetings_and_capabilities(self): pending_imap = self.loop.run_in_executor(None, functools.partial(imaplib.IMAP4, host='127.0.0.1', port=12345)) - imap_client = yield from asyncio.wait_for(pending_imap, 1) + imap_client = await asyncio.wait_for(pending_imap, 1) self.assertEqual('NONAUTH', imap_client.state) - @asyncio.coroutine - def test_server_login(self): + async def test_server_login(self): pending_imap = self.loop.run_in_executor(None, functools.partial(imaplib.IMAP4, host='127.0.0.1', port=12345)) - imap_client = yield from asyncio.wait_for(pending_imap, 1) + imap_client = await asyncio.wait_for(pending_imap, 1) pending_login = self.loop.run_in_executor(None, functools.partial(imap_client.login, 'user', 'pass')) - result, data = yield from asyncio.wait_for(pending_login, 1) + result, data = await asyncio.wait_for(pending_login, 1) self.assertEqual('OK', result) self.assertEqual([b'LOGIN completed'], data) self.assertEquals(imapserver.AUTH, self.imapserver.get_connection('user').state) - @asyncio.coroutine - def test_select_no_messages_in_mailbox(self): - imap_client = yield from self.login_user('user@mail', 'pass') + async def test_select_no_messages_in_mailbox(self): + imap_client = await self.login_user('user@mail', 'pass') - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select)), 1) self.assertEqual('OK', result) self.assertEqual([b'0'], data) self.assertEquals(imapserver.SELECTED, self.imapserver.get_connection('user@mail').state) - @asyncio.coroutine - def test_select_one_message_in_mailbox(self): + async def test_select_one_message_in_mailbox(self): self.imapserver.receive(Mail.create(to=['user'], mail_from='me', subject='hello')) - imap_client = yield from self.login_user('user', 'pass') + imap_client = await self.login_user('user', 'pass') - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select)), 1) self.assertEqual('OK', result) self.assertEqual([b'1'], data) - @asyncio.coroutine - def test_select_one_message_in_INBOX_zero_in_OTHER(self): + async def test_select_one_message_in_INBOX_zero_in_OTHER(self): self.imapserver.receive(Mail.create(to=['user'], mail_from='me', subject='hello')) - imap_client = yield from self.login_user('user', 'pass') + imap_client = await self.login_user('user', 'pass') - _, data = yield from asyncio.wait_for( + _, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select)), 1) self.assertEqual([b'1'], data) - _, data = yield from asyncio.wait_for( + _, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select, 'OTHER')), 1) self.assertEqual([b'0'], data) - @asyncio.coroutine - def test_examine_no_messages_in_mailbox(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_examine_no_messages_in_mailbox(self): + imap_client = await self.login_user('user', 'pass') - self.assertEquals(('OK', [b'0']), (yield from asyncio.wait_for( + self.assertEquals(('OK', [b'0']), (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select, readonly=True)), 1))) self.assertEquals(imapserver.AUTH, self.imapserver.get_connection('user').state) - @asyncio.coroutine - def test_search_by_uid_two_messages(self): + async def test_search_by_uid_two_messages(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'search', 'utf-8', 'ALL')), 1) self.assertEqual('OK', result) self.assertEqual([b'1 2'], data) - @asyncio.coroutine - def test_search_by_uid_one_message_two_recipients(self): + async def test_search_by_uid_one_message_two_recipients(self): self.imapserver.receive(Mail.create(['user1', 'user2'])) - imap_client = yield from self.login_user('user1', 'pass', select=True) + imap_client = await self.login_user('user1', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'ALL')), 1) self.assertEqual('OK', result) self.assertEqual([b'1'], data) - imap_client = yield from self.login_user('user2', 'pass', select=True) + imap_client = await self.login_user('user2', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'ALL')), 1) self.assertEqual('OK', result) self.assertEqual([b'1'], data) - @asyncio.coroutine - def test_fetch_one_message_by_uid(self): + async def test_fetch_one_message_by_uid(self): mail = Mail.create(['user'], mail_from='me', subject='hello', content='pleased to meet you, wont you guess my name ?') self.imapserver.receive(mail) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(RFC822)')), 1) self.assertEqual('OK', result) self.assertEqual([(b'1 (UID 1 RFC822 {360}', mail.as_bytes()), b')'], data) - @asyncio.coroutine - def test_fetch_bad_range(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_fetch_bad_range(self): + imap_client = await self.login_user('user', 'pass', select=True) with self.assertRaises(Exception) as expected: - yield from asyncio.wait_for( + await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '0:*', '(RFC822)')), 1) self.assertEqual('UID command error: BAD [b\'Error in IMAP command: Invalid uidset\']', str(expected.exception)) with self.assertRaises(Exception) as expected: - yield from asyncio.wait_for( + await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '2:0', '(RFC822)')), 1) self.assertEqual('UID command error: BAD [b\'Error in IMAP command: Invalid uidset\']', str(expected.exception)) - @asyncio.coroutine - def test_fetch_one_message_by_uid_with_bodypeek(self): + async def test_fetch_one_message_by_uid_with_bodypeek(self): mail = Mail.create(['user'], mail_from='me', subject='hello', content='this mail is still unread') self.imapserver.receive(mail) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(UID BODY.PEEK[])')), 1) self.assertEqual('OK', result) self.assertEqual([(b'1 (UID 1 BODY.PEEK[] {340}', mail.as_bytes()), b')'], data) - @asyncio.coroutine - def test_fetch_one_messages_by_uid_without_body(self): + async def test_fetch_one_messages_by_uid_without_body(self): mail = Mail.create(['user'], mail_from='me', subject='hello', content='whatever') self.imapserver.receive(mail) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(UID FLAGS)')), 1) self.assertEqual('OK', result) self.assertEqual([(b'1 (UID 1 FLAGS ())')], data) - @asyncio.coroutine - def test_fetch_one_messages_by_id_without_body(self): + async def test_fetch_one_messages_by_id_without_body(self): mail = Mail.create(['user'], mail_from='me', subject='hello', content='whatever') self.imapserver.receive(mail) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.fetch, '1', '(UID FLAGS)')), 1) self.assertEqual([(b'1 (UID 1 FLAGS ())')], data) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.fetch, '1', '(FLAGS)')), 1) self.assertEqual([(b'1 (FLAGS ())')], data) - @asyncio.coroutine - def test_fetch_messages_by_uid_range(self): + async def test_fetch_messages_by_uid_range(self): mail = Mail.create(['user'], mail_from='me', subject='hello', content='whatever') self.imapserver.receive(mail) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1:1', '(FLAGS)')), 1) self.assertEqual([(b'1 (UID 1 FLAGS ())')], data) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.fetch, '1:*', '(UID FLAGS)')), 1) self.assertEqual([(b'1 (UID 1 FLAGS ())')], data) - @asyncio.coroutine - def test_fetch_one_messages_by_uid_encoding_cp1252(self): + async def test_fetch_one_messages_by_uid_encoding_cp1252(self): self.imapserver.receive(Mail.create(['user'], mail_from='me', subject='hello', content='maître', encoding='cp1252')) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - _, data = yield from asyncio.wait_for( + _, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(RFC822)')), 1) mail_content = data[0][1] @@ -233,252 +217,234 @@ def test_fetch_one_messages_by_uid_encoding_cp1252(self): self.assertTrue(b'ma\xeetre' in mail_content) self.assertEqual('maître', email.message_from_bytes(mail_content).get_payload().strip()) - @asyncio.coroutine - def test_fetch_one_messages_out_of_two(self): + async def test_fetch_one_messages_out_of_two(self): self.imapserver.receive(Mail.create(['user'], mail_from='me', subject='hello', content='maître')) self.imapserver.receive(Mail.create(['user'], mail_from='you', subject='yo', content='bro')) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - _, data = yield from asyncio.wait_for( + _, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(RFC822)')), 1) self.assertEqual(2, len(data)) - @asyncio.coroutine - def test_fetch_one_message_with_headers(self): + async def test_fetch_one_message_with_headers(self): self.imapserver.receive(Mail.create(['user'], mail_from='me', subject='hello', content='maître')) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - _, data = yield from asyncio.wait_for( + _, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', '(BODY.PEEK[HEADER.FIELDS (Content-type From)])')), 1) self.assertEqual(b'1 (UID 1 BODY[HEADER.FIELDS (Content-type From)] {57}', data[0][0]) self.assertEqual(b'Content-type: text/plain; charset="utf-8"\r\nFrom: \r\n\r\n', data[0][1]) - @asyncio.coroutine - def test_store(self): + async def test_store(self): self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'store', '1', '+FLAGS.SILENT (\Seen \Answered)')), 1) self.assertEqual('OK', result) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'fetch', '1', 'UID (FLAGS)')), 1) self.assertEqual('OK', result) self.assertEqual([b'1 (UID 1 FLAGS (\Seen \Answered))'], data) - @asyncio.coroutine - def test_store_and_search_by_keyword(self): + async def test_store_and_search_by_keyword(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'KEYWORD FOO')), 1) self.assertEqual('OK', result) self.assertEqual([b''], data) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'store', '1', '+FLAGS (FOO)')), 1) self.assertEqual('OK', result) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'KEYWORD FOO')), 1) self.assertEqual('OK', result) self.assertEqual([b'1'], data) - result, data = yield from asyncio.wait_for( + result, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'search', None, 'UNKEYWORD FOO')), 1) self.assertEqual('OK', result) self.assertEqual([b'2'], data) - @asyncio.coroutine - def test_search_by_uid_range(self): + async def test_search_by_uid_range(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - _, data = yield from asyncio.wait_for( + _, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'search', None, '1:2')), 1) self.assertEqual([b'1 2'], data) - _, data = yield from asyncio.wait_for( + _, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'search', None, '1:*')), 1) self.assertEqual([b'1 2'], data) - _, data = yield from asyncio.wait_for( + _, data = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.uid, 'search', None, '1:1')), 1) self.assertEqual([b'1'], data) - @asyncio.coroutine - def test_expunge_messages(self): + async def test_expunge_messages(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - yield from asyncio.wait_for(self.loop.run_in_executor(None, imap_client.expunge), 1) + await asyncio.wait_for(self.loop.run_in_executor(None, imap_client.expunge), 1) - self.assertEquals(('OK', [b'0']), (yield from asyncio.wait_for( + self.assertEquals(('OK', [b'0']), (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select)), 1))) - @asyncio.coroutine - def test_noop(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_noop(self): + imap_client = await self.login_user('user', 'pass', select=True) self.assertEquals(('OK', [b'NOOP completed.']), - (yield from asyncio.wait_for(self.loop.run_in_executor(None, imap_client.noop), 1))) + (await asyncio.wait_for(self.loop.run_in_executor(None, imap_client.noop), 1))) - @asyncio.coroutine - def test_check(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_check(self): + imap_client = await self.login_user('user', 'pass', select=True) self.assertEquals(('OK', [b'CHECK completed.']), - (yield from asyncio.wait_for(self.loop.run_in_executor(None, imap_client.check), 1))) + (await asyncio.wait_for(self.loop.run_in_executor(None, imap_client.check), 1))) - @asyncio.coroutine - def test_status(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_status(self): + imap_client = await self.login_user('user', 'pass') self.assertEquals(('OK', [b'INBOX (MESSAGES 0 UIDNEXT 1)']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.status, 'INBOX', '(MESSAGES UIDNEXT)')), 1))) - @asyncio.coroutine - def test_subscribe_unsubscribe_lsub(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_subscribe_unsubscribe_lsub(self): + imap_client = await self.login_user('user', 'pass') self.assertEquals(('OK', [b'SUBSCRIBE completed.']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial( imap_client.subscribe, '#fr.soc.feminisme')), 1))) self.assertEquals(('OK', [b'() "." #fr.soc.feminisme']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial( imap_client.lsub, '#fr', 'soc.*')), 1))) self.assertEquals(('OK', [b'UNSUBSCRIBE completed.']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial( imap_client.unsubscribe, '#fr.soc.feminisme')), 1))) self.assertEquals(('OK', [None]), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial( imap_client.lsub, '#fr', '.*')), 1))) - @asyncio.coroutine - def test_close(self): - imap_client = yield from self.login_user('user', 'pass', select=True) + async def test_close(self): + imap_client = await self.login_user('user', 'pass', select=True) self.assertEquals(imapserver.SELECTED, self.imapserver.get_connection('user').state) self.assertEquals(('OK', [b'CLOSE completed.']), - (yield from asyncio.wait_for(self.loop.run_in_executor(None, imap_client.close), 1))) + (await asyncio.wait_for(self.loop.run_in_executor(None, imap_client.close), 1))) self.assertEquals(imapserver.AUTH, self.imapserver.get_connection('user').state) - @asyncio.coroutine - def test_copy_messages(self): + async def test_copy_messages(self): self.imapserver.receive(Mail.create(['user'])) self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - result, _ = yield from asyncio.wait_for( + result, _ = await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.copy, '1 2', 'MAILBOX')), 20) self.assertEqual('OK', result) - self.assertEquals(('OK', [b'2']), (yield from asyncio.wait_for( + self.assertEquals(('OK', [b'2']), (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select, 'MAILBOX')), 20))) - @asyncio.coroutine - def test_create_delete_mailbox(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_create_delete_mailbox(self): + imap_client = await self.login_user('user', 'pass') self.assertEquals(('NO', [b'STATUS completed.']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))) self.assertEquals(('OK', [b'CREATE completed.']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.create, 'MBOX')), 1))) self.assertEquals(('OK', [b'MBOX (MESSAGES 0)']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))) self.assertEquals(('OK', [b'DELETE completed.']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.delete, 'MBOX')), 1))) self.assertEquals(('NO', [b'STATUS completed.']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))) - @asyncio.coroutine - def test_rename_mailbox(self): + async def test_rename_mailbox(self): self.imapserver.receive(Mail.create(['user'])) - imap_client = yield from self.login_user('user', 'pass') + imap_client = await self.login_user('user', 'pass') self.assertEquals(('NO', [b'STATUS completed.']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))) self.assertEquals(('OK', [b'RENAME completed.']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.rename, 'INBOX', 'MBOX')), 1))) self.assertEquals(('OK', [b'MBOX (MESSAGES 1)']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.status, 'MBOX', '(MESSAGES)')), 1))) - @asyncio.coroutine - def test_list(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_list(self): + imap_client = await self.login_user('user', 'pass') self.assertEquals(('OK', [b'() "/" Drafts', b'() "/" INBOX', b'() "/" Sent', b'() "/" Trash']), - (yield from asyncio.wait_for( + (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.list, '""', '*')), 1))) - @asyncio.coroutine - def test_append(self): - imap_client = yield from self.login_user('user@mail', 'pass') + async def test_append(self): + imap_client = await self.login_user('user@mail', 'pass') - self.assertEquals(('OK', [b'0']), (yield from asyncio.wait_for( + self.assertEquals(('OK', [b'0']), (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select, 'INBOX', readonly=True)), 2))) msg = Mail.create(['user@mail'], subject='append msg', content='do you see me ?') - self.assertEquals('OK', (yield from asyncio.wait_for( + self.assertEquals('OK', (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial( imap_client.append, 'INBOX', 'FOO BAR', datetime.now(tz=utc), msg.as_bytes())), 2))[0]) - self.assertEquals(('OK', [b'1']), (yield from asyncio.wait_for( + self.assertEquals(('OK', [b'1']), (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.select, 'INBOX', readonly=True)), 2))) - @asyncio.coroutine - def test_logout(self): - imap_client = yield from self.login_user('user', 'pass') + async def test_logout(self): + imap_client = await self.login_user('user', 'pass') - result, data = yield from asyncio.wait_for(self.loop.run_in_executor(None, imap_client.logout), 1) + result, data = await asyncio.wait_for(self.loop.run_in_executor(None, imap_client.logout), 1) self.assertEqual('BYE', result) # uhh ? self.assertEqual([b'Logging out'], data) self.assertEquals(imapserver.LOGOUT, self.imapserver.get_connection('user').state) - @asyncio.coroutine - def test_rfc5032_within(self): + async def test_rfc5032_within(self): self.imapserver.receive(Mail.create(['user'], date=datetime.now(tz=utc) - timedelta(seconds=84600 * 3))) # 1 self.imapserver.receive(Mail.create(['user'], date=datetime.now(tz=utc) - timedelta(seconds=84600))) # 2 self.imapserver.receive(Mail.create(['user'])) # 3 - imap_client = yield from self.login_user('user', 'pass', select=True) + imap_client = await self.login_user('user', 'pass', select=True) - self.assertEquals([b'2 3'], (yield from asyncio.wait_for( + self.assertEquals([b'2 3'], (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.search, 'utf-8', 'YOUNGER', '84700')), 1))[1]) - self.assertEquals([b'1'], (yield from asyncio.wait_for( + self.assertEquals([b'1'], (await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.search, 'utf-8', 'OLDER', '84700')), 1))[1]) @@ -497,9 +463,8 @@ def setUp(self): self._init_server(self.loop, ssl_context=ssl_context) - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() os.remove(self._cert_file) os.remove(self._cert_key) @@ -508,8 +473,7 @@ def __init__(self, methodName='runTest'): add_charset('utf-8', SHORTEST, None, 'utf-8') add_charset('cp1252', SHORTEST, None, 'cp1252') - @asyncio.coroutine - def test_client_can_connect_to_server_over_ssl(self): + async def test_client_can_connect_to_server_over_ssl(self): ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=self._cert_file) pending_imap = self.loop.run_in_executor(None, functools.partial( @@ -518,6 +482,6 @@ def test_client_can_connect_to_server_over_ssl(self): port=12345, ssl_context=ssl_context) ) - imap_client = yield from asyncio.wait_for(pending_imap, 1) + imap_client = await asyncio.wait_for(pending_imap, 1) self.assertEqual('NONAUTH', imap_client.state) diff --git a/aioimaplib/tests/test_imapserver_imaplib2.py b/aioimaplib/tests/test_imapserver_imaplib2.py index c1770a0..e567ebc 100644 --- a/aioimaplib/tests/test_imapserver_imaplib2.py +++ b/aioimaplib/tests/test_imapserver_imaplib2.py @@ -31,30 +31,27 @@ class TestImapServerIdle(WithImapServer, TestCase): def setUp(self): self._init_server(self.loop) - @asyncio.coroutine - def tearDown(self): - yield from self._shutdown_server() + async def tearDown(self): + await self._shutdown_server() - @asyncio.coroutine - def test_idle(self): - imap_client = yield from self.login_user('user', 'pass', select=True, lib=imaplib2.IMAP4) + async def test_idle(self): + imap_client = await self.login_user('user', 'pass', select=True, lib=imaplib2.IMAP4) idle_callback = Mock() self.loop.run_in_executor(None, functools.partial(imap_client.idle, callback=idle_callback)) - yield from asyncio.wait_for(self.imapserver.get_connection('user').wait(imapserver.IDLE), 1) + await asyncio.wait_for(self.imapserver.get_connection('user').wait(imapserver.IDLE), 1) self.loop.run_in_executor(None, functools.partial(self.imapserver.receive, Mail.create(to=['user'], mail_from='me', subject='hello'))) - yield from asyncio.wait_for(self.imapserver.get_connection('user').wait(imapserver.SELECTED), 1) + await asyncio.wait_for(self.imapserver.get_connection('user').wait(imapserver.SELECTED), 1) time.sleep(0.1) # eurk hate sleeps but I don't know how to wait for the lib to receive end of IDLE idle_callback.assert_called_once() - @asyncio.coroutine - def test_login_twice(self): + async def test_login_twice(self): with self.assertRaises(imaplib2.IMAP4.error) as expected: - imap_client = yield from self.login_user('user', 'pass', lib=imaplib2.IMAP4) + imap_client = await self.login_user('user', 'pass', lib=imaplib2.IMAP4) - yield from asyncio.wait_for( + await asyncio.wait_for( self.loop.run_in_executor(None, functools.partial(imap_client.login, 'user', 'pass')), 1) self.assertEqual(expected.exception.args, ('command LOGIN illegal in state AUTH',))