Skip to content

Commit

Permalink
Compatibility with Django > 1.10
Browse files Browse the repository at this point in the history
Changes to reflect changes after Django 1.10:

  - login and logout are now class-based views in `django.contrib.auth.views`
  - added test_middleware to confirm middleware match requests for login
	and logout views
  • Loading branch information
Jordan Reiter authored and Jordan Reiter committed Mar 22, 2020
1 parent f8143be commit ae11543
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
11 changes: 9 additions & 2 deletions cas/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

try:
from django.contrib.auth.views import login, logout
except:
from django.contrib.auth import login, logout
except ImportError:
from django.contrib.auth.views import LoginView, LogoutView
login = LoginView.as_view().view_class
logout = LogoutView.as_view().view_class

from django.http import HttpResponseRedirect, HttpResponseForbidden
from django.core.exceptions import ImproperlyConfigured
Expand Down Expand Up @@ -57,6 +59,11 @@ def process_view(self, request, view_func, view_args, view_kwargs):
logout.
"""

try:
view_func = view_func.view_class
except AttributeError:
pass

if view_func == login:
return cas_login(request, *view_args, **view_kwargs)
elif view_func == logout:
Expand Down
45 changes: 45 additions & 0 deletions cas/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
try:
from unittest import mock
except ImportError:
import mock

from urllib.parse import quote_plus, urlencode

from django.conf import settings
from django.test import TestCase, Client, override_settings, modify_settings



@override_settings(MIDDLEWARE=[
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'cas.middleware.CASMiddleware'
])
class CASBackendTest(TestCase):

def setUp(self):
from cas.tests import factories
self.user = factories.UserFactory.create()
self.client = Client()

def test_login_calls_cas_login(self):
resp = self.client.get('/login/')
self.assertTrue(resp.has_header('Location'))
expected_url = '{}/login?{}'.format(
settings.CAS_SERVER_URL,
urlencode({
'service': 'http://testserver/login/?next={}'.format(quote_plus('/'))
})
)
self.assertRedirects(resp, expected_url, fetch_redirect_response=False)

def test_logout_calls_cas_logout(self):
resp = self.client.get('/logout/')
self.assertTrue(resp.has_header('Location'))
expected_url = '{}/logout?{}'.format(
settings.CAS_SERVER_URL,
urlencode({
'service': 'http://testserver/'
})
)
self.assertRedirects(resp, expected_url, fetch_redirect_response=False)
5 changes: 5 additions & 0 deletions cas/tests/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.urls import path, include

urlpatterns = [
path('', include('django.contrib.auth.urls')),
]
4 changes: 2 additions & 2 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'ENGINE': 'django.db.backends.sqlite3',
}
},
#ROOT_URLCONF='mailqueue.urls',
ROOT_URLCONF='cas.tests.urls',
INSTALLED_APPS=('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
Expand All @@ -30,7 +30,7 @@
'ENGINE': 'django.db.backends.sqlite3',
}
},
#ROOT_URLCONF='mailqueue.urls',
ROOT_URLCONF='cas.tests.urls',
INSTALLED_APPS=('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
Expand Down

0 comments on commit ae11543

Please sign in to comment.