1
0
Fork 0

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:
Adam Coddington 2015-04-12 11:34:44 -07:00
parent 634f2f4b26
commit abad769fd6
4 changed files with 76 additions and 15 deletions

View file

@ -2,3 +2,4 @@ from .test_mailbox import *
from .test_message_flattening import * from .test_message_flattening import *
from .test_process_email import * from .test_process_email import *
from .test_transports import * from .test_transports import *
from .test_integration_imap import *

View file

@ -1,15 +1,21 @@
import os import os
import urllib
import uuid import uuid
from six.moves.urllib import parse
from django.conf import settings
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
from django_mailbox.models import Mailbox from django_mailbox.models import Mailbox
from django_mailbox.tests.base import EmailMessageTestCase from django_mailbox.tests.base import EmailMessageTestCase
__all__ = ['TestImap']
class TestImap(EmailMessageTestCase): class TestImap(EmailMessageTestCase):
def setUp(self): def setUp(self):
super(TestImap, self).setUp()
self.test_imap_server = ( self.test_imap_server = (
os.environ['EMAIL_IMAP_SERVER'] os.environ['EMAIL_IMAP_SERVER']
) )
@ -17,19 +23,25 @@ class TestImap(EmailMessageTestCase):
name='Integration Test Imap', name='Integration Test Imap',
uri=self.get_connection_string() 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): def get_connection_string(self):
return "imap+ssl://{account}:{password}@{server}".format( return "imap+ssl://{account}:{password}@{server}".format(
account=urllib.quote(self.test_account), account=parse.quote(self.test_account),
password=urllib.quote(self.test_password), password=parse.quote(self.test_password),
server=self.test_imap_server, server=self.test_imap_server,
) )
def test_get_imap_message(self): def test_get_imap_message(self):
arbitrary_identifier = uuid.uuid4()
text_content = 'This is some content' text_content = 'This is some content'
msg = EmailMultiAlternatives( msg = EmailMultiAlternatives(
arbitrary_identifier, self.arbitrary_identifier,
text_content, text_content,
self.test_from_email, self.test_from_email,
[ [
@ -38,8 +50,8 @@ class TestImap(EmailMessageTestCase):
) )
msg.send() msg.send()
messages = self.get_new_messages() messages = self._get_new_messages(self.mailbox)
self.assertEqual(1, len(messages)) 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) self.assertEqual(messages[0].text, text_content)

View file

@ -1,15 +1,49 @@
import mock import mock
import six
from django.test.utils import override_settings from django.test.utils import override_settings
from django_mailbox.tests.base import EmailMessageTestCase, get_email_as_text from django_mailbox.tests.base import EmailMessageTestCase, get_email_as_text
from django_mailbox.transports import ImapTransport, Pop3Transport 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_SEARCH_ANSWER = (
FAKE_UID_FETCH_SIZES = ('OK', ['1 (UID 18 RFC822.SIZE 58070000000)', '2 (UID 19 RFC822.SIZE 2593)']) 'OK',
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)']) six.b(
FAKE_LIST_ARCHIVE_FOLDERS_ANSWERS = ('OK', ['(\\HasNoChildren \\All) "/" "[Gmail]/All Mail"']) '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): class IMAPTestCase(EmailMessageTestCase):
def setUp(self): def setUp(self):

View file

@ -10,12 +10,19 @@ logger = logging.getLogger(__name__)
class ImapTransport(EmailTransport): 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( self.max_message_size = getattr(
settings, settings,
'DJANGO_MAILBOX_MAX_MESSAGE_SIZE', 'DJANGO_MAILBOX_MAX_MESSAGE_SIZE',
False False
) )
self.integration_testing_subject = getattr(
settings,
'DJANGO_MAILBOX_INTEGRATION_TESTING_SUBJECT',
None
)
self.hostname = hostname self.hostname = hostname
self.port = port self.port = port
self.archive = archive self.archive = archive
@ -38,7 +45,6 @@ class ImapTransport(EmailTransport):
else: else:
self.server.select() self.server.select()
def _get_all_message_ids(self): def _get_all_message_ids(self):
# Fetch all the message uids # Fetch all the message uids
response, message_ids = self.server.uid('search', None, 'ALL') 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 # ids; we must make sure that it isn't an empty string before
# splitting into individual UIDs. # splitting into individual UIDs.
if message_id_string: if message_id_string:
return message_id_string.split(' ') return message_id_string.decode().split(' ')
return [] return []
def _get_small_message_ids(self, message_ids): def _get_small_message_ids(self, message_ids):
@ -63,6 +69,7 @@ class ImapTransport(EmailTransport):
) )
for each_msg in data: for each_msg in data:
each_msg = each_msg.decode()
try: try:
uid = each_msg.split(' ')[2] uid = each_msg.split(' ')[2]
size = each_msg.split(' ')[4].rstrip(')') size = each_msg.split(' ')[4].rstrip(')')
@ -95,6 +102,13 @@ class ImapTransport(EmailTransport):
try: try:
typ, msg_contents = self.server.uid('fetch', uid, '(RFC822)') typ, msg_contents = self.server.uid('fetch', uid, '(RFC822)')
message = self.get_email_from_bytes(msg_contents[0][1]) 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 yield message
except MessageParseError: except MessageParseError:
continue continue