2015-04-11 23:42:14 -07:00
|
|
|
import os
|
|
|
|
|
import uuid
|
2019-10-15 05:31:13 +02:00
|
|
|
from urllib import parse
|
2015-04-12 11:34:44 -07:00
|
|
|
|
2015-04-11 23:42:14 -07:00
|
|
|
from django.core.mail import EmailMultiAlternatives
|
|
|
|
|
|
|
|
|
|
from django_mailbox.models import Mailbox
|
|
|
|
|
from django_mailbox.tests.base import EmailMessageTestCase
|
|
|
|
|
|
|
|
|
|
|
2015-04-12 11:34:44 -07:00
|
|
|
__all__ = ['TestImap']
|
|
|
|
|
|
|
|
|
|
|
2015-04-11 23:42:14 -07:00
|
|
|
class TestImap(EmailMessageTestCase):
|
|
|
|
|
def setUp(self):
|
2019-10-15 05:31:13 +02:00
|
|
|
super().setUp()
|
2015-07-21 22:57:41 -07:00
|
|
|
|
2015-04-11 23:42:14 -07:00
|
|
|
self.test_imap_server = (
|
2015-07-21 22:57:41 -07:00
|
|
|
os.environ.get('EMAIL_IMAP_SERVER')
|
2015-04-11 23:42:14 -07:00
|
|
|
)
|
2015-07-21 22:57:41 -07:00
|
|
|
|
|
|
|
|
required_settings = [
|
|
|
|
|
self.test_imap_server,
|
|
|
|
|
self.test_account,
|
|
|
|
|
self.test_password,
|
|
|
|
|
self.test_smtp_server,
|
|
|
|
|
self.test_from_email,
|
|
|
|
|
]
|
|
|
|
|
if not all(required_settings):
|
|
|
|
|
self.skipTest(
|
|
|
|
|
"Integration tests are not available without having "
|
|
|
|
|
"the the following environment variables set: "
|
|
|
|
|
"EMAIL_ACCOUNT, EMAIL_PASSWORD, EMAIL_SMTP_SERVER, "
|
|
|
|
|
"EMAIL_IMAP_SERVER."
|
|
|
|
|
)
|
|
|
|
|
|
2015-04-11 23:42:14 -07:00
|
|
|
self.mailbox = Mailbox.objects.create(
|
|
|
|
|
name='Integration Test Imap',
|
|
|
|
|
uri=self.get_connection_string()
|
|
|
|
|
)
|
2015-04-12 11:34:44 -07:00
|
|
|
self.arbitrary_identifier = str(uuid.uuid4())
|
2015-04-11 23:42:14 -07:00
|
|
|
|
|
|
|
|
def get_connection_string(self):
|
|
|
|
|
return "imap+ssl://{account}:{password}@{server}".format(
|
2015-04-12 11:34:44 -07:00
|
|
|
account=parse.quote(self.test_account),
|
|
|
|
|
password=parse.quote(self.test_password),
|
2015-04-11 23:42:14 -07:00
|
|
|
server=self.test_imap_server,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_get_imap_message(self):
|
|
|
|
|
text_content = 'This is some content'
|
|
|
|
|
msg = EmailMultiAlternatives(
|
2015-04-12 11:34:44 -07:00
|
|
|
self.arbitrary_identifier,
|
2015-04-11 23:42:14 -07:00
|
|
|
text_content,
|
|
|
|
|
self.test_from_email,
|
|
|
|
|
[
|
|
|
|
|
self.test_account,
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
msg.send()
|
|
|
|
|
|
2015-07-07 22:22:15 -07:00
|
|
|
messages = self._get_new_messages(
|
|
|
|
|
self.mailbox,
|
|
|
|
|
condition=lambda m: m['subject'] == self.arbitrary_identifier
|
|
|
|
|
)
|
2018-03-09 19:09:45 +01:00
|
|
|
message = next(messages)
|
2015-04-11 23:42:14 -07:00
|
|
|
|
2018-03-09 19:09:45 +01:00
|
|
|
self.assertEqual(message.subject, self.arbitrary_identifier)
|
|
|
|
|
self.assertEqual(message.text, text_content)
|
|
|
|
|
self.assertEqual(0, len(list(messages)))
|