mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-09 22:38:19 +02:00
Adding basic integration tests.
This commit is contained in:
parent
e5d36cbabf
commit
634f2f4b26
3 changed files with 92 additions and 13 deletions
|
|
@ -1,13 +1,20 @@
|
|||
import email
|
||||
import os.path
|
||||
import time
|
||||
|
||||
import six
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
from django_mailbox import models
|
||||
from django_mailbox.models import Mailbox, Message
|
||||
|
||||
|
||||
class EmailIntegrationTimeout(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def get_email_as_text(name):
|
||||
with open(
|
||||
os.path.join(
|
||||
|
|
@ -32,8 +39,32 @@ class EmailMessageTestCase(TestCase):
|
|||
self._TEXT_STORED_MIMETYPES = models.TEXT_STORED_MIMETYPES
|
||||
|
||||
self.mailbox = Mailbox.objects.create(from_email='from@example.com')
|
||||
|
||||
self.test_account = os.environ['EMAIL_ACCOUNT']
|
||||
self.test_password = os.environ['EMAIL_PASSWORD']
|
||||
self.test_smtp_server = os.environ['EMAIL_SMTP_SERVER']
|
||||
self.test_from_email = 'nobody@nowhere.com'
|
||||
|
||||
self.maximum_wait_seconds = 60 * 5
|
||||
|
||||
settings.EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
|
||||
settings.EMAIL_HOST = self.test_smtp_server
|
||||
settings.EMAIL_PORT = 587
|
||||
settings.EMAIL_HOST_USER = self.test_account
|
||||
settings.EMAIL_HOST_PASSWORD = self.test_password
|
||||
settings.EMAIL_USE_TLS = True
|
||||
super(EmailMessageTestCase, self).setUp()
|
||||
|
||||
def _get_new_messages(self, mailbox):
|
||||
maximum_wait = time.time() + self.maximum_wait_seconds
|
||||
while True:
|
||||
if time.time() > maximum_wait:
|
||||
raise EmailIntegrationTimeout()
|
||||
messages = self.mailbox.get_new_mail()
|
||||
if messages:
|
||||
return messages
|
||||
time.sleep(5)
|
||||
|
||||
def _get_email_as_text(self, name):
|
||||
with open(
|
||||
os.path.join(
|
||||
|
|
|
|||
45
django_mailbox/tests/test_integration_imap.py
Normal file
45
django_mailbox/tests/test_integration_imap.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import os
|
||||
import urllib
|
||||
import uuid
|
||||
|
||||
from django.core.mail import EmailMultiAlternatives
|
||||
|
||||
from django_mailbox.models import Mailbox
|
||||
from django_mailbox.tests.base import EmailMessageTestCase
|
||||
|
||||
|
||||
class TestImap(EmailMessageTestCase):
|
||||
def setUp(self):
|
||||
self.test_imap_server = (
|
||||
os.environ['EMAIL_IMAP_SERVER']
|
||||
)
|
||||
self.mailbox = Mailbox.objects.create(
|
||||
name='Integration Test Imap',
|
||||
uri=self.get_connection_string()
|
||||
)
|
||||
|
||||
def get_connection_string(self):
|
||||
return "imap+ssl://{account}:{password}@{server}".format(
|
||||
account=urllib.quote(self.test_account),
|
||||
password=urllib.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,
|
||||
text_content,
|
||||
self.test_from_email,
|
||||
[
|
||||
self.test_account,
|
||||
]
|
||||
)
|
||||
msg.send()
|
||||
|
||||
messages = self.get_new_messages()
|
||||
|
||||
self.assertEqual(1, len(messages))
|
||||
self.assertEqual(messages[0].subject, arbitrary_identifier)
|
||||
self.assertEqual(messages[0].text, text_content)
|
||||
Loading…
Add table
Add a link
Reference in a new issue