Use real Response object in DjrillBackendMockAPITestCase tests.

(Improves testing accuracy around API response encoding.)

* Add `six` as test dependency (six.BytesIO, six.b)
* Change MockResponse content to bytes (because HTTP responses
  are bytes, not strings)
This commit is contained in:
medmunds
2015-01-16 13:18:17 -08:00
parent 4754ef7650
commit 11961b57e5
4 changed files with 13 additions and 10 deletions

View File

@@ -1,5 +1,7 @@
import json
from mock import patch
import requests
import six
from django.test import TestCase
@@ -11,15 +13,13 @@ from .utils import override_settings
class DjrillBackendMockAPITestCase(TestCase):
"""TestCase that uses Djrill EmailBackend with a mocked Mandrill API"""
class MockResponse:
class MockResponse(requests.Response):
"""requests.post return value mock sufficient for DjrillBackend"""
def __init__(self, status_code=200, content="{}", json=None):
def __init__(self, status_code=200, raw=six.b("{}"), encoding='utf-8'):
super(DjrillBackendMockAPITestCase.MockResponse, self).__init__()
self.status_code = status_code
self.content = content
self._json = json if json is not None else ['']
def json(self):
return self._json
self.encoding = encoding
self.raw = six.BytesIO(raw)
def setUp(self):
self.patch = patch('requests.post', autospec=True)

View File

@@ -6,7 +6,9 @@ from base64 import b64decode
from datetime import date, datetime, timedelta, tzinfo
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
import json
import os
import six
from django.core import mail
from django.core.exceptions import ImproperlyConfigured
@@ -466,7 +468,7 @@ class DjrillMandrillFeatureTests(DjrillBackendMockAPITestCase):
def test_send_attaches_mandrill_response(self):
""" The mandrill_response should be attached to the message when it is sent """
response = [{'mandrill_response': 'would_be_here'}]
self.mock_post.return_value = self.MockResponse(json=response)
self.mock_post.return_value = self.MockResponse(raw=six.b(json.dumps(response)))
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
sent = msg.send()
self.assertEqual(sent, 1)

View File

@@ -45,7 +45,8 @@ combinations of Django and Python versions. (Full list in
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 <http://www.voidspace.org.uk/python/mock/index.html>`_ (``pip install mock``).
`mock <http://www.voidspace.org.uk/python/mock/index.html>`_
and `six <https://pythonhosted.org/six/>`_ (``pip install mock six``).
To run the tests, either::

View File

@@ -32,7 +32,7 @@ setup(
install_requires=["requests>=1.0.0", "django>=1.3"],
include_package_data=True,
test_suite="runtests.runtests",
tests_require=["mock"],
tests_require=["mock", "six"],
classifiers=[
"Programming Language :: Python",
"Programming Language :: Python :: Implementation :: PyPy",