diff --git a/rest_auth/tests/test_base.py b/rest_auth/tests/mixins.py similarity index 78% rename from rest_auth/tests/test_base.py rename to rest_auth/tests/mixins.py index 48d94f0..437f458 100644 --- a/rest_auth/tests/test_base.py +++ b/rest_auth/tests/mixins.py @@ -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() diff --git a/rest_auth/tests/test_api.py b/rest_auth/tests/test_api.py index 0356d19..d33635c 100644 --- a/rest_auth/tests/test_api.py +++ b/rest_auth/tests/test_api.py @@ -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() diff --git a/rest_auth/tests/test_social.py b/rest_auth/tests/test_social.py index 47ac0bb..dff6438 100644 --- a/rest_auth/tests/test_social.py +++ b/rest_auth/tests/test_social.py @@ -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'