mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-09 08:36:23 +01:00
Ensure backwards compatibility when fetching a redis client
This commit is contained in:
@@ -8,7 +8,7 @@ from uuid import uuid1
|
||||
from redis import Redis
|
||||
import requests
|
||||
|
||||
from django.core.cache import caches
|
||||
from allianceauth.utils.cache import get_redis_client
|
||||
|
||||
from allianceauth import __title__ as AUTH_TITLE, __url__, __version__
|
||||
|
||||
@@ -103,8 +103,7 @@ class DiscordClient:
|
||||
self._access_token = str(access_token)
|
||||
self._is_rate_limited = bool(is_rate_limited)
|
||||
if not redis:
|
||||
default_cache = caches['default']
|
||||
self._redis = default_cache.get_master_client()
|
||||
self._redis = get_redis_client()
|
||||
if not isinstance(self._redis, Redis):
|
||||
raise RuntimeError(
|
||||
'This class requires a Redis client, but none was provided '
|
||||
|
||||
@@ -85,29 +85,18 @@ class TestBasicsAndHelpers(TestCase):
|
||||
client = DiscordClient(TEST_BOT_TOKEN, mock_redis, is_rate_limited=True)
|
||||
self.assertTrue(client.is_rate_limited)
|
||||
|
||||
@patch(MODULE_PATH + '.caches')
|
||||
def test_use_default_redis_if_none_provided(self, mock_caches):
|
||||
my_redis = MagicMock(spec=Redis)
|
||||
mock_default_cache = MagicMock(**{'get_master_client.return_value': my_redis})
|
||||
my_dict = {'default': mock_default_cache}
|
||||
mock_caches.__getitem__.side_effect = my_dict.__getitem__
|
||||
|
||||
def test_use_default_redis_if_none_provided(self):
|
||||
client = DiscordClient(TEST_BOT_TOKEN)
|
||||
self.assertTrue(mock_default_cache.get_master_client.called)
|
||||
self.assertEqual(client._redis, my_redis)
|
||||
|
||||
@patch(MODULE_PATH + '.caches')
|
||||
def test_raise_exception_if_default_cache_is_not_redis(self, mock_caches):
|
||||
my_redis = MagicMock()
|
||||
mock_default_cache = MagicMock(**{'get_master_client.return_value': my_redis})
|
||||
my_dict = {'default': mock_default_cache}
|
||||
mock_caches.__getitem__.side_effect = my_dict.__getitem__
|
||||
self.assertIsInstance(client._redis, Redis)
|
||||
|
||||
@patch(MODULE_PATH + '.get_redis_client')
|
||||
def test_raise_exception_if_redis_client_not_found(self, mock_get_redis_client):
|
||||
# given
|
||||
mock_get_redis_client.return_value = None
|
||||
# when
|
||||
with self.assertRaises(RuntimeError):
|
||||
DiscordClient(TEST_BOT_TOKEN)
|
||||
|
||||
self.assertTrue(mock_default_cache.get_master_client.called)
|
||||
|
||||
|
||||
@requests_mock.Mocker()
|
||||
class TestOtherMethods(TestCase):
|
||||
|
||||
@@ -35,17 +35,17 @@ import logging
|
||||
from uuid import uuid1
|
||||
import random
|
||||
|
||||
from django.core.cache import caches
|
||||
from django.contrib.auth.models import User, Group
|
||||
|
||||
from allianceauth.services.modules.discord.models import DiscordUser
|
||||
from allianceauth.utils.cache import get_redis_client
|
||||
|
||||
logger = logging.getLogger('allianceauth')
|
||||
MAX_RUNS = 3
|
||||
|
||||
|
||||
def clear_cache():
|
||||
default_cache = caches['default']
|
||||
redis = default_cache.get_master_client()
|
||||
redis = get_redis_client()
|
||||
redis.flushall()
|
||||
logger.info('Cache flushed')
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ from requests.exceptions import HTTPError
|
||||
import requests_mock
|
||||
|
||||
from django.contrib.auth.models import Group, User
|
||||
from django.core.cache import caches
|
||||
from django.shortcuts import reverse
|
||||
from django.test import TransactionTestCase, TestCase
|
||||
from django.test.utils import override_settings
|
||||
@@ -23,6 +22,7 @@ from allianceauth.authentication.models import State
|
||||
from allianceauth.eveonline.models import EveCharacter
|
||||
from allianceauth.notifications.models import Notification
|
||||
from allianceauth.tests.auth_utils import AuthUtils
|
||||
from allianceauth.utils.cache import get_redis_client
|
||||
|
||||
from . import (
|
||||
TEST_GUILD_ID,
|
||||
@@ -87,8 +87,7 @@ remove_guild_member_request = DiscordRequest(
|
||||
|
||||
|
||||
def clear_cache():
|
||||
default_cache = caches['default']
|
||||
redis = default_cache.get_master_client()
|
||||
redis = get_redis_client()
|
||||
redis.flushall()
|
||||
logger.info('Cache flushed')
|
||||
|
||||
@@ -109,7 +108,6 @@ def reset_testdata():
|
||||
class TestServiceFeatures(TransactionTestCase):
|
||||
fixtures = ['disable_analytics.json']
|
||||
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
|
||||
Reference in New Issue
Block a user