mirror of
https://github.com/pacnpal/django-anymail.git
synced 2025-12-20 11:51:05 -05:00
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:
@@ -1,5 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
from mock import patch
|
from mock import patch
|
||||||
|
import requests
|
||||||
|
import six
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
@@ -11,15 +13,13 @@ from .utils import override_settings
|
|||||||
class DjrillBackendMockAPITestCase(TestCase):
|
class DjrillBackendMockAPITestCase(TestCase):
|
||||||
"""TestCase that uses Djrill EmailBackend with a mocked Mandrill API"""
|
"""TestCase that uses Djrill EmailBackend with a mocked Mandrill API"""
|
||||||
|
|
||||||
class MockResponse:
|
class MockResponse(requests.Response):
|
||||||
"""requests.post return value mock sufficient for DjrillBackend"""
|
"""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.status_code = status_code
|
||||||
self.content = content
|
self.encoding = encoding
|
||||||
self._json = json if json is not None else ['']
|
self.raw = six.BytesIO(raw)
|
||||||
|
|
||||||
def json(self):
|
|
||||||
return self._json
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.patch = patch('requests.post', autospec=True)
|
self.patch = patch('requests.post', autospec=True)
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ from base64 import b64decode
|
|||||||
from datetime import date, datetime, timedelta, tzinfo
|
from datetime import date, datetime, timedelta, tzinfo
|
||||||
from email.mime.base import MIMEBase
|
from email.mime.base import MIMEBase
|
||||||
from email.mime.image import MIMEImage
|
from email.mime.image import MIMEImage
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
|
import six
|
||||||
|
|
||||||
from django.core import mail
|
from django.core import mail
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
@@ -466,7 +468,7 @@ class DjrillMandrillFeatureTests(DjrillBackendMockAPITestCase):
|
|||||||
def test_send_attaches_mandrill_response(self):
|
def test_send_attaches_mandrill_response(self):
|
||||||
""" The mandrill_response should be attached to the message when it is sent """
|
""" The mandrill_response should be attached to the message when it is sent """
|
||||||
response = [{'mandrill_response': 'would_be_here'}]
|
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'],)
|
msg = mail.EmailMessage('Subject', 'Message', 'from@example.com', ['to1@example.com'],)
|
||||||
sent = msg.send()
|
sent = msg.send()
|
||||||
self.assertEqual(sent, 1)
|
self.assertEqual(sent, 1)
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ combinations of Django and Python versions. (Full list in
|
|||||||
The included tests verify that Djrill constructs the expected Mandrill API
|
The included tests verify that Djrill constructs the expected Mandrill API
|
||||||
calls, without actually calling Mandrill or sending any email. So the tests
|
calls, without actually calling Mandrill or sending any email. So the tests
|
||||||
don't require a Mandrill API key, but they *do* require
|
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::
|
To run the tests, either::
|
||||||
|
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -32,7 +32,7 @@ setup(
|
|||||||
install_requires=["requests>=1.0.0", "django>=1.3"],
|
install_requires=["requests>=1.0.0", "django>=1.3"],
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
test_suite="runtests.runtests",
|
test_suite="runtests.runtests",
|
||||||
tests_require=["mock"],
|
tests_require=["mock", "six"],
|
||||||
classifiers=[
|
classifiers=[
|
||||||
"Programming Language :: Python",
|
"Programming Language :: Python",
|
||||||
"Programming Language :: Python :: Implementation :: PyPy",
|
"Programming Language :: Python :: Implementation :: PyPy",
|
||||||
|
|||||||
Reference in New Issue
Block a user