| 1 |
# aioimaplib : an IMAPrev4 lib using python asyncio |
| 2 |
# Copyright (C) 2016 Bruno Thomas |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
# -*- coding: utf-8 -*- |
| 17 |
import email |
| 18 |
import imaplib |
| 19 |
import unittest |
| 20 |
from datetime import datetime |
| 21 |
|
| 22 |
import asyncio |
| 23 |
|
| 24 |
import functools |
| 25 |
|
| 26 |
import pytz |
| 27 |
import sys |
| 28 |
|
| 29 |
from aioimaplib.imap_testing_server import ServerState, Mail, MockImapServer, ImapProtocol, InvalidUidSet |
| 30 |
import pytest |
| 31 |
|
| 32 |
|
| 33 |
class TestMailToString(unittest.TestCase): |
| 34 |
def test_message_date_string(self): |
| 35 |
now = pytz.timezone('Europe/Paris').localize(datetime(2016, 2, 2, 12, 13, 14, 151)) |
| 36 |
|
| 37 |
mail = Mail.create(['user'], date=now) |
| 38 |
|
| 39 |
assert mail.email.get('Date') == 'Tue, 02 Feb 2016 12:13:14 +0100' |
| 40 |
|
| 41 |
def test_message_default_date_string_is_utc(self): |
| 42 |
mail = Mail.create(['user']) |
| 43 |
|
| 44 |
assert mail.email.get('Date').endswith('+0000') |
| 45 |
|
| 46 |
def test_message_title_string_without_accents_isnot_encoded(self): |
| 47 |
now = pytz.timezone('Europe/Paris').localize(datetime(2016, 2, 2, 12, 13, 14, 151)) |
| 48 |
|
| 49 |
mail = Mail.create(['user'], subject='subject', date=now) |
| 50 |
|
| 51 |
assert mail.email.get('Subject') == 'subject' |
| 52 |
|
| 53 |
def test_message_title_string_with_accents_is_base64encoded(self): |
| 54 |
mail = Mail.create(['user'], subject='Classé ?') |
| 55 |
|
| 56 |
assert '=?utf-8?b?Q2xhc3PDqSA/?=' in mail.as_string() |
| 57 |
|
| 58 |
def test_message_quoted_printable(self): |
| 59 |
mail = Mail.create(['user'], content='Bonjour à vous', quoted_printable=True) |
| 60 |
|
| 61 |
assert 'Bonjour =C3=A0 vous' in mail.as_string(), '"=C3=A0" not found in %s' % mail.as_string() |
| 62 |
|
| 63 |
def test_message_not_quoted_printable(self): |
| 64 |
mail = Mail.create(['user'], subject='élo ?', content='Bonjour à vous').as_bytes() |
| 65 |
|
| 66 |
m = email.message_from_bytes(mail) |
| 67 |
assert 'Bonjour à vous' == m.get_payload(decode=True).decode() |
| 68 |
|
| 69 |
def test_header_encode_to(self): |
| 70 |
mail = Mail.create(['Zébulon Durand <[email protected]>'], mail_from='[email protected]', subject='subject') |
| 71 |
|
| 72 |
assert '=?utf-8?q?Z=C3=A9bulon_Durand_=3Czeb=40zebulon=2Eio=3E?=' in mail.as_string(), 'expected string not found in :%s\n' % mail.as_string() |
| 73 |
|
| 74 |
def test_mail_from(self): |
| 75 |
mail = Mail.create(['user'], subject='subject') |
| 76 |
assert mail.email.get('From') == '' |
| 77 |
|
| 78 |
mail = Mail.create(['user'], mail_from='<test@test>', subject='subject') |
| 79 |
assert mail.email.get('From') == '<test@test>' |
| 80 |
|
| 81 |
mail = Mail.create(['user'], mail_from='test@test', subject='subject') |
| 82 |
assert mail.email.get('From') == '<test@test>' |
| 83 |
|
| 84 |
mail = Mail.create(['user'], mail_from='Test <test@test>', subject='subject') |
| 85 |
assert mail.email.get('From') == 'Test <test@test>' |
| 86 |
|
| 87 |
def test_build_sequence_range(self): |
| 88 |
assert range(1, 3) == ImapProtocol(None)._build_sequence_range('1:2') |
| 89 |
assert range(1, 12) == ImapProtocol(None)._build_sequence_range('1:11') |
| 90 |
assert range(1234, 12346) == ImapProtocol(None)._build_sequence_range('1234:12345') |
| 91 |
assert range(1, sys.maxsize) == ImapProtocol(None)._build_sequence_range('1:*') |
| 92 |
assert [42] == ImapProtocol(None)._build_sequence_range('42') |
| 93 |
|
| 94 |
def test_build_sequence_badrange(self): |
| 95 |
with pytest.raises(InvalidUidSet): |
| 96 |
ImapProtocol(None)._build_sequence_range('0:2') |
| 97 |
|
| 98 |
with pytest.raises(InvalidUidSet): |
| 99 |
ImapProtocol(None)._build_sequence_range('2:0') |
| 100 |
|
| 101 |
with pytest.raises(InvalidUidSet): |
| 102 |
ImapProtocol(None)._build_sequence_range('2:1') |
| 103 |
|
| 104 |
|
| 105 |
class TestServerState(unittest.TestCase): |
| 106 |
def test_max_ids_with_no_user(self): |
| 107 |
assert 0 == ServerState().max_uid('user', 'INBOX') |
| 108 |
assert 0 == ServerState().max_id('user', 'INBOX') |
| 109 |
|
| 110 |
def test_max_ids_one_user_one_mail(self): |
| 111 |
server_state = ServerState() |
| 112 |
server_state.add_mail('user', Mail.create(['user'])) |
| 113 |
|
| 114 |
assert 1 == server_state.max_id('user', 'INBOX') |
| 115 |
assert 1 == server_state.max_uid('user', 'INBOX') |
| 116 |
assert 0 == server_state.max_id('user', 'OTHER_MAILBOX') |
| 117 |
|
| 118 |
def test_max_ids_one_user_three_mails_in_two_mailboxes(self): |
| 119 |
server_state = ServerState() |
| 120 |
server_state.add_mail('user', Mail.create(['user']), mailbox='INBOX') |
| 121 |
server_state.add_mail('user', Mail.create(['user']), mailbox='INBOX') |
| 122 |
server_state.add_mail('user', Mail.create(['user']), mailbox='OUTBOX') |
| 123 |
|
| 124 |
assert 1 == server_state.max_id('user', 'OUTBOX') |
| 125 |
assert 2 == server_state.max_id('user', 'INBOX') |
| 126 |
assert 2 == server_state.max_uid('user', 'INBOX') |
| 127 |
|
| 128 |
def test_reprocess_ids_if_a_message_is_removed(self): |
| 129 |
server_state = ServerState() |
| 130 |
server_state.add_mail('user', Mail.create(['user'], 'from1'), mailbox='INBOX') |
| 131 |
server_state.add_mail('user', Mail.create(['user'], 'from2'), mailbox='INBOX') |
| 132 |
server_state.add_mail('user', Mail.create(['user'], 'from3'), mailbox='INBOX') |
| 133 |
|
| 134 |
server_state.remove_byid('user', 'INBOX', 1) |
| 135 |
assert 1 == server_state.get_mailbox_messages('user', 'INBOX')[0].id |
| 136 |
assert 2 == server_state.get_mailbox_messages('user', 'INBOX')[1].id |
| 137 |
assert 2 == server_state.max_id('user', 'INBOX') |
| 138 |
assert 3 == server_state.max_uid('user', 'INBOX') |
| 139 |
|
| 140 |
server_state.remove_byid('user', 'INBOX', 1) |
| 141 |
assert 1 == server_state.get_mailbox_messages('user', 'INBOX')[0].id |
| 142 |
assert 1 == server_state.max_id('user', 'INBOX') |
| 143 |
assert 3 == server_state.max_uid('user', 'INBOX') |
| 144 |
|
| 145 |
|
| 146 |
class WithImapServer: |
| 147 |
def _init_server(self, loop, capabilities=None, ssl_context=None): |
| 148 |
self.loop = loop |
| 149 |
if capabilities is not None: |
| 150 |
self.imapserver = MockImapServer(loop=loop, capabilities=capabilities) |
| 151 |
else: |
| 152 |
self.imapserver = MockImapServer(loop=loop) |
| 153 |
self.server = self.imapserver.run_server( |
| 154 |
host='127.0.0.1', port=12345, fetch_chunk_size=64, ssl_context=ssl_context |
| 155 |
) |
| 156 |
|
| 157 |
async def _shutdown_server(self): |
| 158 |
self.imapserver.reset() |
| 159 |
self.server.close() |
| 160 |
await asyncio.wait_for(self.server.wait_closed(), 1) |
| 161 |
|
| 162 |
async def login_user(self, login, password, select=False, lib=imaplib.IMAP4): |
| 163 |
imap_client = await asyncio.wait_for( |
| 164 |
self.loop.run_in_executor(None, functools.partial(lib, host='127.0.0.1', port=12345)), 1) |
| 165 |
|
| 166 |
await asyncio.wait_for( |
| 167 |
self.loop.run_in_executor(None, functools.partial(imap_client.login, login, password)), 1) |
| 168 |
|
| 169 |
if select: |
| 170 |
await asyncio.wait_for( |
| 171 |
self.loop.run_in_executor(None, functools.partial(imap_client.select)), 1) |
| 172 |
|
| 173 |
return imap_client |