2015-02-18 01:45:39 +01:00
|
|
|
import json
|
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
import pytest
|
2018-07-09 23:22:26 +02:00
|
|
|
from django.urls import reverse
|
2015-02-18 01:45:39 +01:00
|
|
|
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
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
from openslides import __license__ as license, __url__ as url, __version__ as version
|
2016-05-29 08:29:14 +02:00
|
|
|
from openslides.core.config import ConfigVariable, config
|
2019-01-19 10:37:05 +01:00
|
|
|
from openslides.core.models import Projector
|
2015-06-29 12:08:15 +02:00
|
|
|
from openslides.utils.rest_api import ValidationError
|
2019-10-18 14:18:49 +02:00
|
|
|
from tests.test_case import TestCase
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
@pytest.mark.django_db(transaction=False)
|
|
|
|
def test_invalid_element_non_existing_slide(client):
|
|
|
|
client.login(username="admin", password="admin")
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
response = client.put(
|
|
|
|
reverse("projector-detail", args=["1"]),
|
|
|
|
{"elements": [{"name": "invalid_slide_name", "id": 1}]},
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2019-01-18 19:11:22 +01:00
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
assert response.status_code == 400
|
2019-01-18 19:11:22 +01:00
|
|
|
|
2015-06-18 21:48:20 +02:00
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
@pytest.mark.django_db(transaction=False)
|
|
|
|
def test_invalid_element_no_name_attribute(client):
|
|
|
|
client.login(username="admin", password="admin")
|
2019-01-18 19:11:22 +01:00
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
response = client.put(
|
|
|
|
reverse("projector-detail", args=["1"]),
|
|
|
|
{"elements": [{"id": 1}]},
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2019-01-18 19:11:22 +01:00
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
assert response.status_code == 400
|
2019-01-18 19:11:22 +01:00
|
|
|
|
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
@pytest.mark.django_db(transaction=False)
|
|
|
|
def test_invalid_element_not_a_inner_dict(client):
|
|
|
|
client.login(username="admin", password="admin")
|
|
|
|
|
|
|
|
response = client.put(
|
|
|
|
reverse("projector-detail", args=["1"]),
|
|
|
|
{"elements": ["not a dict"]},
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db(transaction=False)
|
|
|
|
def test_invalid_element_a_list(client):
|
|
|
|
client.login(username="admin", password="admin")
|
|
|
|
|
|
|
|
response = client.put(
|
|
|
|
reverse("projector-detail", args=["1"]),
|
|
|
|
{"elements": {"name": "invalid_slide_name", "id": 1}},
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
|
|
|
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.django_db(transaction=False)
|
|
|
|
def test_project_view(client):
|
|
|
|
client.login(username="admin", password="admin")
|
|
|
|
projector = Projector.objects.get(pk=1)
|
|
|
|
projector.elements_history = [[{"name": "topics/topic", "id": 3}]]
|
|
|
|
projector.save()
|
2019-01-18 19:11:22 +01:00
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
response = client.post(
|
|
|
|
reverse("projector-project", args=["1"]),
|
|
|
|
{
|
|
|
|
"append_to_history": [{"name": "topics/topic", "id": 1}],
|
|
|
|
"elements": [{"name": "topics/topic", "id": 2}],
|
2019-01-24 16:25:50 +01:00
|
|
|
"preview": [{"name": "topics/topic", "id": 3}],
|
2019-01-19 10:37:05 +01:00
|
|
|
},
|
|
|
|
content_type="application/json",
|
|
|
|
)
|
2019-01-18 19:11:22 +01:00
|
|
|
|
2019-01-19 10:37:05 +01:00
|
|
|
projector.refresh_from_db()
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert projector.elements == [{"name": "topics/topic", "id": 2}]
|
|
|
|
assert projector.elements_history == [
|
|
|
|
[{"name": "topics/topic", "id": 3}],
|
|
|
|
[{"name": "topics/topic", "id": 1}],
|
|
|
|
]
|
2019-01-24 16:25:50 +01:00
|
|
|
assert projector.elements_preview == [{"name": "topics/topic", "id": 3}]
|
2019-01-18 19:11:22 +01:00
|
|
|
|
2015-06-18 21:48:20 +02:00
|
|
|
|
2019-11-01 09:48:58 +01:00
|
|
|
@pytest.mark.django_db(transaction=False)
|
2019-11-05 08:28:10 +01:00
|
|
|
def test_set_reference_projector(client):
|
2019-11-01 09:48:58 +01:00
|
|
|
client.login(username="admin", password="admin")
|
|
|
|
Projector.objects.create(name="test_name_rePaODETymV4eFM3aOBD")
|
|
|
|
reference_projector = Projector.objects.create(
|
|
|
|
name="test_name_S2vXmumTMKyT4yjgEoyF"
|
|
|
|
)
|
|
|
|
|
|
|
|
response = client.post(
|
2019-11-05 08:28:10 +01:00
|
|
|
reverse("projector-set-reference-projector", args=[reference_projector.pk])
|
2019-11-01 09:48:58 +01:00
|
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
for projector in Projector.objects.all():
|
|
|
|
assert projector.reference_projector_id == reference_projector.id
|
|
|
|
|
|
|
|
|
2019-01-19 17:42:18 +01:00
|
|
|
@pytest.mark.django_db(transaction=False)
|
|
|
|
def test_get(client):
|
|
|
|
client.login(username="admin", password="admin")
|
|
|
|
response = client.get(reverse("core_version"))
|
|
|
|
values = json.loads(response.content.decode())
|
|
|
|
assert values["openslides_version"] == version
|
|
|
|
assert values["openslides_license"] == license
|
|
|
|
assert values["openslides_url"] == url
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigViewSet(TestCase):
|
|
|
|
"""
|
|
|
|
Tests requests to deal with config variables.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-06-02 12:47:01 +02:00
|
|
|
def setUp(self):
|
2016-05-29 08:29:14 +02:00
|
|
|
# Save the old value of the config object and add the test values
|
|
|
|
# TODO: Can be changed to setUpClass when Django 1.8 is no longer supported
|
|
|
|
self._config_values = config.config_variables.copy()
|
2018-07-09 23:22:26 +02:00
|
|
|
config.key_to_id = None
|
2016-06-08 22:18:35 +02:00
|
|
|
config.update_config_variables(set_simple_config_view_integration_config_test())
|
2017-08-22 14:17:20 +02:00
|
|
|
config.save_default_values()
|
2016-06-02 12:47:01 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# Reset the config variables
|
2016-05-29 08:29:14 +02:00
|
|
|
config.config_variables = self._config_values
|
2017-08-22 14:17:20 +02:00
|
|
|
super().tearDown()
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
def test_update(self):
|
|
|
|
self.client = APIClient()
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
2015-06-29 12:08:15 +02:00
|
|
|
response = self.client.put(
|
2019-01-06 16:22:33 +01:00
|
|
|
reverse("config-detail", args=["test_var_Xeiizi7ooH8Thuk5aida"]),
|
|
|
|
{"value": "test_value_Phohx3oopeichaiTheiw"},
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(
|
|
|
|
config["test_var_Xeiizi7ooH8Thuk5aida"], "test_value_Phohx3oopeichaiTheiw"
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
def test_update_wrong_datatype(self):
|
|
|
|
self.client = APIClient()
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
2015-06-29 12:08:15 +02:00
|
|
|
response = self.client.put(
|
2019-01-06 16:22:33 +01:00
|
|
|
reverse("config-detail", args=["test_var_ohhii4iavoh5Phoh5ahg"]),
|
|
|
|
{"value": "test_value_string"},
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(
|
|
|
|
response.data,
|
|
|
|
{"detail": "Wrong datatype. Expected <class 'int'>, got <class 'str'>."},
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
|
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()
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
2015-06-29 14:17:05 +02:00
|
|
|
response = self.client.put(
|
2019-01-06 16:22:33 +01:00
|
|
|
reverse("config-detail", args=["test_var_ohhii4iavoh5Phoh5ahg"]),
|
|
|
|
{"value": "12345"},
|
|
|
|
)
|
2015-06-29 14:17:05 +02:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2015-06-29 12:08:15 +02:00
|
|
|
def test_update_good_choice(self):
|
|
|
|
self.client = APIClient()
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
2015-06-29 12:08:15 +02:00
|
|
|
response = self.client.put(
|
2019-01-06 16:22:33 +01:00
|
|
|
reverse("config-detail", args=["test_var_wei0Rei9ahzooSohK1ph"]),
|
|
|
|
{"value": "key_2_yahb2ain1aeZ1lea1Pei"},
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(
|
|
|
|
config["test_var_wei0Rei9ahzooSohK1ph"], "key_2_yahb2ain1aeZ1lea1Pei"
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
def test_update_bad_choice(self):
|
|
|
|
self.client = APIClient()
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
2015-06-29 12:08:15 +02:00
|
|
|
response = self.client.put(
|
2019-01-06 16:22:33 +01:00
|
|
|
reverse("config-detail", args=["test_var_wei0Rei9ahzooSohK1ph"]),
|
|
|
|
{"value": "test_value_bad_string"},
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(
|
|
|
|
response.data, {"detail": "Invalid input. Choice does not match."}
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
def test_update_validator_ok(self):
|
|
|
|
self.client = APIClient()
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
2015-06-29 12:08:15 +02:00
|
|
|
response = self.client.put(
|
2019-01-06 16:22:33 +01:00
|
|
|
reverse("config-detail", args=["test_var_Hi7Oje8Oith7goopeeng"]),
|
|
|
|
{"value": "valid_string"},
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(config["test_var_Hi7Oje8Oith7goopeeng"], "valid_string")
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
def test_update_validator_invalid(self):
|
|
|
|
self.client = APIClient()
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
2015-06-29 12:08:15 +02:00
|
|
|
response = self.client.put(
|
2019-01-06 16:22:33 +01:00
|
|
|
reverse("config-detail", args=["test_var_Hi7Oje8Oith7goopeeng"]),
|
|
|
|
{"value": "invalid_string"},
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(response.data, {"detail": "Invalid input."})
|
2015-06-29 12:08:15 +02:00
|
|
|
|
2016-01-13 15:04:47 +01:00
|
|
|
def test_update_only_with_key(self):
|
|
|
|
self.client = APIClient()
|
2019-01-06 16:22:33 +01:00
|
|
|
self.client.login(username="admin", password="admin")
|
2016-01-13 15:04:47 +01:00
|
|
|
response = self.client.put(
|
2019-01-06 16:22:33 +01:00
|
|
|
reverse("config-detail", args=["test_var_Xeiizi7ooH8Thuk5aida"])
|
|
|
|
)
|
2016-01-13 15:04:47 +01:00
|
|
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(
|
2019-05-27 18:38:43 +02:00
|
|
|
response.data, {"detail": "Got None for test_var_Xeiizi7ooH8Thuk5aida"}
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2016-01-13 15:04:47 +01:00
|
|
|
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
def validator_for_testing(value):
|
|
|
|
"""
|
|
|
|
Validator for testing.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
if value == "invalid_string":
|
|
|
|
raise ValidationError({"detail": "Invalid input."})
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
|
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(
|
2019-01-06 16:22:33 +01:00
|
|
|
name="test_var_aeW3Quahkah1phahCheo",
|
2015-06-29 12:08:15 +02:00
|
|
|
default_value=None,
|
2019-01-06 16:22:33 +01:00
|
|
|
label="test_label_aeNahsheu8phahk8taYo",
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
yield ConfigVariable(name="test_var_Xeiizi7ooH8Thuk5aida", default_value="")
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
yield ConfigVariable(
|
2019-01-06 16:22:33 +01:00
|
|
|
name="test_var_ohhii4iavoh5Phoh5ahg", default_value=0, input_type="integer"
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
yield ConfigVariable(
|
2019-01-06 16:22:33 +01:00
|
|
|
name="test_var_wei0Rei9ahzooSohK1ph",
|
|
|
|
default_value="key_1_Queit2juchoocos2Vugh",
|
|
|
|
input_type="choice",
|
2015-06-29 12:08:15 +02:00
|
|
|
choices=(
|
2019-01-06 16:22:33 +01:00
|
|
|
{
|
|
|
|
"value": "key_1_Queit2juchoocos2Vugh",
|
|
|
|
"display_name": "label_1_Queit2juchoocos2Vugh",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"value": "key_2_yahb2ain1aeZ1lea1Pei",
|
|
|
|
"display_name": "label_2_yahb2ain1aeZ1lea1Pei",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
)
|
2015-06-29 12:08:15 +02:00
|
|
|
|
|
|
|
yield ConfigVariable(
|
2019-01-06 16:22:33 +01:00
|
|
|
name="test_var_Hi7Oje8Oith7goopeeng",
|
|
|
|
default_value="",
|
|
|
|
validators=(validator_for_testing,),
|
|
|
|
)
|
2016-02-14 21:38:26 +01:00
|
|
|
|
|
|
|
yield ConfigVariable(
|
2019-01-06 16:22:33 +01:00
|
|
|
name="test_var_pud2zah2teeNaiP7IoNa",
|
2016-02-14 21:38:26 +01:00
|
|
|
default_value=None,
|
2019-01-06 16:22:33 +01:00
|
|
|
label="test_label_xaing7eefaePheePhei6",
|
|
|
|
hidden=True,
|
|
|
|
)
|