mirror of
				https://github.com/Tivix/django-rest-auth.git
				synced 2025-11-04 09:37:35 +03:00 
			
		
		
		
	Increased test coverage
+ minor fixes
This commit is contained in:
		
							parent
							
								
									3189a5c7a0
								
							
						
					
					
						commit
						8a004bb48a
					
				
							
								
								
									
										26
									
								
								.coveragerc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								.coveragerc
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,26 @@
 | 
			
		|||
# .coveragerc to control coverage.py
 | 
			
		||||
[run]
 | 
			
		||||
omit=*site-packages*,*distutils*,*migrations*
 | 
			
		||||
 | 
			
		||||
[report]
 | 
			
		||||
# Regexes for lines to exclude from consideration
 | 
			
		||||
exclude_lines =
 | 
			
		||||
    # Have to re-enable the standard pragma
 | 
			
		||||
    pragma: no cover
 | 
			
		||||
 | 
			
		||||
    # Don't complain about missing debug-only code:
 | 
			
		||||
    def __repr__
 | 
			
		||||
    if self\.debug
 | 
			
		||||
 | 
			
		||||
    # Don't complain if tests don't hit defensive assertion code:
 | 
			
		||||
    raise AssertionError
 | 
			
		||||
    raise NotImplementedError
 | 
			
		||||
 | 
			
		||||
    # Don't complain if non-runnable code isn't run:
 | 
			
		||||
    if 0:
 | 
			
		||||
    if __name__ == .__main__.:
 | 
			
		||||
 | 
			
		||||
ignore_errors = True
 | 
			
		||||
 | 
			
		||||
[html]
 | 
			
		||||
directory = coverage_html
 | 
			
		||||
| 
						 | 
				
			
			@ -68,9 +68,6 @@ class VerifyEmailView(APIView, ConfirmEmailView):
 | 
			
		|||
    permission_classes = (AllowAny,)
 | 
			
		||||
    allowed_methods = ('POST', 'OPTIONS', 'HEAD')
 | 
			
		||||
 | 
			
		||||
    def get(self, *args, **kwargs):
 | 
			
		||||
        raise MethodNotAllowed('GET')
 | 
			
		||||
 | 
			
		||||
    def post(self, request, *args, **kwargs):
 | 
			
		||||
        serializer = VerifyEmailSerializer(data=request.data)
 | 
			
		||||
        serializer.is_valid(raise_exception=True)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -13,67 +13,11 @@ from django.template import RequestContext, Template
 | 
			
		|||
from django.views.decorators.cache import never_cache
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CustomRequestAuthenticationForm(AuthenticationForm):
 | 
			
		||||
    def __init__(self, request, *args, **kwargs):
 | 
			
		||||
        assert isinstance(request, HttpRequest)
 | 
			
		||||
        super(CustomRequestAuthenticationForm, self).__init__(request, *args, **kwargs)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@never_cache
 | 
			
		||||
def remote_user_auth_view(request):
 | 
			
		||||
    """
 | 
			
		||||
    Dummy view for remote user tests
 | 
			
		||||
    """
 | 
			
		||||
    t = Template("Username is {{ user }}.")
 | 
			
		||||
    c = RequestContext(request, {})
 | 
			
		||||
    return HttpResponse(t.render(c))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def auth_processor_no_attr_access(request):
 | 
			
		||||
    render(request, 'context_processors/auth_attrs_no_access.html')
 | 
			
		||||
    # *After* rendering, we check whether the session was accessed
 | 
			
		||||
    return render(request,
 | 
			
		||||
                  'context_processors/auth_attrs_test_access.html',
 | 
			
		||||
                  {'session_accessed': request.session.accessed})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def auth_processor_attr_access(request):
 | 
			
		||||
    render(request, 'context_processors/auth_attrs_access.html')
 | 
			
		||||
    return render(request,
 | 
			
		||||
                  'context_processors/auth_attrs_test_access.html',
 | 
			
		||||
                  {'session_accessed': request.session.accessed})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def auth_processor_user(request):
 | 
			
		||||
    return render(request, 'context_processors/auth_attrs_user.html')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def auth_processor_perms(request):
 | 
			
		||||
    return render(request, 'context_processors/auth_attrs_perms.html')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def auth_processor_perm_in_perms(request):
 | 
			
		||||
    return render(request, 'context_processors/auth_attrs_perm_in_perms.html')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def auth_processor_messages(request):
 | 
			
		||||
    info(request, "Message 1")
 | 
			
		||||
    return render(request, 'context_processors/auth_attrs_messages.html')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def userpage(request):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def custom_request_auth_login(request):
 | 
			
		||||
    return views.login(request, authentication_form=CustomRequestAuthenticationForm)
 | 
			
		||||
 | 
			
		||||
# special urls for auth test cases
 | 
			
		||||
urlpatterns += [
 | 
			
		||||
    url(r'^logout/custom_query/$', views.logout, dict(redirect_field_name='follow')),
 | 
			
		||||
    url(r'^logout/next_page/$', views.logout, dict(next_page='/somewhere/')),
 | 
			
		||||
    url(r'^logout/next_page/named/$', views.logout, dict(next_page='password_reset')),
 | 
			
		||||
    url(r'^remote_user/$', remote_user_auth_view),
 | 
			
		||||
    url(r'^password_reset_from_email/$', views.password_reset, dict(from_email='staffmember@example.com')),
 | 
			
		||||
    url(r'^password_reset/custom_redirect/$', views.password_reset, dict(post_reset_redirect='/custom/')),
 | 
			
		||||
    url(r'^password_reset/custom_redirect/named/$', views.password_reset, dict(post_reset_redirect='password_reset')),
 | 
			
		||||
| 
						 | 
				
			
			@ -90,16 +34,4 @@ urlpatterns += [
 | 
			
		|||
    url(r'^admin_password_reset/$', views.password_reset, dict(is_admin_site=True)),
 | 
			
		||||
    url(r'^login_required/$', login_required(views.password_reset)),
 | 
			
		||||
    url(r'^login_required_login_url/$', login_required(views.password_reset, login_url='/somewhere/')),
 | 
			
		||||
 | 
			
		||||
    url(r'^auth_processor_no_attr_access/$', auth_processor_no_attr_access),
 | 
			
		||||
    url(r'^auth_processor_attr_access/$', auth_processor_attr_access),
 | 
			
		||||
    url(r'^auth_processor_user/$', auth_processor_user),
 | 
			
		||||
    url(r'^auth_processor_perms/$', auth_processor_perms),
 | 
			
		||||
    url(r'^auth_processor_perm_in_perms/$', auth_processor_perm_in_perms),
 | 
			
		||||
    url(r'^auth_processor_messages/$', auth_processor_messages),
 | 
			
		||||
    url(r'^custom_request_auth_login/$', custom_request_auth_login),
 | 
			
		||||
    url(r'^userpage/(.+)/$', userpage, name="userpage"),
 | 
			
		||||
 | 
			
		||||
    # This line is only required to render the password reset with is_admin=True
 | 
			
		||||
    url(r'^admin/', include(admin.site.urls)),
 | 
			
		||||
]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -45,6 +45,20 @@ TEMPLATE_CONTEXT_PROCESSORS = [
 | 
			
		|||
    "allauth.socialaccount.context_processors.socialaccount",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
# avoid deprecation warnings during tests
 | 
			
		||||
TEMPLATES = [
 | 
			
		||||
    {
 | 
			
		||||
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
 | 
			
		||||
        'DIRS': [
 | 
			
		||||
            # insert your TEMPLATE_DIRS here
 | 
			
		||||
        ],
 | 
			
		||||
        'APP_DIRS': True,
 | 
			
		||||
        'OPTIONS': {
 | 
			
		||||
            'context_processors': TEMPLATE_CONTEXT_PROCESSORS,
 | 
			
		||||
        },
 | 
			
		||||
    },
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
REST_FRAMEWORK = {
 | 
			
		||||
    'DEFAULT_AUTHENTICATION_CLASSES': (
 | 
			
		||||
        'rest_framework.authentication.SessionAuthentication',
 | 
			
		||||
| 
						 | 
				
			
			@ -79,3 +93,10 @@ INSTALLED_APPS = [
 | 
			
		|||
SECRET_KEY = "38dh*skf8sjfhs287dh&^hd8&3hdg*j2&sd"
 | 
			
		||||
ACCOUNT_ACTIVATION_DAYS = 1
 | 
			
		||||
SITE_ID = 1
 | 
			
		||||
 | 
			
		||||
AUTHENTICATION_BACKENDS = (
 | 
			
		||||
    # Needed to login by username in Django admin, regardless of `allauth`
 | 
			
		||||
    'django.contrib.auth.backends.ModelBackend',
 | 
			
		||||
    # `allauth` specific authentication methods, such as login by e-mail
 | 
			
		||||
    'allauth.account.auth_backends.AuthenticationBackend',
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,16 +1,16 @@
 | 
			
		|||
from django.core.urlresolvers import reverse
 | 
			
		||||
from django.test import TestCase
 | 
			
		||||
from django.test import TestCase, override_settings
 | 
			
		||||
from django.contrib.auth import get_user_model
 | 
			
		||||
from django.core import mail
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
from django.test.utils import override_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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@override_settings(ROOT_URLCONF="tests.urls")
 | 
			
		||||
class APITestCase1(TestCase, BaseAPITestCase):
 | 
			
		||||
    """
 | 
			
		||||
    Case #1:
 | 
			
		||||
| 
						 | 
				
			
			@ -18,7 +18,7 @@ class APITestCase1(TestCase, BaseAPITestCase):
 | 
			
		|||
    - custom registration: backend defined
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    urls = 'tests.urls'
 | 
			
		||||
    # urls = 'tests.urls'
 | 
			
		||||
 | 
			
		||||
    USERNAME = 'person'
 | 
			
		||||
    PASS = 'person'
 | 
			
		||||
| 
						 | 
				
			
			@ -57,7 +57,36 @@ class APITestCase1(TestCase, BaseAPITestCase):
 | 
			
		|||
        result['token'] = default_token_generator.make_token(user)
 | 
			
		||||
        return result
 | 
			
		||||
 | 
			
		||||
    def test_login(self):
 | 
			
		||||
    @override_settings(ACCOUNT_AUTHENTICATION_METHOD=account_app_settings.AuthenticationMethod.EMAIL)
 | 
			
		||||
    def test_login_failed_email_validation(self):
 | 
			
		||||
        payload = {
 | 
			
		||||
            "email": '',
 | 
			
		||||
            "password": self.PASS
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        resp = self.post(self.login_url, data=payload, status_code=400)
 | 
			
		||||
        self.assertEqual(resp.json['non_field_errors'][0], u'Must include "email" and "password".')
 | 
			
		||||
 | 
			
		||||
    @override_settings(ACCOUNT_AUTHENTICATION_METHOD=account_app_settings.AuthenticationMethod.USERNAME)
 | 
			
		||||
    def test_login_failed_username_validation(self):
 | 
			
		||||
        payload = {
 | 
			
		||||
            "username": '',
 | 
			
		||||
            "password": self.PASS
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        resp = self.post(self.login_url, data=payload, status_code=400)
 | 
			
		||||
        self.assertEqual(resp.json['non_field_errors'][0], u'Must include "username" and "password".')
 | 
			
		||||
 | 
			
		||||
    @override_settings(ACCOUNT_AUTHENTICATION_METHOD=account_app_settings.AuthenticationMethod.USERNAME_EMAIL)
 | 
			
		||||
    def test_login_failed_username_email_validation(self):
 | 
			
		||||
        payload = {
 | 
			
		||||
            "password": self.PASS
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        resp = self.post(self.login_url, data=payload, status_code=400)
 | 
			
		||||
        self.assertEqual(resp.json['non_field_errors'][0], u'Must include either "username" or "email" and "password".')
 | 
			
		||||
 | 
			
		||||
    def test_allauth_login_with_username(self):
 | 
			
		||||
        payload = {
 | 
			
		||||
            "username": self.USERNAME,
 | 
			
		||||
            "password": self.PASS
 | 
			
		||||
| 
						 | 
				
			
			@ -91,6 +120,22 @@ class APITestCase1(TestCase, BaseAPITestCase):
 | 
			
		|||
        # test empty payload
 | 
			
		||||
        self.post(self.login_url, data={}, status_code=400)
 | 
			
		||||
 | 
			
		||||
    @override_settings(ACCOUNT_AUTHENTICATION_METHOD=account_app_settings.AuthenticationMethod.EMAIL)
 | 
			
		||||
    def test_allauth_login_with_email(self):
 | 
			
		||||
        payload = {
 | 
			
		||||
            "email": self.EMAIL,
 | 
			
		||||
            "password": self.PASS
 | 
			
		||||
        }
 | 
			
		||||
        # there is no users in db so it should throw error (400)
 | 
			
		||||
        self.post(self.login_url, data=payload, status_code=400)
 | 
			
		||||
 | 
			
		||||
        self.post(self.password_change_url, status_code=403)
 | 
			
		||||
 | 
			
		||||
        # create user
 | 
			
		||||
        user = get_user_model().objects.create_user(self.EMAIL, email=self.EMAIL, password=self.PASS)
 | 
			
		||||
 | 
			
		||||
        self.post(self.login_url, data=payload, status_code=200)
 | 
			
		||||
 | 
			
		||||
    @override_settings(REST_USE_JWT=True)
 | 
			
		||||
    def test_login_jwt(self):
 | 
			
		||||
        payload = {
 | 
			
		||||
| 
						 | 
				
			
			@ -148,6 +193,9 @@ class APITestCase1(TestCase, BaseAPITestCase):
 | 
			
		|||
        # test empty payload
 | 
			
		||||
        self.post(self.login_url, data={}, status_code=400)
 | 
			
		||||
 | 
			
		||||
        # bring back allauth
 | 
			
		||||
        settings.INSTALLED_APPS.append('allauth')
 | 
			
		||||
 | 
			
		||||
    def test_password_change(self):
 | 
			
		||||
        login_payload = {
 | 
			
		||||
            "username": self.USERNAME,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -45,12 +45,14 @@ class BaseAPITestCase(object):
 | 
			
		|||
        self.response = request_func(*args, **kwargs)
 | 
			
		||||
        is_json = bool(
 | 
			
		||||
            [x for x in self.response._headers['content-type'] if 'json' in x])
 | 
			
		||||
 | 
			
		||||
        self.response.json = {}
 | 
			
		||||
        if is_json and self.response.content:
 | 
			
		||||
            self.response.json = json.loads(force_text(self.response.content))
 | 
			
		||||
        else:
 | 
			
		||||
            self.response.json = {}
 | 
			
		||||
            
 | 
			
		||||
        if status_code:
 | 
			
		||||
            self.assertEqual(self.response.status_code, status_code)
 | 
			
		||||
 | 
			
		||||
        return self.response
 | 
			
		||||
 | 
			
		||||
    def post(self, *args, **kwargs):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,10 +12,9 @@ from rest_framework import status
 | 
			
		|||
from .test_base import BaseAPITestCase
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@override_settings(ROOT_URLCONF="tests.urls")
 | 
			
		||||
class TestSocialAuth(TestCase, BaseAPITestCase):
 | 
			
		||||
 | 
			
		||||
    urls = 'tests.urls'
 | 
			
		||||
 | 
			
		||||
    USERNAME = 'person'
 | 
			
		||||
    PASS = 'person'
 | 
			
		||||
    EMAIL = "person1@world.com"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user