Commit 240db4b2 by Jeremy Bowman

Support and tests for Django 1.9-1.11

parent 47cfef16
......@@ -7,3 +7,8 @@
# PyDev garbage
.tmp*
# Testing garbage
.cache
.coverage*
.tox
......@@ -7,18 +7,26 @@ from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.views import login, logout
from django.core.urlresolvers import reverse
try:
from django.utils.deprecation import MiddlewareMixin as base_class
except ImportError:
base_class = object
from django_cas.views import login as cas_login, logout as cas_logout, _service_url
__all__ = ['CASMiddleware']
class CASMiddleware(object):
class CASMiddleware(base_class):
"""Middleware that allows CAS authentication on admin pages"""
def process_request(self, request):
"""Logs in the user if a ticket is append as parameter"""
ticket = request.REQUEST.get('ticket')
if request.method == 'POST':
ticket = request.POST.get('ticket')
else:
ticket = request.GET.get('ticket')
if ticket:
from django.contrib import auth
......@@ -26,9 +34,6 @@ class CASMiddleware(object):
if user is not None:
auth.login(request, user)
def process_view(self, request, view_func, view_args, view_kwargs):
"""Forwards unauthenticated requests to the admin page to the CAS
login URL, as well as calls to django.contrib.auth.views.login and
......
from StringIO import StringIO
import urllib
from django.conf import settings
from django.utils.unittest.case import TestCase
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test import TestCase
import mock
from django_cas.backends import _verify_cas2
from django_cas.models import PgtIOU, Tgt
......@@ -19,6 +23,24 @@ def dummyUrlOpenWithProxyGrantingTikcet(url):
class backendTest(TestCase):
def test_login_via_middleware(self):
user = User.objects.create_user('test_user')
ticket = 'thats_the_ticket'
url = '{}?ticket={}'.format(reverse('cas_proxy_callback'), ticket)
with mock.patch('django_cas.backends._verify', return_value=(user.username, None)) as mock_verify:
response = self.client.get(url)
self.assertEqual(200, response.status_code)
mock_verify.assert_called_once()
def test_login_view(self):
user = User.objects.create_user('test_user')
ticket = 'thats_the_ticket'
url = '{}?ticket={}'.format(reverse('cas_login'), ticket)
with mock.patch('django_cas.backends._verify', return_value=(user.username, None)) as mock_verify:
response = self.client.get(url)
self.assertRedirects(response, '/', target_status_code=404)
mock_verify.assert_called_once()
def test_verify_cas2_no_pgt(self):
urllib.urlopen = dummyUrlOpenNoProxyGrantingTicket
settings.CAS_PROXY_CALLBACK = None
......@@ -27,8 +49,8 @@ class backendTest(TestCase):
def test_verify_cas2_with_pgt(self):
urllib.urlopen = dummyUrlOpenWithProxyGrantingTikcet
#st = ServiceTicket.objects.create();
tgt = Tgt.objects.create(username='sannies');
#st = ServiceTicket.objects.create()
tgt = Tgt.objects.create(username='sannies')
PgtIOU.objects.create(tgt=tgt, pgtIou='PGTIOU-NUYny6RiAfHBsuWq270m3l1kgPTjEOCexpowQV9ZJDrh8cGKzb')
settings.CAS_PROXY_CALLBACK = "http://dummy2"
......
__author__ = 'sannies'
from django.conf.urls.defaults import patterns, url
from django.conf.urls import url
from django_cas.views import login, logout, proxy_callback
urlpatterns = patterns('django_cas.views',
url(r'^login$', "login", name="cas_login"),
url(r'^logout$', "logout", name="cas_logout"),
url(r'^proxycallback$', "proxy_callback", name="cas_proxy_callback"),
)
urlpatterns = [
url(r'^login$', login, name="cas_login"),
url(r'^logout$', logout, name="cas_logout"),
url(r'^proxycallback$', proxy_callback, name="cas_proxy_callback"),
]
#!/usr/bin/env python
"""
Django administration utility.
"""
from __future__ import absolute_import, unicode_literals
import os
import sys
PWD = os.path.abspath(os.path.dirname(__file__))
if __name__ == '__main__':
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_settings')
sys.path.append(PWD)
try:
from django.core.management import execute_from_command_line # pylint: disable=wrong-import-position
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django # pylint: disable=unused-import, wrong-import-position
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
# Additional requirements for development of this application
pip-tools # Requirements file management
tox # Virtualenv management for tests
tox-battery # Makes tox aware of requirements file changes
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file requirements/dev.txt requirements/dev.in
#
click==6.7 # via pip-tools
first==2.0.1 # via pip-tools
pip-tools==1.10.1
pluggy==0.5.2 # via tox
py==1.4.34 # via tox
six==1.11.0 # via pip-tools, tox
tox-battery==0.5
tox==2.9.1
virtualenv==15.1.0 # via tox
# Additional requirements for test runs.
mock # Mock network calls during test runs
pytest-catchlog # Show log output for test failures
pytest-cov # pytest extension for code coverage statistics
pytest-django # pytest extension for better Django support
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file requirements/test.txt requirements/test.in
#
coverage==4.4.1 # via pytest-cov
funcsigs==1.0.2 # via mock
mock==2.0.0
pbr==3.1.1 # via mock
py==1.4.34 # via pytest, pytest-catchlog
pytest-catchlog==1.2.2
pytest-cov==2.5.1
pytest-django==3.1.2
pytest==3.2.3 # via pytest-catchlog, pytest-cov, pytest-django
six==1.11.0 # via mock
......@@ -13,6 +13,10 @@ setup(
'Environment :: Web Environment',
'Intended Audience :: Developers',
'Framework :: Django',
'Framework :: Django :: 1.8',
'Framework :: Django :: 1.9',
'Framework :: Django :: 1.10',
'Framework :: Django :: 1.11',
'License :: OSI Approved :: MIT License',
'Natural Language :: English',
'Operating System :: OS Independent',
......
"""
These settings are here to use during tests, because django requires them.
In a real-world use case, apps in this project are installed into other
Django applications, so these settings will not be used.
"""
from __future__ import absolute_import, unicode_literals
import tempfile
import django
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'django_cas.backends.CASBackend',
)
CAS_SERVER_URL = 'https://my.sso.server/'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'default.db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.messages',
'django.contrib.sessions',
'django_cas',
)
if django.VERSION[0] == 1 and django.VERSION[1] < 10:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django_cas.middleware.CASMiddleware',
)
else:
MIDDLEWARE = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django_cas.middleware.CASMiddleware',
)
MEDIA_ROOT = tempfile.mkdtemp()
ROOT_URLCONF = 'django_cas.urls'
SECRET_KEY = 'insecure-secret-key'
[tox]
envlist = {py27}-django{18,19,110,111}
[pytest]
DJANGO_SETTINGS_MODULE = test_settings
addopts = --cov django_cas --cov-report term-missing
norecursedirs = requirements
python_files = tests.py
[testenv]
deps =
django18: Django>=1.8,<1.9
django19: Django>=1.9,<1.10
django110: Django>=1.10,<1.11
django111: Django>=1.11,<2.0
-r{toxinidir}/requirements/test.txt
commands =
pytest {posargs}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment