OpenSlides/tests/unit/users/test_serializers.py
Norman Jäckel 3a19218bd5 Refactored parts of users app.
Refactored user creation and update via REST API. Used new serializer.
Cleaned up management commands, signals and imports. Moved code from 'api.py' to 'models.py'.
Changed usage of group 'Registered'. Now the users don't have to be members to gain its permissions. Used customized auth backend for this.
Added and changed some tests.
2015-02-14 02:29:53 +01:00

84 lines
3.4 KiB
Python

from unittest import TestCase
from unittest.mock import MagicMock, patch
from django.core.exceptions import ImproperlyConfigured
from rest_framework import status
from rest_framework.viewsets import ModelViewSet
from rest_framework.test import APIRequestFactory
class UserCreateUpdateSerializer(TestCase):
def test_improperly_configured_exception_list_request(self):
"""
Tests that ImproperlyConfigured is raised if one tries to use the
UserCreateUpdateSerializer with a list request.
"""
# Global import is not possible for some unknown magic.
from openslides.users.serializers import UserCreateUpdateSerializer
factory = APIRequestFactory()
request = factory.get('/')
view_class = ModelViewSet
view_class.queryset = MagicMock()
view_class.serializer_class = UserCreateUpdateSerializer
view = view_class.as_view({'get': 'list'})
with self.assertRaises(ImproperlyConfigured):
view(request)
@patch('rest_framework.generics.get_object_or_404')
def test_improperly_configured_exception_retrieve_request(self, mock_get_object_or_404):
"""
Tests that ImproperlyConfigured is raised if one tries to use the
UserCreateUpdateSerializer with a retrieve request.
"""
# Global import is not possible for some unknown magic.
from openslides.users.serializers import UserCreateUpdateSerializer
factory = APIRequestFactory()
request = factory.get('/')
view_class = ModelViewSet
view_class.queryset = MagicMock()
view_class.serializer_class = UserCreateUpdateSerializer
view = view_class.as_view({'get': 'retrieve'})
mock_get_object_or_404.return_value = MagicMock()
with self.assertRaises(ImproperlyConfigured):
view(request, pk='1')
def test_no_improperly_configured_exception_create_request(self):
"""
Tests that ImproperlyConfigured is not raised if one tries to use the
UserCreateUpdateSerializer with a create request.
"""
# Global import is not possible for some unknown magic.
from openslides.users.serializers import UserCreateUpdateSerializer
factory = APIRequestFactory()
request = factory.get('/')
view_class = ModelViewSet
view_class.queryset = MagicMock()
view_class.serializer_class = UserCreateUpdateSerializer
view = view_class.as_view({'get': 'create'})
response = view(request)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
@patch('rest_framework.generics.get_object_or_404')
def test_no_improperly_configured_exception_update_request(self, mock_get_object_or_404):
"""
Tests that ImproperlyConfigured is not raised if one tries to use the
UserCreateUpdateSerializer with a update request.
"""
# Global import is not possible for some unknown magic.
from openslides.users.serializers import UserCreateUpdateSerializer
factory = APIRequestFactory()
request = factory.get('/')
view_class = ModelViewSet
view_class.queryset = MagicMock()
view_class.serializer_class = UserCreateUpdateSerializer
view = view_class.as_view({'get': 'update'})
mock_get_object_or_404.return_value = MagicMock()
response = view(request, pk='1')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)