1
0
Fork 1
mirror of https://github.com/coddingtonbear/django-mailbox.git synced 2026-07-09 22:38:19 +02:00
django-mailbox/django_mailbox/tests/__init__.py

66 lines
1.8 KiB
Python
Raw Normal View History

2013-01-19 00:26:58 -08:00
import email
import os.path
import shutil
from django.db import models
from django.test import TestCase
from django_mailbox.models import Mailbox
class TestProcessMessage(TestCase):
def _get_email_object(self, name):
with open(os.path.join(os.path.dirname(__file__), name), 'r') as f:
return email.message_from_string(
f.read()
)
def tearDown(self):
try:
shutil.rmtree('mailbox_attachments')
except OSError:
pass
def test_message_without_attachments(self):
message = self._get_email_object('generic_message.eml')
mailbox = Mailbox.objects.create()
msg = mailbox.process_incoming_message(message)
self.assertEqual(
msg.mailbox,
mailbox
)
self.assertEqual(msg.subject, 'Message Without Attachment')
self.assertEqual(
msg.message_id,
'<CAMdmm+hGH8Dgn-_0xnXJCd=PhyNAiouOYm5zFP0z-foqTO60zA@mail.gmail.com>'
)
self.assertEqual(
msg.from_header,
'Adam Coddington <test@adamcoddington.net>',
)
self.assertEqual(
msg.to_header,
'Adam Coddington <test@adamcoddington.net>',
)
def test_message_with_attachments(self):
message = self._get_email_object('message_with_attachment.eml')
mailbox = Mailbox.objects.create()
msg = mailbox.process_incoming_message(message)
expected_count = 1
actual_count = msg.attachments.count()
self.assertEquals(
expected_count,
actual_count,
)
attachment = msg.attachments.all()[0]
self.assertEquals(
os.path.basename(attachment.document.name),
'heart.png',
)