mirror of
https://github.com/jazzband/django-defender.git
synced 2026-03-16 22:10:32 +00:00
Allow redis connections to be mocked outside tests
Use a mocked instance of redis outside tests so we can use it on django projects.
This commit is contained in:
parent
2a1ab5c7ac
commit
d72e32cfc1
2 changed files with 17 additions and 23 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import mockredis
|
||||
import redis
|
||||
try:
|
||||
import urlparse
|
||||
|
|
@ -10,14 +11,20 @@ from . import config
|
|||
urlparse.uses_netloc.append("redis")
|
||||
|
||||
|
||||
mocked_redis = mockredis.mock_strict_redis_client()
|
||||
|
||||
|
||||
def get_redis_connection():
|
||||
""" Get the redis connection """
|
||||
redis_config = parse_redis_url(config.DEFENDER_REDIS_URL)
|
||||
return redis.StrictRedis(
|
||||
host=redis_config.get('HOST'),
|
||||
port=redis_config.get('PORT'),
|
||||
db=redis_config.get('DB'),
|
||||
password=redis_config.get('PASSWORD'))
|
||||
""" Get the redis connection if not using mock """
|
||||
if config.MOCK_REDIS: # pragma: no cover
|
||||
return mocked_redis # pragma: no cover
|
||||
else: # pragma: no cover
|
||||
redis_config = parse_redis_url(config.DEFENDER_REDIS_URL)
|
||||
return redis.StrictRedis(
|
||||
host=redis_config.get('HOST'),
|
||||
port=redis_config.get('PORT'),
|
||||
db=redis_config.get('DB'),
|
||||
password=redis_config.get('PASSWORD'))
|
||||
|
||||
|
||||
def parse_redis_url(url):
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import string
|
|||
import time
|
||||
|
||||
from mock import patch
|
||||
import mockredis
|
||||
|
||||
from django.test import TestCase
|
||||
from django.test.client import RequestFactory
|
||||
|
|
@ -14,21 +13,11 @@ from django.core.urlresolvers import NoReverseMatch
|
|||
from django.core.urlresolvers import reverse
|
||||
from django.http import HttpRequest
|
||||
|
||||
from .connection import parse_redis_url
|
||||
from .connection import parse_redis_url, get_redis_connection
|
||||
from . import utils
|
||||
from . import config
|
||||
from .models import AccessAttempt
|
||||
|
||||
mocked_redis = mockredis.mock_strict_redis_client()
|
||||
|
||||
|
||||
def mock_get_connection():
|
||||
if config.MOCK_REDIS: # pragma: no cover
|
||||
return mocked_redis # pragma: no cover
|
||||
else: # pragma: no cover
|
||||
from .connection import get_redis_connection # pragma: no cover
|
||||
return get_redis_connection() # pragma: no cover
|
||||
|
||||
|
||||
# Django >= 1.7 compatibility
|
||||
try:
|
||||
|
|
@ -39,8 +28,6 @@ except NoReverseMatch:
|
|||
LOGIN_FORM_KEY = 'this_is_the_login_form'
|
||||
|
||||
|
||||
@patch('defender.connection.get_redis_connection', mock_get_connection)
|
||||
@patch('defender.utils.redis_server', mock_get_connection())
|
||||
class AccessAttemptTest(TestCase):
|
||||
""" Test case using custom settings for testing
|
||||
"""
|
||||
|
|
@ -81,7 +68,7 @@ class AccessAttemptTest(TestCase):
|
|||
|
||||
def tearDown(self):
|
||||
""" clean up the db """
|
||||
mock_get_connection().flushdb()
|
||||
get_redis_connection().flushdb()
|
||||
|
||||
def test_login_get(self):
|
||||
""" visit the login page """
|
||||
|
|
@ -141,7 +128,7 @@ class AccessAttemptTest(TestCase):
|
|||
|
||||
if config.MOCK_REDIS:
|
||||
# mock redis require that we expire on our own
|
||||
mock_get_connection().do_expire() # pragma: no cover
|
||||
get_redis_connection().do_expire() # pragma: no cover
|
||||
# It should be possible to login again, make sure it is.
|
||||
self.test_valid_login()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue