From 0eb63b44cff22dcd4e11a014ba041d62eb2e3546 Mon Sep 17 00:00:00 2001 From: Adam Coddington Date: Sat, 19 Jan 2013 00:26:58 -0800 Subject: [PATCH] Adding tests. --- .travis.yml | 12 ++++ django_mailbox/runtests.py | 36 ++++++++++ django_mailbox/tests/__init__.py | 65 +++++++++++++++++++ django_mailbox/tests/generic_message.eml | 14 ++++ .../tests/message_with_attachment.eml | 28 ++++++++ setup.py | 9 ++- 6 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 .travis.yml create mode 100644 django_mailbox/runtests.py create mode 100644 django_mailbox/tests/__init__.py create mode 100644 django_mailbox/tests/generic_message.eml create mode 100644 django_mailbox/tests/message_with_attachment.eml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..900a718 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: python +python: + - "2.6" + - "2.7" +env: + - DJANGO=1.3.1 + - DJANGO=1.4 +install: + - pip install -q Django==$DJANGO --use-mirrors + - pip install -q -e . --use-mirrors +script: + - python setup.py test diff --git a/django_mailbox/runtests.py b/django_mailbox/runtests.py new file mode 100644 index 0000000..628054a --- /dev/null +++ b/django_mailbox/runtests.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +import sys + +from os.path import dirname, abspath + +from django.conf import settings + +if not settings.configured: + settings.configure( + DATABASES={ + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + }, + }, + INSTALLED_APPS=[ + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django_mailbox', + ] + ) + +from django.test.simple import DjangoTestSuiteRunner + + +def runtests(*test_args): + if not test_args: + test_args = ['django_mailbox'] + parent = dirname(abspath(__file__)) + sys.path.insert(0, parent) + runner = DjangoTestSuiteRunner(verbosity=1, interactive=True) + failures = runner.run_tests(test_args) + sys.exit(failures) + + +if __name__ == '__main__': + runtests(*sys.argv[1:]) diff --git a/django_mailbox/tests/__init__.py b/django_mailbox/tests/__init__.py new file mode 100644 index 0000000..fff82f8 --- /dev/null +++ b/django_mailbox/tests/__init__.py @@ -0,0 +1,65 @@ +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, + '' + ) + self.assertEqual( + msg.from_header, + 'Adam Coddington ', + ) + self.assertEqual( + msg.to_header, + 'Adam Coddington ', + ) + + 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', + ) diff --git a/django_mailbox/tests/generic_message.eml b/django_mailbox/tests/generic_message.eml new file mode 100644 index 0000000..438eea7 --- /dev/null +++ b/django_mailbox/tests/generic_message.eml @@ -0,0 +1,14 @@ +MIME-Version: 1.0 +Received: by 10.221.0.211 with HTTP; Sun, 20 Jan 2013 11:53:53 -0800 (PST) +X-Originating-IP: [24.22.122.177] +Date: Sun, 20 Jan 2013 11:53:53 -0800 +Delivered-To: test@adamcoddington.net +Message-ID: +Subject: Message Without Attachment +From: Adam Coddington +To: Adam Coddington +Content-Type: text/plain; + charset="iso-8859-1" +Content-Transfer-Encoding: 7bit + +Hello there. diff --git a/django_mailbox/tests/message_with_attachment.eml b/django_mailbox/tests/message_with_attachment.eml new file mode 100644 index 0000000..d678018 --- /dev/null +++ b/django_mailbox/tests/message_with_attachment.eml @@ -0,0 +1,28 @@ +MIME-Version: 1.0 +Received: by 10.221.0.211 with HTTP; Sun, 20 Jan 2013 12:07:07 -0800 (PST) +X-Originating-IP: [24.22.122.177] +Date: Sun, 20 Jan 2013 12:07:07 -0800 +Delivered-To: test@adamcoddington.net +Message-ID: +Subject: Message With Attachment +From: Adam Coddington +To: Adam Coddington +Content-Type: multipart/mixed; boundary=047d7b33dd729737fe04d3bde348 + +--047d7b33dd729737fe04d3bde348 +Content-Type: text/plain; charset=UTF-8 + +This message has an attachment. + +--047d7b33dd729737fe04d3bde348 +Content-Type: image/png; name="heart.png" +Content-Disposition: attachment; filename="heart.png" +Content-Transfer-Encoding: base64 +X-Attachment-Id: f_hc6mair60 + +iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAFoTx1HAAAAzUlEQVQoz32RWxXDIBBEr4NIQEIl +ICESkFAJkRAJSIgEpEQCEqYfu6QUkn7sCcyDGQiSACKSKCAkGwBJwhDwZQNMEiYAIBdQvk7rfaHf +AO8NBJwCxTGhtFgTHVNaNaJeWFu44AXEHzKCktc7zZ0vss+bMoHSiM2b9mQoX1eZCgGqnWskY3gi +XXAAxb8BqFiUgBNY7k49Tu/kV7UKPsefrjEOT9GmghYzrk9V03pjDGYKj3d0c06dKZkpTboRaD9o +B+1m2m81d2Az948xzgdjLaFe95e83AAAAABJRU5ErkJggg== +--047d7b33dd729737fe04d3bde348-- diff --git a/setup.py b/setup.py index ffb5c5e..a2e9f34 100755 --- a/setup.py +++ b/setup.py @@ -1,12 +1,17 @@ -from distutils.core import setup +from setuptools import setup + +tests_require=['django'] setup( name='django-mailbox', - version='1.5', + version='1.6', url='http://bitbucket.org/latestrevision/django-mailbox/', description='Import mail from POP3, IMAP, local mailboxes or directly from Postfix or Exim4 into your Django application automatically.', author='Adam Coddington', author_email='me@adamcoddington.net', + tests_require=tests_require, + extras_require={'test': tests_require}, + test_suite='django_mailbox.runtests.runtests', classifiers=[ 'Framework :: Django', 'Intended Audience :: Developers',