2
0

handlers.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from __future__ import unicode_literals
  2. from django.contrib.auth.handlers.modwsgi import check_password, groups_for_user
  3. from django.contrib.auth.models import User, Group
  4. from django.contrib.auth.tests.utils import skipIfCustomUser
  5. from django.test import TransactionTestCase
  6. class ModWsgiHandlerTestCase(TransactionTestCase):
  7. """
  8. Tests for the mod_wsgi authentication handler
  9. """
  10. def setUp(self):
  11. user1 = User.objects.create_user('test', 'test@example.com', 'test')
  12. User.objects.create_user('test1', 'test1@example.com', 'test1')
  13. group = Group.objects.create(name='test_group')
  14. user1.groups.add(group)
  15. def test_check_password(self):
  16. """
  17. Verify that check_password returns the correct values as per
  18. http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
  19. because the custom user available in the test framework does not
  20. support the is_active attribute, we can't test this with a custom
  21. user.
  22. """
  23. # User not in database
  24. self.assertTrue(check_password({}, 'unknown', '') is None)
  25. # Valid user with correct password
  26. self.assertTrue(check_password({}, 'test', 'test'))
  27. # Valid user with incorrect password
  28. self.assertFalse(check_password({}, 'test', 'incorrect'))
  29. @skipIfCustomUser
  30. def test_groups_for_user(self):
  31. """
  32. Check that groups_for_user returns correct values as per
  33. http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Group_Authorisation
  34. """
  35. # User not in database
  36. self.assertEqual(groups_for_user({}, 'unknown'), [])
  37. self.assertEqual(groups_for_user({}, 'test'), [b'test_group'])
  38. self.assertEqual(groups_for_user({}, 'test1'), [])