test_handlers.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.custom_user import CustomUser
  5. from django.contrib.auth.tests.utils import skipIfCustomUser
  6. from django.test import TransactionTestCase
  7. from django.test import override_settings
  8. # This must be a TransactionTestCase because the WSGI auth handler performs
  9. # its own transaction management.
  10. class ModWsgiHandlerTestCase(TransactionTestCase):
  11. """
  12. Tests for the mod_wsgi authentication handler
  13. """
  14. available_apps = [
  15. 'django.contrib.auth',
  16. 'django.contrib.contenttypes',
  17. ]
  18. @skipIfCustomUser
  19. def test_check_password(self):
  20. """
  21. Verify that check_password returns the correct values as per
  22. http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
  23. """
  24. User.objects.create_user('test', 'test@example.com', 'test')
  25. # User not in database
  26. self.assertTrue(check_password({}, 'unknown', '') is None)
  27. # Valid user with correct password
  28. 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'))
  32. # Valid user with incorrect password
  33. self.assertFalse(check_password({}, 'test', 'incorrect'))
  34. @override_settings(AUTH_USER_MODEL='auth.CustomUser')
  35. def test_check_password_custom_user(self):
  36. """
  37. Verify that check_password returns the correct values as per
  38. http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Authentication_Provider
  39. with custom user installed
  40. """
  41. CustomUser._default_manager.create_user('test@example.com', '1990-01-01', 'test')
  42. # User not in database
  43. self.assertTrue(check_password({}, 'unknown', '') is None)
  44. # Valid user with correct password'
  45. self.assertTrue(check_password({}, 'test@example.com', 'test'))
  46. # Valid user with incorrect password
  47. self.assertFalse(check_password({}, 'test@example.com', 'incorrect'))
  48. @skipIfCustomUser
  49. def test_groups_for_user(self):
  50. """
  51. Check that groups_for_user returns correct values as per
  52. http://code.google.com/p/modwsgi/wiki/AccessControlMechanisms#Apache_Group_Authorisation
  53. """
  54. user1 = User.objects.create_user('test', 'test@example.com', 'test')
  55. User.objects.create_user('test1', 'test1@example.com', 'test1')
  56. group = Group.objects.create(name='test_group')
  57. user1.groups.add(group)
  58. # User not in database
  59. self.assertEqual(groups_for_user({}, 'unknown'), [])
  60. self.assertEqual(groups_for_user({}, 'test'), [b'test_group'])
  61. self.assertEqual(groups_for_user({}, 'test1'), [])