2015-02-18 01:45:39 +01:00
|
|
|
import json
|
2016-06-02 12:47:01 +02:00
|
|
|
from unittest.mock import patch
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from rest_framework import status
|
2015-06-29 12:08:15 +02:00
|
|
|
from rest_framework.test import APIClient
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2015-06-18 21:48:20 +02:00
|
|
|
from openslides import __version__ as version
|
2016-06-02 12:47:01 +02:00
|
|
|
from openslides.core.config import ConfigHandler, ConfigVariable
|
2015-02-18 01:45:39 +01:00
|
|
|
from openslides.core.models import CustomSlide, Projector
|
2015-06-29 12:08:15 +02:00
|
|
|
from openslides.utils.rest_api import ValidationError
|
2015-06-16 10:37:23 +02:00
|
|
|
from openslides.utils.test import TestCase
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
config = ConfigHandler()
|
|
|
|
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
class ProjectorAPI(TestCase):
|
|
|
|
"""
|
|
|
|
Tests requests from the anonymous user.
|
|
|
|
"""
|
|
|
|
def test_slide_on_default_projector(self):
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
customslide = CustomSlide.objects.create(title='title_que1olaish5Wei7que6i', text='text_aishah8Eh7eQuie5ooji')
|
|
|
|
default_projector = Projector.objects.get(pk=1)
|
2015-09-06 13:28:25 +02:00
|
|
|
default_projector.config = {
|
|
|
|
'aae4a07b26534cfb9af4232f361dce73': {'name': 'core/customslide', 'id': customslide.id}}
|
2015-02-18 01:45:39 +01:00
|
|
|
default_projector.save()
|
|
|
|
|
|
|
|
response = self.client.get(reverse('projector-detail', args=['1']))
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(json.loads(response.content.decode()), {
|
2015-06-17 09:45:00 +02:00
|
|
|
'id': 1,
|
2015-09-06 13:28:25 +02:00
|
|
|
'elements': {
|
|
|
|
'aae4a07b26534cfb9af4232f361dce73':
|
|
|
|
{'id': customslide.id,
|
2015-09-05 23:32:10 +02:00
|
|
|
'uuid': 'aae4a07b26534cfb9af4232f361dce73',
|
2016-02-27 20:25:06 +01:00
|
|
|
'name': 'core/customslide'}},
|
2015-09-14 23:16:31 +02:00
|
|
|
'scale': 0,
|
|
|
|
'scroll': 0})
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
def test_invalid_slide_on_default_projector(self):
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
default_projector = Projector.objects.get(pk=1)
|
2015-09-06 13:28:25 +02:00
|
|
|
default_projector.config = {
|
|
|
|
'fc6ef43b624043068c8e6e7a86c5a1b0': {'name': 'invalid_slide'}}
|
2015-02-18 01:45:39 +01:00
|
|
|
default_projector.save()
|
|
|
|
|
|
|
|
response = self.client.get(reverse('projector-detail', args=['1']))
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(json.loads(response.content.decode()), {
|
2015-06-17 09:45:00 +02:00
|
|
|
'id': 1,
|
2015-09-06 13:28:25 +02:00
|
|
|
'elements': {
|
|
|
|
'fc6ef43b624043068c8e6e7a86c5a1b0':
|
|
|
|
{'name': 'invalid_slide',
|
2015-09-05 23:32:10 +02:00
|
|
|
'uuid': 'fc6ef43b624043068c8e6e7a86c5a1b0',
|
2015-09-14 23:16:31 +02:00
|
|
|
'error': 'Projector element does not exist.'}},
|
|
|
|
'scale': 0,
|
|
|
|
'scroll': 0})
|
2015-06-18 21:48:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class VersionView(TestCase):
|
|
|
|
"""
|
|
|
|
Tests the version info view.
|
|
|
|
"""
|
|
|
|
def test_get(self):
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.get(reverse('core_version'))
|
|
|
|
self.assertEqual(json.loads(response.content.decode()), {
|
|
|
|
'openslides_version': version,
|
|
|
|
'plugins': [
|
2016-02-16 22:51:38 +01:00
|
|
|
{'verbose_name': 'OpenSlides Test Plugin',
|
|
|
|
'description': 'This is a test plugin for OpenSlides.',
|
2015-06-18 21:48:20 +02:00
|
|
|
'version': 'unknown'}]})
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
@patch('openslides.core.config.config', config)
|
|
|
|
@patch('openslides.core.views.config', config)
|
2015-06-29 12:08:15 +02:00
|
|
|
class ConfigViewSet(TestCase):
|
|
|
|
"""
|
|
|
|
Tests requests to deal with config variables.
|
|
|
|
"""
|
2016-06-02 12:47:01 +02:00
|
|
|
def setUp(self):
|
|
|
|
config.update_config_varialbes(set_simple_config_view_integration_config_test())
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# Reset the config variables
|
|
|
|
config.config_variables = {}
|
|
|
|
|
2015-06-29 12:08:15 +02:00
|
|
|
def test_retrieve(self):
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
config['test_var_aeW3Quahkah1phahCheo'] = 'test_value_Oovoojieme7eephaed2A'
|
|
|
|
response = self.client.get(reverse('config-detail', args=['test_var_aeW3Quahkah1phahCheo']))
|
|
|
|
self.assertEqual(
|
|
|
|
response.data,
|
|
|
|
{'key': 'test_var_aeW3Quahkah1phahCheo',
|
|
|
|
'value': 'test_value_Oovoojieme7eephaed2A'})
|
|
|
|
|
|
|
|
def test_update(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.put(
|
|
|
|
reverse('config-detail', args=['test_var_Xeiizi7ooH8Thuk5aida']),
|
|
|
|
{'value': 'test_value_Phohx3oopeichaiTheiw'})
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(config['test_var_Xeiizi7ooH8Thuk5aida'], 'test_value_Phohx3oopeichaiTheiw')
|
|
|
|
|
|
|
|
def test_update_wrong_datatype(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.put(
|
|
|
|
reverse('config-detail', args=['test_var_ohhii4iavoh5Phoh5ahg']),
|
|
|
|
{'value': 'test_value_string'})
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
|
|
self.assertEqual(response.data, {'detail': "Wrong datatype. Expected <class 'int'>, got <class 'str'>."})
|
|
|
|
|
2015-06-29 14:17:05 +02:00
|
|
|
def test_update_wrong_datatype_that_can_be_converted(self):
|
|
|
|
"""
|
|
|
|
Try to send a string that can be converted to an integer to an integer
|
|
|
|
field.
|
|
|
|
"""
|
|
|
|
self.client = APIClient()
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.put(
|
|
|
|
reverse('config-detail', args=['test_var_ohhii4iavoh5Phoh5ahg']),
|
|
|
|
{'value': '12345'})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2015-06-29 12:08:15 +02:00
|
|
|
def test_update_good_choice(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.put(
|
|
|
|
reverse('config-detail', args=['test_var_wei0Rei9ahzooSohK1ph']),
|
|
|
|
{'value': 'key_2_yahb2ain1aeZ1lea1Pei'})
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(config['test_var_wei0Rei9ahzooSohK1ph'], 'key_2_yahb2ain1aeZ1lea1Pei')
|
|
|
|
|
|
|
|
def test_update_bad_choice(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.put(
|
|
|
|
reverse('config-detail', args=['test_var_wei0Rei9ahzooSohK1ph']),
|
|
|
|
{'value': 'test_value_bad_string'})
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
|
|
self.assertEqual(response.data, {'detail': 'Invalid input. Choice does not match.'})
|
|
|
|
|
|
|
|
def test_update_validator_ok(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.put(
|
|
|
|
reverse('config-detail', args=['test_var_Hi7Oje8Oith7goopeeng']),
|
|
|
|
{'value': 'valid_string'})
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
|
|
self.assertEqual(config['test_var_Hi7Oje8Oith7goopeeng'], 'valid_string')
|
|
|
|
|
|
|
|
def test_update_validator_invalid(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.put(
|
|
|
|
reverse('config-detail', args=['test_var_Hi7Oje8Oith7goopeeng']),
|
|
|
|
{'value': 'invalid_string'})
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
|
|
self.assertEqual(response.data, {'detail': 'Invalid input.'})
|
|
|
|
|
2016-01-13 15:04:47 +01:00
|
|
|
def test_update_only_with_key(self):
|
|
|
|
self.client = APIClient()
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.put(
|
|
|
|
reverse('config-detail', args=['test_var_Xeiizi7ooH8Thuk5aida']))
|
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
|
|
|
self.assertEqual(response.data, {'detail': 'Invalid input. Config value is missing.'})
|
|
|
|
|
2016-02-14 21:38:26 +01:00
|
|
|
def test_metadata_with_hidden(self):
|
|
|
|
self.client.login(username='admin', password='admin')
|
|
|
|
response = self.client.options(reverse('config-list'))
|
|
|
|
filter_obj = filter(
|
|
|
|
lambda item: item['key'] == 'test_var_pud2zah2teeNaiP7IoNa',
|
|
|
|
response.data['config_groups'][0]['subgroups'][0]['items'])
|
|
|
|
self.assertEqual(len(list(filter_obj)), 0)
|
|
|
|
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
def validator_for_testing(value):
|
|
|
|
"""
|
|
|
|
Validator for testing.
|
|
|
|
"""
|
|
|
|
if value == 'invalid_string':
|
|
|
|
raise ValidationError({'detail': 'Invalid input.'})
|
|
|
|
|
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
def set_simple_config_view_integration_config_test():
|
2015-06-29 12:08:15 +02:00
|
|
|
"""
|
|
|
|
Sets a simple config view with some config variables but without
|
|
|
|
grouping.
|
|
|
|
"""
|
|
|
|
yield ConfigVariable(
|
|
|
|
name='test_var_aeW3Quahkah1phahCheo',
|
|
|
|
default_value=None,
|
|
|
|
label='test_label_aeNahsheu8phahk8taYo')
|
|
|
|
|
|
|
|
yield ConfigVariable(
|
|
|
|
name='test_var_Xeiizi7ooH8Thuk5aida',
|
|
|
|
default_value='')
|
|
|
|
|
|
|
|
yield ConfigVariable(
|
|
|
|
name='test_var_ohhii4iavoh5Phoh5ahg',
|
|
|
|
default_value=0,
|
|
|
|
input_type='integer')
|
|
|
|
|
|
|
|
yield ConfigVariable(
|
|
|
|
name='test_var_wei0Rei9ahzooSohK1ph',
|
|
|
|
default_value='key_1_Queit2juchoocos2Vugh',
|
|
|
|
input_type='choice',
|
|
|
|
choices=(
|
|
|
|
{'value': 'key_1_Queit2juchoocos2Vugh', 'display_name': 'label_1_Queit2juchoocos2Vugh'},
|
|
|
|
{'value': 'key_2_yahb2ain1aeZ1lea1Pei', 'display_name': 'label_2_yahb2ain1aeZ1lea1Pei'}))
|
|
|
|
|
|
|
|
yield ConfigVariable(
|
|
|
|
name='test_var_Hi7Oje8Oith7goopeeng',
|
|
|
|
default_value='',
|
|
|
|
validators=(validator_for_testing,))
|
2016-02-14 21:38:26 +01:00
|
|
|
|
|
|
|
yield ConfigVariable(
|
|
|
|
name='test_var_pud2zah2teeNaiP7IoNa',
|
|
|
|
default_value=None,
|
|
|
|
label='test_label_xaing7eefaePheePhei6',
|
|
|
|
hidden=True)
|