1
from __future__ import unicode_literals
3
from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
4
from django.contrib.auth.models import User, Group
5
from django.contrib.auth.tests import CustomUser
6
from django.contrib.auth.tests.utils import skipIfCustomUser
7
from django.test import TransactionTestCase
8
from django.test.utils import override_settings
11
class ModWsgiHandlerTestCase(TransactionTestCase):
13
Tests for the mod_wsgi authentication handler
16
def test_check_password(self):
18
Verify that check_password returns the correct values as per
19
http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
21
User.objects.create_user('test', 'test@example.com', 'test')
23
# User not in database
24
self.assertTrue(check_password({}, 'unknown', '') is None)
26
# Valid user with correct password
27
self.assertTrue(check_password({}, 'test', 'test'))
29
# correct password, but user is inactive
30
User.objects.filter(username='test').update(is_active=False)
31
self.assertFalse(check_password({}, 'test', 'test'))
33
# Valid user with incorrect password
34
self.assertFalse(check_password({}, 'test', 'incorrect'))
36
@override_settings(AUTH_USER_MODEL='auth.CustomUser')
37
def test_check_password_custom_user(self):
39
Verify that check_password returns the correct values as per
40
http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
42
with custom user installed
45
CustomUser._default_manager.create_user('test@example.com', '1990-01-01', 'test')
47
# User not in database
48
self.assertTrue(check_password({}, 'unknown', '') is None)
50
# Valid user with correct password'
51
self.assertTrue(check_password({}, 'test@example.com', 'test'))
53
# Valid user with incorrect password
54
self.assertFalse(check_password({}, 'test@example.com', 'incorrect'))
57
def test_groups_for_user(self):
59
Check that groups_for_user returns correct values as per
60
http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Group_Authorisation
62
user1 = User.objects.create_user('test', 'test@example.com', 'test')
63
User.objects.create_user('test1', 'test1@example.com', 'test1')
64
group = Group.objects.create(name='test_group')
65
user1.groups.add(group)
67
# User not in database
68
self.assertEqual(groups_for_user({}, 'unknown'), [])
70
self.assertEqual(groups_for_user({}, 'test'), [b'test_group'])
71
self.assertEqual(groups_for_user({}, 'test1'), [])