2012-09-24 13:48:13 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
|
|
|
|
from django.contrib.auth.models import User, Group
|
2013-05-11 11:08:45 +08:00
|
|
|
from django.contrib.auth.tests.test_custom_user import CustomUser
|
2012-10-02 19:19:44 +08:00
|
|
|
from django.contrib.auth.tests.utils import skipIfCustomUser
|
2012-09-29 18:10:52 +08:00
|
|
|
from django.test import TransactionTestCase
|
2012-10-03 00:16:37 +08:00
|
|
|
from django.test.utils import override_settings
|
2012-09-24 13:48:13 +08:00
|
|
|
|
|
|
|
|
2013-06-04 14:09:29 +08:00
|
|
|
# This must be a TransactionTestCase because the WSGI auth handler performs
|
|
|
|
# its own transaction management.
|
2012-09-29 18:10:52 +08:00
|
|
|
class ModWsgiHandlerTestCase(TransactionTestCase):
|
2012-09-24 13:48:13 +08:00
|
|
|
"""
|
|
|
|
Tests for the mod_wsgi authentication handler
|
|
|
|
"""
|
2013-06-04 14:09:29 +08:00
|
|
|
|
|
|
|
available_apps = [
|
|
|
|
'django.contrib.auth',
|
|
|
|
'django.contrib.contenttypes',
|
|
|
|
]
|
|
|
|
|
2012-10-30 10:27:01 +08:00
|
|
|
@skipIfCustomUser
|
2012-09-24 13:48:13 +08:00
|
|
|
def test_check_password(self):
|
|
|
|
"""
|
|
|
|
Verify that check_password returns the correct values as per
|
|
|
|
http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
|
|
|
|
"""
|
2012-10-03 00:16:37 +08:00
|
|
|
User.objects.create_user('test', 'test@example.com', 'test')
|
2012-09-24 13:48:13 +08:00
|
|
|
|
|
|
|
# User not in database
|
|
|
|
self.assertTrue(check_password({}, 'unknown', '') is None)
|
|
|
|
|
|
|
|
# Valid user with correct password
|
|
|
|
self.assertTrue(check_password({}, 'test', 'test'))
|
|
|
|
|
2012-10-03 00:16:37 +08:00
|
|
|
# correct password, but user is inactive
|
|
|
|
User.objects.filter(username='test').update(is_active=False)
|
|
|
|
self.assertFalse(check_password({}, 'test', 'test'))
|
|
|
|
|
2012-09-24 13:48:13 +08:00
|
|
|
# Valid user with incorrect password
|
|
|
|
self.assertFalse(check_password({}, 'test', 'incorrect'))
|
|
|
|
|
2012-10-03 00:16:37 +08:00
|
|
|
@override_settings(AUTH_USER_MODEL='auth.CustomUser')
|
|
|
|
def test_check_password_custom_user(self):
|
|
|
|
"""
|
|
|
|
Verify that check_password returns the correct values as per
|
|
|
|
http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
|
|
|
|
|
|
|
|
with custom user installed
|
|
|
|
"""
|
|
|
|
|
2013-01-22 19:47:34 +08:00
|
|
|
CustomUser._default_manager.create_user('test@example.com', '1990-01-01', 'test')
|
2012-10-03 00:16:37 +08:00
|
|
|
|
|
|
|
# User not in database
|
|
|
|
self.assertTrue(check_password({}, 'unknown', '') is None)
|
|
|
|
|
|
|
|
# Valid user with correct password'
|
|
|
|
self.assertTrue(check_password({}, 'test@example.com', 'test'))
|
|
|
|
|
|
|
|
# Valid user with incorrect password
|
|
|
|
self.assertFalse(check_password({}, 'test@example.com', 'incorrect'))
|
|
|
|
|
2012-10-02 19:19:44 +08:00
|
|
|
@skipIfCustomUser
|
2012-09-24 13:48:13 +08:00
|
|
|
def test_groups_for_user(self):
|
|
|
|
"""
|
|
|
|
Check that groups_for_user returns correct values as per
|
|
|
|
http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Group_Authorisation
|
|
|
|
"""
|
2012-10-03 00:16:37 +08:00
|
|
|
user1 = User.objects.create_user('test', 'test@example.com', 'test')
|
|
|
|
User.objects.create_user('test1', 'test1@example.com', 'test1')
|
|
|
|
group = Group.objects.create(name='test_group')
|
|
|
|
user1.groups.add(group)
|
2012-09-24 13:48:13 +08:00
|
|
|
|
|
|
|
# User not in database
|
|
|
|
self.assertEqual(groups_for_user({}, 'unknown'), [])
|
|
|
|
|
|
|
|
self.assertEqual(groups_for_user({}, 'test'), [b'test_group'])
|
|
|
|
self.assertEqual(groups_for_user({}, 'test1'), [])
|