From e644e7468bd3d3096728c512279edf46e1758e8e Mon Sep 17 00:00:00 2001
From: Paul Marillonnet <pmarillonnet@entrouvert.com>
Date: Fri, 17 Apr 2020 10:46:28 +0200
Subject: [PATCH] tests: add cache clearance autouse fixture (#41813)

---
 tests/conftest.py      |  6 ++++++
 tests/test_idp_oidc.py | 13 -------------
 2 files changed, 6 insertions(+), 13 deletions(-)

diff --git a/tests/conftest.py b/tests/conftest.py
index 179c28c28..1a88e4017 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -22,6 +22,7 @@ import mock
 import django_webtest
 
 from django.contrib.auth import get_user_model
+from django.core.cache import cache
 from django_rbac.utils import get_ou_model, get_role_model
 from django.conf import settings
 from django.utils.six.moves.urllib import parse as urlparse
@@ -381,3 +382,8 @@ def service(db):
         ou=get_default_ou(),
         slug='service',
         name='Service')
+
+
+@pytest.fixture(autouse=True)
+def clear_cache():
+    cache.clear()
diff --git a/tests/test_idp_oidc.py b/tests/test_idp_oidc.py
index baf2399fc..6eeb58451 100644
--- a/tests/test_idp_oidc.py
+++ b/tests/test_idp_oidc.py
@@ -25,7 +25,6 @@ from jwcrypto.jwk import JWKSet, JWK
 
 import utils
 
-from django.core.cache import cache
 from django.core.urlresolvers import reverse
 from django.core.files import File
 from django.db import connection
@@ -1290,7 +1289,6 @@ def test_filter_api_users(app, oidc_client, admin, simple_user, role_random):
 
 
 def test_credentials_grant(app, oidc_client, admin, simple_user):
-    cache.clear()
     oidc_client.authorization_flow = OIDCClient.FLOW_RESOURCE_OWNER_CRED
     oidc_client.scope = 'openid'
     oidc_client.save()
@@ -1342,7 +1340,6 @@ def test_credentials_grant_ratelimitation_invalid_client(
         app, oidc_client, admin, simple_user, oidc_settings, freezer):
     freezer.move_to('2020-01-01')
 
-    cache.clear()
     oidc_client.authorization_flow = OIDCClient.FLOW_RESOURCE_OWNER_CRED
     oidc_client.save()
     token_url = make_url('oidc-token')
@@ -1366,7 +1363,6 @@ def test_credentials_grant_ratelimitation_valid_client(
         app, oidc_client, admin, simple_user, oidc_settings, freezer):
     freezer.move_to('2020-01-01')
 
-    cache.clear()
     oidc_client.authorization_flow = OIDCClient.FLOW_RESOURCE_OWNER_CRED
     oidc_client.save()
     token_url = make_url('oidc-token')
@@ -1388,7 +1384,6 @@ def test_credentials_grant_retrytimout(
         app, oidc_client, admin, simple_user, settings, freezer):
     freezer.move_to('2020-01-01')
 
-    cache.clear()
     settings.A2_LOGIN_EXPONENTIAL_RETRY_TIMEOUT_DURATION = 2
     oidc_client.authorization_flow = OIDCClient.FLOW_RESOURCE_OWNER_CRED
     oidc_client.save()
@@ -1419,7 +1414,6 @@ def test_credentials_grant_retrytimout(
 
 def test_credentials_grant_invalid_flow(
         app, oidc_client, admin, simple_user, settings):
-    cache.clear()
     params = {
         'client_id': oidc_client.client_id,
         'client_secret': oidc_client.client_secret,
@@ -1435,7 +1429,6 @@ def test_credentials_grant_invalid_flow(
 
 def test_credentials_grant_invalid_client(
         app, oidc_client, admin, simple_user, settings):
-    cache.clear()
     oidc_client.authorization_flow = OIDCClient.FLOW_RESOURCE_OWNER_CRED
     oidc_client.save()
     params = {
@@ -1453,7 +1446,6 @@ def test_credentials_grant_invalid_client(
 
 def test_credentials_grant_unauthz_client(
         app, oidc_client, admin, simple_user, settings):
-    cache.clear()
     params = {
         'client_id': oidc_client.client_id,
         'client_secret': oidc_client.client_secret,
@@ -1469,7 +1461,6 @@ def test_credentials_grant_unauthz_client(
 
 def test_credentials_grant_invalid_content_type(
         app, oidc_client, admin, simple_user, settings):
-    cache.clear()
     oidc_client.authorization_flow = OIDCClient.FLOW_RESOURCE_OWNER_CRED
     oidc_client.save()
     params = {
@@ -1490,7 +1481,6 @@ def test_credentials_grant_invalid_content_type(
 
 def test_credentials_grant_ou_selection_simple(
         app, oidc_client, admin, user_ou1, user_ou2, ou1, ou2, settings):
-    cache.clear()
     oidc_client.authorization_flow = OIDCClient.FLOW_RESOURCE_OWNER_CRED
     oidc_client.save()
     params = {
@@ -1511,7 +1501,6 @@ def test_credentials_grant_ou_selection_simple(
 
 def test_credentials_grant_ou_selection_username_not_unique(
         app, oidc_client, admin, user_ou1, admin_ou2, ou1, ou2, settings):
-    cache.clear()
     settings.A2_USERNAME_IS_UNIQUE = False
     oidc_client.authorization_flow = OIDCClient.FLOW_RESOURCE_OWNER_CRED
     oidc_client.save()
@@ -1539,7 +1528,6 @@ def test_credentials_grant_ou_selection_username_not_unique(
 
 def test_credentials_grant_ou_selection_username_not_unique_wrong_ou(
         app, oidc_client, admin, user_ou1, admin_ou2, ou1, ou2, settings):
-    cache.clear()
     settings.A2_USERNAME_IS_UNIQUE = False
     oidc_client.authorization_flow = OIDCClient.FLOW_RESOURCE_OWNER_CRED
     oidc_client.save()
@@ -1562,7 +1550,6 @@ def test_credentials_grant_ou_selection_username_not_unique_wrong_ou(
 
 def test_credentials_grant_ou_selection_invalid_ou(
         app, oidc_client, admin, user_ou1, settings):
-    cache.clear()
     params = {
         'client_id': oidc_client.client_id,
         'client_secret': oidc_client.client_secret,
-- 
2.26.1

