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
2015-05-05 10:42:31 +02:00
from openslides.users.serializers import UserFullSerializer
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.
"""
2015-05-05 10:42:31 +02:00
serializer = UserFullSerializer()
data = {}
2015-05-05 10:42:31 +02:00
with self.assertRaises(ValidationError):
serializer.validate(data)
2015-05-05 10:42:31 +02:00
@patch('openslides.users.serializers.User.objects.generate_username')
def test_validate_no_username(self, generate_username):
"""
2015-05-05 10:42:31 +02:00
Tests, that an empty username is generated.
"""
2015-05-05 10:42:31 +02:00
generate_username.return_value = 'test_value'
serializer = UserFullSerializer()
data = {'first_name': 'TestName'}
2015-05-05 10:42:31 +02:00
new_data = serializer.validate(data)
2015-05-05 10:42:31 +02: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.
"""
2015-05-05 10:42:31 +02:00
view = MagicMock(action='partial_update')
serializer = UserFullSerializer(context={'view': view})
data = {'first_name': 'TestName'}
2015-05-05 10:42:31 +02:00
new_data = serializer.validate(data)
2015-05-05 10:42:31 +02:00
self.assertIsNone(new_data.get('username'))