Use override_settings rather than mucking with settings in tests

Second attempt to address possible test sequencing issue around tests that alter settings. (Failures in Travis tests not reproducible locally.)

Back-ports override_settings from Django 1.4 for compatibility with Django 1.3.
This commit is contained in:
medmunds
2014-04-20 15:48:12 -07:00
parent ea2499f92f
commit 8815601b65
5 changed files with 110 additions and 58 deletions

View File

@@ -1,19 +1,22 @@
import json
from mock import patch
from django.conf import settings
from django.test import TestCase
from .utils import override_settings
@override_settings(MANDRILL_API_KEY="FAKE_API_KEY_FOR_TESTING",
EMAIL_BACKEND="djrill.mail.backends.djrill.DjrillBackend")
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, content="{}", json=['']):
def __init__(self, status_code=200, content="{}", json=None):
self.status_code = status_code
self.content = content
self._json = json
self._json = json if json is not None else ['']
def json(self):
return self._json
@@ -23,15 +26,8 @@ class DjrillBackendMockAPITestCase(TestCase):
self.mock_post = self.patch.start()
self.mock_post.return_value = self.MockResponse()
settings.MANDRILL_API_KEY = "FAKE_API_KEY_FOR_TESTING"
# 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 assert_mandrill_called(self, endpoint):
"""Verifies the (mock) Mandrill API was called on endpoint.