2015-02-12 20:57:05 +01:00
|
|
|
from unittest import TestCase
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
2020-04-06 14:14:00 +02:00
|
|
|
from openslides.users.serializers import UserSerializer
|
2015-05-05 10:42:31 +02:00
|
|
|
from openslides.utils.rest_api import ValidationError
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
|
2015-05-05 10:42:31 +02:00
|
|
|
class UserCreateUpdateSerializerTest(TestCase):
|
|
|
|
def test_validate_no_data(self):
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
2015-05-05 10:42:31 +02:00
|
|
|
Tests, that the validator raises a ValidationError, if not data is given.
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
2020-04-06 14:14:00 +02:00
|
|
|
serializer = UserSerializer()
|
2018-10-28 10:04:52 +01:00
|
|
|
data: object = {}
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2015-05-05 10:42:31 +02:00
|
|
|
with self.assertRaises(ValidationError):
|
|
|
|
serializer.validate(data)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
@patch("openslides.users.serializers.User.objects.generate_username")
|
2015-05-05 10:42:31 +02:00
|
|
|
def test_validate_no_username(self, generate_username):
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
2015-05-05 10:42:31 +02:00
|
|
|
Tests, that an empty username is generated.
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
generate_username.return_value = "test_value"
|
2020-04-06 14:14:00 +02:00
|
|
|
serializer = UserSerializer()
|
2019-01-06 16:22:33 +01:00
|
|
|
data = {"first_name": "TestName"}
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2015-05-05 10:42:31 +02:00
|
|
|
new_data = serializer.validate(data)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertEqual(new_data["username"], "test_value")
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2015-05-05 10:42:31 +02:00
|
|
|
def test_validate_no_username_in_patch_request(self):
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
2015-05-05 10:42:31 +02:00
|
|
|
Tests, that an empty username is not set in a patch request context.
|
2015-02-12 20:57:05 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
view = MagicMock(action="partial_update")
|
2020-04-06 14:14:00 +02:00
|
|
|
serializer = UserSerializer(context={"view": view})
|
2019-01-06 16:22:33 +01:00
|
|
|
data = {"first_name": "TestName"}
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2015-05-05 10:42:31 +02:00
|
|
|
new_data = serializer.validate(data)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
self.assertIsNone(new_data.get("username"))
|