From 11961b57e5a4058854699ee8bd404d62e07bce67 Mon Sep 17 00:00:00 2001 From: medmunds Date: Fri, 16 Jan 2015 13:18:17 -0800 Subject: [PATCH] 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) --- djrill/tests/mock_backend.py | 14 +++++++------- djrill/tests/test_mandrill_send.py | 4 +++- docs/contributing.rst | 3 ++- setup.py | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/djrill/tests/mock_backend.py b/djrill/tests/mock_backend.py index a806282..bc46689 100644 --- a/djrill/tests/mock_backend.py +++ b/djrill/tests/mock_backend.py @@ -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) diff --git a/djrill/tests/test_mandrill_send.py b/djrill/tests/test_mandrill_send.py index 403584a..0d3b47a 100644 --- a/djrill/tests/test_mandrill_send.py +++ b/djrill/tests/test_mandrill_send.py @@ -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) diff --git a/docs/contributing.rst b/docs/contributing.rst index 54956ff..a3e2a81 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -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 `_ (``pip install mock``). +`mock `_ +and `six `_ (``pip install mock six``). To run the tests, either:: diff --git a/setup.py b/setup.py index 3e77cc8..2c5ef2e 100644 --- a/setup.py +++ b/setup.py @@ -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",