tests: disable cache decorators by default (fixes #22227)

It makes behavior of tests erratic as it incurs side effects between
tests and even inside of tests. For example, caching of the OU count for
short periods is not harmful for day to day use as creating new OUs are
rare events but during tests it makes reasoning about code behavior
really difficult.
This commit is contained in:
Benjamin Dauvergne 2018-03-02 12:05:09 +01:00
parent f52c7445e6
commit 22a885edfd
5 changed files with 19 additions and 55 deletions

View File

@ -191,6 +191,7 @@ default_settings = dict(
default=True,
definition='Set a random password on request to reset the password from the front-office'),
A2_ACCOUNTS_URL=Setting(default=None, definition='IdP has no account page, redirect to this one.'),
A2_CACHE_ENABLED=Setting(default=True, definition='Disable all cache decorators for testing purpose.'),
)
app_settings = AppSettings(default_settings)

View File

@ -131,6 +131,8 @@ class CacheDecoratorBase(object):
@wraps(func)
def f(*args, **kwargs):
try:
if not app_settings.A2_CACHE_ENABLED:
raise CacheUnusable
now = time.time()
key = self.key(*args, **kwargs)
value, tstamp = self.get(key)

View File

@ -6,6 +6,8 @@ if 'PASSWORD_HASHERS' not in locals():
PASSWORD_HASHERS = ('django.contrib.auth.hashers.UnsaltedMD5PasswordHasher',) + PASSWORD_HASHERS
A2_CACHE_ENABLED = False
LANGUAGE_CODE = 'en'
DATABASES = {
'default': {

View File

@ -3,6 +3,8 @@ import json
import urlparse
import base64
import pytest
from django.core import mail
from django.core.urlresolvers import reverse
from django.test import TestCase
@ -290,6 +292,10 @@ class UserProfileTests(TestCase):
class CacheTests(TestCase):
urls = 'cache_urls'
@pytest.fixture(autouse=True)
def cache_settings(self, settings):
settings.A2_CACHE_ENABLED = True
def test_cache_decorator_base(self):
import random
from authentic2.decorators import CacheDecoratorBase

View File

@ -294,15 +294,8 @@ def test_manager_one_ou(app, superuser, admin, simple_role, settings):
# test user listing ou search
response = response.click(href='users')
assert len(response.form.fields['search-ou']) == 1
assert 'search-ou' not in response.form.fields
assert len(response.form.fields['search-text']) == 1
field = response.form['search-ou']
options = field.options
assert len(options) == 3
for key, checked, label in options:
assert not checked or key == 'all'
assert 'all' in [o[0] for o in options]
assert 'none' in [o[0] for o in options]
# verify table shown
q = response.pyquery.remove_namespaces()
assert len(q('table tbody tr')) == 2
@ -310,61 +303,29 @@ def test_manager_one_ou(app, superuser, admin, simple_role, settings):
# test user's role page
response = app.get('/manage/users/%d/roles/' % admin.pk)
assert len(response.form.fields['search-ou']) == 1
field = response.form['search-ou']
options = field.options
assert len(options) == 3
for key, checked, label in options:
assert not checked or key == str(get_default_ou().pk)
form = response.forms['search-form']
assert 'search-ou' not in form.fields
q = response.pyquery.remove_namespaces()
assert len(q('table tbody tr')) == 1
assert q('table tbody tr').text() == u'simple role'
response.form.set('search-ou', 'all')
response = response.form.submit()
q = response.pyquery.remove_namespaces()
assert len(q('table tbody tr')) == 1
assert q('table tbody tr').text() == 'None'
form = response.forms['search-form']
form.set('search-internals', True)
response = form.submit()
q = response.pyquery.remove_namespaces()
assert len(q('table tbody tr')) == 4
assert len(q('table tbody tr')) == 5
# admin enroled only in the Manager role, other roles are inherited
assert len(q('table tbody tr td.via')) == 4
assert len(q('table tbody tr td.via:empty')) == 1
assert len(q('table tbody tr td.via')) == 5
assert len(q('table tbody tr td.via:empty')) == 2
for elt in q('table tbody td.name a'):
assert 'Manager' in elt.text
form = response.forms['search-form']
form.set('search-ou', 'none')
form.set('search-internals', True)
response = form.submit()
q = response.pyquery.remove_namespaces()
assert len(q('table tbody tr')) == 4
for elt in q('table tbody td.name a'):
assert 'Manager' in elt.text
assert 'Manager' in elt.text or elt.text == 'simple role'
# test role listing
response = app.get('/manage/roles/')
assert len(response.form.fields['search-ou']) == 1
field = response.form['search-ou']
options = field.options
assert len(options) == 3
for key, checked, label in options:
assert not checked or key == 'all'
assert 'search-ou' not in response.form.fields
q = response.pyquery.remove_namespaces()
assert len(q('table tbody tr')) == 1
assert q('table tbody td.name').text() == u'simple role'
response.form.set('search-ou', 'all')
response = response.form.submit()
q = response.pyquery.remove_namespaces()
assert len(q('table tbody tr')) == 1
assert q('table tbody td.name').text() == u'simple role'
response.form.set('search-ou', 'all')
response.form.set('search-internals', True)
response = response.form.submit()
q = response.pyquery.remove_namespaces()
@ -372,14 +333,6 @@ def test_manager_one_ou(app, superuser, admin, simple_role, settings):
for elt in q('table tbody td.name a'):
assert 'Manager' in elt.text or elt.text == u'simple role'
response.form.set('search-ou', 'none')
response.form.set('search-internals', True)
response = response.form.submit()
q = response.pyquery.remove_namespaces()
assert len(q('table tbody tr')) == 4
for elt in q('table tbody td.name a'):
assert 'Manager' in elt.text
test_user_listing(admin)
app.session.flush()
test_user_listing(superuser)