From 8ff5d8ca89e6e89a52bbbcc30e47b059e661e8bb Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Thu, 16 Jan 2020 13:02:06 -0800 Subject: [PATCH] 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). --- zerver/lib/test_classes.py | 16 +++++++--------- zerver/lib/test_runner.py | 7 +------ zerver/tests/test_push_notifications.py | 5 ++--- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.py index c8e02b2a72..78ebf38132 100644 --- a/zerver/lib/test_classes.py +++ b/zerver/lib/test_classes.py @@ -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') diff --git a/zerver/lib/test_runner.py b/zerver/lib/test_runner.py index 2874da3471..f8e13c9001 100644 --- a/zerver/lib/test_runner.py +++ b/zerver/lib/test_runner.py @@ -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) diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index a0520e487e..c6eacc1c25 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -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')