2017-04-03 11:05:54 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
2018-07-09 23:22:26 +02:00
|
|
|
from django.urls import reverse
|
2017-09-04 00:25:45 +02:00
|
|
|
from django_redis import get_redis_connection
|
2017-04-03 11:05:54 +02:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.test import APIClient
|
|
|
|
|
|
|
|
from openslides.agenda.models import Item
|
|
|
|
from openslides.core.config import config
|
|
|
|
from openslides.topics.models import Topic
|
2017-09-04 00:25:45 +02:00
|
|
|
from openslides.utils.test import TestCase
|
2017-04-03 11:05:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestDBQueries(TestCase):
|
|
|
|
"""
|
|
|
|
Tests that receiving elements only need the required db queries.
|
|
|
|
|
|
|
|
Therefore in setup some topics are created and received with different
|
|
|
|
user accounts.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
config['general_system_enable_anonymous'] = True
|
2017-08-22 14:17:20 +02:00
|
|
|
config.save_default_values()
|
2017-04-03 11:05:54 +02:00
|
|
|
for index in range(10):
|
|
|
|
Topic.objects.create(title='topic-{}'.format(index))
|
|
|
|
|
|
|
|
def test_admin(self):
|
|
|
|
"""
|
|
|
|
Tests that only the following db queries are done:
|
2017-09-04 00:25:45 +02:00
|
|
|
* 7 requests to get the session an the request user with its permissions,
|
|
|
|
* 1 requests to get the list of all topics,
|
2017-04-03 11:05:54 +02:00
|
|
|
* 1 request to get attachments,
|
|
|
|
* 1 request to get the agenda item
|
|
|
|
"""
|
|
|
|
self.client.force_login(get_user_model().objects.get(pk=1))
|
2017-09-04 00:25:45 +02:00
|
|
|
get_redis_connection('default').flushall()
|
|
|
|
with self.assertNumQueries(10):
|
2017-04-03 11:05:54 +02:00
|
|
|
self.client.get(reverse('topic-list'))
|
|
|
|
|
|
|
|
def test_anonymous(self):
|
|
|
|
"""
|
|
|
|
Tests that only the following db queries are done:
|
|
|
|
* 3 requests to get the permission for anonymous,
|
2017-09-04 00:25:45 +02:00
|
|
|
* 1 requests to get the list of all topics,
|
2017-04-03 11:05:54 +02:00
|
|
|
* 1 request to get attachments,
|
|
|
|
* 1 request to get the agenda item,
|
|
|
|
"""
|
2017-09-04 00:25:45 +02:00
|
|
|
get_redis_connection('default').flushall()
|
|
|
|
with self.assertNumQueries(6):
|
2017-04-03 11:05:54 +02:00
|
|
|
self.client.get(reverse('topic-list'))
|
|
|
|
|
|
|
|
|
|
|
|
class TopicCreate(TestCase):
|
|
|
|
"""
|
|
|
|
Tests creation of new topics.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
self.client.login(
|
|
|
|
username='admin',
|
|
|
|
password='admin',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_simple_create(self):
|
|
|
|
response = self.client.post(
|
|
|
|
reverse('topic-list'),
|
|
|
|
{'title': 'test_title_ahyo1uifoo9Aiph2av5a',
|
|
|
|
'text': 'test_text_chu9Uevoo5choo0Xithe'})
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
|
|
|
topic = Topic.objects.get()
|
|
|
|
self.assertEqual(topic.title, 'test_title_ahyo1uifoo9Aiph2av5a')
|
|
|
|
self.assertEqual(topic.text, 'test_text_chu9Uevoo5choo0Xithe')
|
|
|
|
self.assertEqual(Item.objects.get(), topic.agenda_item)
|