OpenSlides/tests/unit/users/test_serializers.py

43 lines
1.4 KiB
Python
Raw Normal View History

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