Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #19057 (again) -- added additional tests

  • Loading branch information...
commit 2b5f848207b1dab35afd6f63d0107629c76d4d9a 1 parent 81f5d4a
Preston Holmes authored October 02, 2012
7  django/contrib/auth/handlers/modwsgi.py
@@ -21,17 +21,12 @@ def check_password(environ, username, password):
21 21
             user = UserModel.objects.get_by_natural_key(username)
22 22
         except UserModel.DoesNotExist:
23 23
             return None
24  
-        try:
25  
-            if not user.is_active:
26  
-                return None
27  
-        except AttributeError as e:
28  
-            # a custom user may not support is_active
  24
+        if not user.is_active:
29 25
             return None
30 26
         return user.check_password(password)
31 27
     finally:
32 28
         db.close_connection()
33 29
 
34  
-
35 30
 def groups_for_user(environ, username):
36 31
     """
37 32
     Authorizes a user based on groups
42  django/contrib/auth/tests/handlers.py
@@ -2,31 +2,23 @@
2 2
 
3 3
 from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
4 4
 from django.contrib.auth.models import User, Group
  5
+from django.contrib.auth.tests import CustomUser
5 6
 from django.contrib.auth.tests.utils import skipIfCustomUser
6 7
 from django.test import TransactionTestCase
  8
+from django.test.utils import override_settings
7 9
 
8 10
 
9 11
 class ModWsgiHandlerTestCase(TransactionTestCase):
10 12
     """
11 13
     Tests for the mod_wsgi authentication handler
12 14
     """
13  
-
14  
-    def setUp(self):
15  
-        user1 = User.objects.create_user('test', 'test@example.com', 'test')
16  
-        User.objects.create_user('test1', 'test1@example.com', 'test1')
17  
-        group = Group.objects.create(name='test_group')
18  
-        user1.groups.add(group)
19  
-
20 15
     @skipIfCustomUser
21 16
     def test_check_password(self):
22 17
         """
23 18
         Verify that check_password returns the correct values as per
24 19
         http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
25  
-
26  
-        because the custom user available in the test framework does not
27  
-        support the is_active attribute, we can't test this with a custom
28  
-        user.
29 20
         """
  21
+        User.objects.create_user('test', 'test@example.com', 'test')
30 22
 
31 23
         # User not in database
32 24
         self.assertTrue(check_password({}, 'unknown', '') is None)
@@ -34,15 +26,43 @@ def test_check_password(self):
34 26
         # Valid user with correct password
35 27
         self.assertTrue(check_password({}, 'test', 'test'))
36 28
 
  29
+        # correct password, but user is inactive
  30
+        User.objects.filter(username='test').update(is_active=False)
  31
+        self.assertFalse(check_password({}, 'test', 'test'))
  32
+
37 33
         # Valid user with incorrect password
38 34
         self.assertFalse(check_password({}, 'test', 'incorrect'))
39 35
 
  36
+    @override_settings(AUTH_USER_MODEL='auth.CustomUser')
  37
+    def test_check_password_custom_user(self):
  38
+        """
  39
+        Verify that check_password returns the correct values as per
  40
+        http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
  41
+
  42
+        with custom user installed
  43
+        """
  44
+
  45
+        CustomUser.objects.create_user('test@example.com', '1990-01-01', 'test')
  46
+
  47
+        # User not in database
  48
+        self.assertTrue(check_password({}, 'unknown', '') is None)
  49
+
  50
+        # Valid user with correct password'
  51
+        self.assertTrue(check_password({}, 'test@example.com', 'test'))
  52
+
  53
+        # Valid user with incorrect password
  54
+        self.assertFalse(check_password({}, 'test@example.com', 'incorrect'))
  55
+
40 56
     @skipIfCustomUser
41 57
     def test_groups_for_user(self):
42 58
         """
43 59
         Check that groups_for_user returns correct values as per
44 60
         http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Group_Authorisation
45 61
         """
  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)
46 66
 
47 67
         # User not in database
48 68
         self.assertEqual(groups_for_user({}, 'unknown'), [])

0 notes on commit 2b5f848

Please sign in to comment.
Something went wrong with that request. Please try again.