Repository: mdn/kuma

Issue:
Cache is not cleared between tests
When working on https://github.com/mdn/kuma/pull/6143 I ran into a problem that tests failed or worked depending on the order you run them. 
The reason was that some tests make assumptions about the *state* of the cache. Most likely, the assumptions are that the cache is empty. But it can sometimes NOT be. 


Discussion:

Resolving patch:
diff --git a/kuma/conftest.py b/kuma/conftest.py
--- a/kuma/conftest.py
+++ b/kuma/conftest.py
@@ -3,7 +3,6 @@
 import pytest
 import requests_mock
 from allauth.socialaccount.models import SocialAccount
-from django.conf import settings
 from django.contrib.auth.models import Group
 from django.core.cache import caches
 from django.urls import set_urlconf
@@ -15,6 +14,14 @@
 from kuma.wiki.models import Document, Revision
 
 
+@pytest.fixture(autouse=True)
+def clear_cache():
+    """
+    Before every pytest test function starts, it clears the cache.
+    """
+    caches["default"].clear()
+
+
 @pytest.fixture(autouse=True)
 def set_default_language():
     activate('en-US')
@@ -51,13 +58,6 @@ def reset_urlconf():
     set_urlconf(None)
 
 
-@pytest.fixture()
-def cleared_cacheback_cache():
-    caches[settings.CACHEBACK_CACHE_ALIAS].clear()
-    yield
-    caches[settings.CACHEBACK_CACHE_ALIAS].clear()
-
-
 class ConstanceConfigWrapper(object):
     """A Constance configuration wrapper to allow overriding the config."""
     _original_values = []
diff --git a/kuma/settings/common.py b/kuma/settings/common.py
--- a/kuma/settings/common.py
+++ b/kuma/settings/common.py
@@ -123,8 +123,6 @@ def parse_conn_max_age(value):
     }
 }
 
-CACHEBACK_CACHE_ALIAS = 'default'
-
 # Email
 vars().update(config('EMAIL_URL',
                      default='console://',


Test patch:
diff --git a/kuma/api/v1/tests/test_views.py b/kuma/api/v1/tests/test_views.py
--- a/kuma/api/v1/tests/test_views.py
+++ b/kuma/api/v1/tests/test_views.py
@@ -75,7 +75,7 @@ def test_doc_api_404(client, root_doc):
     assert_no_cache_header(response)
 
 
-def test_doc_api(client, trans_doc, cleared_cacheback_cache):
+def test_doc_api(client, trans_doc):
     """On success we get document details in a JSON response."""
     url = reverse('api.v1.doc', args=[trans_doc.locale, trans_doc.slug])
     response = client.get(url)
@@ -110,8 +110,7 @@ def test_doc_api(client, trans_doc, cleared_cacheback_cache):
     assert doc_data['lastModified'] == '2017-04-14T12:20:00'
 
 
-def test_doc_api_for_redirect_to_doc(client, root_doc, redirect_doc,
-                                     cleared_cacheback_cache):
+def test_doc_api_for_redirect_to_doc(client, root_doc, redirect_doc):
     """
     Test the document API when we're requesting data for a document that
     redirects to another document.
diff --git a/kuma/settings/testing.py b/kuma/settings/testing.py
--- a/kuma/settings/testing.py
+++ b/kuma/settings/testing.py
@@ -32,9 +32,6 @@
     },
 })
 
-# Use the local memory cache in tests, so that test cacheback jobs
-# will expire at the end of the test.
-CACHEBACK_CACHE_ALIAS = 'default'
 
 # Change the cache key prefix for tests, to avoid overwriting runtime.
 for cache_settings in CACHES.values():
@@ -88,3 +85,6 @@
 
 # Never enabled in tests.
 SENTRY_DSN = None
+
+# Because that's what all the tests presume.
+SITE_ID = 1
diff --git a/kuma/wiki/tests/test_jobs.py b/kuma/wiki/tests/test_jobs.py
--- a/kuma/wiki/tests/test_jobs.py
+++ b/kuma/wiki/tests/test_jobs.py
@@ -9,7 +9,7 @@
 
 
 @pytest.mark.parametrize("mode", ["maintenance-mode", "normal-mode"])
-def test_contributors(db, cleared_cacheback_cache, settings, wiki_user_3,
+def test_contributors(db, settings, wiki_user_3,
                       root_doc_with_mixed_contributors, mode):
     """
     Tests basic operation, ordering, caching, and handling of banned and
diff --git a/kuma/wiki/tests/test_views_edit.py b/kuma/wiki/tests/test_views_edit.py
--- a/kuma/wiki/tests/test_views_edit.py
+++ b/kuma/wiki/tests/test_views_edit.py
@@ -18,8 +18,7 @@ def test_edit_get(editor_client, root_doc):
 
 
 @pytest.mark.parametrize('method', ('GET', 'POST'))
-def test_edit_banned_ip_not_allowed(method, editor_client, root_doc,
-                                    cleared_cacheback_cache):
+def test_edit_banned_ip_not_allowed(method, editor_client, root_doc):
     ip = '127.0.0.1'
     IPBan.objects.create(ip=ip)
     url = reverse('wiki.edit', args=[root_doc.slug])