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:
Tim Abbott
2020-01-16 13:02:06 -08:00
parent d0492b103b
commit 8ff5d8ca89
3 changed files with 10 additions and 18 deletions

View File

@@ -73,12 +73,6 @@ import urllib
import shutil
import tempfile
API_KEYS = {} # type: Dict[str, str]
def flush_caches_for_testing() -> None:
global API_KEYS
API_KEYS = {}
class UploadSerializeMixin(SerializeMixin):
"""
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
maxDiff = None # type: Optional[int]
def setUp(self) -> None:
super().setUp()
self.API_KEYS = {} # type: Dict[str, str]
def tearDown(self) -> None:
super().tearDown()
# 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.
"""
if identifier in API_KEYS:
api_key = API_KEYS[identifier]
if identifier in self.API_KEYS:
api_key = self.API_KEYS[identifier]
else:
if is_remote_server(identifier):
api_key = get_remote_server_by_uuid(identifier).api_key
else:
user = get_user(identifier, get_realm(realm))
api_key = get_api_key(user)
API_KEYS[identifier] = api_key
self.API_KEYS[identifier] = api_key
credentials = "%s:%s" % (identifier, api_key)
return 'Basic ' + base64.b64encode(credentials.encode('utf-8')).decode('utf-8')

View File

@@ -14,10 +14,9 @@ from django.test import runner as django_runner
from django.test.runner import DiscoverRunner
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.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.test_helpers import (
write_instrumentation_reports,
@@ -122,8 +121,6 @@ def run_test(test: TestCase, result: TestResult) -> bool:
bounce_key_prefix_for_testing(test_name)
bounce_redis_key_prefix_for_testing(test_name)
flush_caches_for_testing()
if not hasattr(test, "_pre_setup"):
msg = "Test doesn't have _pre_setup; something is wrong."
error_pre_setup = (Exception, Exception(msg), None)
@@ -295,8 +292,6 @@ def init_worker(counter: Synchronized) -> None:
You can now use _worker_id.
"""
test_classes.API_KEYS = {}
# Clear the cache
from zerver.lib.cache import get_cache_backend
cache = get_cache_backend(None)

View File

@@ -187,15 +187,14 @@ class PushBouncerNotificationTest(BouncerTestCase):
# We do a bit of hackery here to the API_KEYS cache just to
# make the code simple for sending an incorrect API key.
from zerver.lib.test_classes import API_KEYS
API_KEYS[self.server_uuid] = 'invalid'
self.API_KEYS[self.server_uuid] = 'invalid'
result = self.api_post(self.server_uuid, endpoint, {'user_id': user_id,
'token_kind': token_kind,
'token': token})
self.assert_json_error(result, "Zulip server auth failure: key does not match role 1234-abcd",
status_code=401)
del API_KEYS[self.server_uuid]
del self.API_KEYS[self.server_uuid]
credentials = "%s:%s" % ("5678-efgh", 'invalid')
api_auth = 'Basic ' + base64.b64encode(credentials.encode('utf-8')).decode('utf-8')