mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 17:07:07 +00:00
test_classes: Clean up API_KEYS cache.
Since the intent of our testing code was clearly to clear this cache for every test, there's no reason for it to be a module-level global. This allows us to remove an unnecessary import from test_runner.py, which in combination with DEFAULT_REALM's definition was causing us to run models code before running migrations inside test-backend. (That bug, in turn, caused test-backend's check for whether migrations needs to be run to happen sadly after trying to access a Realm, trigger a test-backend crash if the Realm model had changed since the last provision).
This commit is contained in:
@@ -73,12 +73,6 @@ import urllib
|
|||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
API_KEYS = {} # type: Dict[str, str]
|
|
||||||
|
|
||||||
def flush_caches_for_testing() -> None:
|
|
||||||
global API_KEYS
|
|
||||||
API_KEYS = {}
|
|
||||||
|
|
||||||
class UploadSerializeMixin(SerializeMixin):
|
class UploadSerializeMixin(SerializeMixin):
|
||||||
"""
|
"""
|
||||||
We cannot use override_settings to change upload directory because
|
We cannot use override_settings to change upload directory because
|
||||||
@@ -101,6 +95,10 @@ class ZulipTestCase(TestCase):
|
|||||||
# Ensure that the test system just shows us diffs
|
# Ensure that the test system just shows us diffs
|
||||||
maxDiff = None # type: Optional[int]
|
maxDiff = None # type: Optional[int]
|
||||||
|
|
||||||
|
def setUp(self) -> None:
|
||||||
|
super().setUp()
|
||||||
|
self.API_KEYS = {} # type: Dict[str, str]
|
||||||
|
|
||||||
def tearDown(self) -> None:
|
def tearDown(self) -> None:
|
||||||
super().tearDown()
|
super().tearDown()
|
||||||
# Important: we need to clear event queues to avoid leaking data to future tests.
|
# Important: we need to clear event queues to avoid leaking data to future tests.
|
||||||
@@ -409,15 +407,15 @@ class ZulipTestCase(TestCase):
|
|||||||
"""
|
"""
|
||||||
identifier: Can be an email or a remote server uuid.
|
identifier: Can be an email or a remote server uuid.
|
||||||
"""
|
"""
|
||||||
if identifier in API_KEYS:
|
if identifier in self.API_KEYS:
|
||||||
api_key = API_KEYS[identifier]
|
api_key = self.API_KEYS[identifier]
|
||||||
else:
|
else:
|
||||||
if is_remote_server(identifier):
|
if is_remote_server(identifier):
|
||||||
api_key = get_remote_server_by_uuid(identifier).api_key
|
api_key = get_remote_server_by_uuid(identifier).api_key
|
||||||
else:
|
else:
|
||||||
user = get_user(identifier, get_realm(realm))
|
user = get_user(identifier, get_realm(realm))
|
||||||
api_key = get_api_key(user)
|
api_key = get_api_key(user)
|
||||||
API_KEYS[identifier] = api_key
|
self.API_KEYS[identifier] = api_key
|
||||||
|
|
||||||
credentials = "%s:%s" % (identifier, api_key)
|
credentials = "%s:%s" % (identifier, api_key)
|
||||||
return 'Basic ' + base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
|
return 'Basic ' + base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
|
||||||
|
|||||||
@@ -14,10 +14,9 @@ from django.test import runner as django_runner
|
|||||||
from django.test.runner import DiscoverRunner
|
from django.test.runner import DiscoverRunner
|
||||||
from django.test.signals import template_rendered
|
from django.test.signals import template_rendered
|
||||||
|
|
||||||
from zerver.lib import test_classes, test_helpers
|
from zerver.lib import test_helpers
|
||||||
from zerver.lib.cache import bounce_key_prefix_for_testing
|
from zerver.lib.cache import bounce_key_prefix_for_testing
|
||||||
from zerver.lib.rate_limiter import bounce_redis_key_prefix_for_testing
|
from zerver.lib.rate_limiter import bounce_redis_key_prefix_for_testing
|
||||||
from zerver.lib.test_classes import flush_caches_for_testing
|
|
||||||
from zerver.lib.sqlalchemy_utils import get_sqlalchemy_connection
|
from zerver.lib.sqlalchemy_utils import get_sqlalchemy_connection
|
||||||
from zerver.lib.test_helpers import (
|
from zerver.lib.test_helpers import (
|
||||||
write_instrumentation_reports,
|
write_instrumentation_reports,
|
||||||
@@ -122,8 +121,6 @@ def run_test(test: TestCase, result: TestResult) -> bool:
|
|||||||
bounce_key_prefix_for_testing(test_name)
|
bounce_key_prefix_for_testing(test_name)
|
||||||
bounce_redis_key_prefix_for_testing(test_name)
|
bounce_redis_key_prefix_for_testing(test_name)
|
||||||
|
|
||||||
flush_caches_for_testing()
|
|
||||||
|
|
||||||
if not hasattr(test, "_pre_setup"):
|
if not hasattr(test, "_pre_setup"):
|
||||||
msg = "Test doesn't have _pre_setup; something is wrong."
|
msg = "Test doesn't have _pre_setup; something is wrong."
|
||||||
error_pre_setup = (Exception, Exception(msg), None)
|
error_pre_setup = (Exception, Exception(msg), None)
|
||||||
@@ -295,8 +292,6 @@ def init_worker(counter: Synchronized) -> None:
|
|||||||
You can now use _worker_id.
|
You can now use _worker_id.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
test_classes.API_KEYS = {}
|
|
||||||
|
|
||||||
# Clear the cache
|
# Clear the cache
|
||||||
from zerver.lib.cache import get_cache_backend
|
from zerver.lib.cache import get_cache_backend
|
||||||
cache = get_cache_backend(None)
|
cache = get_cache_backend(None)
|
||||||
|
|||||||
@@ -187,15 +187,14 @@ class PushBouncerNotificationTest(BouncerTestCase):
|
|||||||
|
|
||||||
# We do a bit of hackery here to the API_KEYS cache just to
|
# We do a bit of hackery here to the API_KEYS cache just to
|
||||||
# make the code simple for sending an incorrect API key.
|
# make the code simple for sending an incorrect API key.
|
||||||
from zerver.lib.test_classes import API_KEYS
|
self.API_KEYS[self.server_uuid] = 'invalid'
|
||||||
API_KEYS[self.server_uuid] = 'invalid'
|
|
||||||
result = self.api_post(self.server_uuid, endpoint, {'user_id': user_id,
|
result = self.api_post(self.server_uuid, endpoint, {'user_id': user_id,
|
||||||
'token_kind': token_kind,
|
'token_kind': token_kind,
|
||||||
'token': token})
|
'token': token})
|
||||||
self.assert_json_error(result, "Zulip server auth failure: key does not match role 1234-abcd",
|
self.assert_json_error(result, "Zulip server auth failure: key does not match role 1234-abcd",
|
||||||
status_code=401)
|
status_code=401)
|
||||||
|
|
||||||
del API_KEYS[self.server_uuid]
|
del self.API_KEYS[self.server_uuid]
|
||||||
|
|
||||||
credentials = "%s:%s" % ("5678-efgh", 'invalid')
|
credentials = "%s:%s" % ("5678-efgh", 'invalid')
|
||||||
api_auth = 'Basic ' + base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
|
api_auth = 'Basic ' + base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
|
||||||
|
|||||||
Reference in New Issue
Block a user