mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-09 22:38:19 +02:00
Fixing test discovery, adding functionality allowing one to restict
message gathering (via the IMAP transport) to only messages matching a specified subject. Fixes #49.
This commit is contained in:
parent
634f2f4b26
commit
abad769fd6
4 changed files with 76 additions and 15 deletions
|
|
@ -2,3 +2,4 @@ from .test_mailbox import *
|
|||
from .test_message_flattening import *
|
||||
from .test_process_email import *
|
||||
from .test_transports import *
|
||||
from .test_integration_imap import *
|
||||
|
|
|
|||
|
|
@ -1,15 +1,21 @@
|
|||
import os
|
||||
import urllib
|
||||
import uuid
|
||||
|
||||
from six.moves.urllib import parse
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMultiAlternatives
|
||||
|
||||
from django_mailbox.models import Mailbox
|
||||
from django_mailbox.tests.base import EmailMessageTestCase
|
||||
|
||||
|
||||
__all__ = ['TestImap']
|
||||
|
||||
|
||||
class TestImap(EmailMessageTestCase):
|
||||
def setUp(self):
|
||||
super(TestImap, self).setUp()
|
||||
self.test_imap_server = (
|
||||
os.environ['EMAIL_IMAP_SERVER']
|
||||
)
|
||||
|
|
@ -17,19 +23,25 @@ class TestImap(EmailMessageTestCase):
|
|||
name='Integration Test Imap',
|
||||
uri=self.get_connection_string()
|
||||
)
|
||||
self.arbitrary_identifier = str(uuid.uuid4())
|
||||
settings.DJANGO_MAILBOX_INTEGRATION_TESTING_SUBJECT = (
|
||||
self.arbitrary_identifier
|
||||
)
|
||||
|
||||
def tearDown(self):
|
||||
settings.DJANGO_MAILBOX_INTEGRATION_TESTING_SUBJECT = None
|
||||
|
||||
def get_connection_string(self):
|
||||
return "imap+ssl://{account}:{password}@{server}".format(
|
||||
account=urllib.quote(self.test_account),
|
||||
password=urllib.quote(self.test_password),
|
||||
account=parse.quote(self.test_account),
|
||||
password=parse.quote(self.test_password),
|
||||
server=self.test_imap_server,
|
||||
)
|
||||
|
||||
def test_get_imap_message(self):
|
||||
arbitrary_identifier = uuid.uuid4()
|
||||
text_content = 'This is some content'
|
||||
msg = EmailMultiAlternatives(
|
||||
arbitrary_identifier,
|
||||
self.arbitrary_identifier,
|
||||
text_content,
|
||||
self.test_from_email,
|
||||
[
|
||||
|
|
@ -38,8 +50,8 @@ class TestImap(EmailMessageTestCase):
|
|||
)
|
||||
msg.send()
|
||||
|
||||
messages = self.get_new_messages()
|
||||
messages = self._get_new_messages(self.mailbox)
|
||||
|
||||
self.assertEqual(1, len(messages))
|
||||
self.assertEqual(messages[0].subject, arbitrary_identifier)
|
||||
self.assertEqual(messages[0].subject, self.arbitrary_identifier)
|
||||
self.assertEqual(messages[0].text, text_content)
|
||||
|
|
|
|||
|
|
@ -1,15 +1,49 @@
|
|||
import mock
|
||||
import six
|
||||
|
||||
from django.test.utils import override_settings
|
||||
|
||||
from django_mailbox.tests.base import EmailMessageTestCase, get_email_as_text
|
||||
from django_mailbox.transports import ImapTransport, Pop3Transport
|
||||
|
||||
FAKE_UID_SEARCH_ANSWER = ('OK', ['18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44'])
|
||||
FAKE_UID_FETCH_SIZES = ('OK', ['1 (UID 18 RFC822.SIZE 58070000000)', '2 (UID 19 RFC822.SIZE 2593)'])
|
||||
FAKE_UID_FETCH_MSG = ('OK', [('1 (UID 18 RFC822 {5807}', get_email_as_text('generic_message.eml') ),])
|
||||
FAKE_UID_COPY_MSG = ('OK', ['[COPYUID 1 2 2] (Success)'])
|
||||
FAKE_LIST_ARCHIVE_FOLDERS_ANSWERS = ('OK', ['(\\HasNoChildren \\All) "/" "[Gmail]/All Mail"'])
|
||||
FAKE_UID_SEARCH_ANSWER = (
|
||||
'OK',
|
||||
[
|
||||
six.b(
|
||||
'18 19 20 21 22 23 24 25 26 27 28 29 '
|
||||
'30 31 32 33 34 35 36 37 38 39 40 41 42 43 44'
|
||||
)
|
||||
]
|
||||
)
|
||||
FAKE_UID_FETCH_SIZES = (
|
||||
'OK',
|
||||
[
|
||||
six.b('1 (UID 18 RFC822.SIZE 58070000000)'),
|
||||
six.b('2 (UID 19 RFC822.SIZE 2593)')
|
||||
]
|
||||
)
|
||||
FAKE_UID_FETCH_MSG = (
|
||||
'OK',
|
||||
[
|
||||
(
|
||||
six.b('1 (UID 18 RFC822 {5807}'),
|
||||
get_email_as_text('generic_message.eml')
|
||||
),
|
||||
]
|
||||
)
|
||||
FAKE_UID_COPY_MSG = (
|
||||
'OK',
|
||||
[
|
||||
six.b('[COPYUID 1 2 2] (Success)')
|
||||
]
|
||||
)
|
||||
FAKE_LIST_ARCHIVE_FOLDERS_ANSWERS = (
|
||||
'OK',
|
||||
[
|
||||
six.b('(\\HasNoChildren \\All) "/" "[Gmail]/All Mail"')
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class IMAPTestCase(EmailMessageTestCase):
|
||||
def setUp(self):
|
||||
|
|
|
|||
|
|
@ -10,12 +10,19 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
|
||||
class ImapTransport(EmailTransport):
|
||||
def __init__(self, hostname, port=None, ssl=False, archive='', folder=None):
|
||||
def __init__(
|
||||
self, hostname, port=None, ssl=False, archive='', folder=None
|
||||
):
|
||||
self.max_message_size = getattr(
|
||||
settings,
|
||||
'DJANGO_MAILBOX_MAX_MESSAGE_SIZE',
|
||||
False
|
||||
)
|
||||
self.integration_testing_subject = getattr(
|
||||
settings,
|
||||
'DJANGO_MAILBOX_INTEGRATION_TESTING_SUBJECT',
|
||||
None
|
||||
)
|
||||
self.hostname = hostname
|
||||
self.port = port
|
||||
self.archive = archive
|
||||
|
|
@ -38,7 +45,6 @@ class ImapTransport(EmailTransport):
|
|||
else:
|
||||
self.server.select()
|
||||
|
||||
|
||||
def _get_all_message_ids(self):
|
||||
# Fetch all the message uids
|
||||
response, message_ids = self.server.uid('search', None, 'ALL')
|
||||
|
|
@ -47,7 +53,7 @@ class ImapTransport(EmailTransport):
|
|||
# ids; we must make sure that it isn't an empty string before
|
||||
# splitting into individual UIDs.
|
||||
if message_id_string:
|
||||
return message_id_string.split(' ')
|
||||
return message_id_string.decode().split(' ')
|
||||
return []
|
||||
|
||||
def _get_small_message_ids(self, message_ids):
|
||||
|
|
@ -63,6 +69,7 @@ class ImapTransport(EmailTransport):
|
|||
)
|
||||
|
||||
for each_msg in data:
|
||||
each_msg = each_msg.decode()
|
||||
try:
|
||||
uid = each_msg.split(' ')[2]
|
||||
size = each_msg.split(' ')[4].rstrip(')')
|
||||
|
|
@ -95,6 +102,13 @@ class ImapTransport(EmailTransport):
|
|||
try:
|
||||
typ, msg_contents = self.server.uid('fetch', uid, '(RFC822)')
|
||||
message = self.get_email_from_bytes(msg_contents[0][1])
|
||||
|
||||
if (
|
||||
self.integration_testing_subject and
|
||||
message['Subject'] != self.integration_testing_subject
|
||||
):
|
||||
continue
|
||||
|
||||
yield message
|
||||
except MessageParseError:
|
||||
continue
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue