chore: added new test case for custom permission classes + general test module cleanups

This commit is contained in:
mario 2017-10-02 21:38:06 +02:00
parent 8afba8ca16
commit 30c89b1cc6
3 changed files with 33 additions and 29 deletions

View File

@ -6,6 +6,14 @@ from django.test.client import Client, MULTIPART_CONTENT
from django.utils.encoding import force_text
from rest_framework import status
from rest_framework import permissions
class CustomPermissionClass(permissions.BasePermission):
message = 'You shall not pass!'
def has_permission(self, request, view):
return False
class APIClient(Client):
@ -17,7 +25,7 @@ class APIClient(Client):
return self.generic('OPTIONS', path, data, content_type, **extra)
class BaseAPITestCase(object):
class TestsMixin(object):
"""
base for API tests:
@ -64,29 +72,6 @@ class BaseAPITestCase(object):
def patch(self, *args, **kwargs):
return self.send_request('patch', *args, **kwargs)
# def put(self, *args, **kwargs):
# return self.send_request('put', *args, **kwargs)
# def delete(self, *args, **kwargs):
# return self.send_request('delete', *args, **kwargs)
# def options(self, *args, **kwargs):
# return self.send_request('options', *args, **kwargs)
# def post_file(self, *args, **kwargs):
# kwargs['content_type'] = MULTIPART_CONTENT
# return self.send_request('post', *args, **kwargs)
# def get_file(self, *args, **kwargs):
# content_type = None
# if 'content_type' in kwargs:
# content_type = kwargs.pop('content_type')
# response = self.send_request('get', *args, **kwargs)
# if content_type:
# self.assertEqual(
# bool(filter(lambda x: content_type in x, response._headers['content-type'])), True)
# return response
def init(self):
settings.DEBUG = True
self.client = APIClient()

View File

@ -5,13 +5,18 @@ from django.core import mail
from django.conf import settings
from django.utils.encoding import force_text
from rest_framework import status
from allauth.account import app_settings as account_app_settings
from .test_base import BaseAPITestCase
from rest_framework import status
from rest_framework.test import APIRequestFactory
from rest_auth.registration.views import RegisterView
from rest_auth.registration.app_settings import register_permission_classes
from .mixins import TestsMixin, CustomPermissionClass
@override_settings(ROOT_URLCONF="tests.urls")
class APITestCase1(TestCase, BaseAPITestCase):
class APIBasicTests(TestsMixin, TestCase):
"""
Case #1:
- user profile: defined
@ -398,6 +403,20 @@ class APITestCase1(TestCase, BaseAPITestCase):
self._login()
self._logout()
@override_settings(REST_AUTH_REGISTER_PERMISSION_CLASSES=(CustomPermissionClass,))
def test_registration_with_custom_permission_class(self):
class CustomRegisterView(RegisterView):
permission_classes = register_permission_classes()
authentication_classes = ()
factory = APIRequestFactory()
request = factory.post('/customer/details', self.REGISTRATION_DATA, format='json')
response = CustomRegisterView.as_view()(request)
self.assertEqual(response.data['detail'], CustomPermissionClass.message)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@override_settings(REST_USE_JWT=True)
def test_registration_with_jwt(self):
user_count = get_user_model().objects.all().count()

View File

@ -11,11 +11,11 @@ import responses
from rest_framework import status
from .test_base import BaseAPITestCase
from .mixins import TestsMixin
@override_settings(ROOT_URLCONF="tests.urls")
class TestSocialAuth(TestCase, BaseAPITestCase):
class TestSocialAuth(TestsMixin, TestCase):
USERNAME = 'person'
PASS = 'person'