From 1bec172611f850015d052ccf32160dce4f20941f Mon Sep 17 00:00:00 2001 From: medmunds Date: Wed, 31 Oct 2012 18:15:54 -0700 Subject: [PATCH] Testing on backend API calls, using mock; add runtests.py. Cherry-picked from: 8c26807a - Add runtests.py for testing separately from other Django apps cd8504b1 - Make tests compatible with setuptools 4ac65b78 - Set up testing on the backend API calls, using mock --- .gitignore | 1 + README.rst | 16 +++++++++++++ djrill/tests.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ runtests.py | 35 ++++++++++++++++++++++++++++ setup.py | 2 ++ 5 files changed, 115 insertions(+) create mode 100644 runtests.py diff --git a/.gitignore b/.gitignore index 6e99639..77e536a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .DS_Store ._* *.pyc +*.egg-info local.py diff --git a/README.rst b/README.rst index ecc48c4..e6285ba 100644 --- a/README.rst +++ b/README.rst @@ -106,6 +106,21 @@ Just like Django's ``EmailMessage`` and ``EmailMultiAlternatives``, ``DjrillMess ``headers`` argument. Currently it only accepts ``Reply-To`` and ``X-*`` headers since that is all that Mandrill accepts. Any extra headers are silently discarded. +Testing +------- + +The included tests verify that Djrill constructs the expected Mandrill API +calls, without actually calling Mandrill or sending any email. So the tests +don't require a Mandrill API key, but they *do* require mock_ +(``pip install mock``). To run the tests, either:: + + python setup.py test + +or:: + + python runtests.py + + Thanks ------ @@ -118,3 +133,4 @@ the awesome ``requests`` library. .. _MailChimp: http://mailchimp.com .. _requests: http://docs.python-requests.org .. _django-adminplus: https://github.com/jsocol/django-adminplus +.. _mock: http://www.voidspace.org.uk/python/mock/index.html diff --git a/djrill/tests.py b/djrill/tests.py index dcedda2..eebba74 100644 --- a/djrill/tests.py +++ b/djrill/tests.py @@ -1,9 +1,70 @@ +from mock import patch + from django.conf import settings +from django.core import mail from django.core.exceptions import ImproperlyConfigured from django.test import TestCase +from django.utils import simplejson as json from djrill.mail import DjrillMessage +class DjrillBackendMockAPITestCase(TestCase): + """TestCase that uses Djrill EmailBackend with a mocked Mandrill API""" + + class MockResponse: + """requests.post return value mock sufficient for DjrillBackend""" + def __init__(self, status_code=200): + self.status_code = status_code + + def setUp(self): + self.patch = patch('requests.post') + self.mock_post = self.patch.start() + self.mock_post.return_value = self.MockResponse() + + settings.MANDRILL_API_KEY = "FAKE_API_KEY_FOR_TESTING" + settings.MANDRILL_API_URL = "http://mandrillapp.com/api/1.0" + + # Django TestCase sets up locmem EmailBackend; override it here + self.original_email_backend = settings.EMAIL_BACKEND + settings.EMAIL_BACKEND = "djrill.mail.backends.djrill.DjrillBackend" + + def tearDown(self): + self.patch.stop() + settings.EMAIL_BACKEND = self.original_email_backend + + def get_api_call_data(self): + """Returns the data posted to the Mandrill API. + + Fails test if API wasn't called. + """ + if self.mock_post.call_args is None: + raise AssertionError("Mandrill API was not called") + (args, kwargs) = self.mock_post.call_args + if 'data' not in kwargs: + raise AssertionError("requests.post was called without data kwarg " + "-- Maybe tests need to be updated for backend changes?") + return json.loads(kwargs['data']) + + +class DjrillBackendTests(DjrillBackendMockAPITestCase): + """Test Djrill's support for Django mail wrappers""" + + def test_send_mail(self): + mail.send_mail('Subject here', 'Here is the message.', + 'from@example.com', ['to@example.com'], fail_silently=False) + data = self.get_api_call_data() + self.assertEqual(data['message']['subject'], "Subject here") + self.assertEqual(data['message']['text'], "Here is the message.") + self.assertEqual(data['message']['from_email'], "from@example.com") + self.assertEqual(len(data['message']['to']), 1) + self.assertEqual(data['message']['to'][0]['email'], "to@example.com") + + def test_missing_api_key(self): + del settings.MANDRILL_API_KEY + with self.assertRaises(ImproperlyConfigured): + mail.send_mail('Subject', 'Message', 'from@example.com', + ['to@example.com']) + class DjrillMessageTests(TestCase): def setUp(self): diff --git a/runtests.py b/runtests.py new file mode 100644 index 0000000..d35ae7c --- /dev/null +++ b/runtests.py @@ -0,0 +1,35 @@ +# python setup.py test +# or +# python runtests.py + +import sys +from django.conf import settings + +APP='djrill' + +settings.configure( + DEBUG=True, + DATABASES={ + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + } + }, + ROOT_URLCONF=APP+'.urls', + INSTALLED_APPS=( + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.admin', + APP, + ) +) + +from django.test.simple import DjangoTestSuiteRunner + +def runtests(): + test_runner = DjangoTestSuiteRunner(verbosity=1) + failures = test_runner.run_tests([APP, ]) + sys.exit(failures) + +if __name__ == '__main__': + runtests() diff --git a/setup.py b/setup.py index f7f822c..95486d0 100644 --- a/setup.py +++ b/setup.py @@ -14,6 +14,8 @@ setup( zip_safe=False, install_requires=["requests", "django"], include_package_data=True, + test_suite="runtests.runtests", + test_requires=["mock"], classifiers=[ "Programming Language :: Python", "Topic :: Software Development :: Libraries :: Python Modules",