tests: add test of CSV export of users (#13587)
This commit is contained in:
parent
cc101ea394
commit
7a6ce74d94
|
@ -4,10 +4,10 @@ from authentic2.models import Attribute, AttributeValue
|
|||
|
||||
import threading
|
||||
|
||||
from utils import skipif_no_partial_index
|
||||
from utils import skipif_sqlite
|
||||
|
||||
|
||||
@skipif_no_partial_index
|
||||
@skipif_sqlite
|
||||
def test_attribute_value_uniqueness(migrations, transactional_db, simple_user, concurrency):
|
||||
from django.db.transaction import set_autocommit
|
||||
# disabled default attributes
|
||||
|
|
|
@ -1,8 +1,15 @@
|
|||
import csv
|
||||
|
||||
from django.core.urlresolvers import reverse
|
||||
|
||||
from authentic2.models import Attribute
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
from authentic2.custom_user.models import User
|
||||
from authentic2.models import Attribute, AttributeValue
|
||||
from authentic2.a2_rbac.utils import get_default_ou
|
||||
from utils import login, get_link_from_mail
|
||||
|
||||
from utils import login, get_link_from_mail, skipif_sqlite
|
||||
|
||||
|
||||
|
||||
def visible_users(response):
|
||||
|
@ -89,3 +96,38 @@ def test_search_by_attribute(app, simple_user, admin):
|
|||
|
||||
# now we see only simple_user
|
||||
assert visible_users(response) == {simple_user.username}
|
||||
|
||||
|
||||
@skipif_sqlite
|
||||
def test_export_csv(settings, app, superuser, django_assert_num_queries):
|
||||
AT_COUNT = 30
|
||||
USER_COUNT = 2000
|
||||
DEFAULT_BATCH_SIZE = 1000
|
||||
|
||||
ats = [Attribute(name='at%s' % i, label='At%s' % i, kind='string') for i in range(AT_COUNT)]
|
||||
Attribute.objects.bulk_create(ats)
|
||||
|
||||
ats = list(Attribute.objects.all())
|
||||
users = [User(username='user%s' % i) for i in range(USER_COUNT)]
|
||||
User.objects.bulk_create(users)
|
||||
users = list(User.objects.filter(username__startswith='user'))
|
||||
|
||||
user_ct = ContentType.objects.get_for_model(User)
|
||||
atvs = []
|
||||
for i in range(USER_COUNT):
|
||||
atvs.extend([AttributeValue(
|
||||
owner=users[i], attribute=ats[j], content='value-%s-%s' % (i, j)) for j in range(AT_COUNT)])
|
||||
AttributeValue.objects.bulk_create(atvs)
|
||||
|
||||
response = login(app, superuser, reverse('a2-manager-users'))
|
||||
settings.A2_CACHE_ENABLED = True
|
||||
user_count = User.objects.count()
|
||||
# queries should be batched to keep prefetching working without
|
||||
# overspending memory for the queryset cache, 4 queries by batches
|
||||
num_queries = 9 + 4 * (user_count / DEFAULT_BATCH_SIZE + bool(user_count % DEFAULT_BATCH_SIZE))
|
||||
with django_assert_num_queries(num_queries):
|
||||
response = response.click('CSV')
|
||||
table = list(csv.reader(response.content.splitlines()))
|
||||
assert len(table) == (user_count + 1)
|
||||
assert len(table[0]) == (15 + AT_COUNT)
|
||||
|
||||
|
|
|
@ -11,8 +11,9 @@ from django.conf import settings
|
|||
|
||||
from authentic2 import utils
|
||||
|
||||
skipif_no_partial_index = pytest.mark.skipif('sqlite' in settings.DATABASES['default']['ENGINE'],
|
||||
reason='partial indexes do not work with sqlite')
|
||||
|
||||
skipif_sqlite = pytest.mark.skipif('sqlite' in settings.DATABASES['default']['ENGINE'],
|
||||
reason='this test does not work with sqlite')
|
||||
|
||||
|
||||
def login(app, user, path=None, password=None):
|
||||
|
|
Loading…
Reference in New Issue