OpenSlides/tests/unit/users/test_serializers.py
2019-01-08 21:51:52 +01:00

43 lines
1.4 KiB
Python

from unittest import TestCase
from unittest.mock import MagicMock, patch
from openslides.users.serializers import UserFullSerializer
from openslides.utils.rest_api import ValidationError
class UserCreateUpdateSerializerTest(TestCase):
def test_validate_no_data(self):
"""
Tests, that the validator raises a ValidationError, if not data is given.
"""
serializer = UserFullSerializer()
data: object = {}
with self.assertRaises(ValidationError):
serializer.validate(data)
@patch("openslides.users.serializers.User.objects.generate_username")
def test_validate_no_username(self, generate_username):
"""
Tests, that an empty username is generated.
"""
generate_username.return_value = "test_value"
serializer = UserFullSerializer()
data = {"first_name": "TestName"}
new_data = serializer.validate(data)
self.assertEqual(new_data["username"], "test_value")
def test_validate_no_username_in_patch_request(self):
"""
Tests, that an empty username is not set in a patch request context.
"""
view = MagicMock(action="partial_update")
serializer = UserFullSerializer(context={"view": view})
data = {"first_name": "TestName"}
new_data = serializer.validate(data)
self.assertIsNone(new_data.get("username"))